Skip to content

Commit c84dfc8

Browse files
authored
Merge pull request #12 from psavery/workflow-fixes
Search for several different ptycho files
2 parents ad6f644 + 2b2cdd4 commit c84dfc8

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

tomviz/python/tomviz/ptycho/ptycho.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ def validate_sid(sid: int, version: str, ptycho_dir: PathLike) -> str:
6464
# If it is valid, the returned string will be empty. Otherwise, the
6565
# returned string will contain the error message as to what is not
6666
# valid.
67-
ptycho_dir = Path(ptycho_dir)
68-
dir_path = ptycho_dir / f'S{sid}/{version}/recon_data'
69-
f_path = dir_path / f'recon_{sid}_{version}_object_ave.npy'
70-
g_path = dir_path / f'recon_{sid}_{version}_probe_ave.npy'
71-
if not f_path.exists():
67+
f_path = find_ptycho_file(sid, version, 'object', ptycho_dir)
68+
if f_path is None:
7269
return 'Ptycho data missing'
7370

74-
if not g_path.exists():
71+
g_path = find_ptycho_file(sid, version, 'probe', ptycho_dir)
72+
if g_path is None:
7573
return 'Probe data missing'
7674

7775
angle = load_angle_from_sid(sid, version, ptycho_dir)
@@ -81,6 +79,32 @@ def validate_sid(sid: int, version: str, ptycho_dir: PathLike) -> str:
8179
return ''
8280

8381

82+
def find_ptycho_file(sid: int, version: str, type_str: str,
83+
ptycho_dir: PathLike) -> Path | None:
84+
# type_str is `ptycho` or `probe`
85+
ptycho_dir = Path(ptycho_dir)
86+
dir_path = ptycho_dir / f'S{sid}/{version}/recon_data'
87+
base_str = f'recon_{sid}_{version}_{type_str}'
88+
# Prefer `_ave.npy` if available, then `.npy`
89+
suffix_to_try = [
90+
'_ave.npy',
91+
'.npy',
92+
]
93+
for suffix in suffix_to_try:
94+
path = dir_path / f'{base_str}{suffix}'
95+
if path.exists():
96+
return path
97+
98+
# If those didn't exist, just try to grab anything that
99+
# matches `{base_str}*.npy`
100+
paths = list(dir_path.glob(f'{base_str}*.npy'))
101+
if paths:
102+
return paths[0].resolve()
103+
104+
# Didn't find any matches
105+
return None
106+
107+
84108
def load_angle_from_sid(sid: int, version: str,
85109
ptycho_dir: PathLike) -> float:
86110
path = locate_ptycho_hyan_file(sid, version, ptycho_dir)
@@ -186,10 +210,9 @@ def load_stack_ptycho(version_list: list[str],
186210
desc="Loading Ptycho"):
187211
sid = int(sid)
188212
version = version_list[i]
189-
dir_path = ptycho_dir / f'S{sid}/{version}/recon_data'
190-
f_path = dir_path / f'recon_{sid}_{version}_object_ave.npy'
191-
g_path = dir_path / f'recon_{sid}_{version}_probe_ave.npy'
192-
if f_path.exists():
213+
f_path = find_ptycho_file(sid, version, 'object', ptycho_dir)
214+
g_path = find_ptycho_file(sid, version, 'probe', ptycho_dir)
215+
if f_path is not None:
193216
filespty_obj.append(f_path)
194217
currentsidlist.append((i, sid, angle_list[i], version))
195218
filespty_prb.append(g_path)

0 commit comments

Comments
 (0)