Skip to content

Commit a8975d9

Browse files
authored
Fix TEMController alignments being incorrectly stored and restored (#133)
1 parent dcbc1c7 commit a8975d9

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/instamatic/config/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,9 @@ def get_base_drc():
9090

9191
def get_alignments() -> dict:
9292
"""Get alignments from the alignment directory and return them as a dict of
93-
dicts.
94-
95-
Use `ctrl.from_dict` to load the alignments
96-
"""
97-
fns = alignments_drc.glob('*.yaml')
98-
alignments = {fn.name: yaml.full_load(open(fn)) for fn in fns}
99-
return alignments
93+
dicts; Use `ctrl.from_dict` to load previously-stored alignments."""
94+
yaml_filenames = alignments_drc.glob('*.yaml')
95+
return {fn.stem: yaml.safe_load(open(fn)) for fn in yaml_filenames}
10096

10197

10298
class ConfigObject:

src/instamatic/controller.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Callable, Generator, Optional, Tuple
77

88
import numpy as np
9+
import yaml
910

1011
from instamatic import config
1112
from instamatic.camera import Camera
@@ -15,6 +16,7 @@
1516
from instamatic.image_utils import rotate_image
1617
from instamatic.microscope import components
1718
from instamatic.microscope.base import MicroscopeBase
19+
from instamatic.microscope.components.deflectors import DeflectorTuple
1820
from instamatic.microscope.microscope import get_microscope
1921

2022
_ctrl = None # store reference of ctrl so it can be accessed without re-initializing
@@ -795,17 +797,14 @@ def store(self, name: str = 'stash', keys: tuple = None, save_to_file: bool = Fa
795797
Multiple settings can be stored under different names. Specify
796798
which settings should be stored using `keys`
797799
"""
798-
if not keys:
799-
keys = ()
800-
d = self.to_dict(*keys)
800+
d = self.to_dict(*keys if keys else ())
801801
d.pop('StagePosition', None)
802802
self._saved_alignments[name] = d
803803

804804
if save_to_file:
805-
import yaml
806-
807805
fn = config.alignments_drc / (name + '.yaml')
808-
yaml.dump(d, stream=open(fn, 'w'))
806+
d2 = {k: list(v) if isinstance(v, DeflectorTuple) else v for k, v in d.items()}
807+
yaml.safe_dump(d2, stream=open(fn, 'w'))
809808
print(f'Saved alignment to file `{fn}`')
810809

811810
def restore(self, name: str = 'stash'):

0 commit comments

Comments
 (0)