sl-shared-assets 1.0.0rc13__py3-none-any.whl → 1.0.0rc15__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 sl-shared-assets might be problematic. Click here for more details.

Files changed (42) hide show
  1. sl_shared_assets/__init__.py +27 -9
  2. sl_shared_assets/__init__.pyi +71 -0
  3. sl_shared_assets/cli.py +13 -14
  4. sl_shared_assets/cli.pyi +28 -0
  5. sl_shared_assets/data_classes/__init__.py +63 -0
  6. sl_shared_assets/data_classes/__init__.pyi +61 -0
  7. sl_shared_assets/data_classes/configuration_data.py +64 -0
  8. sl_shared_assets/data_classes/configuration_data.pyi +37 -0
  9. sl_shared_assets/data_classes/runtime_data.py +233 -0
  10. sl_shared_assets/data_classes/runtime_data.pyi +145 -0
  11. sl_shared_assets/data_classes/session_data.py +1275 -0
  12. sl_shared_assets/data_classes/session_data.pyi +527 -0
  13. sl_shared_assets/data_classes/surgery_data.py +152 -0
  14. sl_shared_assets/data_classes/surgery_data.pyi +89 -0
  15. sl_shared_assets/server/__init__.py +8 -0
  16. sl_shared_assets/server/__init__.pyi +8 -0
  17. sl_shared_assets/server/job.py +140 -0
  18. sl_shared_assets/server/job.pyi +94 -0
  19. sl_shared_assets/server/server.py +213 -0
  20. sl_shared_assets/server/server.pyi +95 -0
  21. sl_shared_assets/suite2p/__init__.py +8 -0
  22. sl_shared_assets/suite2p/__init__.pyi +4 -0
  23. sl_shared_assets/suite2p/multi_day.py +193 -0
  24. sl_shared_assets/suite2p/multi_day.pyi +99 -0
  25. sl_shared_assets/{suite2p.py → suite2p/single_day.py} +55 -32
  26. sl_shared_assets/suite2p/single_day.pyi +192 -0
  27. sl_shared_assets/tools/__init__.py +8 -0
  28. sl_shared_assets/tools/__init__.pyi +5 -0
  29. sl_shared_assets/{ascension_tools.py → tools/ascension_tools.py} +3 -6
  30. sl_shared_assets/tools/ascension_tools.pyi +68 -0
  31. sl_shared_assets/tools/packaging_tools.pyi +52 -0
  32. sl_shared_assets/tools/transfer_tools.pyi +53 -0
  33. {sl_shared_assets-1.0.0rc13.dist-info → sl_shared_assets-1.0.0rc15.dist-info}/METADATA +1 -1
  34. sl_shared_assets-1.0.0rc15.dist-info/RECORD +40 -0
  35. sl_shared_assets/data_classes.py +0 -1656
  36. sl_shared_assets/server.py +0 -293
  37. sl_shared_assets-1.0.0rc13.dist-info/RECORD +0 -14
  38. /sl_shared_assets/{packaging_tools.py → tools/packaging_tools.py} +0 -0
  39. /sl_shared_assets/{transfer_tools.py → tools/transfer_tools.py} +0 -0
  40. {sl_shared_assets-1.0.0rc13.dist-info → sl_shared_assets-1.0.0rc15.dist-info}/WHEEL +0 -0
  41. {sl_shared_assets-1.0.0rc13.dist-info → sl_shared_assets-1.0.0rc15.dist-info}/entry_points.txt +0 -0
  42. {sl_shared_assets-1.0.0rc13.dist-info → sl_shared_assets-1.0.0rc15.dist-info}/licenses/LICENSE +0 -0
@@ -1,293 +0,0 @@
1
- """This module provides the tools for working with the Sun lab BioHPC cluster. Specifically, the classes from this
2
- module establish an API for submitting jobs to the shared data processing cluster (managed via SLURM) and monitoring
3
- the running job status. All lab processing and analysis pipelines use this interface for accessing shared compute
4
- resources.
5
- """
6
-
7
- import re
8
- import time
9
- from pathlib import Path
10
- import datetime
11
- from dataclasses import dataclass
12
-
13
- import paramiko
14
- from simple_slurm import Slurm # type: ignore
15
- from paramiko.client import SSHClient
16
- from ataraxis_base_utilities import LogLevel, console
17
- from ataraxis_data_structures import YamlConfig
18
-
19
-
20
- def generate_server_credentials(
21
- output_directory: Path, username: str, password: str, host: str = "cbsuwsun.biohpc.cornell.edu"
22
- ) -> None:
23
- """Generates a new server_credentials.yaml file under the specified directory, using input information.
24
-
25
- This function provides a convenience interface for generating new BioHPC server credential files. Generally, this is
26
- only used when setting up new host-computers in the lab.
27
- """
28
- ServerCredentials(username=username, password=password, host=host).to_yaml(
29
- file_path=output_directory.joinpath("server_credentials.yaml")
30
- )
31
-
32
-
33
- @dataclass()
34
- class ServerCredentials(YamlConfig):
35
- """This class stores the hostname and credentials used to log into the BioHPC cluster to run Sun lab processing
36
- pipelines.
37
-
38
- Primarily, this is used as part of the sl-experiment library runtime to start data processing once it is
39
- transferred to the BioHPC server during preprocessing.
40
- """
41
-
42
- username: str = "YourNetID"
43
- """The username to use for server authentication."""
44
- password: str = "YourPassword"
45
- """The password to use for server authentication."""
46
- host: str = "cbsuwsun.biohpc.cornell.edu"
47
- """The hostname or IP address of the server to connect to."""
48
-
49
-
50
- class Server:
51
- """Encapsulates access to the Sun lab BioHPC processing server.
52
-
53
- This class provides the API that allows accessing the BioHPC server and creating and submitting various
54
- SLURM-managed jobs to the server. It functions as the central interface used by all processing pipelines in the
55
- lab to execute costly data processing on the server.
56
-
57
- Notes:
58
- All lab processing pipelines expect the data to be stored on the server and all processing logic to be packaged
59
- and installed into dedicated conda environments on the server.
60
-
61
- Args:
62
- credentials_path: The path to the.yaml file containing the server hostname and access credentials.
63
-
64
- Attributes:
65
- _open: Tracks whether the connection to the server is open or not.
66
- _client: Stores the initialized SSHClient instance used to interface with the server.
67
- """
68
-
69
- def __init__(self, credentials_path: Path) -> None:
70
- # Tracker used to prevent __del__ from classing stop() for a partially initialized class.
71
- self._open: bool = False
72
-
73
- # Loads the credentials from the provided .yaml file
74
- self._credentials: ServerCredentials = ServerCredentials.from_yaml(credentials_path) # type: ignore
75
-
76
- # Establishes the SSH connection to the specified processing server. At most, attempts to connect to the server
77
- # 30 times before terminating with an error
78
- attempt = 0
79
- while True:
80
- console.echo(
81
- f"Trying to connect to {self._credentials.host} (attempt {attempt}/30)...", level=LogLevel.INFO
82
- )
83
- try:
84
- self._client: SSHClient = paramiko.SSHClient()
85
- self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
86
- self._client.connect(
87
- self._credentials.host, username=self._credentials.username, password=self._credentials.password
88
- )
89
- console.echo(f"Connected to {self._credentials.host}", level=LogLevel.SUCCESS)
90
- break
91
- except paramiko.AuthenticationException:
92
- message = (
93
- f"Authentication failed when connecting to {self._credentials.host} using "
94
- f"{self._credentials.username} user."
95
- )
96
- console.error(message, RuntimeError)
97
- raise RuntimeError
98
- except:
99
- if attempt == 30:
100
- message = f"Could not connect to {self._credentials.host} after 30 attempts. Aborting runtime."
101
- console.error(message, RuntimeError)
102
- raise RuntimeError
103
-
104
- console.echo(
105
- f"Could not SSH to {self._credentials.host}, retrying after a 2-second delay...",
106
- level=LogLevel.WARNING,
107
- )
108
- attempt += 1
109
- time.sleep(2)
110
-
111
- def __del__(self) -> None:
112
- """If the instance is connected to the server, terminates the connection before the instance is destroyed."""
113
- self.close()
114
-
115
- @staticmethod
116
- def generate_slurm_header(
117
- job_name: str, output_log: Path, error_log: Path, cpus_to_use: int = 20, ram_gb: int = 4, time_limit: int = 60
118
- ) -> Slurm:
119
- """Creates a SLURM command object and fills it with initial job configuration data.
120
-
121
- This method is used to generate the initial SLURM command object and fill it with job (SLURM) configuration and
122
- (general!) conda initialization data. It is used by all processing pipelines in the lab as the initial
123
- configuration point when writing job shell scripts.
124
-
125
- Notes:
126
- The command header generated by this method does not contain the command to initialize the specific conda
127
- environment to be used during processing. This has to be provided as part of the additional command
128
- configuration, typically by adding the "source activate {ENV_NAME}" subcommand to the end of the header
129
- returned by this method.
130
-
131
- Args:
132
- job_name: The descriptive name of the SLURM job to be created.
133
- output_log: The path to the .txt file on the processing server, where to store the standard output of the
134
- job.
135
- error_log: The path to the .txt file on the processing server, where to store the standard error of the
136
- job.
137
- cpus_to_use: The number of CPUs to use for the job.
138
- ram_gb: The amount of RAM to allocate for the job in Gigabytes.
139
- time_limit: The maximum time limit for the job, in minutes. It is highly advised to set an adequate maximum
140
- runtime limit to prevent jobs from hogging the server for a long period of time.
141
- """
142
-
143
- # Builds the slurm command object filled with configuration information
144
- slurm_command = Slurm(
145
- cpus_per_task=cpus_to_use,
146
- job_name=job_name,
147
- output=str(output_log),
148
- error=str(error_log),
149
- mem=f"{ram_gb}G",
150
- time=datetime.timedelta(minutes=time_limit),
151
- )
152
-
153
- # Adds commands to initialize conda as part of the job runtime
154
- slurm_command.add_cmd("eval $(conda shell.bash hook)")
155
- slurm_command.add_cmd("conda init bash")
156
-
157
- return slurm_command
158
-
159
- def submit_job(self, slurm_command: Slurm, working_directory: Path) -> str:
160
- """Submits the input SLURM command to the managed BioHPC server via the shell script.
161
-
162
- This method submits various commands for execution via SLURM-managed BioHPC cluster. As part of its runtime, the
163
- method translates the Slurm object into the shell script, moves the script to the target working directory on
164
- the server, and instructs the server to execute the shell script (via SLURM).
165
-
166
- Args:
167
- slurm_command: The Slurm (command) object containing the job configuration and individual commands to run
168
- as part of the processing pipeline.
169
- working_directory: The path to the working directory on the server where the shell script is moved
170
- and executed.
171
-
172
- Returns:
173
- The job ID assigned to the job by SLURM manager if the command submission is successful.
174
-
175
- Raises:
176
- RuntimeError: If the command submission to the server fails.
177
- """
178
-
179
- # Extracts the job name from the slurm command text and uses it to generate the name for the remote script
180
- job_name_pattern = r"#SBATCH\s+--job-name\s+(\S+)"
181
- match = re.search(job_name_pattern, str(slurm_command))
182
- if match is None:
183
- message = (
184
- f"Failed to submit the job to the BioHPC cluster. It appears that the job does not contain the "
185
- f"expected SLURM job header. All jobs submitted via this method have to be initialized using the "
186
- f"generate_slurm_header() Server class method."
187
- )
188
- console.error(message, RuntimeError)
189
- raise RuntimeError(message) # This is a fallback to appease mypy, it should not be reachable.
190
- job_name = match.group(1)
191
-
192
- # Resolves the paths to the local and remote (server-side) .sh script files.
193
- local_script_path = Path("temp_script.sh")
194
- remote_script_path = str(working_directory.joinpath(f"{job_name}.sh"))
195
-
196
- # Appends the command to clean up (remove) the temporary script file after processing runtime is over
197
- slurm_command.add_cmd(f"rm -f {remote_script_path}")
198
-
199
- # Translates the command to string format
200
- script_content = str(slurm_command)
201
-
202
- # Replaces escaped $ (/$) with $. This is essential, as without this correction things like conda
203
- # initialization would not work as expected.
204
- fixed_script_content = script_content.replace("\\$", "$")
205
-
206
- # Creates a temporary script file locally and dumps translated command data into the file
207
- with open(local_script_path, "w") as f:
208
- f.write(fixed_script_content)
209
-
210
- # Uploads the command script to the server
211
- sftp = self._client.open_sftp()
212
- sftp.put(localpath=local_script_path, remotepath=remote_script_path)
213
- sftp.close()
214
-
215
- # Removes the temporary local .sh file
216
- local_script_path.unlink()
217
-
218
- # Makes the server-side script executable
219
- self._client.exec_command(f"chmod +x {remote_script_path}")
220
-
221
- # Submits the job to SLURM with sbatch and verifies submission state by returning either the ID of the job or
222
- # None to indicate no job has been submitted.
223
- job_output = self._client.exec_command(f"sbatch {remote_script_path}")[1].read().strip().decode()
224
- if "Submitted batch job" in job_output:
225
- return job_output.split()[-1]
226
- else:
227
- message = f"Failed to submit the {job_name} job to the BioHPC cluster."
228
- console.error(message, RuntimeError)
229
-
230
- # Fallback to appease mypy, should not be reachable
231
- raise RuntimeError(message)
232
-
233
- def job_complete(self, job_id: str) -> bool:
234
- """Returns True if the job with the given ID has been completed or terminated its runtime due to an error.
235
-
236
- If the job is still running or is waiting inside the execution queue, returns False.
237
-
238
- Args:
239
- job_id: The numeric ID of the job to check, assigned by SLURM.
240
- """
241
- if j_id not in self._client.exec_command(f"squeue -j {job_id}")[1].read().decode().strip():
242
- return True
243
- else:
244
- return False
245
-
246
- def close(self) -> None:
247
- """Closes the SSH connection to the server.
248
-
249
- This method has to be called before destroying the class instance to ensure proper resource cleanup.
250
- """
251
- # Prevents closing already closed connections
252
- if self._open:
253
- self._client.close()
254
-
255
-
256
- if __name__ == "__main__":
257
- # Creates SSHClient for server access
258
- console.enable()
259
- cred_path = Path("/home/cyberaxolotl/Desktop/test/server_credentials.yaml")
260
- server = Server(credentials_path=cred_path)
261
-
262
- # Generates SLURM job header
263
- slurm = server.generate_slurm_header(
264
- job_name="test_job",
265
- output_log=Path("/workdir/cbsuwsun/test_job_stdout.txt"),
266
- error_log=Path("/workdir/cbsuwsun/test_job_stderr.txt"),
267
- cpus_to_use=1,
268
- )
269
-
270
- # Adds test runtime command
271
- slurm.add_cmd("python --version > /workdir/cbsuwsun/mamba_version.txt")
272
-
273
- # Submits the job to the server
274
- j_id = server.submit_job(slurm_command=slurm, working_directory=Path("/workdir/cbsuwsun/"))
275
-
276
- if j_id:
277
- console.echo(f"Successfully submitted job with ID {j_id} to the server.", level=LogLevel.SUCCESS)
278
-
279
- max_wait_time = 60 # Maximum wait time in seconds
280
- wait_interval = 1 # Check every 1 second
281
- elapsed_time = 0
282
-
283
- while elapsed_time < max_wait_time:
284
- if server.job_complete(job_id=j_id):
285
- console.echo("Job completed", level=LogLevel.SUCCESS)
286
- break
287
-
288
- console.echo(f"Job still running. Waiting {wait_interval} seconds...", level=LogLevel.INFO)
289
- time.sleep(wait_interval)
290
- elapsed_time += wait_interval
291
-
292
- # Close the connection
293
- server.close()
@@ -1,14 +0,0 @@
1
- sl_shared_assets/__init__.py,sha256=Qe0mb_ixm4T6tgJSBIwzC50xEsP7ZtEWIuTsSt66Nxg,1596
2
- sl_shared_assets/ascension_tools.py,sha256=iOnWTTazMuWt1ILjPlrfj9iuQBJ7dlY_8bp7NrH8_XM,15856
3
- sl_shared_assets/cli.py,sha256=J9zDbDyRVww4CbKhIsm8-7WtYgxrhaThew4uVezADSg,4257
4
- sl_shared_assets/data_classes.py,sha256=vmdIYPSRtg9a6l9zkyx3EzeKLjPIWdE2wAoEvefzf2M,101249
5
- sl_shared_assets/packaging_tools.py,sha256=LOKCKvT6UD_cidCONaI4ctWyej5zEwwdhhgwYrj60Kg,6746
6
- sl_shared_assets/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- sl_shared_assets/server.py,sha256=VtFwS4PEy24n_pGz9W56zufkZEf_PKxIllP2ZnF5Zgc,13269
8
- sl_shared_assets/suite2p.py,sha256=gYWrSNf_FZBHRyidKfjgTkpEU63hmJog-szjU1sXkIM,21114
9
- sl_shared_assets/transfer_tools.py,sha256=J26kwOp_NpPSY0-xu5FTw9udte-rm_mW1FJyaTNoqQI,6606
10
- sl_shared_assets-1.0.0rc13.dist-info/METADATA,sha256=Cf5tqWZPR2LxcSzjWB69YFTvncJPOrLUgEYY3oL_p1o,47807
11
- sl_shared_assets-1.0.0rc13.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
12
- sl_shared_assets-1.0.0rc13.dist-info/entry_points.txt,sha256=bdnmVAcK3nrKi9QEYeNMrCLFH5LQ4BMBfwbLIgLPtq4,222
13
- sl_shared_assets-1.0.0rc13.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
14
- sl_shared_assets-1.0.0rc13.dist-info/RECORD,,