Skip to content

Commit

Permalink
Add method to sort integrals (#23)
Browse files Browse the repository at this point in the history
* Add a method to sort integrals and a test

* Add sorting of integrals (packaged and regular), if in existence

* Add a way to only sort the names -> required for GUI
  • Loading branch information
trappitsch authored Mar 10, 2022
1 parent b50634e commit ef60386
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
33 changes: 33 additions & 0 deletions rimseval/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,39 @@ def run_macro(self, fname: Path) -> None:

sys.path.remove(str(file_path))

def sort_integrals(self, sort_vals: bool = True) -> None:
"""Sort all the integrals that are defined by mass.
Takes the integrals and the names and sorts them by mass. The starting mass
of each integral is used for sorting.
If no integrals are defined, this routine does nothing.
:param sort_vals: Sort the integrals and integral packages? Default: True
Example:
>>> crd.def_integrals
["Fe-56", "Ti-46"], array([[55.8, 56.2], [45.8, 46.2]])
>>> crd.sort_integrals()
>>> crd.def_integrals
["Ti-46", "Fe-56"], array([[45.8, 46.2], [55.8, 56.2]])
"""
if self.def_integrals is None:
return

names, values = self.def_integrals
sort_ind = values[:, 0].argsort()

if (sort_ind == np.arange(len(names))).all(): # already sorted
return

names_sorted = list(np.array(names)[sort_ind])
self.def_integrals = names_sorted, values[sort_ind]

if self.integrals is not None and sort_vals:
self.integrals = self.integrals[sort_ind]
if self.integrals_pkg is not None and sort_vals:
self.integrals_pkg = self.integrals_pkg[:, sort_ind]

def spectrum_full(self) -> None:
"""Create ToF and summed ion count array for the full spectrum.
Expand Down
23 changes: 23 additions & 0 deletions tests/func/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,29 @@ def test_packages(crd_file):
assert crd.nof_shots_pkg.sum() == crd.nof_shots


def test_sort_integrals(crd_file):
"""Sort integrals by first mass."""
_, _, _, fname = crd_file
crd = CRDFileProcessor(Path(fname))
crd.sort_integrals() # does nothing, since None
crd.def_integrals = ["Fe-56", "Ti-46"], np.array([[55.8, 56.2], [45.8, 46.2]])

crd.integrals = np.array([[2, 2], [1, 1]])
crd.integrals_pkg = np.array([crd.integrals, crd.integrals - 1])

names_exp = ["Ti-46", "Fe-56"]
values_exp = np.array([[45.8, 46.2], [55.8, 56.2]])
integrals_exp = np.array([[1, 1], [2, 2]])
integrals_pkg_exp = np.array([[[1, 1], [2, 2]], [[0, 0], [1, 1]]])

crd.sort_integrals()
names_rec, values_rec = crd.def_integrals
assert names_rec == names_exp
np.testing.assert_equal(values_rec, values_exp)
np.testing.assert_equal(crd.integrals, integrals_exp)
np.testing.assert_equal(crd.integrals_pkg, integrals_pkg_exp)


def test_spectrum_part(crd_file):
"""Cut spectrum by two shots."""
_, ions_per_shot, _, fname = crd_file
Expand Down

0 comments on commit ef60386

Please sign in to comment.