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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
# -*- coding: utf-8 -*-
# Copyright 2007-2023 The HyperSpy developers
#
# This file is part of RosettaSciIO.
#
# RosettaSciIO is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RosettaSciIO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with RosettaSciIO. If not, see <https://www.gnu.org/licenses/#GPL>.
import gc
import importlib
from pathlib import Path
import numpy as np
import pytest
hs = pytest.importorskip("hyperspy.api", reason="hyperspy not installed")
testfile_dir = (Path(__file__).parent / "data" / "hamamatsu").resolve()
testfile_focus_mode_path = (testfile_dir / "focus_mode.img").resolve()
testfile_operate_mode_path = (testfile_dir / "operate_mode.img").resolve()
testfile_photon_count_path = (testfile_dir / "photon_counting.img").resolve()
testfile_shading_path = (testfile_dir / "shading_file.img").resolve()
class TestOperate:
@classmethod
def setup_class(cls):
cls.s = hs.load(testfile_operate_mode_path, reader="Hamamatsu")
@classmethod
def teardown_class(cls):
del cls.s
gc.collect()
def test_data_deflection(self):
expected_data_start = [
[0, 0, 715],
[246, 161, 475],
[106, 899, 0],
]
np.testing.assert_allclose(expected_data_start, self.s.isig[:3, :3].data)
def test_axes_deflection(self):
axes = self.s.axes_manager
assert axes.signal_dimension == 2
assert axes.navigation_dimension == 0
ax0 = axes[0]
ax1 = axes[1]
assert ax0.name == "Wavelength"
assert ax1.name == "Time"
assert ax0.size == 672
assert ax1.size == 512
assert ax0.units == "nm"
assert ax1.units == "µs"
expected_data_start_X = [472.252, 472.33337, 472.41473, 472.4961, 472.57745]
expected_data_start_Y = [0.0, 0.031080816, 0.062164314, 0.09325048, 0.12433932]
np.testing.assert_allclose(ax0.axis[:5], expected_data_start_X)
np.testing.assert_allclose(ax1.axis[:5], expected_data_start_Y)
def test_original_metadata_no_comment(self):
original_metadata_no_comment = {
k: v
for (k, v) in self.s.original_metadata.as_dictionary().items()
if k != "Comment"
}
expected_metadata = {
"character_im": "IM",
"offset_x": 0,
"offset_y": 0,
"file_type": "bit32",
"num_images_in_channel": 0,
"num_additional_channels": 0,
"channel_number": 0,
"timestamp": 0.0,
"marker": "",
"additional_info": "",
"image_width_px": 672,
"image_height_lines": 512,
"comment_length": 3311,
}
assert expected_metadata == original_metadata_no_comment
def test_original_metadata_comment(self):
original_metadata = self.s.original_metadata.Comment.as_dictionary()
expected_metadata = {
"Application": {
"Enconding": "UTF-8",
"Date": "20/01/2023",
"Time": "11:34:55.147",
"Software": "HPD-TA",
"Application": "2",
"ApplicationTitle": "High Performance Digital Temporal Analyzer",
"SoftwareVersion": "9.5 pf10",
"SoftwareDate": "15.12.2020",
},
"Camera": {
"CameraName": "Orca R2",
"SerialNumber": "S/N: 891892",
"Type": "37",
"ScanMode": "1",
"Subarray": "0",
"Binning": "2",
"BitsPerChannel": "0",
"HighDynamicRangeMode": "0",
"ScanSpeed": "0",
"SubarrayHpos": "0",
"SubarrayHsize": "0",
"SubarrayVpos": "0",
"SubarrayVsize": "0",
"TriggerMode": "3",
"TriggerModeKeyVal": "Internal",
"TriggerPolarity": "1",
"TriggerPolarityKeyVal": "neg.",
"CoolerSwitch": "-1",
"Gain": "0",
"Prop_SensorMode": "1",
"Prop_Colortype": "1",
"Prop_TriggerTimes": "1",
"Prop_ExposureTimeControl": "2",
"Prop_TimingMinTriggerInterval": "0.500139",
"Prop_TimingExposure": "2",
"Prop_ImageTopOffsetBytes": "0",
"Prop_ImagePixelType": "2",
"Prop_BufferRowbytes": "1344",
"Prop_BufferFramebytes": "688128",
"Prop_BufferTopOffsetBytes": "0",
"Prop_BufferPixelType": "2",
"Prop_RecordFixedBytesPerFile": "256",
"Prop_RecordFixedBytesPerSession": "784",
"Prop_RecordFixedBytesPerFrame": "688176",
"Prop_NumberOfOutputTriggerConnector": "1",
"Prop_OutputTriggerPolarity": "1",
"Prop_OutputTriggerActive": "1",
"Prop_OutputTriggerDelay": "0",
"Prop_OutputTriggerPeriod": "0.0001",
"Prop_SystemAlive": "2",
"Prop_ImageDetectorPixelWidth": "6.45",
"Prop_ImageDetectorPixelHeight": "6.45",
"Prop_TimeStampProducer": "2",
"Prop_FrameStampProducer": "2",
},
"Acquisition": {
"NrExposure": "60",
"NrTrigger": "0",
"ExposureTime": "5 s",
"AcqMode": "4",
"DataType": "8",
"DataTypeOfSingleImage": "7",
"CurveCorr": "0",
"DefectCorrection": "0",
"areSource": "0,0,672,512",
"areGRBScan": "0,0,672,512",
"pntOrigCh": "0,0",
"pntOrigFB": "0,0",
"pntBinning": "2,2",
"BytesPerPixel": "4",
"IsLineData": "0",
"BacksubCorr": "-1",
"ShadingCorr": "0",
"ZAxisLabel": "Intensity",
"ZAxisUnit": "Count",
"miMirrorRotate": "0",
},
"Grabber": {"Type": "5", "SubType": "0"},
"DisplayLUT": {
"EntrySize": "9",
"LowerValue": "764",
"UpperValue": "3562",
"LowerValueEx": "0",
"UpperValueEx": "32767",
"BitRange": "16x bit",
"Color": "2",
"LUTType": "0",
"LUTInverted": "0",
"AutoLutInLiveMode": "0",
"DisplayNegative": "0",
"Gamma": "1",
"First812OvlCol": "1",
"Lut16xShift": "7",
"Lut16xOvlVal": "3932100",
},
"ExternalDevices": {
"TriggerDelay": "150",
"PostTriggerTime": "10",
"ExposureTime": "10",
"TDStatusCableConnected": "0",
"ConnectMonitorOut": "0",
"ConnectResetIn": "0",
"TriggerMethod": "2",
"UseDTBE": "0",
"ExpTimeAddMultiple": "-1",
"DontSendReset": "0",
"MultipleOfSweep": "1",
"A6538Connected": "0",
"CounterBoardInstalled": "0",
"MotorizedSlitInstalled": "0",
"UseSpecAsMono": "0",
"GPIBInstalled": "-1",
"CounterBoardIOBase": "0",
"MotorizedSlitPortID": "1",
"GPIBIOBase": "0",
},
"Streak camera": {
"UseDevice": "-1",
"DeviceName": "C5680",
"PluginName": "M5677",
"GPIBCableConnected": "-1",
"GPIBBase": "8",
"Time Range": "20 us",
"Mode": "Operate",
"Gate Mode": "Normal",
"MCP Gain": "50",
"Shutter": "Open",
"Gate Time": "0",
"Trig. Mode": "Cont",
"Trigger status": "Ready",
"Trig. level": "1",
"Trig. slope": "Rising",
"FocusTimeOver": "11",
},
"Spectrograph": {
"UseDevice": "-1",
"DeviceName": "Andor SG",
"PluginName": "Kymera 328i",
"GPIBCableConnected": "0",
"GPIBBase": "13",
"Wavelength": "500",
"Grating": "300 g/mm",
"Blaze": "500",
"Ruling": "300",
"Exit Mirror": "Front",
"Side Ent. Slitw.": "10",
"Turret": "1",
"Focus Mirror": "234",
"Side Entry Iris": "26",
},
"Delay box": {
"UseDevice": "-1",
"DeviceName": "C4792-01",
"PluginName": "",
"GPIBCableConnected": "0",
"Trig. Mode": "Int. Trig.",
"Repetition Rate": "1 Hz",
"L-Pulsewidth": "300 ns",
"Delay Int.Trig": "-999999999900",
"Delay Ext.Trig": "0",
"Dly Mode-Lock": "0",
"Dly1 DmpMode": "0",
"Dly2 DmpMode": "0",
},
"Delay2 box": {"UseDevice": "0"},
"Filter wheel": {"UseDevice": "0"},
"Scaling": {
"ScalingXType": "2",
"ScalingXScale": "1",
"ScalingXUnit": "nm",
"ScalingXScalingFile": "#1379631,0672",
"ScalingYType": "2",
"ScalingYScale": "1",
"ScalingYUnit": "us",
"ScalingYScalingFile": "#1382319,0512",
},
"Comment": {"UserComment": ""},
}
assert expected_metadata == original_metadata
def test_metadata(self):
metadata = self.s.metadata
detector = self.s.metadata.Acquisition_instrument.Detector
spectrometer = self.s.metadata.Acquisition_instrument.Spectrometer
assert metadata.General.date == "2023-01-20"
assert metadata.General.time == "11:34:55"
assert metadata.General.original_filename == "operate_mode.img"
assert metadata.General.title == metadata.General.original_filename[:-4]
assert metadata.Signal.quantity == "Intensity (Counts)"
if importlib.util.find_spec("lumispy") is None:
signal_type = ""
else:
signal_type = "Luminescence"
assert metadata.Signal.signal_type == signal_type
assert isinstance(detector.binning, tuple)
assert len(detector.binning) == 2
assert detector.binning[0] == 2
assert detector.binning[1] == 2
assert detector.detector_type == "StreakCamera"
assert detector.model == "C5680"
assert detector.frames == 60
np.testing.assert_allclose(detector.integration_time, 300)
assert detector.processing.background_correction is True
assert detector.processing.curvature_correction is False
assert detector.processing.defect_correction is False
assert detector.processing.shading_correction is False
np.testing.assert_allclose(detector.time_range, 20)
assert detector.time_range_units == "µs"
np.testing.assert_allclose(detector.mcp_gain, 50)
assert detector.acquisition_mode == "analog_integration"
np.testing.assert_allclose(spectrometer.entrance_slit_width, 10)
assert spectrometer.model == "Andor SG"
assert spectrometer.Grating.blazing_wavelength == 500
assert spectrometer.Grating.groove_density == 300
np.testing.assert_allclose(spectrometer.central_wavelength, 500)
class TestFocus:
@classmethod
def setup_class(cls):
cls.s_focus = hs.load(testfile_focus_mode_path, reader="Hamamatsu")
@classmethod
def teardown_class(cls):
del cls.s_focus
gc.collect()
def test_data_focus(self):
expected_data_end = [[0, 0, 36], [0, 0, 0], [21, 0, 0]]
np.testing.assert_allclose(expected_data_end, self.s_focus.isig[-3:, -3:].data)
def test_axes_focus(self):
axes = self.s_focus.axes_manager
assert axes.signal_dimension == 2
assert axes.navigation_dimension == 0
ax0 = axes[0]
ax1 = axes[1]
assert ax0.name == "Wavelength"
assert ax1.name == "Vertical CCD Position"
assert ax1.units == "px"
assert ax0.units == "nm"
assert ax0.size == 672
assert ax1.size == 512
np.testing.assert_allclose(ax1.scale, 1)
np.testing.assert_allclose(ax1.offset, 0)
expected_data_start_X = [472.252, 472.33337, 472.41473, 472.4961, 472.57745]
np.testing.assert_allclose(ax0.axis[:5], expected_data_start_X)
class TestPhotonCount:
@classmethod
def setup_class(cls):
cls.s = hs.load(testfile_photon_count_path, reader="Hamamatsu")
@classmethod
def teardown_class(cls):
del cls.s
gc.collect()
def test_data(self):
expected_data = [0, 0, 0]
np.testing.assert_allclose(self.s.isig[-3:, 0].data, expected_data)
def test_metadata(self):
metadata = self.s.metadata
assert metadata.General.date == "2018-08-29"
assert (
metadata.Acquisition_instrument.Detector.acquisition_mode
== "photon_counting"
)
assert "Grating" not in list(
self.s.metadata.Acquisition_instrument.Spectrometer.as_dictionary()
)
assert self.s.original_metadata.file_type == "bit16"
class TestShading:
@classmethod
def setup_class(cls):
cls.s = hs.load(testfile_shading_path, reader="Hamamatsu")
@classmethod
def teardown_class(cls):
del cls.s
gc.collect()
def test_metadata(self):
np.testing.assert_allclose(
self.s.metadata.Acquisition_instrument.Detector.time_range, 4
)
def test_data(self):
expected_data = [9385, 8354, 7658]
np.testing.assert_allclose(self.s.isig[:3, 0].data, expected_data)
|