SPACEtomo 1.2.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.
Files changed (44) hide show
  1. SPACEtomo/CLI.py +105 -0
  2. SPACEtomo/GUI.py +71 -0
  3. SPACEtomo/SerialEM_scripts/SPACEtomo_lamellaDetectionGUI.py +27 -0
  4. SPACEtomo/SerialEM_scripts/SPACEtomo_postAction.py +12 -0
  5. SPACEtomo/SerialEM_scripts/SPACEtomo_run.py +75 -0
  6. SPACEtomo/SerialEM_scripts/SPACEtomo_targetSelectionGUI.py +39 -0
  7. SPACEtomo/SerialEM_scripts/__init__.py +0 -0
  8. SPACEtomo/__init__.py +2 -0
  9. SPACEtomo/config.py +85 -0
  10. SPACEtomo/modules/__init__.py +0 -0
  11. SPACEtomo/modules/buf.py +457 -0
  12. SPACEtomo/modules/dummy/__init__.py +0 -0
  13. SPACEtomo/modules/dummy/buf.py +412 -0
  14. SPACEtomo/modules/dummy/scope.py +696 -0
  15. SPACEtomo/modules/dummy/utils_sem.py +86 -0
  16. SPACEtomo/modules/ext.py +979 -0
  17. SPACEtomo/modules/gui/__init__.py +0 -0
  18. SPACEtomo/modules/gui/flm.py +759 -0
  19. SPACEtomo/modules/gui/gui.py +508 -0
  20. SPACEtomo/modules/gui/lam_sel.py +1110 -0
  21. SPACEtomo/modules/gui/map.py +604 -0
  22. SPACEtomo/modules/gui/menu.py +219 -0
  23. SPACEtomo/modules/gui/plot.py +445 -0
  24. SPACEtomo/modules/gui/tgt_sel.py +1668 -0
  25. SPACEtomo/modules/gui/thmb.py +250 -0
  26. SPACEtomo/modules/lisc.py +237 -0
  27. SPACEtomo/modules/mod_wg.py +291 -0
  28. SPACEtomo/modules/nav.py +711 -0
  29. SPACEtomo/modules/scope.py +572 -0
  30. SPACEtomo/modules/tgt.py +592 -0
  31. SPACEtomo/modules/utils.py +569 -0
  32. SPACEtomo/modules/utils_sem.py +180 -0
  33. SPACEtomo/run.py +569 -0
  34. SPACEtomo/run_monitor.py +116 -0
  35. SPACEtomo/run_nnUNet.py +187 -0
  36. SPACEtomo/run_postaction.py +153 -0
  37. SPACEtomo/setup_config.py +130 -0
  38. SPACEtomo/setup_model.py +359 -0
  39. SPACEtomo-1.2.0.dist-info/LICENSE +674 -0
  40. SPACEtomo-1.2.0.dist-info/METADATA +1267 -0
  41. SPACEtomo-1.2.0.dist-info/RECORD +44 -0
  42. SPACEtomo-1.2.0.dist-info/WHEEL +5 -0
  43. SPACEtomo-1.2.0.dist-info/entry_points.txt +5 -0
  44. SPACEtomo-1.2.0.dist-info/top_level.txt +1 -0
SPACEtomo/CLI.py ADDED
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env python
2
+ # ===================================================================
3
+ # ScriptName: CLI
4
+ # Purpose: User interface for inspecting and labeling lamella grid atlases collected with SPACEtomo.
5
+ # More information at http://github.com/eisfabian/SPACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # Created: 2024/04/26
8
+ # Revision: v1.2
9
+ # Last Change: 2024/07/18: reworked GUI to class
10
+ # # 2024/06/07: added lamella table row highlighting, added save coords in YOLO format
11
+ # 2024/06/05: introduced Menu class, added shortcuts for most buttons
12
+ # 2024/06/04: added pix size check for export, added rescaling of mrc file
13
+ # 2024/06/03: added include empty tiles option, added save json option
14
+ # 2024/05/31: added status line, fixed tiles bounds, added next button, added hide/showElements
15
+ # 2024/05/30: added rescaling, saving as png, fixed export tiles
16
+ # 2024/05/29: outsourced plot clearing
17
+ # 2024/05/23: added lamella labels on plot, added padding when exporting edge tiles, fixed plotTiles
18
+ # 2024/05/10: added reading and writing of YOLO format labels, added saving of tiles with labels, added map class
19
+ # 2024/05/02: added lamella color from config, added reset zoom, delegated plot generation, call lamella detection
20
+ # 2024/04/30: added lamella focus buttons, outline scaling, list sorting
21
+ # 2024/04/26: Copy most of SPACEtomo_tgt
22
+ # ===================================================================
23
+
24
+ import sys
25
+ try:
26
+ import dearpygui.dearpygui as dpg
27
+ except ModuleNotFoundError:
28
+ print("WARNING: GUI library is not installed.")
29
+ import argparse
30
+ import shutil
31
+ from pathlib import Path
32
+
33
+ from SPACEtomo.modules.utils import log
34
+ from SPACEtomo.modules.gui.lam_sel import LamellaGUI
35
+ from SPACEtomo.modules.gui.tgt_sel import TargetGUI
36
+
37
+ # Attempt SerialEM import
38
+ import SPACEtomo.config as config
39
+ sys.path.insert(len(sys.path), config.SERIALEM_PYTHON_PATH)
40
+ try:
41
+ import serialem as sem
42
+ SERIALEM = True
43
+ except ModuleNotFoundError:
44
+ SERIALEM = False
45
+
46
+ def show(gui, path=""):
47
+ dpg.create_context()
48
+ main = gui(path)
49
+ main.show()
50
+ dpg.destroy_context()
51
+
52
+ def run_process(process):
53
+ log(f"Starting {process}...")
54
+ process()
55
+
56
+ def main():
57
+ # Process arguments
58
+ parser = argparse.ArgumentParser(description="Command line interface for different SPACEtomo functions (e.g. GUIs).")
59
+ parser.add_argument("task", type=str, default="targets", help="Name of the SPACEtomo task to run. Options: targets -> Open target selection GUI | lamella -> Open lamella selection GUI | run -> Start SPACEtomo run | scripts -> Get copy of SerialEM scripts")
60
+ parser.add_argument("map_file", nargs="?", type=str, default="", help="Absolute path to a map file to load in GUI [.png].")
61
+ args = parser.parse_args()
62
+
63
+ # Get GUI
64
+ gui = None
65
+ process = None
66
+ if args.task.lower() == "targets" or args.task.lower() == "target":
67
+ gui = TargetGUI
68
+ elif args.task.lower() == "lamella" or args.task.lower() == "lamellae":
69
+ gui = LamellaGUI
70
+ elif args.task.lower() == "run":
71
+ if SERIALEM:
72
+ from SPACEtomo import run
73
+ process = run.main
74
+ else:
75
+ log(f"ERROR: SPACEtomo {args.task} requires SerialEM!")
76
+ elif args.task.lower() == "scripts":
77
+ serialem_script_dir = Path(__file__).parent / "SerialEM_scripts"
78
+ target_path = Path.cwd() / "SerialEM_scripts"
79
+ if not target_path.exists():
80
+ shutil.copytree(serialem_script_dir, target_path)
81
+ (target_path / "__init__.py").unlink()
82
+ log(f"SerialEM scripts were successfully copied to: {target_path}")
83
+ else:
84
+ log(f"ERROR: A folder named SerialEM_scripts already exists!")
85
+ else:
86
+ raise ValueError(f"No task found with name {args.task}!")
87
+
88
+ # Check map file
89
+ if gui:
90
+ map_file = ""
91
+ if args.map_file != "":
92
+ map_file = Path(args.map_file)
93
+ if not map_file.exists():
94
+ map_file = ""
95
+ log("WARNING: Map file does not exist! Attempting to start task without map...")
96
+
97
+ # Run GUI
98
+ show(gui, map_file)
99
+
100
+ if process:
101
+ run_process(process)
102
+
103
+
104
+ if __name__ == "__main__":
105
+ main()
SPACEtomo/GUI.py ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env python
2
+ # ===================================================================
3
+ # ScriptName: GUI
4
+ # Purpose: User interface for inspecting and labeling lamella grid atlases collected with SPACEtomo.
5
+ # More information at http://github.com/eisfabian/SPACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # Created: 2024/04/26
8
+ # Revision: v1.2
9
+ # Last Change: 2024/07/18: reworked GUI to class
10
+ # # 2024/06/07: added lamella table row highlighting, added save coords in YOLO format
11
+ # 2024/06/05: introduced Menu class, added shortcuts for most buttons
12
+ # 2024/06/04: added pix size check for export, added rescaling of mrc file
13
+ # 2024/06/03: added include empty tiles option, added save json option
14
+ # 2024/05/31: added status line, fixed tiles bounds, added next button, added hide/showElements
15
+ # 2024/05/30: added rescaling, saving as png, fixed export tiles
16
+ # 2024/05/29: outsourced plot clearing
17
+ # 2024/05/23: added lamella labels on plot, added padding when exporting edge tiles, fixed plotTiles
18
+ # 2024/05/10: added reading and writing of YOLO format labels, added saving of tiles with labels, added map class
19
+ # 2024/05/02: added lamella color from config, added reset zoom, delegated plot generation, call lamella detection
20
+ # 2024/04/30: added lamella focus buttons, outline scaling, list sorting
21
+ # 2024/04/26: Copy most of SPACEtomo_tgt
22
+ # ===================================================================
23
+
24
+ import sys
25
+ from SPACEtomo.modules.utils import log
26
+ try:
27
+ import dearpygui.dearpygui as dpg
28
+ except:
29
+ log("ERROR: DearPyGUI module not installed! If you cannot install it, please run the target selection GUI from an external machine.")
30
+ sys.exit()
31
+ import argparse
32
+ from pathlib import Path
33
+ from SPACEtomo.modules.gui.lam_sel import LamellaGUI
34
+ from SPACEtomo.modules.gui.tgt_sel import TargetGUI
35
+
36
+ def run_process(gui, path="", auto_close=False):
37
+ dpg.create_context()
38
+ main = gui(path, auto_close=auto_close)
39
+ main.show()
40
+ dpg.destroy_context()
41
+
42
+ def main():
43
+ # Process arguments
44
+ parser = argparse.ArgumentParser(description="Calls GUI to inspect whole grid maps and detected lamellae.")
45
+ parser.add_argument("gui", type=str, default="targets", help="Name of GUI to be opened. Options: targets, lamella")
46
+ parser.add_argument("map_file", nargs="?", type=str, default="", help="Absolute path to a map [.png].")
47
+ parser.add_argument("--auto_close", action="store_true", help="Auto-close GUI after inspection.")
48
+ args = parser.parse_args()
49
+
50
+ # Get GUI
51
+ if args.gui == "targets":
52
+ gui = TargetGUI
53
+ elif args.gui == "lamella":
54
+ gui = LamellaGUI
55
+ else:
56
+ raise ValueError(f"No GUI found with name {args.gui}!")
57
+
58
+ # Check map file
59
+ map_file = ""
60
+ if args.map_file != "":
61
+ map_file = Path(args.map_file)
62
+ if not map_file.exists():
63
+ map_file = ""
64
+ log("WARNING: Map file does not exist! Attempting to open GUI without map...")
65
+
66
+ # Run GUI
67
+ run_process(gui, map_file, args.auto_close)
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()
@@ -0,0 +1,27 @@
1
+ #!Python
2
+ # ===================================================================
3
+ #ScriptName SPACEtomo_lamellaDetectionGUI
4
+ # Purpose: Script to start SPACEtomo lamella detection GUI.
5
+ # More information at http://github.com/eisfabian/SPACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # ===================================================================
8
+
9
+ import serialem as sem
10
+ import sys
11
+ from SPACEtomo.modules.utils import log
12
+ try:
13
+ import dearpygui.dearpygui as dpg
14
+ except:
15
+ log("ERROR: DearPyGUI module not installed! If you cannot install it, please run the target selection GUI from an external machine.")
16
+ sys.exit()
17
+
18
+ from SPACEtomo.modules.gui.lam_sel import LamellaGUI
19
+
20
+ def main():
21
+ dpg.create_context()
22
+ main = LamellaGUI()
23
+ main.show()
24
+ dpg.destroy_context()
25
+
26
+ if __name__ == "__main__":
27
+ main()
@@ -0,0 +1,12 @@
1
+ #!Python
2
+ # ===================================================================
3
+ #ScriptName SPACEtomo_postAction
4
+ # Purpose: Post action script to setup targets from SPACEtomo runs.
5
+ # More information at http://github.com/eisfabian/SPACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # ===================================================================
8
+
9
+ import serialem as sem
10
+ from SPACEtomo.run_postaction import main
11
+
12
+ main()
@@ -0,0 +1,75 @@
1
+ #!Python
2
+ # ===================================================================
3
+ #ScriptName SPACEtomo
4
+ # Purpose: Finds lamellae in grid map, collects lamella montages and finds targets using deep learning models.
5
+ # More information at http://github.com/eisfabian/PACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # Created: 2023/05/31
8
+ # ===================================================================
9
+
10
+ ### TODO before running:
11
+ # - Microscope alignments
12
+ # - Run inventory and name grids in autoloader
13
+ # - Low Dose mode setup (ensure that both WG image state and MM (Low Dose) image states produce good images)
14
+ # - Set Shift offset between Record and View mag and check if "High Def IS" calibrations are necessary (Apply large IS in View and check if Record acquires at same position)
15
+ # - PACEtomo preparation and script settings (for automation level 5)
16
+ # - Setup "Acquire at Items" dialogue to run PACEtomo as main action and SPACEtomo_postAction as "Run after primary action" (for automation level 5)
17
+ # - In case of inference on an external GPU workstation, start SPACEtomo_monitor.py script and set external_map_dir accordingly
18
+ ###
19
+
20
+ ############ SETTINGS ############
21
+
22
+ automation_level = 4 # = 1: collect WG map and find lamellae
23
+ # = 2: collect MM maps for each lamella
24
+ # = 3: segment lamella MM maps
25
+ # = 4: setup targets based on segmentation for each lamella
26
+ # = 5: start PACEtomo batch acquisition of all targets
27
+
28
+
29
+
30
+ script_numbers = [10, 11, 13] # Numbers of [SPACEtomo_run.py, SPACEtomo_postAction.py, PACEtomo.py] scripts in SerialEM script editor
31
+ external_map_dir = "" # Path to directory where maps are saved and segmentations are expected (run "SPACEmonitor" on external machine to manage runs and queue)
32
+
33
+ # Grid settings [Level 1]
34
+
35
+ grid_list = [1] # Number or list of numbers of target grid(s) in autoloader
36
+ exclude_lamella_classes = ["gone"] # Lamella classes to exclude, possible classes: ["broken", "gone", "thick", "wedge", "contaminated", "good"]
37
+
38
+ WG_image_state = 1 # Imaging state name or index for whole grid montage (set up before!)
39
+ IM_mag_index = 10 # Intermediate mag index to use (10: 580x)
40
+ WG_distance_threshold = 5 # Minimum distance [microns] between lamellae to not be considered duplicate detection
41
+
42
+ WG_wait_for_inspection = False # = True: wait for inspection of lamellae detection by operator using the GUI
43
+
44
+ # Lamella montage settings [Level 2]
45
+
46
+ MM_image_state = [13] # Imaging state name or index for Low Dose Mode, this can be several imaging states to specify Record, View, ... (set up before!)
47
+
48
+ # Targeting settings [Level 4]
49
+
50
+ manual_selection = False # = True: creates empty segmentation, allows for manual target setup by operator using the GUI
51
+ MM_wait_for_inspection = False # = True: wait for inspection of targets using the GUI
52
+
53
+ target_list = ["mitos"] # List of target classes, additive, possible classes: ["background", "white", "black", "crack", "coating", "cell", "cellwall", "nucleus", "vacuole", "mitos", "lipiddroplets", "vesicles", "multivesicles", "membranes", "dynabeads", "ice", "cryst", "lamella"]
54
+ avoid_list = ["black", "white", "ice", "crack", "dynabeads"] # list of classes to avoid, additive
55
+ target_score_threshold = 0.01 # Weighted fraction of FOV containing the target, keep at 0 for any structures smaller than the FOV
56
+ sparse_targets = True # Use for sparse targets on the sample (e.g. mitos, vesicles)
57
+ target_edge = False # Use for edge targets (e.g. nuclear envelope, cell periphery)
58
+ penalty_weight = 0.3 # Factor to downweight overlap with classes to avoid
59
+
60
+ extra_tracking = False # Add an extra center target for the tracking tilt series (not working properly yet)
61
+ max_tilt = 60 # Maximum tilt angle [degrees] of tilt series to be acquired
62
+
63
+ ########## END SETTINGS ##########
64
+
65
+ try:
66
+ import serialem as sem
67
+ except ModuleNotFoundError:
68
+ print("WARNING: Trying to run SerialEM externally!")
69
+
70
+ from SPACEtomo.run import main
71
+ import SPACEtomo.modules.utils as utils
72
+
73
+ cur_dir = utils.getCurDir()
74
+ utils.saveSettings(cur_dir / "SPACEtomo_settings.ini", globals())
75
+ main()
@@ -0,0 +1,39 @@
1
+ #!Python
2
+ # ===================================================================
3
+ #ScriptName SPACEtomo_targetSelectionGUI
4
+ # Purpose: Script to start SPACEtomo target selection GUI.
5
+ # More information at http://github.com/eisfabian/SPACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # ===================================================================
8
+
9
+ import serialem as sem
10
+ import sys
11
+ from SPACEtomo.modules.utils import log
12
+ try:
13
+ import dearpygui.dearpygui as dpg
14
+ except:
15
+ log("ERROR: DearPyGUI module not installed! If you cannot install it, please run the target selection GUI from an external machine.")
16
+ sys.exit()
17
+ from pathlib import Path
18
+
19
+ from SPACEtomo.modules import utils
20
+ from SPACEtomo.modules.gui.tgt_sel import TargetGUI
21
+
22
+ def run(path=""):
23
+ dpg.create_context()
24
+ main = TargetGUI(path)
25
+ main.show()
26
+ dpg.destroy_context()
27
+
28
+ def main():
29
+ # Look for SPACE_maps
30
+ cur_dir = utils.getCurDir()
31
+ space_maps = sorted(cur_dir.glob("**/SPACE_maps"))
32
+ print(space_maps)
33
+ if len(space_maps) > 0:
34
+ run(space_maps[0])
35
+ else:
36
+ utils.log(f"ERROR: No SPACE_maps directory found within current directory [{cur_dir}]!")
37
+
38
+ if __name__ == "__main__":
39
+ main()
File without changes
SPACEtomo/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ __version__ = "1.2.0"
2
+ version_SerialEM = ("40200", "20240317")
SPACEtomo/config.py ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env python
2
+ # ===================================================================
3
+ # ScriptName: SPACEtomo_config
4
+ # Purpose: Configurations for deep learning models used in SPACEtomo.
5
+ # More information at http://github.com/eisfabian/SPACEtomo
6
+ # Author: Fabian Eisenstein
7
+ # Created: 2023/10/04
8
+ # Revision: v1.2
9
+ # Last Change: 2024/12/13: added check for backup config
10
+ # 2024/09/26: outsourcing of more settings from SPACEtomo_run scripts
11
+ # 2024/08/20: added SerialEM connection setting
12
+ # 2024/05/02: changed model colors to RGBA
13
+ # 2024/03/12: removed max_runs
14
+ # 2023/10/04: outsourcing of settings from main SPACEtomo script
15
+ # ===================================================================
16
+
17
+ # SerialEM specific settings
18
+ SERIALEM_IP = "127.0.0.1"
19
+ SERIALEM_PORT = 48888
20
+ SERIALEM_PYTHON_PATH = "C:/Program Files/SerialEM/PythonModules"
21
+
22
+ DUMMY = False
23
+ DEBUG = False
24
+
25
+ # Acquisition settings
26
+ WG_montage_overlap = 0.15 # overlap between pieces of whole grid montage
27
+ WG_detection_threshold = 0.1 # confidence threshold for lamella acceptance
28
+
29
+ MM_montage_overlap = 0.25 # overlap between pieces of medium mag montage
30
+ MM_padding_factor = 1.5 # padding of estimated lamella size for medium mag montage
31
+
32
+ aperture_control = True # set to True if SerialEM can control apertures (https://bio3d.colorado.edu/SerialEM/hlp/html/setting_up_serialem.htm#apertures)
33
+ objective_aperture = 70 # diameter of objective aperture
34
+
35
+ # Target selection settings
36
+ max_iterations = 10 # maximum number of iterations for target placement optimization
37
+
38
+ # Model specific settings (depend on how the model was trained)
39
+
40
+ # WG model (YOLOv8)
41
+ WG_model_file = 'model.pt'
42
+ WG_model_pix_size = 400.0 # nm/px
43
+ WG_model_sidelen = 1024 # px
44
+ WG_model_categories = ["broken", "contaminated", "good", "thick", "wedge", "gone" ]
45
+ WG_model_gui_colors = [(255, 125, 0, 255), (255, 215, 0, 255), (50, 150, 50, 255), (59, 92, 128, 255), (87, 138, 191, 255), (200, 0, 0, 255)]
46
+ WG_model_nav_colors = [0, 3, 1, 2, 2, 0 ]
47
+
48
+ # Category name Description Color
49
+ # ----------------- ------------------------------------------------------------------- ----------
50
+ # good: intact lamellae with good thickness green (1)
51
+ # thick: dark lamellae blue (2)
52
+ # wedge/unfinished: lamellae that are good on one end but thick on other end blue (2)
53
+ # contaminated: lamellae with visible ice blobs yellow (3)
54
+ # broken: lamella with crack, only parts remaining, only one side attached orange (0)
55
+ # gone: trench without lamella red (0)
56
+
57
+ # MM model (nnUNet)
58
+ MM_model_script = "run_nnUNet.py"
59
+ MM_model_folder = 'model'
60
+ MM_model_folds = [0, 1, 2, 3, 4]
61
+ MM_model_pix_size = 22.83 / 10 # nm/px
62
+
63
+ #######################################
64
+ # Self-check
65
+ import sys
66
+ import shutil
67
+ from pathlib import Path
68
+
69
+ config_backup = Path(__file__).parent / "models" / "config.py"
70
+ if config_backup.exists():
71
+ from SPACEtomo.models import config as backup
72
+ if WG_model_file != backup.WG_model_file or MM_model_folder != backup.MM_model_folder:
73
+ print(f"WARNING: Loading backup config file from previous SPACEtomo version. Please run the command/script again!")
74
+ shutil.copy(config_backup, __file__)
75
+ sys.exit()
76
+
77
+ if Path(WG_model_file).exists():
78
+ NO_WG_MODEL = False
79
+ else:
80
+ NO_WG_MODEL = True
81
+
82
+ if Path(MM_model_folder).exists():
83
+ NO_MM_MODEL = False
84
+ else:
85
+ NO_MM_MODEL = True
File without changes