pychemstation 0.10.0__py3-none-any.whl → 0.10.2__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.
- pychemstation/control/README.md +132 -0
- pychemstation/control/controllers/README.md +1 -0
- {pychemstation-0.10.0.dist-info → pychemstation-0.10.2.dist-info}/METADATA +11 -9
- {pychemstation-0.10.0.dist-info → pychemstation-0.10.2.dist-info}/RECORD +6 -31
- {pychemstation-0.10.0.dist-info → pychemstation-0.10.2.dist-info}/WHEEL +1 -2
- pychemstation/analysis/spec_utils.py +0 -304
- pychemstation/analysis/utils.py +0 -63
- pychemstation/control/comm.py +0 -206
- pychemstation/control/controllers/devices/column.py +0 -12
- pychemstation/control/controllers/devices/dad.py +0 -0
- pychemstation/control/controllers/devices/pump.py +0 -43
- pychemstation/control/controllers/method.py +0 -338
- pychemstation/control/controllers/sequence.py +0 -190
- pychemstation/control/controllers/table_controller.py +0 -266
- pychemstation/control/table/__init__.py +0 -3
- pychemstation/control/table/method.py +0 -274
- pychemstation/control/table/sequence.py +0 -210
- pychemstation/control/table/table_controller.py +0 -201
- pychemstation-0.10.0.dist-info/top_level.txt +0 -2
- tests/__init__.py +0 -0
- tests/constants.py +0 -134
- tests/test_comb.py +0 -136
- tests/test_comm.py +0 -65
- tests/test_inj.py +0 -39
- tests/test_method.py +0 -99
- tests/test_nightly.py +0 -80
- tests/test_offline_stable.py +0 -69
- tests/test_online_stable.py +0 -275
- tests/test_proc_rep.py +0 -52
- tests/test_runs_stable.py +0 -225
- tests/test_sequence.py +0 -125
- tests/test_stable.py +0 -276
- {pychemstation-0.10.0.dist-info → pychemstation-0.10.2.dist-info}/licenses/LICENSE +0 -0
@@ -1,274 +0,0 @@
|
|
1
|
-
import os
|
2
|
-
import time
|
3
|
-
from typing import Optional, Any
|
4
|
-
|
5
|
-
from xsdata.formats.dataclass.parsers import XmlParser
|
6
|
-
|
7
|
-
from .. import CommunicationController
|
8
|
-
from ...control.table.table_controller import TableController
|
9
|
-
from ...generated import PumpMethod, DadMethod, SolventElement
|
10
|
-
from ...utils.chromatogram import TIME_FORMAT, AgilentHPLCChromatogram
|
11
|
-
from ...utils.macro import Command
|
12
|
-
from ...utils.method_types import PType, TimeTableEntry, Param, MethodTimetable, HPLCMethodParams
|
13
|
-
from ...utils.table_types import RegisterFlag, TableOperation, Table
|
14
|
-
|
15
|
-
|
16
|
-
class MethodController(TableController):
|
17
|
-
"""
|
18
|
-
Class containing method related logic
|
19
|
-
"""
|
20
|
-
|
21
|
-
def __init__(self, controller: CommunicationController, src: str, data_dir: str, table: Table):
|
22
|
-
super().__init__(controller, src, data_dir, table)
|
23
|
-
|
24
|
-
def get_method_params(self) -> HPLCMethodParams:
|
25
|
-
return HPLCMethodParams(organic_modifier=self.controller.get_num_val(
|
26
|
-
cmd=TableOperation.GET_OBJ_HDR_VAL.value.format(
|
27
|
-
register=self.table.register,
|
28
|
-
register_flag=RegisterFlag.SOLVENT_B_COMPOSITION
|
29
|
-
)
|
30
|
-
),
|
31
|
-
flow=self.controller.get_num_val(
|
32
|
-
cmd=TableOperation.GET_OBJ_HDR_VAL.value.format(
|
33
|
-
register=self.table.register,
|
34
|
-
register_flag=RegisterFlag.FLOW
|
35
|
-
)
|
36
|
-
),
|
37
|
-
maximum_run_time=self.controller.get_num_val(
|
38
|
-
cmd=TableOperation.GET_OBJ_HDR_VAL.value.format(
|
39
|
-
register=self.table.register,
|
40
|
-
register_flag=RegisterFlag.MAX_TIME
|
41
|
-
)
|
42
|
-
),
|
43
|
-
)
|
44
|
-
|
45
|
-
def get_row(self, row: int) -> TimeTableEntry:
|
46
|
-
return TimeTableEntry(start_time=self.get_num(row, RegisterFlag.TIME),
|
47
|
-
organic_modifer=self.get_num(row, RegisterFlag.TIMETABLE_SOLVENT_B_COMPOSITION),
|
48
|
-
flow=None)
|
49
|
-
|
50
|
-
def load(self) -> MethodTimetable:
|
51
|
-
rows = self.get_num_rows()
|
52
|
-
if rows.is_ok():
|
53
|
-
timetable_rows = [self.get_row(r + 1) for r in range(int(rows.ok_value.num_response))]
|
54
|
-
return MethodTimetable(
|
55
|
-
first_row=self.get_method_params(),
|
56
|
-
subsequent_rows=timetable_rows)
|
57
|
-
else:
|
58
|
-
raise RuntimeError(rows.err_value)
|
59
|
-
|
60
|
-
def current_method(self, method_name: str):
|
61
|
-
"""
|
62
|
-
Checks if a given method is already loaded into Chemstation. Method name does not need the ".M" extension.
|
63
|
-
|
64
|
-
:param method_name: a Chemstation method
|
65
|
-
:return: True if method is already loaded
|
66
|
-
"""
|
67
|
-
self.send(Command.GET_METHOD_CMD)
|
68
|
-
parsed_response = self.receive()
|
69
|
-
return method_name in parsed_response
|
70
|
-
|
71
|
-
def switch(self, method_name: str):
|
72
|
-
"""
|
73
|
-
Allows the user to switch between pre-programmed methods. No need to append '.M'
|
74
|
-
to the end of the method name. For example. for the method named 'General-Poroshell.M',
|
75
|
-
only 'General-Poroshell' is needed.
|
76
|
-
|
77
|
-
:param method_name: any available method in Chemstation method directory
|
78
|
-
:raise IndexError: Response did not have expected format. Try again.
|
79
|
-
:raise AssertionError: The desired method is not selected. Try again.
|
80
|
-
"""
|
81
|
-
self.send(Command.SWITCH_METHOD_CMD.value.format(method_dir=self.src,
|
82
|
-
method_name=method_name))
|
83
|
-
|
84
|
-
time.sleep(2)
|
85
|
-
self.send(Command.GET_METHOD_CMD)
|
86
|
-
time.sleep(2)
|
87
|
-
res = self.receive()
|
88
|
-
if res.is_ok():
|
89
|
-
parsed_response = res.ok_value.string_response
|
90
|
-
assert parsed_response == f"{method_name}.M", "Switching Methods failed."
|
91
|
-
|
92
|
-
def load_from_disk(self, method_name: str) -> MethodTimetable:
|
93
|
-
"""
|
94
|
-
Retrieve method details of an existing method. Don't need to append ".M" to the end. This assumes the
|
95
|
-
organic modifier is in Channel B and that Channel A contains the aq layer. Additionally, assumes
|
96
|
-
only two solvents are being used.
|
97
|
-
|
98
|
-
:param method_name: name of method to load details of
|
99
|
-
:raises FileNotFoundError: Method does not exist
|
100
|
-
:return: method details
|
101
|
-
"""
|
102
|
-
method_folder = f"{method_name}.M"
|
103
|
-
method_path = os.path.join(self.src, method_folder, "AgilentPumpDriver1.RapidControl.MethodXML.xml")
|
104
|
-
dad_path = os.path.join(self.src, method_folder, "Agilent1200erDadDriver1.RapidControl.MethodXML.xml")
|
105
|
-
|
106
|
-
if os.path.exists(os.path.join(self.src, f"{method_name}.M")):
|
107
|
-
parser = XmlParser()
|
108
|
-
method = parser.parse(method_path, PumpMethod)
|
109
|
-
dad = parser.parse(dad_path, DadMethod)
|
110
|
-
|
111
|
-
organic_modifier: Optional[SolventElement] = None
|
112
|
-
aq_modifier: Optional[SolventElement] = None
|
113
|
-
|
114
|
-
if len(method.solvent_composition.solvent_element) == 2:
|
115
|
-
for solvent in method.solvent_composition.solvent_element:
|
116
|
-
if solvent.channel == "Channel_A":
|
117
|
-
aq_modifier = solvent
|
118
|
-
elif solvent.channel == "Channel_B":
|
119
|
-
organic_modifier = solvent
|
120
|
-
|
121
|
-
return MethodTimetable(
|
122
|
-
first_row=HPLCMethodParams(
|
123
|
-
organic_modifier=organic_modifier.percentage,
|
124
|
-
flow=method.flow,
|
125
|
-
maximum_run_time=method.stop_time,
|
126
|
-
temperature=-1),
|
127
|
-
subsequent_rows=[
|
128
|
-
TimeTableEntry(
|
129
|
-
start_time=tte.time,
|
130
|
-
organic_modifer=tte.percent_b,
|
131
|
-
flow=method.flow
|
132
|
-
) for tte in method.timetable.timetable_entry
|
133
|
-
],
|
134
|
-
dad_wavelengthes=dad.signals.signal,
|
135
|
-
organic_modifier=organic_modifier,
|
136
|
-
modifier_a=aq_modifier
|
137
|
-
)
|
138
|
-
else:
|
139
|
-
raise FileNotFoundError
|
140
|
-
|
141
|
-
def edit(self, updated_method: MethodTimetable, save: bool):
|
142
|
-
"""Updated the currently loaded method in ChemStation with provided values.
|
143
|
-
|
144
|
-
:param updated_method: the method with updated values, to be sent to Chemstation to modify the currently loaded method.
|
145
|
-
"""
|
146
|
-
initial_organic_modifier: Param = Param(val=updated_method.first_row.organic_modifier,
|
147
|
-
chemstation_key=RegisterFlag.SOLVENT_B_COMPOSITION,
|
148
|
-
ptype=PType.NUM)
|
149
|
-
max_time: Param = Param(val=updated_method.first_row.maximum_run_time,
|
150
|
-
chemstation_key=RegisterFlag.MAX_TIME,
|
151
|
-
ptype=PType.NUM)
|
152
|
-
flow: Param = Param(val=updated_method.first_row.flow,
|
153
|
-
chemstation_key=RegisterFlag.FLOW,
|
154
|
-
ptype=PType.NUM)
|
155
|
-
|
156
|
-
# Method settings required for all runs
|
157
|
-
self.delete_table()
|
158
|
-
self._update_param(initial_organic_modifier)
|
159
|
-
self._update_param(flow)
|
160
|
-
self._update_param(Param(val="Set",
|
161
|
-
chemstation_key=RegisterFlag.STOPTIME_MODE,
|
162
|
-
ptype=PType.STR))
|
163
|
-
self._update_param(max_time)
|
164
|
-
self._update_param(Param(val="Off",
|
165
|
-
chemstation_key=RegisterFlag.POSTIME_MODE,
|
166
|
-
ptype=PType.STR))
|
167
|
-
|
168
|
-
self.send("DownloadRCMethod PMP1")
|
169
|
-
|
170
|
-
self._update_method_timetable(updated_method.subsequent_rows)
|
171
|
-
|
172
|
-
if save:
|
173
|
-
self.send(Command.SAVE_METHOD_CMD.value.format(
|
174
|
-
commit_msg=f"saved method at {str(time.time())}"
|
175
|
-
))
|
176
|
-
|
177
|
-
def _update_method_timetable(self, timetable_rows: list[TimeTableEntry]):
|
178
|
-
self.sleepy_send('Local Rows')
|
179
|
-
self.get_num_rows()
|
180
|
-
|
181
|
-
self.sleepy_send('DelTab RCPMP1Method[1], "Timetable"')
|
182
|
-
res = self.get_num_rows()
|
183
|
-
while not res.is_err():
|
184
|
-
self.sleepy_send('DelTab RCPMP1Method[1], "Timetable"')
|
185
|
-
res = self.get_num_rows()
|
186
|
-
|
187
|
-
self.sleepy_send('NewTab RCPMP1Method[1], "Timetable"')
|
188
|
-
self.get_num_rows()
|
189
|
-
|
190
|
-
for i, row in enumerate(timetable_rows):
|
191
|
-
if i == 0:
|
192
|
-
self.send('Sleep 1')
|
193
|
-
self.sleepy_send('InsTabRow RCPMP1Method[1], "Timetable"')
|
194
|
-
self.send('Sleep 1')
|
195
|
-
|
196
|
-
self.sleepy_send('NewColText RCPMP1Method[1], "Timetable", "Function", "SolventComposition"')
|
197
|
-
self.sleepy_send(f'NewColVal RCPMP1Method[1], "Timetable", "Time", {row.start_time}')
|
198
|
-
self.sleepy_send(
|
199
|
-
f'NewColVal RCPMP1Method[1], "Timetable", "SolventCompositionPumpChannel2_Percentage", {row.organic_modifer}')
|
200
|
-
|
201
|
-
self.send('Sleep 1')
|
202
|
-
self.sleepy_send("DownloadRCMethod PMP1")
|
203
|
-
self.send('Sleep 1')
|
204
|
-
else:
|
205
|
-
self.sleepy_send('InsTabRow RCPMP1Method[1], "Timetable"')
|
206
|
-
self.get_num_rows()
|
207
|
-
|
208
|
-
self.sleepy_send(
|
209
|
-
f'SetTabText RCPMP1Method[1], "Timetable", Rows, "Function", "SolventComposition"')
|
210
|
-
self.sleepy_send(
|
211
|
-
f'SetTabVal RCPMP1Method[1], "Timetable", Rows, "Time", {row.start_time}')
|
212
|
-
self.sleepy_send(
|
213
|
-
f'SetTabVal RCPMP1Method[1], "Timetable", Rows, "SolventCompositionPumpChannel2_Percentage", {row.organic_modifer}')
|
214
|
-
|
215
|
-
self.send("Sleep 1")
|
216
|
-
self.sleepy_send("DownloadRCMethod PMP1")
|
217
|
-
self.send("Sleep 1")
|
218
|
-
self.get_num_rows()
|
219
|
-
|
220
|
-
def _update_param(self, method_param: Param):
|
221
|
-
"""Change a method parameter, changes what is visibly seen in Chemstation GUI.
|
222
|
-
(changes the first row in the timetable)
|
223
|
-
|
224
|
-
:param method_param: a parameter to update for currently loaded method.
|
225
|
-
"""
|
226
|
-
register = self.table.register
|
227
|
-
setting_command = TableOperation.UPDATE_OBJ_HDR_VAL if method_param.ptype == PType.NUM else TableOperation.UPDATE_OBJ_HDR_TEXT
|
228
|
-
if isinstance(method_param.chemstation_key, list):
|
229
|
-
for register_flag in method_param.chemstation_key:
|
230
|
-
self.send(setting_command.value.format(register=register,
|
231
|
-
register_flag=register_flag,
|
232
|
-
val=method_param.val))
|
233
|
-
else:
|
234
|
-
self.send(setting_command.value.format(register=register,
|
235
|
-
register_flag=method_param.chemstation_key,
|
236
|
-
val=method_param.val))
|
237
|
-
time.sleep(2)
|
238
|
-
|
239
|
-
def stop(self):
|
240
|
-
"""
|
241
|
-
Stops the method run. A dialog window will pop up and manual intervention may be required.\
|
242
|
-
"""
|
243
|
-
self.send(Command.STOP_METHOD_CMD)
|
244
|
-
|
245
|
-
def run(self, experiment_name: str):
|
246
|
-
"""
|
247
|
-
This is the preferred method to trigger a run.
|
248
|
-
Starts the currently selected method, storing data
|
249
|
-
under the <data_dir>/<experiment_name>.D folder.
|
250
|
-
The <experiment_name> will be appended with a timestamp in the '%Y-%m-%d-%H-%M' format.
|
251
|
-
Device must be ready.
|
252
|
-
|
253
|
-
:param experiment_name: Name of the experiment
|
254
|
-
"""
|
255
|
-
timestamp = time.strftime(TIME_FORMAT)
|
256
|
-
self.send(Command.RUN_METHOD_CMD.value.format(data_dir=self.data_dir,
|
257
|
-
experiment_name=experiment_name,
|
258
|
-
timestamp=timestamp))
|
259
|
-
|
260
|
-
if self.check_hplc_is_running():
|
261
|
-
folder_name = f"{experiment_name}_{timestamp}.D"
|
262
|
-
self.data_files.append(os.path.join(self.data_dir, folder_name))
|
263
|
-
|
264
|
-
run_completed = self.check_hplc_done_running()
|
265
|
-
|
266
|
-
if not run_completed.is_ok():
|
267
|
-
raise RuntimeError("Run did not complete as expected")
|
268
|
-
|
269
|
-
def retrieve_recent_data_files(self) -> str:
|
270
|
-
return self.data_files[-1]
|
271
|
-
|
272
|
-
def get_data(self) -> dict[str, AgilentHPLCChromatogram]:
|
273
|
-
self.get_spectrum(self.data_files[-1])
|
274
|
-
return self.spectra
|
@@ -1,210 +0,0 @@
|
|
1
|
-
from typing import Any
|
2
|
-
|
3
|
-
from copy import deepcopy
|
4
|
-
|
5
|
-
import os
|
6
|
-
import time
|
7
|
-
|
8
|
-
from .table_controller import TableController
|
9
|
-
from ...control import CommunicationController
|
10
|
-
from ...utils.chromatogram import SEQUENCE_TIME_FORMAT, AgilentHPLCChromatogram
|
11
|
-
from ...utils.macro import Command
|
12
|
-
from ...utils.sequence_types import SequenceTable, SequenceEntry, SequenceDataFiles, InjectionSource, SampleType
|
13
|
-
from ...utils.table_types import TableOperation, RegisterFlag, Table
|
14
|
-
from ...utils.tray_types import TenColumn
|
15
|
-
|
16
|
-
|
17
|
-
class SequenceController(TableController):
|
18
|
-
"""
|
19
|
-
Class containing sequence related logic
|
20
|
-
"""
|
21
|
-
|
22
|
-
def __init__(self, controller: CommunicationController, src: str, data_dir: str, table: Table, method_dir: str):
|
23
|
-
self.method_dir = method_dir
|
24
|
-
super().__init__(controller, src, data_dir, table)
|
25
|
-
|
26
|
-
def load(self) -> SequenceTable:
|
27
|
-
rows = self.get_num_rows()
|
28
|
-
self.send(Command.GET_SEQUENCE_CMD)
|
29
|
-
seq_name = self.receive()
|
30
|
-
|
31
|
-
if rows.is_ok() and seq_name.is_ok():
|
32
|
-
return SequenceTable(
|
33
|
-
name=seq_name.ok_value.string_response.partition(".S")[0],
|
34
|
-
rows=[self.get_row(r + 1) for r in range(int(rows.ok_value.num_response))])
|
35
|
-
raise RuntimeError(rows.err_value)
|
36
|
-
|
37
|
-
def get_row(self, row: int) -> SequenceEntry:
|
38
|
-
sample_name = self.get_text(row, RegisterFlag.NAME)
|
39
|
-
vial_location = int(self.get_num(row, RegisterFlag.VIAL_LOCATION))
|
40
|
-
method = self.get_text(row, RegisterFlag.METHOD)
|
41
|
-
num_inj = int(self.get_num(row, RegisterFlag.NUM_INJ))
|
42
|
-
inj_vol = int(self.get_text(row, RegisterFlag.INJ_VOL))
|
43
|
-
inj_source = InjectionSource(self.get_text(row, RegisterFlag.INJ_SOR))
|
44
|
-
sample_type = SampleType(self.get_num(row, RegisterFlag.SAMPLE_TYPE))
|
45
|
-
return SequenceEntry(sample_name=sample_name,
|
46
|
-
vial_location=vial_location,
|
47
|
-
method=None if len(method) == 0 else method,
|
48
|
-
num_inj=num_inj,
|
49
|
-
inj_vol=inj_vol,
|
50
|
-
inj_source=inj_source,
|
51
|
-
sample_type=sample_type, )
|
52
|
-
|
53
|
-
def switch(self, seq_name: str):
|
54
|
-
"""
|
55
|
-
Switch to the specified sequence. The sequence name does not need the '.S' extension.
|
56
|
-
|
57
|
-
:param seq_name: The name of the sequence file
|
58
|
-
"""
|
59
|
-
self.send(f'_SeqFile$ = "{seq_name}.S"')
|
60
|
-
self.send(f'_SeqPath$ = "{self.src}"')
|
61
|
-
self.send(Command.SWITCH_SEQUENCE_CMD)
|
62
|
-
time.sleep(2)
|
63
|
-
self.send(Command.GET_SEQUENCE_CMD)
|
64
|
-
time.sleep(2)
|
65
|
-
parsed_response = self.receive().value.string_response
|
66
|
-
|
67
|
-
assert parsed_response == f"{seq_name}.S", "Switching sequence failed."
|
68
|
-
|
69
|
-
def edit(self, sequence_table: SequenceTable):
|
70
|
-
"""
|
71
|
-
Updates the currently loaded sequence table with the provided table. This method will delete the existing sequence table and remake it.
|
72
|
-
If you would only like to edit a single row of a sequence table, use `edit_sequence_table_row` instead.
|
73
|
-
|
74
|
-
:param sequence_table:
|
75
|
-
"""
|
76
|
-
|
77
|
-
rows = self.get_num_rows()
|
78
|
-
if rows.is_ok():
|
79
|
-
existing_row_num = rows.value.num_response
|
80
|
-
wanted_row_num = len(sequence_table.rows)
|
81
|
-
while existing_row_num != wanted_row_num:
|
82
|
-
if wanted_row_num > existing_row_num:
|
83
|
-
self.add_row()
|
84
|
-
elif wanted_row_num < existing_row_num:
|
85
|
-
self.delete_row(int(existing_row_num))
|
86
|
-
self.send(Command.SAVE_SEQUENCE_CMD)
|
87
|
-
existing_row_num = self.get_num_rows().ok_value.num_response
|
88
|
-
self.send(Command.SWITCH_SEQUENCE_CMD)
|
89
|
-
|
90
|
-
for i, row in enumerate(sequence_table.rows):
|
91
|
-
self.edit_row(row=row, row_num=i + 1)
|
92
|
-
self.sleep(1)
|
93
|
-
self.send(Command.SAVE_SEQUENCE_CMD)
|
94
|
-
self.send(Command.SWITCH_SEQUENCE_CMD)
|
95
|
-
|
96
|
-
def edit_row(self, row: SequenceEntry, row_num: int):
|
97
|
-
"""
|
98
|
-
Edits a row in the sequence table. If a row does NOT exist, a new one will be created.
|
99
|
-
|
100
|
-
:param row: sequence row entry with updated information
|
101
|
-
:param row_num: the row to edit, based on 1-based indexing
|
102
|
-
"""
|
103
|
-
num_rows = self.get_num_rows()
|
104
|
-
if num_rows.is_ok():
|
105
|
-
while num_rows.ok_value.num_response < row_num:
|
106
|
-
self.add_row()
|
107
|
-
self.send(Command.SAVE_SEQUENCE_CMD)
|
108
|
-
num_rows = self.get_num_rows()
|
109
|
-
|
110
|
-
table_register = self.table.register
|
111
|
-
table_name = self.table.name
|
112
|
-
|
113
|
-
if row.vial_location:
|
114
|
-
loc = row.vial_location
|
115
|
-
if isinstance(row.vial_location, InjectionSource):
|
116
|
-
loc = row.vial_location.value
|
117
|
-
self.sleepy_send(TableOperation.EDIT_ROW_VAL.value.format(register=table_register,
|
118
|
-
table_name=table_name,
|
119
|
-
row=row_num,
|
120
|
-
col_name=RegisterFlag.VIAL_LOCATION,
|
121
|
-
val=loc))
|
122
|
-
if row.method:
|
123
|
-
possible_path = os.path.join(self.method_dir, row.method) + ".M\\"
|
124
|
-
method = row.method
|
125
|
-
if os.path.exists(possible_path):
|
126
|
-
method = os.path.join(self.method_dir, row.method)
|
127
|
-
self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=table_register,
|
128
|
-
table_name=table_name,
|
129
|
-
row=row_num,
|
130
|
-
col_name=RegisterFlag.METHOD,
|
131
|
-
val=method))
|
132
|
-
|
133
|
-
if row.num_inj:
|
134
|
-
self.sleepy_send(TableOperation.EDIT_ROW_VAL.value.format(register=table_register,
|
135
|
-
table_name=table_name,
|
136
|
-
row=row_num,
|
137
|
-
col_name=RegisterFlag.NUM_INJ,
|
138
|
-
val=row.num_inj))
|
139
|
-
|
140
|
-
if row.inj_vol:
|
141
|
-
self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=table_register,
|
142
|
-
table_name=table_name,
|
143
|
-
row=row_num,
|
144
|
-
col_name=RegisterFlag.INJ_VOL,
|
145
|
-
val=row.inj_vol))
|
146
|
-
|
147
|
-
if row.inj_source:
|
148
|
-
self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=table_register,
|
149
|
-
table_name=table_name,
|
150
|
-
row=row_num,
|
151
|
-
col_name=RegisterFlag.INJ_SOR,
|
152
|
-
val=row.inj_source.value))
|
153
|
-
|
154
|
-
if row.sample_name:
|
155
|
-
self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=table_register,
|
156
|
-
table_name=table_name,
|
157
|
-
row=row_num,
|
158
|
-
col_name=RegisterFlag.NAME,
|
159
|
-
val=row.sample_name))
|
160
|
-
self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=table_register,
|
161
|
-
table_name=table_name,
|
162
|
-
row=row_num,
|
163
|
-
col_name=RegisterFlag.DATA_FILE,
|
164
|
-
val=row.sample_name))
|
165
|
-
if row.sample_type:
|
166
|
-
self.sleepy_send(TableOperation.EDIT_ROW_VAL.value.format(register=table_register,
|
167
|
-
table_name=table_name,
|
168
|
-
row=row_num,
|
169
|
-
col_name=RegisterFlag.SAMPLE_TYPE,
|
170
|
-
val=row.sample_type.value))
|
171
|
-
|
172
|
-
self.send(Command.SAVE_SEQUENCE_CMD)
|
173
|
-
|
174
|
-
def run(self):
|
175
|
-
"""
|
176
|
-
Starts the currently loaded sequence, storing data
|
177
|
-
under the <data_dir>/<sequence table name> folder.
|
178
|
-
Device must be ready.
|
179
|
-
"""
|
180
|
-
timestamp = time.strftime(SEQUENCE_TIME_FORMAT)
|
181
|
-
seq_table = self.load()
|
182
|
-
self.send(Command.RUN_SEQUENCE_CMD.value)
|
183
|
-
|
184
|
-
if self.check_hplc_is_running():
|
185
|
-
folder_name = f"{seq_table.name} {timestamp}"
|
186
|
-
self.data_files.append(SequenceDataFiles(dir=folder_name,
|
187
|
-
sequence_name=seq_table.name))
|
188
|
-
|
189
|
-
run_completed = self.check_hplc_done_running(sequence=seq_table)
|
190
|
-
|
191
|
-
if run_completed.is_ok():
|
192
|
-
self.data_files[-1].dir = run_completed.value
|
193
|
-
else:
|
194
|
-
raise RuntimeError("Run error has occured.")
|
195
|
-
|
196
|
-
def retrieve_recent_data_files(self):
|
197
|
-
sequence_data_files: SequenceDataFiles = self.data_files[-1]
|
198
|
-
return sequence_data_files.dir
|
199
|
-
|
200
|
-
def get_data(self) -> list[dict[str, AgilentHPLCChromatogram]]:
|
201
|
-
parent_dir = self.data_files[-1].dir
|
202
|
-
subdirs = [x[0] for x in os.walk(self.data_dir)]
|
203
|
-
potential_folders = sorted(list(filter(lambda d: parent_dir in d, subdirs)))
|
204
|
-
self.data_files[-1].child_dirs = [f for f in potential_folders if parent_dir in f and ".M" not in f and ".D" in f]
|
205
|
-
|
206
|
-
spectra: list[dict[str, AgilentHPLCChromatogram]] = []
|
207
|
-
for row in self.data_files[-1].child_dirs:
|
208
|
-
self.get_spectrum(row)
|
209
|
-
spectra.append(deepcopy(self.spectra))
|
210
|
-
return spectra
|
@@ -1,201 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
Abstract module containing shared logic for Method and Sequence tables.
|
3
|
-
|
4
|
-
Authors: Lucy Hao
|
5
|
-
"""
|
6
|
-
|
7
|
-
import abc
|
8
|
-
import os
|
9
|
-
import time
|
10
|
-
from typing import Union, Optional
|
11
|
-
|
12
|
-
import polling
|
13
|
-
from result import Result, Ok, Err
|
14
|
-
|
15
|
-
from ...control.controllers.comm import CommunicationController
|
16
|
-
from ...utils.chromatogram import AgilentHPLCChromatogram, AgilentChannelChromatogramData
|
17
|
-
from ...utils.macro import Command, HPLCRunningStatus, Response
|
18
|
-
from ...utils.method_types import MethodTimetable
|
19
|
-
from ...utils.sequence_types import SequenceDataFiles, SequenceTable
|
20
|
-
from ...utils.table_types import Table, TableOperation, RegisterFlag
|
21
|
-
|
22
|
-
|
23
|
-
class TableController(abc.ABC):
|
24
|
-
|
25
|
-
def __init__(self, controller: CommunicationController, src: str, data_dir: str, table: Table):
|
26
|
-
self.controller = controller
|
27
|
-
self.table = table
|
28
|
-
|
29
|
-
if os.path.isdir(src):
|
30
|
-
self.src: str = src
|
31
|
-
else:
|
32
|
-
raise FileNotFoundError(f"dir: {src} not found.")
|
33
|
-
|
34
|
-
if os.path.isdir(data_dir):
|
35
|
-
self.data_dir: str = data_dir
|
36
|
-
else:
|
37
|
-
raise FileNotFoundError(f"dir: {data_dir} not found.")
|
38
|
-
|
39
|
-
self.spectra: dict[str, AgilentHPLCChromatogram] = {
|
40
|
-
"A": AgilentHPLCChromatogram(self.data_dir),
|
41
|
-
"B": AgilentHPLCChromatogram(self.data_dir),
|
42
|
-
"C": AgilentHPLCChromatogram(self.data_dir),
|
43
|
-
"D": AgilentHPLCChromatogram(self.data_dir),
|
44
|
-
}
|
45
|
-
|
46
|
-
self.data_files: Union[list[SequenceDataFiles], list[str]] = []
|
47
|
-
|
48
|
-
def receive(self) -> Result[Response, str]:
|
49
|
-
for _ in range(10):
|
50
|
-
try:
|
51
|
-
return self.controller.receive()
|
52
|
-
except IndexError:
|
53
|
-
continue
|
54
|
-
return Err("Could not parse response")
|
55
|
-
|
56
|
-
def send(self, cmd: Union[Command, str]):
|
57
|
-
self.controller.send(cmd)
|
58
|
-
|
59
|
-
def sleepy_send(self, cmd: Union[Command, str]):
|
60
|
-
self.controller.sleepy_send(cmd)
|
61
|
-
|
62
|
-
def sleep(self, seconds: int):
|
63
|
-
"""
|
64
|
-
Tells the HPLC to wait for a specified number of seconds.
|
65
|
-
|
66
|
-
:param seconds: number of seconds to wait
|
67
|
-
"""
|
68
|
-
self.send(Command.SLEEP_CMD.value.format(seconds=seconds))
|
69
|
-
|
70
|
-
def get_num(self, row: int, col_name: RegisterFlag) -> float:
|
71
|
-
return self.controller.get_num_val(TableOperation.GET_ROW_VAL.value.format(register=self.table.register,
|
72
|
-
table_name=self.table.name,
|
73
|
-
row=row,
|
74
|
-
col_name=col_name.value))
|
75
|
-
|
76
|
-
def get_text(self, row: int, col_name: RegisterFlag) -> str:
|
77
|
-
return self.controller.get_text_val(TableOperation.GET_ROW_TEXT.value.format(register=self.table.register,
|
78
|
-
table_name=self.table.name,
|
79
|
-
row=row,
|
80
|
-
col_name=col_name.value))
|
81
|
-
|
82
|
-
@abc.abstractmethod
|
83
|
-
def get_row(self, row: int):
|
84
|
-
pass
|
85
|
-
|
86
|
-
def delete_row(self, row: int):
|
87
|
-
self.sleepy_send(TableOperation.DELETE_ROW.value.format(register=self.table.register,
|
88
|
-
table_name=self.table.name,
|
89
|
-
row=row))
|
90
|
-
|
91
|
-
def add_row(self):
|
92
|
-
"""
|
93
|
-
Adds a row to the provided table for currently loaded method or sequence.
|
94
|
-
Import either the SEQUENCE_TABLE or METHOD_TIMETABLE from hein_analytical_control.constants.
|
95
|
-
You can also provide your own table.
|
96
|
-
|
97
|
-
:param table: the table to add a new row to
|
98
|
-
"""
|
99
|
-
self.sleepy_send(TableOperation.NEW_ROW.value.format(register=self.table.register,
|
100
|
-
table_name=self.table.name))
|
101
|
-
|
102
|
-
def delete_table(self):
|
103
|
-
"""
|
104
|
-
Deletes the table for the current loaded method or sequence.
|
105
|
-
Import either the SEQUENCE_TABLE or METHOD_TIMETABLE from hein_analytical_control.constants.
|
106
|
-
You can also provide your own table.
|
107
|
-
|
108
|
-
:param table: the table to delete
|
109
|
-
"""
|
110
|
-
self.sleepy_send(TableOperation.DELETE_TABLE.value.format(register=self.table.register,
|
111
|
-
table_name=self.table.name))
|
112
|
-
|
113
|
-
def new_table(self):
|
114
|
-
"""
|
115
|
-
Creates the table for the currently loaded method or sequence. Import either the SEQUENCE_TABLE or
|
116
|
-
METHOD_TIMETABLE from hein_analytical_control.constants. You can also provide your own table.
|
117
|
-
|
118
|
-
:param table: the table to create
|
119
|
-
"""
|
120
|
-
self.send(TableOperation.CREATE_TABLE.value.format(register=self.table.register,
|
121
|
-
table_name=self.table.name))
|
122
|
-
|
123
|
-
def get_num_rows(self) -> Result[int, str]:
|
124
|
-
self.send(TableOperation.GET_NUM_ROWS.value.format(register=self.table.register,
|
125
|
-
table_name=self.table.name,
|
126
|
-
col_name=RegisterFlag.NUM_ROWS))
|
127
|
-
self.send(Command.GET_ROWS_CMD.value.format(register=self.table.register,
|
128
|
-
table_name=self.table.name,
|
129
|
-
col_name=RegisterFlag.NUM_ROWS))
|
130
|
-
res = self.controller.receive()
|
131
|
-
|
132
|
-
if res.is_ok():
|
133
|
-
self.send("Sleep 0.1")
|
134
|
-
self.send('Print Rows')
|
135
|
-
return res
|
136
|
-
else:
|
137
|
-
return Err("No rows could be read.")
|
138
|
-
|
139
|
-
def check_hplc_is_running(self) -> bool:
|
140
|
-
started_running = polling.poll(
|
141
|
-
lambda: isinstance(self.controller.get_status(), HPLCRunningStatus),
|
142
|
-
step=5,
|
143
|
-
max_tries=100)
|
144
|
-
return started_running
|
145
|
-
|
146
|
-
def check_hplc_done_running(self,
|
147
|
-
method: Optional[MethodTimetable] = None,
|
148
|
-
sequence: Optional[SequenceTable] = None) -> Result[str, str]:
|
149
|
-
"""
|
150
|
-
Checks if ChemStation has finished running and can read data back
|
151
|
-
|
152
|
-
:param method: if you are running a method and want to read back data, the timeout period will be adjusted to be longer than the method's runtime
|
153
|
-
:return: Return True if data can be read back, else False.
|
154
|
-
"""
|
155
|
-
timeout = 10 * 60
|
156
|
-
if method:
|
157
|
-
timeout = ((method.first_row.maximum_run_time + 2) * 60)
|
158
|
-
if sequence:
|
159
|
-
timeout *= len(sequence.rows)
|
160
|
-
|
161
|
-
most_recent_folder = self.retrieve_recent_data_files()
|
162
|
-
finished_run = polling.poll(
|
163
|
-
lambda: self.controller.check_if_running(),
|
164
|
-
timeout=timeout,
|
165
|
-
step=12
|
166
|
-
)
|
167
|
-
|
168
|
-
if finished_run:
|
169
|
-
if os.path.exists(most_recent_folder):
|
170
|
-
return Ok(most_recent_folder)
|
171
|
-
else:
|
172
|
-
return self.fuzzy_match_most_recent_folder(most_recent_folder)
|
173
|
-
else:
|
174
|
-
return Err("Run did not complete as expected")
|
175
|
-
|
176
|
-
def fuzzy_match_most_recent_folder(self, most_recent_folder) -> Result[str, str]:
|
177
|
-
subdirs = [x[0] for x in os.walk(self.data_dir)]
|
178
|
-
potential_folders = sorted(list(filter(lambda d: most_recent_folder in d, subdirs)))
|
179
|
-
parent_dirs = []
|
180
|
-
for folder in potential_folders:
|
181
|
-
path = os.path.normpath(folder)
|
182
|
-
split_folder = path.split(os.sep)
|
183
|
-
if most_recent_folder in split_folder[-1]:
|
184
|
-
parent_dirs.append(folder)
|
185
|
-
parent_dir = sorted(parent_dirs, reverse=True)[0]
|
186
|
-
return Ok(parent_dir)
|
187
|
-
|
188
|
-
@abc.abstractmethod
|
189
|
-
def retrieve_recent_data_files(self):
|
190
|
-
pass
|
191
|
-
|
192
|
-
@abc.abstractmethod
|
193
|
-
def get_data(self) -> Union[list[AgilentChannelChromatogramData], AgilentChannelChromatogramData]:
|
194
|
-
pass
|
195
|
-
|
196
|
-
def get_spectrum(self, data_file: str):
|
197
|
-
"""
|
198
|
-
Load chromatogram for any channel in spectra dictionary.
|
199
|
-
"""
|
200
|
-
for channel, spec in self.spectra.items():
|
201
|
-
spec.load_spectrum(data_path=data_file, channel=channel)
|