sapiopycommons 2025.2.14a436__py3-none-any.whl → 2025.2.17a437__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.

Potentially problematic release.


This version of sapiopycommons might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sapiopycommons
3
- Version: 2025.2.14a436
3
+ Version: 2025.2.17a437
4
4
  Summary: Official Sapio Python API Utilities Package
5
5
  Project-URL: Homepage, https://github.com/sapiosciences
6
6
  Author-email: Jonathan Steck <jsteck@sapiosciences.com>, Yechen Qiao <yqiao@sapiosciences.com>
@@ -1,6 +1,4 @@
1
1
  sapiopycommons/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- sapiopycommons/ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- sapiopycommons/ai/tool_of_tools.py,sha256=zdQIgZV0ISyE75GhWY-YHy6V9ZM6FM4xXZvEmlJ0lME,33737
4
2
  sapiopycommons/callbacks/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
3
  sapiopycommons/callbacks/callback_util.py,sha256=68lPK8GtE6zd8C0fQaMLxT57PtU-6HnO37Zk-u56Mrs,129933
6
4
  sapiopycommons/callbacks/field_builder.py,sha256=p2XacN99MuKk3ite8GAqstUMpixqugul2CsC4gB83-o,38620
@@ -58,7 +56,7 @@ sapiopycommons/webhook/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
58
56
  sapiopycommons/webhook/webhook_context.py,sha256=D793uLsb1691SalaPnBUk3rOSxn_hYLhdvkaIxjNXss,1909
59
57
  sapiopycommons/webhook/webhook_handlers.py,sha256=L0HetSm43NvA5KyW3xbLpGFh2DbAaeZJVtXIEl2fvV8,39689
60
58
  sapiopycommons/webhook/webservice_handlers.py,sha256=Y5dHx_UFWFuSqaoPL6Re-fsKYRuxvCWZ8bj6KSZ3jfM,14285
61
- sapiopycommons-2025.2.14a436.dist-info/METADATA,sha256=8bjTtK1h5j5TnmkppQddbsbMOj5Bz9E5wO60JoVfnEs,3143
62
- sapiopycommons-2025.2.14a436.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
63
- sapiopycommons-2025.2.14a436.dist-info/licenses/LICENSE,sha256=HyVuytGSiAUQ6ErWBHTqt1iSGHhLmlC8fO7jTCuR8dU,16725
64
- sapiopycommons-2025.2.14a436.dist-info/RECORD,,
59
+ sapiopycommons-2025.2.17a437.dist-info/METADATA,sha256=PWJpeeghHQyDO4hbeo61_euWtC0ecVPpEbvUerj5Esk,3143
60
+ sapiopycommons-2025.2.17a437.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
61
+ sapiopycommons-2025.2.17a437.dist-info/licenses/LICENSE,sha256=HyVuytGSiAUQ6ErWBHTqt1iSGHhLmlC8fO7jTCuR8dU,16725
62
+ sapiopycommons-2025.2.17a437.dist-info/RECORD,,
File without changes
@@ -1,718 +0,0 @@
1
- import base64
2
- import io
3
- import math
4
- import re
5
- from typing import Final, Mapping, Any
6
-
7
- from pandas import DataFrame
8
- from sapiopylib.rest.DataRecordManagerService import DataRecordManager
9
- from sapiopylib.rest.ELNService import ElnManager
10
- from sapiopylib.rest.User import SapioUser
11
- from sapiopylib.rest.pojo.DataRecord import DataRecord
12
- from sapiopylib.rest.pojo.chartdata.DashboardDefinition import GaugeChartDefinition
13
- from sapiopylib.rest.pojo.chartdata.DashboardEnums import ChartGroupingType, ChartOperationType
14
- from sapiopylib.rest.pojo.chartdata.DashboardSeries import GaugeChartSeries
15
- from sapiopylib.rest.pojo.datatype.FieldDefinition import AbstractVeloxFieldDefinition, FieldType
16
- from sapiopylib.rest.pojo.eln.ElnEntryPosition import ElnEntryPosition
17
- from sapiopylib.rest.pojo.eln.ElnExperiment import ElnExperiment
18
- from sapiopylib.rest.pojo.eln.ExperimentEntry import ExperimentEntry
19
- from sapiopylib.rest.pojo.eln.ExperimentEntryCriteria import ElnEntryCriteria, ElnFormEntryUpdateCriteria, \
20
- ElnDashboardEntryUpdateCriteria, ElnTextEntryUpdateCriteria
21
- from sapiopylib.rest.pojo.eln.SapioELNEnums import ElnEntryType, ElnBaseDataType
22
- from sapiopylib.rest.pojo.eln.eln_headings import ElnExperimentTabAddCriteria, ElnExperimentTab
23
- from sapiopylib.rest.pojo.eln.field_set import ElnFieldSetInfo
24
- from sapiopylib.rest.utils.ProtocolUtils import ELNStepFactory
25
- from sapiopylib.rest.utils.Protocols import ElnEntryStep, ElnExperimentProtocol
26
-
27
- from sapiopycommons.callbacks.field_builder import FieldBuilder
28
- from sapiopycommons.general.aliases import AliasUtil, SapioRecord
29
- from sapiopycommons.general.exceptions import SapioException
30
- from sapiopycommons.general.time_util import TimeUtil
31
- from sapiopycommons.multimodal.multimodal import MultiModalManager
32
- from sapiopycommons.multimodal.multimodal_data import ImageDataRequestPojo
33
-
34
- CREDENTIALS_HEADER: Final[str] = "SAPIO_APP_API_KEY"
35
- API_URL_HEADER: Final[str] = "SAPIO_APP_API_URL"
36
- EXP_ID_HEADER: Final[str] = "EXPERIMENT_ID"
37
- TAB_PREFIX_HEADER: Final[str] = "TAB_PREFIX"
38
-
39
-
40
- # FR-47422: Create utility classes and methods to assist the tool of tools.
41
- def create_tot_headers(url: str, username: str, password: str, experiment_id: int, tab_prefix: str) \
42
- -> dict[str, str]:
43
- """
44
- Create the headers to be passed to a tool of tools endpoint.
45
-
46
- :param url: The webservice URL of the system to make the changes in.
47
- :param username: The username of the user making the changes.
48
- :param password: The password of the user making the changes.
49
- :param experiment_id: The ID of the experiment to make the changes in.
50
- :param tab_prefix: The prefix to use for the tab name that will be created by the tool.
51
- :return: The headers to be passed to the endpoint.
52
- """
53
- # Combine the credentials into the format "username:password"
54
- credentials: str = f"{username}:{password}"
55
- # Encode the credentials to bytes, then encode them using base64,
56
- # and finally convert the result back into a string.
57
- encoded_credentials: str = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
58
- headers: dict[str, str] = {
59
- CREDENTIALS_HEADER: f"Basic {encoded_credentials}",
60
- API_URL_HEADER: url,
61
- EXP_ID_HEADER: str(experiment_id),
62
- TAB_PREFIX_HEADER: tab_prefix
63
- }
64
- return headers
65
-
66
-
67
- def create_user_from_tot_headers(headers: Mapping[str, str]) -> SapioUser:
68
- """
69
- Create a SapioUser object from the headers passed to a tool of tools endpoint.
70
-
71
- :param headers: The headers that were passed to the endpoint.
72
- :return: A SapioUser object created from the headers that can be used to communicate with the Sapio server.
73
- """
74
- headers: dict[str, str] = format_tot_headers(headers)
75
- credentials = (base64.b64decode(headers[CREDENTIALS_HEADER.lower()].removeprefix("Basic "))
76
- .decode("utf-8").split(":", 1))
77
- return SapioUser(headers[API_URL_HEADER.lower()], username=credentials[0], password=credentials[1])
78
-
79
-
80
- def format_tot_headers(headers: Mapping[str, str]) -> dict[str, str]:
81
- """
82
- Format the headers passed to a tool of tools endpoint to guarantee that the keys are lowercase.
83
-
84
- :param headers: The headers that were passed to the endpoint.
85
- :return: The headers with all keys converted to lowercase. (Conflicting keys will cause one to overwrite the other,
86
- but there should not be any conflicting keys in the headers passed to a tool of tools endpoint.)
87
- """
88
- return {k.lower(): v for k, v in headers.items()}
89
-
90
-
91
- class HtmlFormatter:
92
- """
93
- A class for formatting text in HTML with tag classes supported by the client.
94
- """
95
- TIMESTAMP_TEXT__CSS_CLASS_NAME: Final[str] = "timestamp-text"
96
- HEADER_1_TEXT__CSS_CLASS_NAME: Final[str] = "header1-text"
97
- HEADER_2_TEXT__CSS_CLASS_NAME: Final[str] = "header2-text"
98
- HEADER_3_TEXT__CSS_CLASS_NAME: Final[str] = "header3-text"
99
- BODY_TEXT__CSS_CLASS_NAME: Final[str] = "body-text"
100
- CAPTION_TEXT__CSS_CLASS_NAME: Final[str] = "caption-text"
101
-
102
- @staticmethod
103
- def timestamp(text: str) -> str:
104
- """
105
- Given a text string, return that same text string HTML formatted using the timestamp CSS class.
106
-
107
- :param text: The text to format.
108
- :return: The HTML formatted text.
109
- """
110
- return f"<span class=\"{HtmlFormatter.TIMESTAMP_TEXT__CSS_CLASS_NAME}\">{text}</span>"
111
-
112
- @staticmethod
113
- def header_1(text: str) -> str:
114
- """
115
- Given a text string, return that same text string HTML formatted using the header 1 CSS class.
116
-
117
- :param text: The text to format.
118
- :return: The HTML formatted text.
119
- """
120
- return f"<span class=\"{HtmlFormatter.HEADER_1_TEXT__CSS_CLASS_NAME}\">{text}</span>"
121
-
122
- @staticmethod
123
- def header_2(text: str) -> str:
124
- """
125
- Given a text string, return that same text string HTML formatted using the header 2 CSS class.
126
-
127
- :param text: The text to format.
128
- :return: The HTML formatted text.
129
- """
130
- return f"<span class=\"{HtmlFormatter.HEADER_2_TEXT__CSS_CLASS_NAME}\">{text}</span>"
131
-
132
- @staticmethod
133
- def header_3(text: str) -> str:
134
- """
135
- Given a text string, return that same text string HTML formatted using the header 3 CSS class.
136
-
137
- :param text: The text to format.
138
- :return: The HTML formatted text.
139
- """
140
- return f"<span class=\"{HtmlFormatter.HEADER_3_TEXT__CSS_CLASS_NAME}\">{text}</span>"
141
-
142
- @staticmethod
143
- def body(text: str) -> str:
144
- """
145
- Given a text string, return that same text string HTML formatted using the body CSS class.
146
-
147
- :param text: The text to format.
148
- :return: The HTML formatted text.
149
- """
150
- return f"<span class=\"{HtmlFormatter.BODY_TEXT__CSS_CLASS_NAME}\">{text}</span>"
151
-
152
- @staticmethod
153
- def caption(text: str) -> str:
154
- """
155
- Given a text string, return that same text string HTML formatted using the caption CSS class.
156
-
157
- :param text: The text to format.
158
- :return: The HTML formatted text.
159
- """
160
- return f"<span class=\"{HtmlFormatter.CAPTION_TEXT__CSS_CLASS_NAME}\">{text}</span>"
161
-
162
- @staticmethod
163
- def replace_newlines(text: str) -> str:
164
- """
165
- Given a text string, return that same text string HTML formatted with newlines replaced by HTML line breaks.
166
-
167
- :param text: The text to format.
168
- :return: The HTML formatted text.
169
- """
170
- return re.sub("\r?\n", "<br>", text)
171
-
172
-
173
- class AiHelper:
174
- """
175
- A class with helper methods for the AI to make use of when creating/updating experiment tabs and entries.
176
- """
177
- # Contextual info.
178
- user: SapioUser
179
- exp_id: int
180
-
181
- # Managers.
182
- dr_man: DataRecordManager
183
- eln_man: ElnManager
184
-
185
- def __init__(self, user: SapioUser, exp_id: int):
186
- """
187
- :param user: The user to send the requests from.
188
- :param exp_id: The ID of the experiment to create the entries in.
189
- """
190
- self.user = user
191
- self.exp_id = exp_id
192
-
193
- self.dr_man = DataRecordManager(self.user)
194
- self.eln_man = ElnManager(self.user)
195
-
196
- @property
197
- def protocol(self) -> ElnExperimentProtocol:
198
- """
199
- :return: An experiment protocol object for this helper's experiment. (Recreating a new protocol object every
200
- time this is called since the protocol's cache could be invalidated by things that the AI is doing.)
201
- """
202
- # The experiment name and record ID aren't necessary to know for our purposes.
203
- return ElnExperimentProtocol(ElnExperiment(self.exp_id, "", 0), self.user)
204
-
205
- def create_tab(self, name: str) -> ElnExperimentTab:
206
- """
207
- Create a new tab in the experiment.
208
-
209
- :param name: The name of the tab to create.
210
- :return: The newly created tab.
211
- """
212
- tab_crit = ElnExperimentTabAddCriteria(name, [])
213
- return self.eln_man.add_tab_for_experiment(self.exp_id, tab_crit)
214
-
215
- def tab_next_entry_order(self, tab: ElnExperimentTab) -> int:
216
- """
217
- :param tab: A tab in this helper's experiment.
218
- :return: The order that the next entry that gets created in the tab should have.
219
- """
220
- max_order: int = 0
221
- for step in self.protocol.get_sorted_step_list():
222
- if step.eln_entry.notebook_experiment_tab_id == tab.tab_id and step.eln_entry.order > max_order:
223
- max_order = step.eln_entry.order
224
- return max_order + 1
225
-
226
- def create_experiment_details_from_data_frame(self,
227
- tab: ElnExperimentTab,
228
- entry_name: str,
229
- df: DataFrame) -> ExperimentEntry | None:
230
- """
231
- Create an experiment detail entry from a DataFrame.
232
-
233
- :param tab: The tab that the entry should be added to.
234
- :param entry_name: The name of the entry.
235
- :param df: The DataFrame to create the entry from.
236
- :return: The newly created experiment detail entry.
237
- """
238
- json_list: list[dict[str, Any]] = []
239
- for _, row in df.iterrows():
240
- json_list.append(row.to_dict())
241
- return self.create_experiment_details_from_json(tab, entry_name, json_list)
242
-
243
- def create_experiment_details_from_json(self,
244
- tab: ElnExperimentTab,
245
- entry_name: str,
246
- json_list: list[dict[str, Any]]) -> ExperimentEntry | None:
247
- """
248
- Create an experiment detail entry from a list of JSON dictionaries.
249
-
250
- :param tab: The tab that the entry should be added to.
251
- :param entry_name: The name of the entry.
252
- :param json_list: The list of JSON dictionaries to create the entry from. Each dictionary is expected to have the
253
- same keys.
254
- :return: The newly created experiment detail entry.
255
- """
256
- if not json_list:
257
- return None
258
-
259
- # Determine which fields in the JSON can be used to create field definitions.
260
- fb = FieldBuilder()
261
- fields: list[AbstractVeloxFieldDefinition] = []
262
- fields_by_name: dict[str, AbstractVeloxFieldDefinition] = {}
263
- for key, value in json_list[0].items():
264
- field_name: str = key.replace(" ", "_")
265
- if isinstance(value, str):
266
- field = fb.string_field(field_name, display_name=key)
267
- fields.append(field)
268
- fields_by_name[key] = field
269
- elif isinstance(value, (int, float)):
270
- field = fb.double_field(field_name, display_name=key, precision=3)
271
- fields.append(field)
272
- fields_by_name[key] = field
273
-
274
- # Extract the valid field values from the JSON.
275
- field_maps: list[dict[str, Any]] = []
276
- for json_dict in json_list:
277
- field_map: dict[str, Any] = {}
278
- for key, field in fields_by_name.items():
279
- # Watch out for NaN values or other special values.
280
- val: Any = json_dict.get(key)
281
- if (field.data_field_type == FieldType.DOUBLE
282
- and (not isinstance(val, (int, float))) or (isinstance(val, float) and math.isnan(val))):
283
- val = None
284
- field_map[field.data_field_name] = val
285
- field_maps.append(field_map)
286
-
287
- detail_entry = ElnEntryCriteria(ElnEntryType.Table, entry_name,
288
- ElnBaseDataType.EXPERIMENT_DETAIL.data_type_name,
289
- self.tab_next_entry_order(tab),
290
- notebook_experiment_tab_id=tab.tab_id,
291
- field_definition_list=fields)
292
- entry = self.eln_man.add_experiment_entry(self.exp_id, detail_entry)
293
- self.dr_man.add_data_records_with_data(entry.data_type_name, field_maps)
294
- return entry
295
-
296
- def create_text_entry(self, tab: ElnExperimentTab, timestamp: str, description: str, auto_format: bool = True) \
297
- -> ExperimentEntry:
298
- """
299
- Create a new text entry in the experiment.
300
-
301
- :param tab: The tab to create the text entry in.
302
- :param timestamp: The timestamp to display at the top of the text entry.
303
- :param description: The description to display in the text entry.
304
- :param auto_format: Whether to automatically format the text to be added.
305
- :return: The newly created text entry.
306
- """
307
- if auto_format:
308
- description: str = f"<p>{HtmlFormatter.timestamp(timestamp)}<br>{HtmlFormatter.body(description)}</p>"
309
- else:
310
- description: str = f"<p>{timestamp}<br>{description}</p>"
311
- position = ElnEntryPosition(tab.tab_id, self.tab_next_entry_order(tab))
312
- text_entry: ElnEntryStep = ELNStepFactory.create_text_entry(self.protocol, description, position)
313
- return text_entry.eln_entry
314
-
315
- def set_text_entry(self, text_entry: ExperimentEntry, timestamp: str, description: str,
316
- auto_format: bool = True) -> None:
317
- """
318
- Set the text of a text entry.
319
-
320
- :param text_entry: The text entry to set the text of.
321
- :param timestamp: The timestamp to display at the top of the text entry.
322
- :param description: The description to display in the text entry.
323
- :param auto_format: Whether to automatically format the text to be added.
324
- """
325
- if auto_format:
326
- timestamp = HtmlFormatter.timestamp(timestamp)
327
- description = HtmlFormatter.body(description)
328
- description: str = f"<p>{timestamp}<br>{description}</p>"
329
- step = ElnEntryStep(self.protocol, text_entry)
330
- text_record: DataRecord = step.get_records()[0]
331
- text_record.set_field_value(ElnBaseDataType.get_text_entry_data_field_name(), description)
332
- self.dr_man.commit_data_records([text_record])
333
-
334
- def add_to_text_entry(self, text_entry: ExperimentEntry, description: str, auto_format: bool = True) -> None:
335
- """
336
- Add to the text of a text entry.
337
-
338
- :param text_entry: The text entry to add the text to.
339
- :param description: The text to add to the text entry.
340
- :param auto_format: Whether to automatically format the text to be added.
341
- """
342
- step = ElnEntryStep(self.protocol, text_entry)
343
- text_record: DataRecord = step.get_records()[0]
344
- update: str = text_record.get_field_value(ElnBaseDataType.get_text_entry_data_field_name())
345
- if auto_format:
346
- description = HtmlFormatter.body(description)
347
- update += f"<p style=\"padding-top: 10px;\">{description}</p>"
348
- text_record.set_field_value(ElnBaseDataType.get_text_entry_data_field_name(), update)
349
- self.dr_man.commit_data_records([text_record])
350
-
351
- def create_attachment_entry(self, tab: ElnExperimentTab, entry_name: str, file_name: str, file_data: str | bytes) \
352
- -> ExperimentEntry:
353
- """
354
- Add a new attachment entry to the experiment with the provided attachment data.
355
-
356
- :param tab: The tab where the attachment entry will be added.
357
- :param entry_name: Name of the attachment entry to create in the experiment.
358
- :param file_name: The name of the attachment.
359
- :param file_data: The data of the attachment. This can be a string or bytes.
360
- :return: The newly created attachment entry.
361
- """
362
- tab_id: int = tab.tab_id
363
-
364
- # Encode the file contents in base64.
365
- if isinstance(file_data, str):
366
- file_data: bytes = file_data.encode("utf-8")
367
- base64_encoded: str = base64.b64encode(file_data).decode("utf-8")
368
-
369
- # Crete an attachment entry with the provided data.
370
- attachment_entry = self.eln_man.add_experiment_entry(
371
- self.exp_id,
372
- ElnEntryCriteria(ElnEntryType.Attachment, entry_name, "Attachment", order=2,
373
- notebook_experiment_tab_id=tab_id, attachment_file_name=file_name,
374
- attachment_data_base64=base64_encoded)
375
- )
376
-
377
- # Return the entry object for further use.
378
- return attachment_entry
379
-
380
- def create_attachment_entry_from_file(self, tab: ElnExperimentTab, entry_name: str, file_path: str) \
381
- -> ExperimentEntry:
382
- """
383
- Add a new attachment entry to the experiment with the provided file path to a file in the file system.
384
-
385
- :param tab: The tab where the attachment entry will be added.
386
- :param entry_name: Name of the attachment entry to create in the experiment.
387
- :param file_path: The path to a file in the system to attach to the experiment.
388
- :return: The newly created attachment entry.
389
- """
390
- with open(file_path, 'rb') as f:
391
- file_contents: bytes = f.read()
392
- return self.create_attachment_entry(tab, entry_name, file_path, file_contents)
393
-
394
- def smiles_to_svg(self, smiles: list[str]) -> list[bytes]:
395
- """
396
- Given a list of SMILES strings, return a list of the corresponding images in SVG format.
397
-
398
- :param smiles: The SMILES strings to retrieve images for.
399
- :return: The images in SVG format. The indices of the returned list correspond to the indices of the input
400
- SMILES.
401
- """
402
- if not smiles:
403
- return []
404
- reg_man = MultiModalManager(self.user)
405
- image_list: list[str] = reg_man.load_image_data(ImageDataRequestPojo(smiles, False))
406
- return [x.encode() for x in image_list]
407
-
408
- def update_record_images(self, records: list[SapioRecord], images: list[bytes]) -> None:
409
- """
410
- Update the images of the given records with the given images.
411
-
412
- :param records: The records to update the images of.
413
- :param images: The images to update the records with. Records will be match with the image in the matching
414
- index of this list.
415
- """
416
- for record, image in zip(AliasUtil.to_data_records(records), images):
417
- with io.BytesIO(image) as bytes_io:
418
- self.dr_man.set_record_image(record, bytes_io)
419
-
420
-
421
- class ToolOfToolsHelper:
422
- """
423
- A class with helper methods utilized by the Tool of Tools for the creation and updating of experiment tabs that
424
- track a tool's progress and results.
425
- """
426
- # Contextual info.
427
- user: SapioUser
428
- tab_prefix: str
429
- exp_id: int
430
- helper: AiHelper
431
-
432
- # Tool info.
433
- name: str
434
- description: str
435
- results_data_type: str | None
436
-
437
- # Managers.
438
- dr_man: DataRecordManager
439
- eln_man: ElnManager
440
-
441
- # Stuff created by this helper.
442
- _initialized: bool
443
- """Whether a tab for this tool has been initialized."""
444
- tab: ElnExperimentTab
445
- """The tab that contains the tool's entries."""
446
- description_entry: ElnEntryStep | None
447
- """The text entry that displays the description of the tool."""
448
- description_record: DataRecord | None
449
- """The record that stores the description of the tool."""
450
- progress_entry: ElnEntryStep | None
451
- """A hidden entry for tracking the progress of the tool."""
452
- progress_record: DataRecord | None
453
- """The record that stores the progress of the tool."""
454
- progress_gauge_entry: ElnEntryStep | None
455
- """A chart entry that displays the progress of the tool using the hidden progress entry."""
456
- results_entry: ElnEntryStep | None
457
- """An entry for displaying the results of the tool. If None, the tool does not produce result records."""
458
-
459
- def __init__(self, headers: Mapping[str, str], name: str, description: str,
460
- results_data_type: str | None = None):
461
- """
462
- :param headers: The headers that were passed to the endpoint.
463
- :param name: The name of the tool.
464
- :param description: A description of the tool.
465
- :param results_data_type: The data type name for the results of the tool. If None, the tool does not produce
466
- result records.
467
- """
468
- headers: dict[str, str] = format_tot_headers(headers)
469
- self.user = create_user_from_tot_headers(headers)
470
- self.exp_id = int(headers[EXP_ID_HEADER.lower()])
471
- self.tab_prefix = headers[TAB_PREFIX_HEADER.lower()]
472
- self.helper = AiHelper(self.user, self.exp_id)
473
-
474
- self.name = name
475
- self.description = description
476
- self.results_data_type = results_data_type
477
-
478
- self.dr_man = DataRecordManager(self.user)
479
- self.eln_man = ElnManager(self.user)
480
-
481
- self._initialized = False
482
-
483
- def initialize_tab(self) -> ElnExperimentTab:
484
- if self._initialized:
485
- return self.tab
486
- self._initialized = True
487
-
488
- # Determine if a previous call to this endpoint already created a tab for these results. If so, grab the entries
489
- # from that tab.
490
- tab_name: str = f"{self.tab_prefix.strip()} {self.name.strip()}"
491
- tabs: list[ElnExperimentTab] = self.eln_man.get_tabs_for_experiment(self.exp_id)
492
- for tab in tabs:
493
- if tab.tab_name != tab_name:
494
- continue
495
-
496
- for entry in self.helper.protocol.get_sorted_step_list():
497
- if entry.eln_entry.notebook_experiment_tab_id != tab.tab_id:
498
- continue
499
-
500
- dt: str = entry.get_data_type_names()[0] if entry.get_data_type_names() else None
501
- if (entry.eln_entry.entry_type == ElnEntryType.Form
502
- and ElnBaseDataType.get_base_type(dt) == ElnBaseDataType.EXPERIMENT_DETAIL
503
- and not hasattr(self, "progress_entry")):
504
- self.progress_entry = entry
505
- self.progress_record = entry.get_records()[0]
506
- elif (entry.eln_entry.entry_type == ElnEntryType.Dashboard
507
- and not hasattr(self, "progress_gauge_entry")):
508
- self.progress_gauge_entry = entry
509
- elif (entry.eln_entry.entry_type == ElnEntryType.Text
510
- and not hasattr(self, "description_entry")):
511
- self.description_entry = entry
512
- self.description_record = entry.get_records()[0]
513
- elif (entry.eln_entry.entry_type == ElnEntryType.Table
514
- and dt == self.results_data_type
515
- and not hasattr(self, "results_entry")):
516
- self.results_entry = entry
517
-
518
- if not hasattr(self, "progress_entry"):
519
- self.progress_entry = None
520
- self.progress_record = None
521
- if not hasattr(self, "progress_gauge_entry"):
522
- self.progress_gauge_entry = None
523
- if not hasattr(self, "description_entry"):
524
- self.description_entry = None
525
- self.description_record = None
526
- if not hasattr(self, "results_entry"):
527
- self.results_entry = None
528
-
529
- self.tab = tab
530
- return tab
531
-
532
- # Otherwise, create the tab for the tool progress and results.
533
- self.tab = self.helper.create_tab(tab_name)
534
-
535
- # Create a hidden entry for tracking the progress of the tool.
536
- field_sets: list[ElnFieldSetInfo] = self.eln_man.get_field_set_info_list()
537
- progress_field_set: list[ElnFieldSetInfo] = [x for x in field_sets if
538
- x.field_set_name == "Tool of Tools Progress"]
539
- if not progress_field_set:
540
- raise SapioException("Unable to locate the field set for the Tool of Tools progress.")
541
- progress_entry_crit = ElnEntryCriteria(ElnEntryType.Form, f"ELaiN: {self.name} Progress",
542
- ElnBaseDataType.EXPERIMENT_DETAIL.data_type_name, 1,
543
- notebook_experiment_tab_id=self.tab.tab_id,
544
- enb_field_set_id=progress_field_set[0].field_set_id)
545
- progress_entry = ElnEntryStep(self.helper.protocol,
546
- self.eln_man.add_experiment_entry(self.exp_id, progress_entry_crit))
547
- self.progress_entry = progress_entry
548
- self.progress_record = progress_entry.get_records()[0]
549
-
550
- # Hide the progress entry.
551
- form_update_crit = ElnFormEntryUpdateCriteria()
552
- form_update_crit.is_hidden = True
553
- self.eln_man.update_experiment_entry(self.exp_id, self.progress_entry.get_id(), form_update_crit)
554
-
555
- # Create the text entry that displays the description of the tool. Include the timestamp of when the
556
- # tool started and format the description so that the text isn't too small to read.
557
- # TODO: Get the UTC offset in seconds from the header once that's being sent.
558
- now: str = TimeUtil.now_in_format("%Y-%m-%d %H:%M:%S UTC", "UTC")
559
- text_entry = ElnEntryStep(self.helper.protocol, self.helper.create_text_entry(self.tab, now, self.description))
560
- self.description_entry = text_entry
561
- self.description_record = text_entry.get_records()[0]
562
-
563
- # Shrink the text entry by one column.
564
- text_update_crit = ElnTextEntryUpdateCriteria()
565
- text_update_crit.column_order = 0
566
- text_update_crit.column_span = 2
567
- self.eln_man.update_experiment_entry(self.exp_id, self.description_entry.get_id(), text_update_crit)
568
-
569
- # Create a gauge entry to display the progress.
570
- gauge_entry: ElnEntryStep = self._create_gauge_chart(self.helper.protocol, progress_entry,
571
- f"{self.name} Progress", "Progress", "StatusMsg")
572
- self.progress_gauge_entry = gauge_entry
573
-
574
- # Make sure the gauge entry isn't too big and stick it to the right of the text entry.
575
- dash_update_crit = ElnDashboardEntryUpdateCriteria()
576
- dash_update_crit.entry_height = 250
577
- dash_update_crit.column_order = 2
578
- dash_update_crit.column_span = 2
579
- self.eln_man.update_experiment_entry(self.exp_id, self.progress_gauge_entry.get_id(), dash_update_crit)
580
-
581
- # TODO: Bulk updates aren't working?
582
- # self.eln_man.update_experiment_entries(self.exp_id, {
583
- # self.progress_entry.get_id(): form_update_crit,
584
- # self.progress_gauge_entry.get_id(): dash_update_crit,
585
- # self.description_entry.get_id(): text_update_crit
586
- # })
587
-
588
- # Create a results entry if this tool produces result records.
589
- if self.results_data_type:
590
- results_entry = ELNStepFactory.create_table_step(self.helper.protocol, f"{self.name} Results",
591
- self.results_data_type)
592
- self.results_entry = results_entry
593
- else:
594
- self.results_entry = None
595
-
596
- return self.tab
597
-
598
- def add_to_description(self, description: str, auto_format: bool = True) -> None:
599
- """
600
- Add to the description entry of the tool.
601
-
602
- :param description: The text to add to the description.
603
- :param auto_format: Whether to automatically format the text to be added.
604
- """
605
- if not self._initialized:
606
- raise SapioException("The tab for this tool has not been initialized.")
607
- field: str = ElnBaseDataType.get_text_entry_data_field_name()
608
- update: str = self.description_record.get_field_value(field)
609
- if auto_format:
610
- description = HtmlFormatter.body(description)
611
- update += f"<p style=\"padding-top: 10px;\">{description}</p>"
612
- self.description_record.set_field_value(field, update)
613
- self.dr_man.commit_data_records([self.description_record])
614
-
615
- def update_progress(self, progress: float, status_msg: str | None = None) -> None:
616
- """
617
- Updates the progress of the tool.
618
-
619
- :param progress: A value between 0 and 100 representing the progress of the tool.
620
- :param status_msg: A status message to display to the user alongside the progress gauge.
621
- """
622
- if not self._initialized:
623
- raise SapioException("The tab for this tool has not been initialized.")
624
- self.progress_record.set_field_value("Progress", progress)
625
- self.progress_record.set_field_value("StatusMsg", status_msg)
626
- self.dr_man.commit_data_records([self.progress_record])
627
-
628
- def add_results(self, results: list[SapioRecord]) -> None:
629
- """
630
- Add the results of the tool to the results entry.
631
-
632
- :param results: The result records to add to the results entry.
633
- """
634
- if not self._initialized:
635
- raise SapioException("The tab for this tool has not been initialized.")
636
- self.results_entry.add_records(AliasUtil.to_data_records(results))
637
-
638
- def add_results_bar_chart(self, x_axis: str, y_axis: str) -> ExperimentEntry:
639
- """
640
- Create a bar chart entry for the results of the tool.
641
-
642
- :param x_axis: The data field to use for the x-axis of the chart.
643
- :param y_axis: The data field to use for the y-axis of the chart.
644
- :return: The newly created chart entry.
645
- """
646
- if not self._initialized:
647
- raise SapioException("The tab for this tool has not been initialized.")
648
- if not self.results_entry:
649
- raise SapioException("This tool does not produce result records.")
650
- return ELNStepFactory.create_bar_chart_step(self.helper.protocol, self.results_entry,
651
- f"{self.name} Results Chart", x_axis, y_axis)[0].eln_entry
652
-
653
- def add_attachment_entry(self, entry_name: str, file_name: str, file_data: str | bytes) -> ExperimentEntry:
654
- """
655
- Add a new attachment entry to the experiment with the provided attachment data.
656
-
657
- :param entry_name: Name of the attachment entry to create in the experiment.
658
- :param file_name: The name of the attachment.
659
- :param file_data: The data of the attachment. This can be a string or bytes.
660
- :return: The newly created attachment entry.
661
- """
662
- if not self._initialized:
663
- raise SapioException("The tab for this tool has not been initialized.")
664
-
665
- return self.helper.create_attachment_entry(self.tab, entry_name, file_name, file_data)
666
-
667
- def add_attachment_entry_from_file(self, entry_name: str, file_path: str) -> ExperimentEntry:
668
- """
669
- Add a new attachment entry to the experiment with the provided file path to a file in the file system.
670
-
671
- :param entry_name: Name of the attachment entry to create in the experiment.
672
- :param file_path: The path to a file in the system to attach to the experiment.
673
- :return: The newly created attachment entry.
674
- """
675
- if not self._initialized:
676
- raise SapioException("The tab for this tool has not been initialized.")
677
-
678
- return self.helper.create_attachment_entry_from_file(self.tab, entry_name, file_path)
679
-
680
- # TODO: Remove this once pylib's gauge chart definition is up to date.
681
- @staticmethod
682
- def _create_gauge_chart(protocol: ElnExperimentProtocol, data_source_step: ElnEntryStep, step_name: str,
683
- field_name: str, status_field: str, group_by_field_name: str = "DataRecordName") \
684
- -> ElnEntryStep:
685
- """
686
- Create a gauge chart step in the experiment protocol.
687
- """
688
- if not data_source_step.get_data_type_names():
689
- raise ValueError("The data source step did not declare a data type name.")
690
- data_type_name: str = data_source_step.get_data_type_names()[0]
691
- series = GaugeChartSeries(data_type_name, field_name)
692
- series.operation_type = ChartOperationType.VALUE
693
- chart = _GaugeChartDefinition()
694
- chart.main_data_type_name = data_type_name
695
- chart.status_field = status_field
696
- chart.minimum_value = 0.
697
- chart.maximum_value = 100.
698
- chart.series_list = [series]
699
- chart.grouping_type = ChartGroupingType.GROUP_BY_FIELD
700
- chart.grouping_type_data_type_name = data_type_name
701
- chart.grouping_type_data_field_name = group_by_field_name
702
- dashboard, step = ELNStepFactory._create_dashboard_step_from_chart(chart, data_source_step, protocol, step_name,
703
- None)
704
- protocol.invalidate()
705
- return step
706
-
707
-
708
- # TODO: Using this to set the new status field setting.
709
- class _GaugeChartDefinition(GaugeChartDefinition):
710
- status_field: str
711
-
712
- def to_json(self) -> dict[str, Any]:
713
- result = super().to_json()
714
- result["statusValueField"] = {
715
- "dataTypeName": self.main_data_type_name,
716
- "dataFieldName": self.status_field
717
- }
718
- return result