-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuresim_simulator.py
63 lines (56 loc) · 2.49 KB
/
suresim_simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import subprocess as sp
import json
import shutil
class Simulator:
"""
Class for simulating SMLM data using SuReSim simulator (http://www.ana.uni-heidelberg.de/?id=198)
For examples of model and simulation parameters files, go to https://github.com/tkunerlab/JavaUmsetzungSTORMSimulation/tree/master/examples/cli_example
"""
def __init__(self, jar: str, path: str, model: str, sim_params: dict):
"""
Initialization function
:param jar: path to the suresim .jar file
:param path: path to current working directory
:param sim_params: path to the simulation folder
"""
self.jar = jar
self.path = path
self.model = os.path.join(self.path, model)
self.params = sim_params
# Dumping the simulation parameters in a json file for reproducibility
with open(os.path.join(self.path, 'simulationParameters.json'), 'w') as fp:
json.dump(self.params, fp)
fp.close()
self.params_json = os.path.join(self.path, 'simulationParameters.json')
def get_params_dict(self) -> dict:
"""
Gets the parameters file as a dictionary.
:return: dictionary
"""
# p = open(self.parameters)
# self.params_dict = json.load(p)
return self.params
# def update_simulation_params(self, folder_name : str):
# """
# Method to update the simulation parameters for a new simulation and make a new directory with the new file
# :param folder_name: name for the new simulation folder
# :return:
# """
# self.new_path = os.path.join(self.path, folder_name)
# os.mkdir(self.new_path)
# with open(os.path.join(self.new_path, 'simulationParameters.json'), 'w') as fp:
# json.dump(self.params, fp)
# self.params_json = os.path.join(self.new_path, 'simulationParameters.json')
# self.model = shutil.copy(self.model, self.new_path)
# self.path = self.new_path
def simulate(self, output):
"""
Method which actually calls the .jar file and runs the simulation
:param output: name of output folder
:return: None
"""
cmd = ['java', '-jar', self.jar, self.model, self.params_json, os.path.join(self.path, output)]
sp.check_output(cmd)
# Copying the simulation parameters json in the sample folder before overwriting in the next sample.
shutil.copy(self.params_json, os.path.join(self.path, output))