8000 Transmission reader: Fixes reading of detector ranges by domna · Pull Request #112 · FAIRmat-NFDI/pynxtools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Transmission reader: Fixes reading of detector ranges #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pynxtools/dataconverter/readers/json_yml/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def read(

sorted_paths = sorted(file_paths, key=lambda f: os.path.splitext(f)[1])
for file_path in sorted_paths:
extension = os.path.splitext(file_path)[1]
extension = os.path.splitext(file_path)[1].lower()
if extension not in self.extensions:
print(
f"WARNING: "
Expand Down
73 changes: 40 additions & 33 deletions pynxtools/dataconverter/readers/transmission/reader.py
8000
Original file line number Diff line numberDiff line change
Expand Up @@ -42,22 +42,17 @@
"/ENTRY[entry]/instrument/spectrometer/GRATING[grating1]/wavelength_range":
mpars.read_visir_monochromator_range,
"/ENTRY[entry]/instrument/SOURCE[source]/type": "D2",
"/ENTRY[entry]/instrument/SOURCE[source]/wavelength_range":
mpars.get_d2_range,
"/ENTRY[entry]/instrument/SOURCE[source]/wavelength_range": mpars.get_d2_range,
"/ENTRY[entry]/instrument/SOURCE[source1]/type": "halogen",
"/ENTRY[entry]/instrument/SOURCE[source1]/wavelength_range":
mpars.get_halogen_range
"/ENTRY[entry]/instrument/SOURCE[source1]/wavelength_range": mpars.get_halogen_range,
}
# Dictionary to map value during the yaml eln reading
# This is typically a mapping from ELN signifier to NeXus path
CONVERT_DICT: Dict[str, str] = {
'Sample': 'SAMPLE[sample]',
"unit": "@units"
}
CONVERT_DICT: Dict[str, str] = {"Sample": "SAMPLE[sample]", "unit": "@units"}
# Dictionary to map nested values during the yaml eln reading
# This is typically a mapping from nested ELN signifiers to NeXus group
REPLACE_NESTED: Dict[str, str] = {
'Sample[sample]/experiment_identifier': 'experiment_identifier'
"Sample[sample]/experiment_identifier": "experiment_identifier"
}


Expand Down Expand Up @@ -93,19 +88,21 @@ def parse_detector_line(line: str, convert: Callable[[str], Any] = None) -> List
List[Any]: The list of detector settings.
"""
if convert is None:

def convert(val):
return val
return [convert(s.split('/')[-1]) for s in line.split()]

return [convert(s.split("/")[-1]) for s in line.split()]


# pylint: disable=too-many-arguments
def convert_detector_to_template(
det_type: str,
slit: str,
time: float,
gain: float,
det_idx: int,
wavelength_range: List[float]
det_type: str,
slit: str,
time: float,
gain: float,
det_idx: int,
wavelength_range: List[float],
) -> Dict[str, Any]:
"""Writes the detector settings to the template.

Expand All @@ -119,9 +116,9 @@ def convert_detector_to_template(
Dict[str, Any]: The dictionary containing the data readout from the asc file.
"""
if det_idx == 0:
path = '/ENTRY[entry]/instrument/DETECTOR[detector]'
path = "/ENTRY[entry]/instrument/DETECTOR[detector]"
else:
path = f'/ENTRY[entry]/instrument/DETECTOR[detector{det_idx}]'
path = f"/ENTRY[entry]/instrument/DETECTOR[detector{det_idx}]"
template: Dict[str, Any] = {}
template[f"{path}/type"] = det_type
template[f"{path}/response_time"] = time
Expand Down Expand Up @@ -149,25 +146,36 @@ def read_detectors(metadata: list) -> Dict[str, Any]:
detector_times = parse_detector_line(metadata[32], float)
detector_gains = parse_detector_line(metadata[35], float)
detector_changes = [float(x) for x in metadata[43].split()]
wavelength_ranges = \
wavelength_ranges = (
[mpars.MIN_WAVELENGTH] + detector_changes[::-1] + [mpars.MAX_WAVELENGTH]
)

template.update(
convert_detector_to_template(
"PMT", detector_slits[2], detector_times[2],
None, 2, [wavelength_ranges[0], wavelength_ranges[1]]
"PMT",
detector_slits[-1],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also relevant to use -1 instead of 2 here. This line was actually failing if there was only one detector

detector_times[-1],
None,
2,
[wavelength_ranges[0], wavelength_ranges[1]],
)
)

for name, idx in zip(["PbS", "InGaAs"], [1, 0]):
for name, idx, slit, time, gain in zip(
["PbS", "InGaAs"],
[1, 0],
detector_slits[1:],
detector_times[1:],
detector_gains[1:],
Comment on lines +167 to +169
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is the important change here? This looks fine how its being used below. If so, this is ready to merge. I'll approve this unless something else needs attention to.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this and the other line I added a comment for. Sorry that I committed my auto-formatting here, too

):
template.update(
convert_detector_to_template(
name,
detector_slits[idx],
detector_times[idx],
detector_gains[idx],
slit,
time,
gain,
idx,
[wavelength_ranges[2 - idx], wavelength_ranges[3 - idx]]
[wavelength_ranges[2 - idx], wavelength_ranges[3 - idx]],
)
)

Expand All @@ -193,9 +201,7 @@ def parse_asc(file_path: str) -> Dict[str, Any]:
break
metadata.append(line.strip())

data = pd.read_csv(
fobj, delim_whitespace=True, header=None, index_col=0
)
data = pd.read_csv(fobj, delim_whitespace=True, header=None, index_col=0)

for path, val in METADATA_MAP.items():
# If the dict value is an int just get the data with it's index
Expand Down Expand Up @@ -224,9 +230,10 @@ def add_def_info() -> Dict[str, str]:
template["/ENTRY[entry]/@default"] = "data"
template["/ENTRY[entry]/definition"] = "NXtransmission"
template["/ENTRY[entry]/definition/@version"] = "v2022.06"
template["/ENTRY[entry]/definition/@url"] = \
"https://fairmat-experimental.github.io/nexus-fairmat-proposal/" + \
"50433d9039b3f33299bab338998acb5335cd8951/index.html"
template["/ENTRY[entry]/definition/@url"] = (
"https://fairmat-experimental.github.io/nexus-fairmat-proposal/"
+ "50433d9039b3f33299bab338998acb5335cd8951/index.html"
)

return template

Expand All @@ -242,7 +249,7 @@ class TransmissionReader(YamlJsonReader):
".json": parse_json,
".yml": lambda fname: parse_yml(fname, CONVERT_DICT, REPLACE_NESTED),
".yaml": lambda fname: parse_yml(fname, CONVERT_DICT, REPLACE_NESTED),
"default": lambda _: add_def_info()
"default": lambda _: add_def_info(),
}


Expand Down
0