Skip to content

Commit 5285b01

Browse files
committed
Store averaged real time / live time into metadata
1 parent 0d19389 commit 5285b01

File tree

1 file changed

+98
-29
lines changed

1 file changed

+98
-29
lines changed

rsciio/digitalmicrograph/_api.py

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def get_axes_dict(self):
739739
)
740740
]
741741

742-
def get_metadata(self, metadata=None):
742+
def new_metadata(self, metadata=None):
743743
if metadata is None:
744744
metadata = {}
745745
if "General" not in metadata:
@@ -772,6 +772,24 @@ def _get_mode(self, mode):
772772
else:
773773
return "TEM"
774774

775+
def _get_illumination_mode(self):
776+
# check if instrument is SEM or TEM
777+
if (
778+
"Microscope Info" in self.imdict.ImageTags
779+
and "Illumination Mode" in self.imdict.ImageTags["Microscope Info"]
780+
):
781+
microscope = (
782+
"SEM"
783+
if self._get_mode(
784+
self.imdict.ImageTags["Microscope Info"]["Illumination Mode"]
785+
)
786+
== "SEM"
787+
else "TEM"
788+
)
789+
else:
790+
microscope = "TEM"
791+
return microscope
792+
775793
def _get_time(self, time):
776794
try:
777795
dt = dateutil.parser.parse(time)
@@ -807,6 +825,81 @@ def _get_microscope_name(self, ImageTags):
807825
_logger.info("Microscope name not present")
808826
return None
809827

828+
829+
def get_EDS_pixel_time(self, mp, om, overwrite_pixeltime=False):
830+
# mp and om are dict, not Box in this stage
831+
def _get_item(dic, tag_name):
832+
tags = tag_name.split('.')
833+
for tag in tags:
834+
dic = dic.get(tag)
835+
if dic is None:
836+
break
837+
return dic
838+
def _set_item(dic, tag_name, val):
839+
tags = tag_name.split('.')
840+
for tag in tags[:-1]:
841+
if tag not in dic:
842+
dic[tag] = {}
843+
dic = dic[tag]
844+
dic[tags[-1]] = val
845+
846+
_mode = self._get_illumination_mode()
847+
if "SEM" not in _mode: # SEM/TEM/STEM
848+
_mode = "TEM"
849+
_om_eds_images = _get_item(om, "ImageTags.EDS.Images")
850+
_om_si = _get_item(om, "ImageTags.SI.Acquisition")
851+
_eds_tag_name = "Acquisition_instrument."+_mode+".Detector.EDS"
852+
if _om_eds_images is None or _om_si is None:
853+
# No pixel-wise EDS live-time data in original_metadata
854+
return
855+
_notes = _get_item(mp, "General.notes")
856+
if _notes is None:
857+
_notes = ""
858+
_num_cycle = _om_si.get("Number of cycles")
859+
_pi = _om_si.get("Pixel iterator")
860+
_pt = _om_si.get("Pixel time (s)")
861+
_shape = self.shape[1:] # (Energy, Height, Width)
862+
_rtm = _om_eds_images.get("Real time")
863+
_ltm = _om_eds_images.get("Live time")
864+
_ctm = _om_eds_images.get("Count rate")
865+
if _ltm is None or _rtm is None:
866+
# No pixel-wise EDS live-time data in original_metadata
867+
return
868+
_ct = np.array(_ctm).reshape(_shape)
869+
_rt = np.array(_rtm).reshape(_shape)
870+
_lt = np.array(_ltm).reshape(_shape)
871+
872+
###
873+
### Overwrite data shape
874+
###
875+
_om_eds_images["Real time"] = _rt
876+
_om_eds_images["Live time"] = _lt
877+
_om_eds_images["Count rate"] = _ct
878+
879+
_rta = np.average(_rt)
880+
_lta = np.average(_lt)
881+
_cta = np.average(_ct)
882+
_rts = np.std(_rt)
883+
_lts = np.std(_lt)
884+
_cts = np.std(_ct)
885+
_set_item(mp, _eds_tag_name+".real_time (s)", _rta)
886+
_set_item(mp, _eds_tag_name+".live_time (s)", _lta)
887+
888+
_notes += "Pixel time(Setting) = {:.1f} ms, ".format(_pt * 1000)
889+
_notes += "ave. real time = {:.2f} ms (std = {:.2f}), ".format(_rta * 1000, _rts * 1000)
890+
_notes += "ave. live time = {:.2f} ms (std = {:.2f}), ".format(_rta * 1000, _lts * 1000)
891+
_notes += "ave. count rate = {:.2f} ms (std = {:.2f}), ".format(_cta * 1000, _cts * 1000)
892+
_notes += "number of cycles = {}.\n".format(_num_cycle)
893+
894+
if _pt * 1.1 < _rta:
895+
_logger.warning("Pixel time ({:.1f} ms) / EDS real time ({:.1f} ms) mismatch".format(_pt * 1000, _rta * 1000))
896+
if _num_cycle != 1:
897+
_warn = "Part of pixel-wise real-time/live-time data was lost when number of cycles != 1"
898+
if _notes != "":
899+
_set_item(mp, "General.notes", _notes)
900+
return
901+
902+
810903
def _parse_string(self, tag, convert_to_float=False, tag_name=None):
811904
if len(tag) == 0:
812905
return None
@@ -855,6 +948,7 @@ def _get_CL_detector_type(self, tags):
855948
_logger.info("CL detector type can't be read.")
856949
return None
857950

951+
858952
def get_mapping(self):
859953
if "source" in self.imdict.ImageTags.keys():
860954
# For stack created with the stack builder plugin
@@ -865,21 +959,7 @@ def get_mapping(self):
865959
tags_path = "ImageList.TagGroup0.ImageTags"
866960
image_tags_dict = self.imdict.ImageTags
867961
is_scanning = "DigiScan" in image_tags_dict.keys()
868-
# check if instrument is SEM or TEM
869-
if (
870-
"Microscope Info" in self.imdict.ImageTags
871-
and "Illumination Mode" in self.imdict.ImageTags["Microscope Info"]
872-
):
873-
microscope = (
874-
"SEM"
875-
if self._get_mode(
876-
self.imdict.ImageTags["Microscope Info"]["Illumination Mode"]
877-
)
878-
== "SEM"
879-
else "TEM"
880-
)
881-
else:
882-
microscope = "TEM"
962+
microscope = self._get_illumination_mode()
883963
mapping = {
884964
"{}.DataBar.Acquisition Date".format(tags_path): (
885965
"General.date",
@@ -1064,18 +1144,6 @@ def get_mapping(self):
10641144
"Acquisition_instrument.TEM.Detector.EDS.solid_angle",
10651145
None,
10661146
),
1067-
"{}.EDS.Images.Count rate".format(tags_path): (
1068-
"EDS.Count_rate_map",
1069-
lambda x: np.array(x).reshape(self.shape[1:]),
1070-
),
1071-
"{}.EDS.Images.Live time".format(tags_path): (
1072-
"EDS.Live_time_map",
1073-
lambda x: np.array(x).reshape(self.shape[1:])
1074-
),
1075-
"{}.EDS.Images.Real time".format(tags_path): (
1076-
"EDS.Real_time_map",
1077-
lambda x: np.array(x).reshape(self.shape[1:])
1078-
),
10791147
"{}.EDS.Live_time".format(tags_path): (
10801148
"Acquisition_instrument.TEM.Detector.EDS.live_time",
10811149
None,
@@ -1291,8 +1359,9 @@ def file_reader(filename, lazy=False, order=None, optimize=True, **kwargs):
12911359
for image in images:
12921360
dm.tags_dict["ImageList"]["TagGroup0"] = image.imdict.to_dict()
12931361
axes = image.get_axes_dict()
1294-
mp = image.get_metadata()
1362+
mp = image.new_metadata()
12951363
mp["General"]["original_filename"] = os.path.split(filename)[1]
1364+
image.get_EDS_pixel_time(mp, dm.tags_dict["ImageList"]["TagGroup0"])
12961365
post_process = []
12971366
if image.to_spectrum is True:
12981367
post_process.append(lambda s: s.to_signal1D(optimize=optimize))

0 commit comments

Comments
 (0)