run_cell_simulation.py
This file contains the command-line interface (CLI) for the SMS_BP package, which is used for simulating single molecule localization microscopy experiments.
The CLI is built using Typer and provides two main commands:
1. 'config': Generates a sample configuration file.
2. 'runsim': Runs the cell simulation using a provided configuration file.
Main Components:
- typer_app_sms_bp: The main Typer application object.
- cell_simulation(): Callback function that displays the version information.
- generate_config(): Command to generate a sample configuration file.
- run_cell_simulation(): Command to run the cell simulation using a configuration file.
Usage:
- To generate a config file: python run_cell_simulation.py config [OPTIONS]
- To run a simulation: python run_cell_simulation.py runsim [CONFIG_FILE]
The file uses Rich for enhanced console output and progress tracking.
generate_config(output_path=Path('.'), output_path_make_recursive=None)
Generate a sample configuration file for the cell simulation and save it to the specified output path.
Source code in SMS_BP/run_cell_simulation.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 | @typer_app_sms_bp.command(name="config")
def generate_config(
output_path: Annotated[
Path,
typer.Option("--output_path", "-o", help="Path to the output file"),
] = Path("."),
output_path_make_recursive: Annotated[
Optional[bool],
typer.Option(
"--recursive_o",
"-r",
help="Make the output directory if it does not exist",
),
] = None,
) -> None:
"""
Generate a sample configuration file for the cell simulation and save it to the specified output path.
"""
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
task_1 = progress.add_task(
description="Processing request to create a default config file ...",
total=10,
)
# check if the output path is provided and is a valid directory | if not none
try:
output_path = Path(output_path)
except ValueError:
print("FileNotFoundError: Invalid output path.")
raise typer.Abort()
# double check if the output path is a valid directory
if not output_path.is_dir():
# if not, make the directory
if output_path_make_recursive:
try:
output_path.mkdir(parents=True, exist_ok=True)
except FileExistsError:
print(f"FileExistsError: Directory {output_path} already exists.")
else:
print(f"FileNotFoundError: {output_path} is not a valid directory.")
raise typer.Abort()
# find the parent dir
project_directory = Path(__file__).parent
# find the config file
config_file = project_directory / "sim_config.json"
output_path = output_path / "sim_config.json"
# copy the config file to the output path
# complete last progress
progress.update(task_1, completed=10)
task_2 = progress.add_task(
description="Copying the config file to the output path ...", total=10
)
try:
shutil.copy(config_file, output_path)
except FileNotFoundError:
rich.print(f"Error: No config file found in {project_directory}.")
raise typer.Abort()
progress.update(task_2, completed=10)
# complete
rich.print(f"Config file saved to {output_path.resolve()}")
|
run_cell_simulation(config_file)
Run the cell simulation using the configuration file provided.
Source code in SMS_BP/run_cell_simulation.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 | @typer_app_sms_bp.command(name="runsim")
def run_cell_simulation(
config_file: Annotated[Path, typer.Argument(help="Path to the configuration file")],
) -> None:
"""
Run the cell simulation using the configuration file provided.
"""
from contextlib import contextmanager
@contextmanager
def progress_context():
progress = Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
)
try:
with progress:
yield progress
finally:
progress.stop()
# Use in functions
with progress_context() as progress:
start_task_1 = time.time()
task_1 = progress.add_task(
description="Processing request to run the simulation ...", total=10
)
# check if the config file is a valid file
if not os.path.isfile(config_file):
rich.print("FileNotFoundError: Configuration file not found.")
raise typer.Abort()
# check if the config file is a valid json file
try:
with open(config_file) as f:
config = json.load(f)
except json.JSONDecodeError:
rich.print("JSONDecodeError: Configuration file is not a valid JSON file.")
raise typer.Abort()
validate_config(config)
output_parameters = config["Output_Parameters"]
output_path = output_parameters["output_path"]
# find the version flag in the config file
if "version" in config:
version = config["version"]
rich.print(f"Using config version: [bold]{version}[/bold]")
# complete last progress
progress.update(task_1, completed=10)
rich.print(
"Prep work done in {:.2f} seconds.".format(time.time() - start_task_1)
)
time_task_2 = time.time()
task_2 = progress.add_task(description="Running the simulation ...", total=None)
# run the simulation
sim = Simulate_cells(str(config_file))
sim.get_and_save_sim(
cd=output_path,
img_name=output_parameters.get("output_name"),
subsegment_type=output_parameters.get("subsegment_type"),
subsegment_num=int(output_parameters.get("subsegment_number")),
)
progress.update(task_2, completed=None)
rich.print(
"Simulation completed in {:.2f} seconds.".format(time.time() - time_task_2)
)
|