pychemstation 0.5.2__py3-none-any.whl → 0.5.3.dev1__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.
@@ -1,232 +0,0 @@
1
- import os
2
- import time
3
-
4
- from xsdata.formats.dataclass.parsers import XmlParser
5
-
6
- from ..control.table_controller import TableController, HPLCController
7
- from ..generated import DadMethod, PumpMethod
8
- from ..utils.chromatogram import TIME_FORMAT
9
- from ..utils.constants import METHOD_TIMETABLE
10
- from ..utils.macro import Command
11
- from ..utils.method_types import MethodTimetable, HPLCMethodParams, Param, PType, TimeTableEntry
12
- from ..utils.table_types import RegisterFlag, TableOperation
13
-
14
-
15
- class MethodController(TableController):
16
- """
17
- Class containing method related logic
18
- """
19
-
20
- def __init__(self, controller: HPLCController, src: str):
21
- super().__init__(controller, src)
22
-
23
- def is_loaded(self, method_name: str):
24
- """Checks if a given method is already loaded into Chemstation. Method name does not need the ".M" extension.
25
-
26
- :param method_name: a Chemstation method
27
- :return: True if method is already loaded
28
- """
29
- self.send(Command.GET_METHOD_CMD)
30
- parsed_response = self.receive().splitlines()[1].split()[1:][0]
31
- return method_name in parsed_response
32
-
33
- def switch(self, method_name: str):
34
- """Allows the user to switch between pre-programmed methods. No need to append '.M'
35
- to the end of the method name. For example. for the method named 'General-Poroshell.M',
36
- only 'General-Poroshell' is needed.
37
-
38
- :param method_name: any available method in Chemstation method directory
39
- :raise IndexError: Response did not have expected format. Try again.
40
- :raise AssertionError: The desired method is not selected. Try again.
41
- """
42
- self.send(Command.SWITCH_METHOD_CMD.value.format(method_dir=self.src,
43
- method_name=method_name))
44
-
45
- time.sleep(2)
46
- self.send(Command.GET_METHOD_CMD)
47
- time.sleep(2)
48
-
49
- # check that method switched
50
- for _ in range(10):
51
- try:
52
- parsed_response = self.receive().splitlines()[1].split()[1:][0]
53
- break
54
- except IndexError:
55
- continue
56
-
57
- assert parsed_response == f"{method_name}.M", "Switching Methods failed."
58
-
59
- def run(self, experiment_name: str):
60
- """This is the preferred method to trigger a run.
61
- Starts the currently selected method, storing data
62
- under the <data_dir>/<experiment_name>.D folder.
63
- The <experiment_name> will be appended with a timestamp in the '%Y-%m-%d-%H-%M' format.
64
- Device must be ready.
65
-
66
- :param experiment_name: Name of the experiment
67
- """
68
- timestamp = time.strftime(TIME_FORMAT)
69
-
70
- self.send(Command.RUN_METHOD_CMD.value.format(data_dir=self.src,
71
- experiment_name=experiment_name,
72
- timestamp=timestamp))
73
-
74
- folder_name = f"{experiment_name}_{timestamp}.D"
75
- self.controller.data_files.append(os.path.join(self.src, folder_name))
76
-
77
- def load(self, method_name: str):
78
- """Retrieve method details of an existing method. Don't need to append ".M" to the end. This assumes the
79
- organic modifier is in Channel B and that Channel A contains the aq layer. Additionally, assumes
80
- only two solvents are being used.
81
-
82
- :param method_name: name of method to load details of
83
- :raises FileNotFoundError: Method does not exist
84
- :return: method details
85
- """
86
- method_folder = f"{method_name}.M"
87
- method_path = os.path.join(self.src, method_folder, "AgilentPumpDriver1.RapidControl.MethodXML.xml")
88
- dad_path = os.path.join(self.src, method_folder, "Agilent1200erDadDriver1.RapidControl.MethodXML.xml")
89
-
90
- if os.path.exists(os.path.join(self.src, f"{method_name}.M")):
91
- parser = XmlParser()
92
- method = parser.parse(method_path, PumpMethod)
93
- dad = parser.parse(dad_path, DadMethod)
94
-
95
- organic_modifier = None
96
- aq_modifier = None
97
-
98
- if len(method.solvent_composition.solvent_element) == 2:
99
- for solvent in method.solvent_composition.solvent_element:
100
- if solvent.channel == "Channel_A":
101
- aq_modifier = solvent
102
- elif solvent.channel == "Channel_B":
103
- organic_modifier = solvent
104
-
105
- return MethodTimetable(
106
- first_row=HPLCMethodParams(
107
- organic_modifier=Param(val=organic_modifier.percentage,
108
- chemstation_key=RegisterFlag.SOLVENT_B_COMPOSITION,
109
- ptype=PType.NUM),
110
- flow=Param(val=method.flow,
111
- chemstation_key=RegisterFlag.FLOW,
112
- ptype=PType.NUM),
113
- maximum_run_time=Param(val=method.stop_time,
114
- chemstation_key=RegisterFlag.MAX_TIME,
115
- ptype=PType.NUM),
116
- temperature=Param(val=None,
117
- chemstation_key=[RegisterFlag.COLUMN_OVEN_TEMP1,
118
- RegisterFlag.COLUMN_OVEN_TEMP2],
119
- ptype=PType.NUM),
120
- inj_vol=Param(val=None,
121
- chemstation_key=None,
122
- ptype=PType.NUM),
123
- equ_time=Param(val=None,
124
- chemstation_key=None,
125
- ptype=PType.NUM)),
126
- subsequent_rows=[
127
- TimeTableEntry(
128
- start_time=tte.time,
129
- organic_modifer=tte.percent_b,
130
- flow=method.flow
131
- ) for tte in method.timetable.timetable_entry
132
- ],
133
- dad_wavelengthes=dad.signals.signal,
134
- organic_modifier=organic_modifier,
135
- modifier_a=aq_modifier
136
- )
137
- else:
138
- raise FileNotFoundError
139
-
140
- def edit(self, updated_method: MethodTimetable):
141
- """Updated the currently loaded method in ChemStation with provided values.
142
-
143
- :param updated_method: the method with updated values, to be sent to Chemstation to modify the currently loaded method.
144
- """
145
- initial_organic_modifier: Param = updated_method.first_row.organic_modifier
146
- max_time: Param = updated_method.first_row.maximum_run_time
147
- temp: Param = updated_method.first_row.temperature
148
- injvol: Param = updated_method.first_row.inj_vol
149
- equalib_time: Param = updated_method.first_row.equ_time
150
- flow: Param = updated_method.first_row.flow
151
-
152
- # Method settings required for all runs
153
- self.send(TableOperation.DELETE_TABLE.value.format(register=METHOD_TIMETABLE.register,
154
- table_name=METHOD_TIMETABLE.name, ))
155
- self._update_param(initial_organic_modifier)
156
- self._update_param(flow)
157
- self._update_param(Param(val="Set",
158
- chemstation_key=RegisterFlag.STOPTIME_MODE,
159
- ptype=PType.STR))
160
- self._update_param(max_time)
161
- self._update_param(Param(val="Off",
162
- chemstation_key=RegisterFlag.POSTIME_MODE,
163
- ptype=PType.STR))
164
-
165
- self.send("DownloadRCMethod PMP1")
166
-
167
- self._update_method_timetable(updated_method.subsequent_rows)
168
-
169
- def _update_method_timetable(self, timetable_rows: list[TimeTableEntry]):
170
- self.sleepy_send('Local Rows')
171
- self._get_table_rows(METHOD_TIMETABLE)
172
-
173
- self.sleepy_send('DelTab RCPMP1Method[1], "Timetable"')
174
- res = self._get_table_rows(METHOD_TIMETABLE)
175
- while "ERROR" not in res:
176
- self.sleepy_send('DelTab RCPMP1Method[1], "Timetable"')
177
- res = self._get_table_rows(METHOD_TIMETABLE)
178
-
179
- self.sleepy_send('NewTab RCPMP1Method[1], "Timetable"')
180
- self._get_table_rows(METHOD_TIMETABLE)
181
-
182
- for i, row in enumerate(timetable_rows):
183
- if i == 0:
184
- self.send('Sleep 1')
185
- self.sleepy_send('InsTabRow RCPMP1Method[1], "Timetable"')
186
- self.send('Sleep 1')
187
-
188
- self.sleepy_send('NewColText RCPMP1Method[1], "Timetable", "Function", "SolventComposition"')
189
- self.sleepy_send(f'NewColVal RCPMP1Method[1], "Timetable", "Time", {row.start_time}')
190
- self.sleepy_send(
191
- f'NewColVal RCPMP1Method[1], "Timetable", "SolventCompositionPumpChannel2_Percentage", {row.organic_modifer}')
192
-
193
- self.send('Sleep 1')
194
- self.sleepy_send("DownloadRCMethod PMP1")
195
- self.send('Sleep 1')
196
- else:
197
- self.sleepy_send('InsTabRow RCPMP1Method[1], "Timetable"')
198
- self._get_table_rows(METHOD_TIMETABLE)
199
-
200
- self.sleepy_send(
201
- f'SetTabText RCPMP1Method[1], "Timetable", Rows, "Function", "SolventComposition"')
202
- self.sleepy_send(
203
- f'SetTabVal RCPMP1Method[1], "Timetable", Rows, "Time", {row.start_time}')
204
- self.sleepy_send(
205
- f'SetTabVal RCPMP1Method[1], "Timetable", Rows, "SolventCompositionPumpChannel2_Percentage", {row.organic_modifer}')
206
-
207
- self.send("Sleep 1")
208
- self.sleepy_send("DownloadRCMethod PMP1")
209
- self.send("Sleep 1")
210
- self._get_table_rows(METHOD_TIMETABLE)
211
-
212
- def _update_param(self, method_param: Param):
213
- """Change a method parameter, changes what is visibly seen in Chemstation GUI. (changes the first row in the timetable)
214
-
215
- :param method_param: a parameter to update for currently loaded method.
216
- """
217
- register = METHOD_TIMETABLE.register
218
- setting_command = TableOperation.UPDATE_OBJ_HDR_VAL if method_param.ptype == PType.NUM else TableOperation.UPDATE_OBJ_HDR_TEXT
219
- if isinstance(method_param.chemstation_key, list):
220
- for register_flag in method_param.chemstation_key:
221
- self.send(setting_command.value.format(register=register,
222
- register_flag=register_flag,
223
- val=method_param.val))
224
- else:
225
- self.send(setting_command.value.format(register=register,
226
- register_flag=method_param.chemstation_key,
227
- val=method_param.val))
228
- time.sleep(2)
229
-
230
- def stop(self):
231
- """Stops the run. A dialog window will pop up and manual intervention may be required."""
232
- self.send(Command.STOP_METHOD_CMD)
@@ -1,140 +0,0 @@
1
- import os
2
- import time
3
-
4
- from ..control.table_controller import TableController, HPLCController
5
- from ..utils.chromatogram import SEQUENCE_TIME_FORMAT
6
- from ..utils.constants import SEQUENCE_TABLE
7
- from ..utils.macro import Command
8
- from ..utils.sequence_types import SequenceTable, SequenceEntry
9
- from ..utils.table_types import RegisterFlag, TableOperation
10
-
11
-
12
- class SequenceController(TableController):
13
- """
14
- Class containing sequence related logic
15
- """
16
-
17
- def __init__(self, controller: HPLCController, src: str):
18
- super().__init__(controller, src)
19
-
20
- def switch(self, seq_name: str):
21
- """Switch to the specified sequence. The sequence name does not need the '.S' extension.
22
-
23
- :param seq_name: The name of the sequence file
24
- """
25
- self.send(f'_SeqFile$ = "{seq_name}.S"')
26
- self.send(f'_SeqPath$ = "{self.src}"')
27
- self.send(Command.SWITCH_SEQUENCE_CMD)
28
- time.sleep(2)
29
- self.send(Command.GET_SEQUENCE_CMD)
30
- time.sleep(2)
31
- # check that method switched
32
- for _ in range(10):
33
- try:
34
- parsed_response = self.receive().splitlines()[1].split()[1:][0]
35
- break
36
- except IndexError:
37
- continue
38
-
39
- assert parsed_response == f"{seq_name}.S", "Switching sequence failed."
40
-
41
- def run(self, sequence_table: SequenceTable):
42
- """Starts the currently loaded sequence, storing data
43
- under the <data_dir>/<sequence table name> folder.
44
- The <sequence table name> will be appended with a timestamp in the "%Y %m %d %H %m %s" format.
45
- Device must be ready.
46
-
47
- :param sequence_table:
48
- """
49
- timestamp = time.strftime(SEQUENCE_TIME_FORMAT)
50
- self.send(Command.RUN_SEQUENCE_CMD.value)
51
- self.send(Command.RUN_SEQUENCE_CMD.value)
52
- folder_name = f"{sequence_table.name} {timestamp}"
53
- subdirs = [x[0] for x in os.walk(self.src)]
54
- time.sleep(60)
55
- potential_folders = sorted(list(filter(lambda d: folder_name in d, subdirs)))
56
- self.controller.data_files.append(potential_folders[0])
57
-
58
- def edit(self, sequence_table: SequenceTable):
59
- """Updates the currently loaded sequence table with the provided table. This method will delete the existing sequence table and remake it.
60
- If you would only like to edit a single row of a sequence table, use `edit_sequence_table_row` instead.
61
-
62
- :param sequence_table:
63
- """
64
- self.send("Local Rows")
65
- self.sleep(1)
66
- self.delete_table(SEQUENCE_TABLE)
67
- self.sleep(1)
68
- self.new_table(SEQUENCE_TABLE)
69
- self.sleep(1)
70
- self._get_table_rows(SEQUENCE_TABLE)
71
- for _ in sequence_table.rows:
72
- self.add_table_row(SEQUENCE_TABLE)
73
- self.sleep(1)
74
- self.send(Command.SAVE_SEQUENCE_CMD)
75
- self.sleep(1)
76
- self._get_table_rows(SEQUENCE_TABLE)
77
- self.send(Command.SWITCH_SEQUENCE_CMD)
78
- for i, row in enumerate(sequence_table.rows):
79
- self.edit(row=row, row_num=i + 0)
80
- self.sleep(1)
81
- self.send(Command.SAVE_SEQUENCE_CMD)
82
- self.sleep(1)
83
-
84
- def edit_row(self, row: SequenceEntry, row_num: int):
85
- """Edits a row in the sequence table. Assumes the row already exists.
86
-
87
- :param row: sequence row entry with updated information
88
- :param row_num: the row to edit, based on -1-based indexing
89
- """
90
- if row.vial_location:
91
- self.sleepy_send(TableOperation.EDIT_ROW_VAL.value.format(register=SEQUENCE_TABLE.register,
92
- table_name=SEQUENCE_TABLE.name,
93
- row=row_num,
94
- col_name=RegisterFlag.VIAL_LOCATION,
95
- val=row.vial_location))
96
- if row.method:
97
- self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=SEQUENCE_TABLE.register,
98
- table_name=SEQUENCE_TABLE.name,
99
- row=row_num,
100
- col_name=RegisterFlag.METHOD,
101
- val=row.method))
102
-
103
- if row.num_inj:
104
- self.sleepy_send(TableOperation.EDIT_ROW_VAL.value.format(register=SEQUENCE_TABLE.register,
105
- table_name=SEQUENCE_TABLE.name,
106
- row=row_num,
107
- col_name=RegisterFlag.NUM_INJ,
108
- val=row.num_inj))
109
-
110
- if row.inj_vol:
111
- self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=SEQUENCE_TABLE.register,
112
- table_name=SEQUENCE_TABLE.name,
113
- row=row_num,
114
- col_name=RegisterFlag.INJ_VOL,
115
- val=row.inj_vol))
116
-
117
- if row.inj_source:
118
- self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=SEQUENCE_TABLE.register,
119
- table_name=SEQUENCE_TABLE.name,
120
- row=row_num,
121
- col_name=RegisterFlag.INJ_SOR,
122
- val=row.inj_source))
123
-
124
- if row.sample_name:
125
- self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=SEQUENCE_TABLE.register,
126
- table_name=SEQUENCE_TABLE.name,
127
- row=row_num,
128
- col_name=RegisterFlag.NAME,
129
- val=row.sample_name))
130
- self.sleepy_send(TableOperation.EDIT_ROW_TEXT.value.format(register=SEQUENCE_TABLE.register,
131
- table_name=SEQUENCE_TABLE.name,
132
- row=row_num,
133
- col_name=RegisterFlag.DATA_FILE,
134
- val=row.sample_name))
135
- if row.sample_type:
136
- self.sleepy_send(TableOperation.EDIT_ROW_VAL.value.format(register=SEQUENCE_TABLE.register,
137
- table_name=SEQUENCE_TABLE.name,
138
- row=row_num,
139
- col_name=RegisterFlag.SAMPLE_TYPE,
140
- val=row.sample_type))
@@ -1,75 +0,0 @@
1
- import os
2
- from typing import Union
3
-
4
-
5
- from .comm import HPLCController
6
- from ..utils.macro import Command
7
- from ..utils.table_types import Table, TableOperation, RegisterFlag
8
-
9
-
10
- class TableController:
11
-
12
- def __init__(self, controller: HPLCController, src: str):
13
- self.controller = controller
14
- if os.path.isdir(src):
15
- self.src: str = src
16
- else:
17
- raise FileNotFoundError(f"dir: {src} not found.")
18
-
19
- def sleep(self, seconds: int):
20
- self.controller.sleep(seconds)
21
-
22
- def receive(self):
23
- self.controller.receive()
24
-
25
- def send(self, cmd: Union[Command, str]):
26
- self.controller.send(cmd)
27
-
28
- def sleepy_send(self, cmd: Union[Command, str]):
29
- self.controller.sleepy_send(cmd)
30
-
31
- def add_table_row(self, table: Table):
32
- """Adds a row to the provided table for currently loaded method or sequence.
33
- Import either the SEQUENCE_TABLE or METHOD_TIMETABLE from hein_analytical_control.constants.
34
- You can also provide your own table.
35
-
36
- :param table: the table to add a new row to
37
- """
38
- self.sleepy_send(TableOperation.NEW_ROW.value.format(register=table.register,
39
- table_name=table.name))
40
-
41
- def delete_table(self, table: Table):
42
- """Deletes the table for the current loaded method or sequence.
43
- Import either the SEQUENCE_TABLE or METHOD_TIMETABLE from hein_analytical_control.constants.
44
- You can also provide your own table.
45
-
46
- :param table: the table to delete
47
- """
48
- self.sleepy_send(TableOperation.DELETE_TABLE.value.format(register=table.register,
49
- table_name=table.name))
50
-
51
- def new_table(self, table: Table):
52
- """Creates the table for the currently loaded method or sequence. Import either the SEQUENCE_TABLE or
53
- METHOD_TIMETABLE from hein_analytical_control.constants. You can also provide your own table.
54
-
55
- :param table: the table to create
56
- """
57
- self.send(TableOperation.CREATE_TABLE.value.format(register=table.register,
58
- table_name=table.name))
59
-
60
- def _get_table_rows(self, table: Table) -> str:
61
- self.send(TableOperation.GET_OBJ_HDR_VAL.value.format(internal_val="Rows",
62
- register=table.register,
63
- table_name=table.name,
64
- col_name=RegisterFlag.NUM_ROWS, ))
65
- res = self.controller.receive()
66
- self.send("Sleep 1")
67
- self.send('Print Rows')
68
- return res
69
-
70
- def get_data(self):
71
- self.controller.get_spectrum()
72
- return self.controller.spectra["A"]
73
-
74
- def data_ready(self) -> bool:
75
- return self.controller.check_hplc_ready_with_data()