pychemstation 0.10.0__py3-none-any.whl → 0.10.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. pychemstation/control/README.md +132 -0
  2. pychemstation/control/controllers/README.md +1 -0
  3. {pychemstation-0.10.0.dist-info → pychemstation-0.10.1.dist-info}/METADATA +11 -9
  4. {pychemstation-0.10.0.dist-info → pychemstation-0.10.1.dist-info}/RECORD +6 -31
  5. {pychemstation-0.10.0.dist-info → pychemstation-0.10.1.dist-info}/WHEEL +1 -2
  6. pychemstation/analysis/spec_utils.py +0 -304
  7. pychemstation/analysis/utils.py +0 -63
  8. pychemstation/control/comm.py +0 -206
  9. pychemstation/control/controllers/devices/column.py +0 -12
  10. pychemstation/control/controllers/devices/dad.py +0 -0
  11. pychemstation/control/controllers/devices/pump.py +0 -43
  12. pychemstation/control/controllers/method.py +0 -338
  13. pychemstation/control/controllers/sequence.py +0 -190
  14. pychemstation/control/controllers/table_controller.py +0 -266
  15. pychemstation/control/table/__init__.py +0 -3
  16. pychemstation/control/table/method.py +0 -274
  17. pychemstation/control/table/sequence.py +0 -210
  18. pychemstation/control/table/table_controller.py +0 -201
  19. pychemstation-0.10.0.dist-info/top_level.txt +0 -2
  20. tests/__init__.py +0 -0
  21. tests/constants.py +0 -134
  22. tests/test_comb.py +0 -136
  23. tests/test_comm.py +0 -65
  24. tests/test_inj.py +0 -39
  25. tests/test_method.py +0 -99
  26. tests/test_nightly.py +0 -80
  27. tests/test_offline_stable.py +0 -69
  28. tests/test_online_stable.py +0 -275
  29. tests/test_proc_rep.py +0 -52
  30. tests/test_runs_stable.py +0 -225
  31. tests/test_sequence.py +0 -125
  32. tests/test_stable.py +0 -276
  33. {pychemstation-0.10.0.dist-info → pychemstation-0.10.1.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,132 @@
1
+ # Examples of usecases
2
+
3
+ ## Initialization
4
+
5
+ ```python
6
+ from pychemstation.control import HPLCController
7
+
8
+ DEFAULT_METHOD_DIR = "C:\\ChemStation\\1\\Methods\\"
9
+ DATA_DIR = "C:\\Users\\Public\\Documents\\ChemStation\\3\\Data"
10
+ SEQUENCE_DIR = "C:\\USERS\\PUBLIC\\DOCUMENTS\\CHEMSTATION\\3\\Sequence"
11
+ DEFAULT_COMMAND_PATH = "C:\\Users\\User\\Desktop\\Lucy\\"
12
+
13
+ hplc_controller = HPLCController(data_dir=DATA_DIR,
14
+ comm_dir=DEFAULT_COMMAND_PATH,
15
+ method_dir=DEFAULT_METHOD_DIR,
16
+ sequence_dir=SEQUENCE_DIR)
17
+ ```
18
+
19
+ ## Switching a method
20
+
21
+ ```python
22
+ hplc_controller.switch_method("General-Poroshell")
23
+ ```
24
+
25
+ ## Editing a method
26
+
27
+ ```python
28
+ from pychemstation.utils.method_types import *
29
+
30
+ new_method = MethodDetails(
31
+ name="My_Method",
32
+ params=HPLCMethodParams(
33
+ organic_modifier=7,
34
+ flow=0.44),
35
+ timetable=[
36
+ TimeTableEntry(
37
+ start_time=0.10,
38
+ organic_modifer=7,
39
+ flow=0.34
40
+ ),
41
+ TimeTableEntry(
42
+ start_time=1,
43
+ organic_modifer=99,
44
+ flow=0.55
45
+ )
46
+ ],
47
+ stop_time=5,
48
+ post_time=2
49
+ )
50
+
51
+ hplc_controller.edit_method(new_method)
52
+ ```
53
+
54
+ ## Running a method and get data from last run method
55
+
56
+ ```python
57
+ hplc_controller.run_method(experiment_name="test_experiment")
58
+ chrom = hplc_controller.get_last_run_method_data()
59
+ channel_a_time = chrom.A.x
60
+ ```
61
+
62
+ ## Switching a sequence
63
+
64
+ ```python
65
+ hplc_controller.switch_sequence(sequence_name="hplc_testing")
66
+ ```
67
+
68
+ ## Editing a Sequence Row
69
+
70
+ ```python
71
+ from pychemstation.utils.sequence_types import *
72
+ from pychemstation.utils.tray_types import *
73
+
74
+ hplc_controller.edit_sequence_row(SequenceEntry(
75
+ vial_location=FiftyFourVialPlate(plate=Plate.TWO, letter=Letter.A, num=Num.SEVEN).value(),
76
+ method="General-Poroshell",
77
+ num_inj=3,
78
+ inj_vol=4,
79
+ sample_name="Blank",
80
+ sample_type=SampleType.BLANK,
81
+ inj_source=InjectionSource.HIP_ALS
82
+ ), 1)
83
+ ```
84
+
85
+ ## Editing entire Sequence Table
86
+
87
+ ```python
88
+ from pychemstation.utils.tray_types import *
89
+ from pychemstation.utils.sequence_types import *
90
+
91
+ seq_table = SequenceTable(
92
+ name=DEFAULT_SEQUENCE,
93
+ rows=[
94
+ SequenceEntry(
95
+ vial_location=FiftyFourVialPlate(plate=Plate.TWO, letter=Letter.A, num=Num.SEVEN).value(),
96
+ method="General-Poroshell",
97
+ num_inj=3,
98
+ inj_vol=4,
99
+ sample_name="Control",
100
+ sample_type=SampleType.CONTROL,
101
+ inj_source=InjectionSource.MANUAL
102
+ ),
103
+ SequenceEntry(
104
+ vial_location=TenVialColumn.ONE.value,
105
+ method="General-Poroshell",
106
+ num_inj=1,
107
+ inj_vol=1,
108
+ sample_name="Sample",
109
+ sample_type=SampleType.SAMPLE,
110
+ inj_source=InjectionSource.AS_METHOD
111
+ ),
112
+ SequenceEntry(
113
+ vial_location=10,
114
+ method="General-Poroshell",
115
+ num_inj=3,
116
+ inj_vol=4,
117
+ sample_name="Blank",
118
+ sample_type=SampleType.BLANK,
119
+ inj_source=InjectionSource.HIP_ALS
120
+ ),
121
+ ]
122
+ )
123
+ hplc_controller.edit_sequence(seq_table)
124
+ ```
125
+
126
+ ## Running a sequence and get data from last run sequence
127
+
128
+ ```python
129
+ hplc_controller.run_sequence(seq_table)
130
+ chroms = hplc_controller.get_last_run_sequence_data()
131
+ channel_A_time = chroms[0].A.x
132
+ ```
@@ -0,0 +1 @@
1
+ # Chemstation Devices and Tables
@@ -1,13 +1,17 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pychemstation
3
- Version: 0.10.0
3
+ Version: 0.10.1
4
4
  Summary: Library to interact with Chemstation software, primarily used in Hein lagit branch -mb
5
- Home-page: https://gitlab.com/heingroup/device-api/pychemstation
6
- Author: Lucy Hao
5
+ Project-URL: Documentation, https://pychemstation-e5a086.gitlab.io/pychemstation.html
6
+ Project-URL: Repository, https://gitlab.com/heingroup/device-api/pychemstation
7
7
  Author-email: lucyhao <hao.lucyy@gmail.com>
8
- Requires-Python: >=3.10
9
- Description-Content-Type: text/markdown
10
8
  License-File: LICENSE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Requires-Python: >=3.10
11
15
  Requires-Dist: aghplctools>=4.8.8
12
16
  Requires-Dist: coverage>=7.6.1
13
17
  Requires-Dist: matplotlib>=3.7.5
@@ -22,9 +26,7 @@ Requires-Dist: seabreeze>=2.9.2
22
26
  Requires-Dist: setuptools>=75.3.2
23
27
  Requires-Dist: twine>=6.1.0
24
28
  Requires-Dist: xsdata>=24.9
25
- Dynamic: author
26
- Dynamic: home-page
27
- Dynamic: license-file
29
+ Description-Content-Type: text/markdown
28
30
 
29
31
  # Agilent HPLC Macro Control
30
32
 
@@ -123,4 +125,4 @@ Lucy Hao, Maria Politi
123
125
  automated method development guided by Bayesian optimization
124
126
  **](https://pubs.rsc.org/en/content/articlelanding/2024/dd/d4dd00062e),
125
127
  created by members in the Bourne Group. Copyright © Bourne Group, used under
126
- the [MIT](https://opensource.org/license/mit) license.
128
+ the [MIT](https://opensource.org/license/mit) license.
@@ -2,31 +2,20 @@ pychemstation/__init__.py,sha256=SpTl-Tg1B1HTyjNOE-8ue-N2wGnXN_2zl7RFUSxlkiM,33
2
2
  pychemstation/analysis/__init__.py,sha256=Vi31PZ7fgIvyuIhkgCNvEYeV_jtUCa8FCrAGbpks7zc,83
3
3
  pychemstation/analysis/base_spectrum.py,sha256=9WkOLk2qTAYTF1ALNUenVPoosOtBiLRvy2ni8zlGU5w,16540
4
4
  pychemstation/analysis/process_report.py,sha256=ZOgcRUMGXdGMrMFcdzsSwdOk6OBp-PpcA83vSvnmVSg,11871
5
- pychemstation/analysis/spec_utils.py,sha256=UOo9hJR3evJfmaohEEsyb7aq6X996ofuUfu-GKjiDi8,10201
6
- pychemstation/analysis/utils.py,sha256=ISupAOb_yqA4_DZRK9v18UL-XjUQccAicIJKb1VMnGg,2055
5
+ pychemstation/control/README.md,sha256=y73F-qh4g3k9Z9vBeQATqqhbwMfKB5MvGqJi5GgSUJQ,3357
7
6
  pychemstation/control/__init__.py,sha256=Js79QczKZxDNZrzG1-4yl_whCoP2aw-yDAQJungiiic,100
8
- pychemstation/control/comm.py,sha256=u44g1hTluQ0yUG93Un-QAshScoDpgYRrZfFTgweP5tY,7386
9
7
  pychemstation/control/hplc.py,sha256=f7NHcCWtc_ApasjU5VMQtEvWQxoIboi_-RU9dkjORrs,13215
8
+ pychemstation/control/controllers/README.md,sha256=S5cd4NJmPjs6TUH98BtPJJhiS1Lu-mxLCNS786ogOrQ,32
10
9
  pychemstation/control/controllers/__init__.py,sha256=r7UU0u5zuJHO_KTqt-4Gy65BMlyXtxrdskiOhtO9Yw4,260
11
10
  pychemstation/control/controllers/comm.py,sha256=AN3A3ThvIsOKWY7Kmb_tnE5pRUqI7O2ID8M54z_w-uE,7831
12
- pychemstation/control/controllers/method.py,sha256=XUclB7lQ_SIkquR58MBmmi9drHIPEq9AR8VprTLenvI,15503
13
- pychemstation/control/controllers/sequence.py,sha256=kYNxxck2I-q9mZDEZwG8bJ_99FfLmunS13EAHOS65wU,8288
14
- pychemstation/control/controllers/table_controller.py,sha256=70ovnIjLKkJborS1ztk445Mv42TtUM9jUniaQmZuyWQ,11031
15
11
  pychemstation/control/controllers/devices/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
- pychemstation/control/controllers/devices/column.py,sha256=SCpCnVFZFUM9LM51MbWkVcBRayN3WFxy7lz9gs2PYeY,348
17
- pychemstation/control/controllers/devices/dad.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
12
  pychemstation/control/controllers/devices/device.py,sha256=JNBKVRka1I3LA1lElIeUO0j93BTK5IJufTPNq95OhNE,1473
19
13
  pychemstation/control/controllers/devices/injector.py,sha256=s40jFd0B_wJn4ID6SgAk_F8WhnGbbflpiti4uwIhaSs,1950
20
- pychemstation/control/controllers/devices/pump.py,sha256=DJQh4lNXEraeC1CWrsKmsITOjuYlRI3tih_XRB3F1hg,1404
21
14
  pychemstation/control/controllers/tables/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
15
  pychemstation/control/controllers/tables/method.py,sha256=LHoNRSTsSrrktghqNnU5KTRXDczcuGgqdqKEs_3sUXI,18609
23
16
  pychemstation/control/controllers/tables/ms.py,sha256=JFD-tOhu8uRyKdl-E3-neRssii8MNqVRIlsrnFhNY_M,682
24
17
  pychemstation/control/controllers/tables/sequence.py,sha256=DwX0wi5GhHmk8wnl89X2MKvqTSojLycV4IEvNjdVdWg,13400
25
18
  pychemstation/control/controllers/tables/table.py,sha256=zMzsQgkLxM3LVe9w-OM8WjLZxTo9zrmBTNH182gAyh8,12750
26
- pychemstation/control/table/__init__.py,sha256=RgMN4uIWHdNUHpGRBWdzmzAbk7XEKl6Y-qtqWCxzSZU,124
27
- pychemstation/control/table/method.py,sha256=THVoGomSXff_CTU3eAYme0BYwkPzab5UgZKsiZ29QSk,12196
28
- pychemstation/control/table/sequence.py,sha256=Eri52AnbE3BGthfrRSvYKYciquUzvHKo0lYUTySYYE8,10542
29
- pychemstation/control/table/table_controller.py,sha256=HVNYUXqtyFTAvb67fa3RO5RHgmBTFMsYRHKpiXdYcfs,8313
30
19
  pychemstation/generated/__init__.py,sha256=GAoZFAYbPVEJDkcOw3e1rgOqd7TCW0HyKNPM8OMehMg,1005
31
20
  pychemstation/generated/dad_method.py,sha256=xTUiSCvkXcxBUhjVm1YZKu-tHs16k23pF-0xYrQSwWA,8408
32
21
  pychemstation/generated/pump_method.py,sha256=797RsSDI2-QPf_BX8isZQx0O3aRx84EGIXJXhXw3IS0,12180
@@ -42,21 +31,7 @@ pychemstation/utils/sequence_types.py,sha256=WyJWL18Q86TgoUpYH2_CevoTZuhcui0EnyH
42
31
  pychemstation/utils/spec_utils.py,sha256=MQZPDwCYZRfkEhNJQUt74huPexXBlJ3W4o7_230JWcE,10210
43
32
  pychemstation/utils/table_types.py,sha256=7txqW_oNpkh4venSkGEtreVe6UV9dzNB1DTrIeTkQHA,3217
44
33
  pychemstation/utils/tray_types.py,sha256=eOO-muUjadyvCM8JnYAZVyxJeyYBlENP1zXiFskAFbs,5049
45
- pychemstation-0.10.0.dist-info/licenses/LICENSE,sha256=9bdF75gIf1MecZ7oymqWgJREVz7McXPG-mjqrTmzzD8,18658
46
- tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
47
- tests/constants.py,sha256=4XZkV9pw3PLYy6R-18Pbf5ry2iNmAM-jqXA2MuKKkh4,5422
48
- tests/test_comb.py,sha256=TS-CbtiPbntL4u6E1gSZ6xquNp6cQxIFdJqUr2ak7PA,5515
49
- tests/test_comm.py,sha256=iwl-Ey-xoytXmlNrjG84pDm82Ry_QUX6wY4gmVh4NDc,2516
50
- tests/test_inj.py,sha256=UakPA1Sd1GAbFvFepEredWcWPoW7PMLKotfqVZ1i4hE,1434
51
- tests/test_method.py,sha256=KB7yAtVb4gZftnYzh-VfPb9LGVZOHUIW6OljEYRtbhA,4570
52
- tests/test_nightly.py,sha256=WpLZfs-n9oPrwz-64gWJB5oxJGYz_ab4sLZ4E-xFBpY,2654
53
- tests/test_offline_stable.py,sha256=nUyX-LGiQYMjJ1fvjByFBAUS6eMqaqlq6ufoHb8Ai0E,2911
54
- tests/test_online_stable.py,sha256=f--r8QqGJtx3kDQi1j0pM5v1IOT4SC7B636otZRYfhE,11325
55
- tests/test_proc_rep.py,sha256=sxRiTBybVm6lyAqmgrri-T2EfZgs27oSgbYXSsN9CwU,1380
56
- tests/test_runs_stable.py,sha256=hmsI0ZjJRsjSdiRPqXPqEoIxS2sqZIyybNpQpacRs9Q,10235
57
- tests/test_sequence.py,sha256=vs5-dqkItRds_tPM2-N6MNJ37FB0nLRFaDzBV8d42i8,4880
58
- tests/test_stable.py,sha256=UFb9rUBnOk7JabcweNIYZLWpBHWGbqKAtsTNfxDhEFg,12208
59
- pychemstation-0.10.0.dist-info/METADATA,sha256=8jxjr4cJvOZcxFtKJKwkrriG_prk1tmal1_xPD8ruVM,4998
60
- pychemstation-0.10.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
61
- pychemstation-0.10.0.dist-info/top_level.txt,sha256=zXfKu_4nYWwPHo3OsuhshMNC3SPkcoTGCyODjURaghY,20
62
- pychemstation-0.10.0.dist-info/RECORD,,
34
+ pychemstation-0.10.1.dist-info/METADATA,sha256=gWFw70cbvfeIPV1T3IJDLDktm4FjjXn1JvPfTDQZoDw,5274
35
+ pychemstation-0.10.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
36
+ pychemstation-0.10.1.dist-info/licenses/LICENSE,sha256=9bdF75gIf1MecZ7oymqWgJREVz7McXPG-mjqrTmzzD8,18658
37
+ pychemstation-0.10.1.dist-info/RECORD,,
@@ -1,5 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (78.1.0)
2
+ Generator: hatchling 1.27.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
-
@@ -1,304 +0,0 @@
1
- """
2
- Module contains various utility function for spectral data processing and
3
- analysis.
4
- """
5
-
6
- import numpy as np
7
- import scipy
8
-
9
- from .utils import find_nearest_value_index
10
-
11
-
12
- def create_binary_peak_map(data):
13
- """Return binary map of the peaks within data points.
14
-
15
- True values are assigned to potential peak points, False - to baseline.
16
-
17
- Args:
18
- data (:obj:np.array): 1D array with data points.
19
-
20
- Returns:
21
- :obj:np.array, dtype=bool: Mapping of data points, where True is
22
- potential peak region point, False - baseline.
23
- """
24
- # copying array
25
- data_c = np.copy(data)
26
-
27
- # placeholder for the peak mapping
28
- peak_map = np.full_like(data_c, False, dtype=bool)
29
-
30
- for _ in range(100500): # shouldn't take more iterations
31
-
32
- # looking for peaks
33
- peaks_found = np.logical_or(
34
- data_c > np.mean(data_c) + np.std(data_c) * 3,
35
- data_c < np.mean(data_c) - np.std(data_c) * 3,
36
- )
37
-
38
- # merging with peak mapping
39
- np.logical_or(peak_map, peaks_found, out=peak_map)
40
-
41
- # if no peaks found - break
42
- if not peaks_found.any():
43
- break
44
-
45
- # setting values to 0 and iterating again
46
- data_c[peaks_found] = 0
47
-
48
- return peak_map
49
-
50
-
51
- def combine_map_to_regions(mapping):
52
- """Combine True values into their indexes arrays.
53
-
54
- Args:
55
- mapping (:obj:np.array): Boolean mapping array to extract the indexes
56
- from.
57
-
58
- Returns:
59
- :obj:np.array: 2D array with left and right borders of regions, where
60
- mapping is True.
61
-
62
- Example:
63
- >>> combine_map_to_regions(np.array([True, True, False, True, False]))
64
- array([[0, 1],
65
- [3, 3]])
66
- """
67
-
68
- # No peaks identified, i.e. mapping is all False
69
- if not mapping.any():
70
- return np.array([], dtype="int64")
71
-
72
- # region borders
73
- region_borders = np.diff(mapping)
74
-
75
- # corresponding indexes
76
- border_indexes = np.argwhere(region_borders)
77
-
78
- lefts = border_indexes[::2] + 1 # because diff was used to get the index
79
-
80
- # edge case, where first peak doesn't have left border
81
- if mapping[border_indexes][0]:
82
- # just preppend 0 as first left border
83
- # mind the vstack, as np.argwhere produces a vector array
84
- lefts = np.vstack((0, lefts))
85
-
86
- rights = border_indexes[1::2]
87
-
88
- # another edge case, where last peak doesn't have a right border
89
- if mapping[-1]: # True if last point identified as potential peak
90
- # just append -1 as last peak right border
91
- rights = np.vstack((rights, -1))
92
-
93
- # columns as borders, rows as regions, i.e.
94
- # :output:[0] -> first peak region
95
- return np.hstack((lefts, rights))
96
-
97
-
98
- def filter_regions(x_data, peaks_regions):
99
- """Filter peak regions.
100
-
101
- Peak regions are filtered to remove potential false positives (e.g. noise
102
- spikes).
103
-
104
- Args:
105
- x_data (:obj:np.array): X data points, needed to pick up the data
106
- resolution and map the region indexes to the corresponding data
107
- points.
108
- y_data (:obj:np.array): Y data points, needed to validate if the peaks
109
- are actually present in the region and remove invalid regions.
110
- peaks_regions (:obj:np.array): 2D Nx2 array with peak regions indexes
111
- (rows) as left and right borders (columns).
112
-
113
- Returns:
114
- :obj:np.array: 2D Mx2 array with filtered peak regions indexes(rows) as
115
- left and right borders (columns).
116
- """
117
-
118
- # filter peaks where region is smaller than spectrum resolution
119
- # like single spikes, e.g. noise
120
- # compute the regions first
121
- x_data_regions = np.copy(x_data[peaks_regions])
122
-
123
- # get arguments where absolute difference is greater than data resolution
124
- resolution = np.absolute(np.mean(np.diff(x_data)))
125
-
126
- # (N, 1) array!
127
- valid_regions_map = np.absolute(np.diff(x_data_regions)) > resolution
128
-
129
- # get their indexes, mind the flattening of all arrays!
130
- valid_regions_indexes = np.argwhere(valid_regions_map.flatten()).flatten()
131
-
132
- # filtering!
133
- peaks_regions = peaks_regions[valid_regions_indexes]
134
-
135
- return peaks_regions
136
-
137
-
138
- def filter_noisy_regions(y_data, peaks_regions):
139
- """Remove noisy regions from given regions array.
140
-
141
- Peak regions are filtered to remove false positive noise regions, e.g.
142
- incorrectly assigned due to curvy baseline. Filtering is performed by
143
- computing average peak points/data points ratio.
144
-
145
- Args:
146
- y_data (:obj:np.array): Y data points, needed to validate if the peaks
147
- are actually present in the region and remove invalid regions.
148
- peaks_regions (:obj:np.array): 2D Nx2 array with peak regions indexes
149
- (rows) as left and right borders (columns).
150
-
151
- Returns:
152
- :obj:np.array: 2D Mx2 array with filtered peak regions indexes(rows) as
153
- left and right borders (columns).
154
- """
155
-
156
- # compute the actual regions data points
157
- y_data_regions = []
158
- for region in peaks_regions:
159
- y_data_regions.append(y_data[region[0] : region[-1]])
160
-
161
- # compute noise data regions, i.e. in between peak regions
162
- noise_data_regions = []
163
- for row, _ in enumerate(peaks_regions):
164
- try:
165
- noise_data_regions.append(
166
- y_data[peaks_regions[row][1] : peaks_regions[row + 1][0]]
167
- )
168
- except IndexError:
169
- # exception for the last row -> discard
170
- pass
171
-
172
- # compute average peaks/data points ratio for noisy regions
173
- noise_peaks_ratio = []
174
- for region in noise_data_regions:
175
- # protection from empty regions
176
- if region.size != 0:
177
- # minimum height is pretty low to ensure enough noise is picked
178
- peaks, _ = scipy.signal.find_peaks(region, height=region.max() * 0.2)
179
- noise_peaks_ratio.append(peaks.size / region.size)
180
-
181
- # compute average with weights equal to the region length
182
- noise_peaks_ratio = np.average(
183
- noise_peaks_ratio, weights=[region.size for region in noise_data_regions]
184
- )
185
-
186
- # filtering!
187
- valid_regions_indexes = []
188
- for row, region in enumerate(y_data_regions):
189
- peaks, _ = scipy.signal.find_peaks(region, height=region.max() * 0.2)
190
- if peaks.size != 0 and peaks.size / region.size < noise_peaks_ratio:
191
- valid_regions_indexes.append(row)
192
-
193
- # protecting from complete cleaning
194
- if not valid_regions_indexes:
195
- return peaks_regions
196
-
197
- peaks_regions = peaks_regions[np.array(valid_regions_indexes)]
198
-
199
- return peaks_regions
200
-
201
-
202
- def merge_regions(x_data, peaks_regions, d_merge, recursively=True):
203
- """Merge peak regions if distance between is less than delta.
204
-
205
- Args:
206
- x_data (:obj:np.array): X data points.
207
- peaks_regions (:obj:np.array): 2D Nx2 array with peak regions indexes
208
- (rows) as left and right borders (columns).
209
- d_merge (float): Minimum distance in X data points to merge two or more
210
- regions together.
211
- recursively (bool, optional): If True - will repeat the procedure until
212
- all regions with distance < than d_merge will merge.
213
-
214
- Returns:
215
- :obj:np.array: 2D Mx2 array with peak regions indexes (rows) as left and
216
- right borders (columns), merged according to predefined minimal
217
- distance.
218
-
219
- Example:
220
- >>> regions = np.array([
221
- [1, 10],
222
- [11, 20],
223
- [25, 45],
224
- [50, 75],
225
- [100, 120],
226
- [122, 134]
227
- ])
228
- >>> data = np.ones_like(regions) # ones as example
229
- >>> merge_regions(data, regions, 1)
230
- array([[ 1, 20],
231
- [ 25, 45],
232
- [ 50, 75],
233
- [100, 120],
234
- [122, 134]])
235
- >>> merge_regions(data, regions, 20, True)
236
- array([[ 1, 75],
237
- [100, 134]])
238
- """
239
- # the code is pretty ugly but works
240
- merged_regions = []
241
-
242
- # converting to list to drop the data of the fly
243
- regions = peaks_regions.tolist()
244
-
245
- for i, _ in enumerate(regions):
246
- try:
247
- # check left border of i regions with right of i+1
248
- if abs(x_data[regions[i][-1]] - x_data[regions[i + 1][0]]) <= d_merge:
249
- # if lower append merge the regions
250
- merged_regions.append([regions[i][0], regions[i + 1][-1]])
251
- # drop the merged one
252
- regions.pop(i + 1)
253
- else:
254
- # if nothing to merge, just append the current region
255
- merged_regions.append(regions[i])
256
- except IndexError:
257
- # last row
258
- merged_regions.append(regions[i])
259
-
260
- merged_regions = np.array(merged_regions)
261
-
262
- if not recursively:
263
- return merged_regions
264
-
265
- # if recursively, check for the difference
266
- if (merged_regions == regions).all():
267
- # done
268
- return merged_regions
269
-
270
- return merge_regions(x_data, merged_regions, d_merge, recursively=True)
271
-
272
-
273
- def expand_regions(x_data, peaks_regions, d_expand):
274
- """Expand the peak regions by the desired value.
275
-
276
- Args:
277
- x_data (:obj:np.array): X data points.
278
- peaks_regions (:obj:np.array): 2D Nx2 array with peak regions indexes
279
- (rows) as left and right borders (columns).
280
- d_expand (float): Value to expand borders to (in X data scale).
281
-
282
- Returns:
283
- :obj:np.array: 2D Nx2 array with expanded peak regions indexes (rows) as
284
- left and right borders (columns).
285
- """
286
-
287
- data_regions = np.copy(x_data[peaks_regions])
288
-
289
- # determine scale orientation, i.e. decreasing (e.g. ppm on NMR spectrum)
290
- # or increasing (e.g. wavelength on UV spectrum)
291
- if (data_regions[:, 0] - data_regions[:, 1]).mean() > 0:
292
- # ppm-like scale
293
- data_regions[:, 0] += d_expand
294
- data_regions[:, -1] -= d_expand
295
- else:
296
- # wavelength-like scale
297
- data_regions[:, 0] -= d_expand
298
- data_regions[:, -1] += d_expand
299
-
300
- # converting new values to new indexes
301
- for index_, value in np.ndenumerate(data_regions):
302
- data_regions[index_] = find_nearest_value_index(x_data, value)[1]
303
-
304
- return data_regions.astype(int)
@@ -1,63 +0,0 @@
1
- import numpy as np
2
-
3
-
4
- def find_nearest_value_index(array, value) -> tuple[float, int]:
5
- """Returns closest value and its index in a given array.
6
-
7
- :param array: An array to search in.
8
- :type array: np.array(float)
9
- :param value: Target value.
10
- :type value: float
11
-
12
- :returns: Nearest value in array and its index.
13
- """
14
-
15
- index_ = np.argmin(np.abs(array - value))
16
- return array[index_], index_
17
-
18
-
19
- def interpolate_to_index(array, ids, precision: int = 100) -> np.array:
20
- """Find value in between arrays elements.
21
-
22
- Constructs linspace of size "precision" between index+1 and index to
23
- find approximate value for array[index], where index is float number.
24
- Used for 2D data, where default scipy analysis occurs along one axis only,
25
- e.g. signal.peak_width.
26
-
27
- Rough equivalent of array[index], where index is float number.
28
-
29
- :param array: Target array.
30
- :type array: np.array(float)
31
- :param ids: An array with "intermediate" indexes to interpolate to.
32
- :type ids: np.array[float]
33
- :param precision: Desired presion.
34
-
35
- :returns: New array with interpolated values according to provided indexes "ids".
36
-
37
- Example:
38
- >>> interpolate_to_index(np.array([1.5]), np.array([1,2,3], 100))
39
- array([2.50505051])
40
- """
41
-
42
- # breaking ids into fractional and integral parts
43
- prec, ids = np.modf(ids)
44
-
45
- # rounding and switching type to int
46
- prec = np.around(prec * precision).astype("int32")
47
- ids = ids.astype("int32")
48
-
49
- # linear interpolation for each data point
50
- # as (n x m) matrix where n is precision and m is number of indexes
51
- space = np.linspace(array[ids], array[ids + 1], precision)
52
-
53
- # due to rounding error the index may become 100 in (100, ) array
54
- # as a consequence raising IndexError when such array is indexed
55
- # therefore index 100 will become the last (-1)
56
- prec[prec == 100] = -1
57
-
58
- # precise slicing
59
- true_values = np.array(
60
- [space[:, index[0]][value] for index, value in np.ndenumerate(prec)]
61
- )
62
-
63
- return true_values