Skip to content

Commit

Permalink
Clarify fov_files to fov_metadata, and name to fov_name
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed Nov 7, 2023
1 parent 6a086bd commit 78e3857
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/mibi_bin_tools/bin_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,12 @@ def get_total_spectra(data_dir: str, include_fovs: Union[List[str], None] = None
if range_pad < 0:
raise ValueError("range_pad must be >= 0")

fov_files = _find_bin_files(data_dir, include_fovs)
fov_metadata = _find_bin_files(data_dir, include_fovs)
if panel_df is not None:
for fov in fov_files.values():
_fill_fov_metadata(data_dir, fov, panel_df, False, 500e-6)
for fov_info in fov_metadata.values():
_fill_fov_metadata(data_dir, fov_info, panel_df, False, 500e-6)

bin_files = list(fov_files.items())
bin_metadata = list(fov_metadata.items())

# TODO: this assumes the panel_df is sorted
lowest_mass = panel_df.loc[0, "Stop"] - range_pad
Expand All @@ -580,25 +580,27 @@ def get_total_spectra(data_dir: str, include_fovs: Union[List[str], None] = None
# store the spectra, as well as the time intervals for each FOV
spectra = {}
tof_interval = {}
for name, fov in bin_files:
for fov_name, fov_info in bin_metadata:
# compute the low and high boundaries, this will differ per FOV
mass_offset = fov["mass_offset"]
mass_gain = fov["mass_gain"]
mass_offset = fov_info["mass_offset"]
mass_gain = fov_info["mass_gain"]
tof_boundaries = _mass2tof(
np.array([lowest_mass, highest_mass]), mass_offset, mass_gain, 500e-6
).astype(np.uint16)

# set the boundaries
tof_interval[name] = tof_boundaries
tof_interval[fov_name] = tof_boundaries

# extract the spectra on an individual basis per channel
spectra[name] = _extract_bin.c_total_spectra(
bytes(os.path.join(data_dir, fov["bin"]), "utf-8"), tof_boundaries[0], tof_boundaries[1]
spectra[fov_name] = _extract_bin.c_total_spectra(
bytes(os.path.join(data_dir, fov_info["bin"]), "utf-8"),
tof_boundaries[0],
tof_boundaries[1]
)

# generate equivalent m/z values
tof_arr = np.arange(tof_boundaries[0], tof_boundaries[1] + 1)
mass_arr = _tof2mass(tof_arr, mass_offset, mass_gain, 500e-6)
fov["mass_spectra_points"] = mass_arr
fov_info["mass_spectra_points"] = mass_arr

return spectra, tof_interval, fov_files
return spectra, tof_interval, fov_metadata
9 changes: 9 additions & 0 deletions tests/bin_files_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ def test_get_total_counts(test_dir, fov):
@parametrize_with_cases('test_dir, fov', cases=FovMetadataTestFiles)
@parametrize_with_cases('panel', cases=FovMetadataTestPanels, has_tag='specified')
def test_get_total_spectra(test_dir, fov, panel):
# ensure range_pad is positive
with pytest.raises(ValueError):
bin_files.get_total_spectra(
test_dir,
[fov['json'].split('.')[0]],
panel,
range_pad=-0.1
)

bin_files.get_total_spectra(
test_dir,
[fov['json'].split('.')[0]],
Expand Down

0 comments on commit 78e3857

Please sign in to comment.