sl-shared-assets 1.0.0__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 (36) hide show
  1. sl_shared_assets/__init__.py +80 -0
  2. sl_shared_assets/__init__.pyi +73 -0
  3. sl_shared_assets/cli.py +384 -0
  4. sl_shared_assets/cli.pyi +94 -0
  5. sl_shared_assets/data_classes/__init__.py +66 -0
  6. sl_shared_assets/data_classes/__init__.pyi +61 -0
  7. sl_shared_assets/data_classes/configuration_data.py +479 -0
  8. sl_shared_assets/data_classes/configuration_data.pyi +199 -0
  9. sl_shared_assets/data_classes/runtime_data.py +251 -0
  10. sl_shared_assets/data_classes/runtime_data.pyi +145 -0
  11. sl_shared_assets/data_classes/session_data.py +625 -0
  12. sl_shared_assets/data_classes/session_data.pyi +252 -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/py.typed +0 -0
  16. sl_shared_assets/server/__init__.py +8 -0
  17. sl_shared_assets/server/__init__.pyi +8 -0
  18. sl_shared_assets/server/job.py +140 -0
  19. sl_shared_assets/server/job.pyi +94 -0
  20. sl_shared_assets/server/server.py +214 -0
  21. sl_shared_assets/server/server.pyi +95 -0
  22. sl_shared_assets/tools/__init__.py +15 -0
  23. sl_shared_assets/tools/__init__.pyi +15 -0
  24. sl_shared_assets/tools/ascension_tools.py +277 -0
  25. sl_shared_assets/tools/ascension_tools.pyi +68 -0
  26. sl_shared_assets/tools/packaging_tools.py +148 -0
  27. sl_shared_assets/tools/packaging_tools.pyi +56 -0
  28. sl_shared_assets/tools/project_management_tools.py +201 -0
  29. sl_shared_assets/tools/project_management_tools.pyi +54 -0
  30. sl_shared_assets/tools/transfer_tools.py +119 -0
  31. sl_shared_assets/tools/transfer_tools.pyi +53 -0
  32. sl_shared_assets-1.0.0.dist-info/METADATA +869 -0
  33. sl_shared_assets-1.0.0.dist-info/RECORD +36 -0
  34. sl_shared_assets-1.0.0.dist-info/WHEEL +4 -0
  35. sl_shared_assets-1.0.0.dist-info/entry_points.txt +8 -0
  36. sl_shared_assets-1.0.0.dist-info/licenses/LICENSE +674 -0
@@ -0,0 +1,94 @@
1
+ from pathlib import Path
2
+
3
+ from _typeshed import Incomplete
4
+ from simple_slurm import Slurm
5
+
6
+ class Job:
7
+ """Aggregates the data of a single SLURM-managed job to be executed on the Sun lab BioHPC cluster.
8
+
9
+ This class provides the API for constructing any server-side job in the Sun lab. Internally, it wraps an instance
10
+ of a Slurm class to package the job data into the format expected by the SLURM job manager. All jobs managed by this
11
+ class instance should be submitted to an initialized Server class 'submit_job' method to be executed on the server.
12
+
13
+ Notes:
14
+ The initialization method of the class contains the arguments for configuring the SLURM and Conda environments
15
+ used by the job. Do not submit additional SLURM or Conda commands via the 'add_command' method, as this may
16
+ produce unexpected behavior.
17
+
18
+ Each job can be conceptualized as a sequence of shell instructions to execute on the remote compute server. For
19
+ the lab, that means that the bulk of the command consists of calling various CLIs exposed by data processing or
20
+ analysis pipelines, installed in the Conda environment on the server. Other than that, the job contains commands
21
+ for activating the target conda environment and, in some cases, doing other preparatory or cleanup work. The
22
+ source code of a 'remote' job is typically identical to what a human operator would type in a 'local' terminal
23
+ to run the same job on their PC.
24
+
25
+ A key feature of server-side jobs is that they are executed on virtual machines managed by SLURM. Since the
26
+ server has a lot more compute and memory resources than likely needed by individual jobs, each job typically
27
+ requests a subset of these resources. Upon being executed, SLURM creates an isolated environment with the
28
+ requested resources and runs the job in that environment.
29
+
30
+ Since all jobs are expected to use the CLIs from python packages (pre)installed on the BioHPC server, make sure
31
+ that the target environment is installed and configured before submitting jobs to the server. See notes in
32
+ ReadMe to learn more about configuring server-side conda environments.
33
+
34
+ Args:
35
+ job_name: The descriptive name of the SLURM job to be created. Primarily, this name is used in terminal
36
+ printouts to identify the job to human operators.
37
+ output_log: The absolute path to the .txt file on the processing server, where to store the standard output
38
+ data of the job.
39
+ error_log: The absolute path to the .txt file on the processing server, where to store the standard error
40
+ data of the job.
41
+ working_directory: The absolute path to the directory where temporary job files will be stored. During runtime,
42
+ classes from this library use that directory to store files such as the job's shell script. All such files
43
+ are automatically removed from the directory at the end of a non-errors runtime.
44
+ conda_environment: The name of the conda environment to activate on the server before running the job logic. The
45
+ environment should contain the necessary Python packages and CLIs to support running the job's logic.
46
+ cpus_to_use: The number of CPUs to use for the job.
47
+ ram_gb: The amount of RAM to allocate for the job, in Gigabytes.
48
+ time_limit: The maximum time limit for the job, in minutes. If the job is still running at the end of this time
49
+ period, it will be forcibly terminated. It is highly advised to always set adequate maximum runtime limits
50
+ to prevent jobs from hogging the server in case of runtime or algorithm errors.
51
+
52
+ Attributes:
53
+ remote_script_path: Stores the path to the script file relative to the root of the remote server that runs the
54
+ command.
55
+ job_id: Stores the unique job identifier assigned by the SLURM manager to this job, when it is accepted for
56
+ execution. This field initialized to None and is overwritten by the Server class that submits the job.
57
+ job_name: Stores the descriptive name of the SLURM job.
58
+ _command: Stores the managed SLURM command object.
59
+ """
60
+
61
+ remote_script_path: Incomplete
62
+ job_id: str | None
63
+ job_name: str
64
+ _command: Slurm
65
+ def __init__(
66
+ self,
67
+ job_name: str,
68
+ output_log: Path,
69
+ error_log: Path,
70
+ working_directory: Path,
71
+ conda_environment: str,
72
+ cpus_to_use: int = 10,
73
+ ram_gb: int = 10,
74
+ time_limit: int = 60,
75
+ ) -> None: ...
76
+ def __repr__(self) -> str:
77
+ """Returns the string representation of the Job instance."""
78
+ def add_command(self, command: str) -> None:
79
+ """Adds the input command string to the end of the managed SLURM job command list.
80
+
81
+ This method is a wrapper around simple_slurm's 'add_cmd' method. It is used to iteratively build the shell
82
+ command sequence of the job.
83
+
84
+ Args:
85
+ command: The command string to add to the command list, e.g.: 'python main.py --input 1'.
86
+ """
87
+ @property
88
+ def command_script(self) -> str:
89
+ """Translates the managed job data into a shell-script-writable string and returns it to caller.
90
+
91
+ This method is used by the Server class to translate the job into the format that can be submitted to and
92
+ executed on the remote compute server. Do not call this method manually unless you know what you are doing.
93
+ The returned string is safe to dump into a .sh (shell script) file and move to the BioHPC server for execution.
94
+ """
@@ -0,0 +1,214 @@
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 time
8
+ from pathlib import Path
9
+ import tempfile
10
+ from dataclasses import dataclass
11
+
12
+ import paramiko
13
+
14
+ # noinspection PyProtectedMember
15
+ from simple_slurm import Slurm # type: ignore
16
+ from paramiko.client import SSHClient
17
+ from ataraxis_base_utilities import LogLevel, console
18
+ from ataraxis_data_structures import YamlConfig
19
+
20
+ from .job import Job
21
+
22
+
23
+ def generate_server_credentials(
24
+ output_directory: Path, username: str, password: str, host: str = "cbsuwsun.biohpc.cornell.edu"
25
+ ) -> None:
26
+ """Generates a new server_credentials.yaml file under the specified directory, using input information.
27
+
28
+ This function provides a convenience interface for generating new BioHPC server credential files. Generally, this is
29
+ only used when setting up new host-computers in the lab.
30
+ """
31
+ ServerCredentials(username=username, password=password, host=host).to_yaml(
32
+ file_path=output_directory.joinpath("server_credentials.yaml")
33
+ )
34
+
35
+
36
+ @dataclass()
37
+ class ServerCredentials(YamlConfig):
38
+ """This class stores the hostname and credentials used to log into the BioHPC cluster to run Sun lab processing
39
+ pipelines.
40
+
41
+ Primarily, this is used as part of the sl-experiment library runtime to start data processing once it is
42
+ transferred to the BioHPC server during preprocessing. However, the same file can be used together with the Server
43
+ class API to run any computation jobs on the lab's BioHPC server.
44
+ """
45
+
46
+ username: str = "YourNetID"
47
+ """The username to use for server authentication."""
48
+ password: str = "YourPassword"
49
+ """The password to use for server authentication."""
50
+ host: str = "cbsuwsun.biohpc.cornell.edu"
51
+ """The hostname or IP address of the server to connect to."""
52
+
53
+
54
+ class Server:
55
+ """Encapsulates access to the Sun lab BioHPC processing server.
56
+
57
+ This class provides the API that allows accessing the BioHPC server to create and submit various SLURM-managed jobs
58
+ to the server. It functions as the central interface used by all processing pipelines in the lab to execute costly
59
+ data processing on the server.
60
+
61
+ Notes:
62
+ All lab processing pipelines expect the data to be stored on the server and all processing logic to be packaged
63
+ and installed into dedicated conda environments on the server.
64
+
65
+ This class assumes that the target server has SLURM job manager installed and accessible to the user whose
66
+ credentials are used to connect to the server as part of this class instantiation.
67
+
68
+ Args:
69
+ credentials_path: The path to the locally stored .yaml file that contains the server hostname and access
70
+ credentials.
71
+
72
+ Attributes:
73
+ _open: Tracks whether the connection to the server is open or not.
74
+ _client: Stores the initialized SSHClient instance used to interface with the server.
75
+ """
76
+
77
+ def __init__(self, credentials_path: Path) -> None:
78
+ # Tracker used to prevent __del__ from calling stop() for a partially initialized class.
79
+ self._open: bool = False
80
+
81
+ # Loads the credentials from the provided .yaml file
82
+ self._credentials: ServerCredentials = ServerCredentials.from_yaml(credentials_path) # type: ignore
83
+
84
+ # Establishes the SSH connection to the specified processing server. At most, attempts to connect to the server
85
+ # 30 times before terminating with an error
86
+ attempt = 0
87
+ while True:
88
+ console.echo(
89
+ f"Trying to connect to {self._credentials.host} (attempt {attempt}/30)...", level=LogLevel.INFO
90
+ )
91
+ try:
92
+ self._client: SSHClient = paramiko.SSHClient()
93
+ self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
94
+ self._client.connect(
95
+ self._credentials.host, username=self._credentials.username, password=self._credentials.password
96
+ )
97
+ console.echo(f"Connected to {self._credentials.host}", level=LogLevel.SUCCESS)
98
+ self._open = True
99
+ break
100
+ except paramiko.AuthenticationException:
101
+ message = (
102
+ f"Authentication failed when connecting to {self._credentials.host} using "
103
+ f"{self._credentials.username} user."
104
+ )
105
+ console.error(message, RuntimeError)
106
+ raise RuntimeError
107
+ except:
108
+ if attempt == 30:
109
+ message = f"Could not connect to {self._credentials.host} after 30 attempts. Aborting runtime."
110
+ console.error(message, RuntimeError)
111
+ raise RuntimeError
112
+
113
+ console.echo(
114
+ f"Could not SSH to {self._credentials.host}, retrying after a 2-second delay...",
115
+ level=LogLevel.WARNING,
116
+ )
117
+ attempt += 1
118
+ time.sleep(2)
119
+
120
+ def __del__(self) -> None:
121
+ """If the instance is connected to the server, terminates the connection before the instance is destroyed."""
122
+ self.close()
123
+
124
+ def submit_job(self, job: Job) -> Job:
125
+ """Submits the input job to the managed BioHPC server via SLURM job manager.
126
+
127
+ This method submits various jobs for execution via SLURM-managed BioHPC cluster. As part of its runtime, the
128
+ method translates the Job object into the shell script, moves the script to the target working directory on
129
+ the server, and instructs the server to execute the shell script (via SLURM).
130
+
131
+ Args:
132
+ job: The Job object that contains all job data.
133
+
134
+ Returns:
135
+ The job object whose 'job_id' attribute had been modified with the job ID if the job was successfully
136
+ submitted.
137
+
138
+ Raises:
139
+ RuntimeError: If job submission to the server fails.
140
+ """
141
+
142
+ # Generates a temporary shell script on the local machine. Uses tempfile to automatically remove the
143
+ # local script as soon as it is uploaded to the server.
144
+ with tempfile.TemporaryDirectory() as temp_dir:
145
+ local_script_path = Path(temp_dir).joinpath(f"{job.job_name}.sh")
146
+ fixed_script_content = job.command_script
147
+
148
+ # Creates a temporary script file locally and dumps translated command data into the file
149
+ with open(local_script_path, "w") as f:
150
+ f.write(fixed_script_content)
151
+
152
+ # Uploads the command script to the server
153
+ sftp = self._client.open_sftp()
154
+ sftp.put(localpath=local_script_path, remotepath=job.remote_script_path)
155
+ sftp.close()
156
+
157
+ # Makes the server-side script executable
158
+ self._client.exec_command(f"chmod +x {job.remote_script_path}")
159
+
160
+ # Submits the job to SLURM with sbatch and verifies submission state
161
+ job_output = self._client.exec_command(f"sbatch {job.remote_script_path}")[1].read().strip().decode()
162
+
163
+ # If batch_job is not in the output received from SLURM in response to issuing the submission command, raises an
164
+ # error.
165
+ if "Submitted batch job" not in job_output:
166
+ message = f"Failed to submit the '{job.job_name}' job to the BioHPC cluster."
167
+ console.error(message, RuntimeError)
168
+
169
+ # Fallback to appease mypy, should not be reachable
170
+ raise RuntimeError(message)
171
+
172
+ # Otherwise, extracts the job id assigned to the job by SLURM from the response and writes it to the processed
173
+ # Job object
174
+ job_id = job_output.split()[-1]
175
+ job.job_id = job_id
176
+ return job
177
+
178
+ def job_complete(self, job: Job) -> bool:
179
+ """Returns True if the job managed by the input Job instance has been completed or terminated its runtime due
180
+ to an error.
181
+
182
+ If the job is still running or is waiting inside the execution queue, returns False.
183
+
184
+ Args:
185
+ job: The Job object whose status needs to be checked.
186
+
187
+ Raises:
188
+ ValueError: If the input Job object does not contain a valid job_id, suggesting that it has not been
189
+ submitted to the server.
190
+ """
191
+
192
+ if job.job_id is None:
193
+ message = (
194
+ f"The input Job object for the job {job.job_name} does not contain a valid job_id. This indicates that "
195
+ f"the job has not been submitted to the server."
196
+ )
197
+ console.error(message, ValueError)
198
+
199
+ # This is here to appease mypy, it should not be reachable
200
+ raise ValueError(message)
201
+
202
+ if job.job_id not in self._client.exec_command(f"squeue -j {job.job_id}")[1].read().decode().strip():
203
+ return True
204
+ else:
205
+ return False
206
+
207
+ def close(self) -> None:
208
+ """Closes the SSH connection to the server.
209
+
210
+ This method has to be called before destroying the class instance to ensure proper resource cleanup.
211
+ """
212
+ # Prevents closing already closed connections
213
+ if self._open:
214
+ self._client.close()
@@ -0,0 +1,95 @@
1
+ from pathlib import Path
2
+ from dataclasses import dataclass
3
+
4
+ from simple_slurm import Slurm as Slurm
5
+ from paramiko.client import SSHClient as SSHClient
6
+ from ataraxis_data_structures import YamlConfig
7
+
8
+ from .job import Job as Job
9
+
10
+ def generate_server_credentials(
11
+ output_directory: Path, username: str, password: str, host: str = "cbsuwsun.biohpc.cornell.edu"
12
+ ) -> None:
13
+ """Generates a new server_credentials.yaml file under the specified directory, using input information.
14
+
15
+ This function provides a convenience interface for generating new BioHPC server credential files. Generally, this is
16
+ only used when setting up new host-computers in the lab.
17
+ """
18
+ @dataclass()
19
+ class ServerCredentials(YamlConfig):
20
+ """This class stores the hostname and credentials used to log into the BioHPC cluster to run Sun lab processing
21
+ pipelines.
22
+
23
+ Primarily, this is used as part of the sl-experiment library runtime to start data processing once it is
24
+ transferred to the BioHPC server during preprocessing. However, the same file can be used together with the Server
25
+ class API to run any computation jobs on the lab's BioHPC server.
26
+ """
27
+
28
+ username: str = ...
29
+ password: str = ...
30
+ host: str = ...
31
+
32
+ class Server:
33
+ """Encapsulates access to the Sun lab BioHPC processing server.
34
+
35
+ This class provides the API that allows accessing the BioHPC server to create and submit various SLURM-managed jobs
36
+ to the server. It functions as the central interface used by all processing pipelines in the lab to execute costly
37
+ data processing on the server.
38
+
39
+ Notes:
40
+ All lab processing pipelines expect the data to be stored on the server and all processing logic to be packaged
41
+ and installed into dedicated conda environments on the server.
42
+
43
+ This class assumes that the target server has SLURM job manager installed and accessible to the user whose
44
+ credentials are used to connect to the server as part of this class instantiation.
45
+
46
+ Args:
47
+ credentials_path: The path to the locally stored .yaml file that contains the server hostname and access
48
+ credentials.
49
+
50
+ Attributes:
51
+ _open: Tracks whether the connection to the server is open or not.
52
+ _client: Stores the initialized SSHClient instance used to interface with the server.
53
+ """
54
+
55
+ _open: bool
56
+ _credentials: ServerCredentials
57
+ _client: SSHClient
58
+ def __init__(self, credentials_path: Path) -> None: ...
59
+ def __del__(self) -> None:
60
+ """If the instance is connected to the server, terminates the connection before the instance is destroyed."""
61
+ def submit_job(self, job: Job) -> Job:
62
+ """Submits the input job to the managed BioHPC server via SLURM job manager.
63
+
64
+ This method submits various jobs for execution via SLURM-managed BioHPC cluster. As part of its runtime, the
65
+ method translates the Job object into the shell script, moves the script to the target working directory on
66
+ the server, and instructs the server to execute the shell script (via SLURM).
67
+
68
+ Args:
69
+ job: The Job object that contains all job data.
70
+
71
+ Returns:
72
+ The job object whose 'job_id' attribute had been modified with the job ID if the job was successfully
73
+ submitted.
74
+
75
+ Raises:
76
+ RuntimeError: If job submission to the server fails.
77
+ """
78
+ def job_complete(self, job: Job) -> bool:
79
+ """Returns True if the job managed by the input Job instance has been completed or terminated its runtime due
80
+ to an error.
81
+
82
+ If the job is still running or is waiting inside the execution queue, returns False.
83
+
84
+ Args:
85
+ job: The Job object whose status needs to be checked.
86
+
87
+ Raises:
88
+ ValueError: If the input Job object does not contain a valid job_id, suggesting that it has not been
89
+ submitted to the server.
90
+ """
91
+ def close(self) -> None:
92
+ """Closes the SSH connection to the server.
93
+
94
+ This method has to be called before destroying the class instance to ensure proper resource cleanup.
95
+ """
@@ -0,0 +1,15 @@
1
+ """This package provides helper tools used to automate routine operations, such as transferring or verifying the
2
+ integrity of the data. The tools from this package are used by most other data processing libraries in the lab."""
3
+
4
+ from .transfer_tools import transfer_directory
5
+ from .ascension_tools import ascend_tyche_data
6
+ from .packaging_tools import calculate_directory_checksum
7
+ from .project_management_tools import verify_session_checksum, generate_project_manifest
8
+
9
+ __all__ = [
10
+ "transfer_directory",
11
+ "calculate_directory_checksum",
12
+ "ascend_tyche_data",
13
+ "verify_session_checksum",
14
+ "generate_project_manifest",
15
+ ]
@@ -0,0 +1,15 @@
1
+ from .transfer_tools import transfer_directory as transfer_directory
2
+ from .ascension_tools import ascend_tyche_data as ascend_tyche_data
3
+ from .packaging_tools import calculate_directory_checksum as calculate_directory_checksum
4
+ from .project_management_tools import (
5
+ verify_session_checksum as verify_session_checksum,
6
+ generate_project_manifest as generate_project_manifest,
7
+ )
8
+
9
+ __all__ = [
10
+ "transfer_directory",
11
+ "calculate_directory_checksum",
12
+ "ascend_tyche_data",
13
+ "verify_session_checksum",
14
+ "generate_project_manifest",
15
+ ]