You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

665 lines
21 KiB

6 years ago
7 years ago
6 years ago
5 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
4 years ago
5 years ago
6 years ago
4 years ago
4 years ago
9 years ago
4 years ago
9 years ago
4 years ago
9 years ago
5 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import re
  2. import xmltodict
  3. import six
  4. import numpy as np
  5. import warnings
  6. from nd2reader.common import read_chunk, read_array, read_metadata, parse_date, get_from_dict_if_exists
  7. from nd2reader.common_raw_metadata import parse_dimension_text_line, parse_if_not_none, parse_roi_shape, parse_roi_type, get_loops_from_data, determine_sampling_interval
  8. class RawMetadata(object):
  9. """RawMetadata class parses and stores the raw metadata that is read from the binary file in dict format.
  10. """
  11. def __init__(self, fh, label_map):
  12. self._fh = fh
  13. self._label_map = label_map
  14. self._metadata_parsed = None
  15. @property
  16. def __dict__(self):
  17. """Returns the parsed metadata in dictionary form.
  18. Returns:
  19. dict: the parsed metadata
  20. """
  21. return self.get_parsed_metadata()
  22. def get_parsed_metadata(self):
  23. """Returns the parsed metadata in dictionary form.
  24. Returns:
  25. dict: the parsed metadata
  26. """
  27. if self._metadata_parsed is not None:
  28. return self._metadata_parsed
  29. frames_per_channel = self._parse_total_images_per_channel()
  30. self._metadata_parsed = {
  31. "height": parse_if_not_none(self.image_attributes, self._parse_height),
  32. "width": parse_if_not_none(self.image_attributes, self._parse_width),
  33. "date": parse_if_not_none(self.image_text_info, self._parse_date),
  34. "fields_of_view": self._parse_fields_of_view(),
  35. "frames": self._parse_frames(),
  36. "z_levels": self._parse_z_levels(),
  37. "z_coordinates": parse_if_not_none(self.z_data, self._parse_z_coordinates),
  38. "total_images_per_channel": frames_per_channel,
  39. "channels": self._parse_channels(),
  40. "pixel_microns": parse_if_not_none(self.image_calibration, self._parse_calibration)
  41. }
  42. self._set_default_if_not_empty('fields_of_view')
  43. self._set_default_if_not_empty('frames')
  44. self._metadata_parsed['num_frames'] = len(self._metadata_parsed['frames'])
  45. self._parse_roi_metadata()
  46. self._parse_experiment_metadata()
  47. self._parse_events()
  48. return self._metadata_parsed
  49. def _set_default_if_not_empty(self, entry):
  50. total_images = self._metadata_parsed['total_images_per_channel'] \
  51. if self._metadata_parsed['total_images_per_channel'] is not None else 0
  52. if len(self._metadata_parsed[entry]) == 0 and total_images > 0:
  53. # if the file is not empty, we always have one of this entry
  54. self._metadata_parsed[entry] = [0]
  55. def _parse_width_or_height(self, key):
  56. try:
  57. length = self.image_attributes[six.b('SLxImageAttributes')][six.b(key)]
  58. except KeyError:
  59. length = None
  60. return length
  61. def _parse_height(self):
  62. return self._parse_width_or_height('uiHeight')
  63. def _parse_width(self):
  64. return self._parse_width_or_height('uiWidth')
  65. def _parse_date(self):
  66. try:
  67. return parse_date(self.image_text_info[six.b('SLxImageTextInfo')])
  68. except KeyError:
  69. return None
  70. def _parse_calibration(self):
  71. try:
  72. return self.image_calibration.get(six.b('SLxCalibration'), {}).get(six.b('dCalibration'))
  73. except KeyError:
  74. return None
  75. def _parse_frames(self):
  76. """The number of cycles.
  77. Returns:
  78. list: list of all the frame numbers
  79. """
  80. return self._parse_dimension(r""".*?T'?\((\d+)\).*?""")
  81. def _parse_channels(self):
  82. """These are labels created by the NIS Elements user. Typically they may a short description of the filter cube
  83. used (e.g. 'bright field', 'GFP', etc.)
  84. Returns:
  85. list: the color channels
  86. """
  87. if self.image_metadata_sequence is None:
  88. return []
  89. try:
  90. metadata = self.image_metadata_sequence[six.b('SLxPictureMetadata')][six.b('sPicturePlanes')]
  91. except KeyError:
  92. return []
  93. channels = self._process_channels_metadata(metadata)
  94. return channels
  95. def _process_channels_metadata(self, metadata):
  96. validity = self._get_channel_validity_list(metadata)
  97. # Channel information is contained in dictionaries with the keys a0, a1...an where the number
  98. # indicates the order in which the channel is stored. So by sorting the dicts alphabetically
  99. # we get the correct order.
  100. channels = []
  101. for valid, (label, chan) in zip(validity, sorted(metadata[six.b('sPlaneNew')].items())):
  102. if not valid:
  103. continue
  104. if chan[six.b('sDescription')] is not None:
  105. channels.append(chan[six.b('sDescription')].decode("utf8"))
  106. else:
  107. channels.append('Unknown')
  108. return channels
  109. def _get_channel_validity_list(self, metadata):
  110. try:
  111. validity = self.image_metadata[six.b('SLxExperiment')][six.b('ppNextLevelEx')][six.b('')][0][
  112. six.b('ppNextLevelEx')][six.b('')][0][six.b('pItemValid')]
  113. except (KeyError, TypeError):
  114. # If none of the channels have been deleted, there is no validity list, so we just make one
  115. validity = [True for _ in metadata]
  116. return validity
  117. def _parse_fields_of_view(self):
  118. """The metadata contains information about fields of view, but it contains it even if some fields
  119. of view were cropped. We can't find anything that states which fields of view are actually
  120. in the image data, so we have to calculate it. There probably is something somewhere, since
  121. NIS Elements can figure it out, but we haven't found it yet.
  122. """
  123. return self._parse_dimension(r""".*?XY\((\d+)\).*?""")
  124. def _parse_z_levels(self):
  125. """The different levels in the Z-plane.
  126. If they are not available from the _parse_dimension function AND there
  127. is NO 'Dimensions: ' textinfo item in the file, we return a range with
  128. the length of z_coordinates if available, otherwise an empty list.
  129. Returns:
  130. list: the z levels, just a sequence from 0 to n.
  131. """
  132. # get the dimension text to check if we should apply the fallback or not
  133. dimension_text = self._parse_dimension_text()
  134. # this returns range(len(z_levels))
  135. z_levels = self._parse_dimension(r""".*?Z\((\d+)\).*?""", dimension_text)
  136. if len(z_levels) > 0 or len(dimension_text) > 0:
  137. # Either we have found the z_levels (first condition) so return, or
  138. # don't fallback, because Z is apparently not in Dimensions, so
  139. # there should be no z_levels
  140. return z_levels
  141. # Not available from dimension, get from z_coordinates
  142. z_levels = parse_if_not_none(self.z_data, self._parse_z_coordinates)
  143. if z_levels is None:
  144. # No z coordinates, return empty list
  145. return []
  146. warnings.warn("Z-levels details missing in metadata. Using Z-coordinates instead.")
  147. return range(len(z_levels))
  148. def _parse_z_coordinates(self):
  149. """The coordinate in micron for all z planes.
  150. Returns:
  151. list: the z coordinates in micron
  152. """
  153. return self.z_data.tolist()
  154. def _parse_dimension_text(self):
  155. """While there are metadata values that represent a lot of what we want to capture, they seem to be unreliable.
  156. Sometimes certain elements don't exist, or change their data type randomly. However, the human-readable text
  157. is always there and in the same exact format, so we just parse that instead.
  158. """
  159. dimension_text = six.b("")
  160. if self.image_text_info is None:
  161. return dimension_text
  162. try:
  163. textinfo = self.image_text_info[six.b('SLxImageTextInfo')].values()
  164. except KeyError:
  165. return dimension_text
  166. for line in textinfo:
  167. entry = parse_dimension_text_line(line)
  168. if entry is not None:
  169. return entry
  170. return dimension_text
  171. def _parse_dimension(self, pattern, dimension_text=None):
  172. dimension_text = self._parse_dimension_text() if dimension_text is None else dimension_text
  173. if dimension_text is None:
  174. return []
  175. if six.PY3:
  176. dimension_text = dimension_text.decode("utf8")
  177. match = re.match(pattern, dimension_text)
  178. if not match:
  179. return []
  180. count = int(match.group(1))
  181. return range(count)
  182. def _parse_total_images_per_channel(self):
  183. """The total number of images per channel.
  184. Warning: this may be inaccurate as it includes 'gap' images.
  185. """
  186. if self.image_attributes is None:
  187. return 0
  188. try:
  189. total_images = self.image_attributes[six.b('SLxImageAttributes')][six.b('uiSequenceCount')]
  190. except KeyError:
  191. total_images = None
  192. return total_images
  193. def _parse_roi_metadata(self):
  194. """Parse the raw ROI metadata.
  195. """
  196. if self.roi_metadata is None or not six.b('RoiMetadata_v1') in self.roi_metadata:
  197. return
  198. raw_roi_data = self.roi_metadata[six.b('RoiMetadata_v1')]
  199. if not six.b('m_vectGlobal_Size') in raw_roi_data:
  200. return
  201. number_of_rois = raw_roi_data[six.b('m_vectGlobal_Size')]
  202. roi_objects = []
  203. for i in range(number_of_rois):
  204. current_roi = raw_roi_data[six.b('m_vectGlobal_%d' % i)]
  205. roi_objects.append(self._parse_roi(current_roi))
  206. self._metadata_parsed['rois'] = roi_objects
  207. def _parse_roi(self, raw_roi_dict):
  208. """Extract the vector animation parameters from the ROI.
  209. This includes the position and size at the given timepoints.
  210. Args:
  211. raw_roi_dict: dictionary of raw roi metadata
  212. Returns:
  213. dict: the parsed ROI metadata
  214. """
  215. number_of_timepoints = raw_roi_dict[six.b('m_vectAnimParams_Size')]
  216. roi_dict = {
  217. "timepoints": [],
  218. "positions": [],
  219. "sizes": [],
  220. "shape": parse_roi_shape(raw_roi_dict[six.b('m_sInfo')][six.b('m_uiShapeType')]),
  221. "type": parse_roi_type(raw_roi_dict[six.b('m_sInfo')][six.b('m_uiInterpType')])
  222. }
  223. for i in range(number_of_timepoints):
  224. roi_dict = self._parse_vect_anim(roi_dict, raw_roi_dict[six.b('m_vectAnimParams_%d' % i)])
  225. # convert to NumPy arrays
  226. roi_dict["timepoints"] = np.array(roi_dict["timepoints"], dtype=np.float)
  227. roi_dict["positions"] = np.array(roi_dict["positions"], dtype=np.float)
  228. roi_dict["sizes"] = np.array(roi_dict["sizes"], dtype=np.float)
  229. return roi_dict
  230. def _parse_vect_anim(self, roi_dict, animation_dict):
  231. """
  232. Parses a ROI vector animation object and adds it to the global list of timepoints and positions.
  233. Args:
  234. roi_dict: the raw roi dictionary
  235. animation_dict: the raw animation dictionary
  236. Returns:
  237. dict: the parsed metadata
  238. """
  239. roi_dict["timepoints"].append(animation_dict[six.b('m_dTimeMs')])
  240. image_width = self._metadata_parsed["width"] * self._metadata_parsed["pixel_microns"]
  241. image_height = self._metadata_parsed["height"] * self._metadata_parsed["pixel_microns"]
  242. # positions are taken from the center of the image as a fraction of the half width/height of the image
  243. position = np.array((0.5 * image_width * (1 + animation_dict[six.b('m_dCenterX')]),
  244. 0.5 * image_height * (1 + animation_dict[six.b('m_dCenterY')]),
  245. animation_dict[six.b('m_dCenterZ')]))
  246. roi_dict["positions"].append(position)
  247. size_dict = animation_dict[six.b('m_sBoxShape')]
  248. # sizes are fractions of the half width/height of the image
  249. roi_dict["sizes"].append((size_dict[six.b('m_dSizeX')] * 0.25 * image_width,
  250. size_dict[six.b('m_dSizeY')] * 0.25 * image_height,
  251. size_dict[six.b('m_dSizeZ')]))
  252. return roi_dict
  253. def _parse_experiment_metadata(self):
  254. """Parse the metadata of the ND experiment
  255. """
  256. self._metadata_parsed['experiment'] = {
  257. 'description': 'unknown',
  258. 'loops': []
  259. }
  260. if self.image_metadata is None or six.b('SLxExperiment') not in self.image_metadata:
  261. return
  262. raw_data = self.image_metadata[six.b('SLxExperiment')]
  263. if six.b('wsApplicationDesc') in raw_data:
  264. self._metadata_parsed['experiment']['description'] = raw_data[six.b('wsApplicationDesc')].decode('utf8')
  265. if six.b('uLoopPars') in raw_data:
  266. self._metadata_parsed['experiment']['loops'] = self._parse_loop_data(raw_data[six.b('uLoopPars')])
  267. def _parse_loop_data(self, loop_data):
  268. """Parse the experimental loop data
  269. Args:
  270. loop_data: dictionary of experiment loops
  271. Returns:
  272. list: list of the parsed loops
  273. """
  274. loops = get_loops_from_data(loop_data)
  275. # take into account the absolute time in ms
  276. time_offset = 0
  277. parsed_loops = []
  278. for loop in loops:
  279. # duration of this loop
  280. duration = get_from_dict_if_exists('dDuration', loop) or 0
  281. interval = determine_sampling_interval(duration, loop)
  282. # if duration is not saved, infer it
  283. duration = self.get_duration_from_interval_and_loops(duration, interval, loop)
  284. # uiLoopType == 6 is a stimulation loop
  285. is_stimulation = get_from_dict_if_exists('uiLoopType', loop) == 6
  286. parsed_loop = {
  287. 'start': time_offset,
  288. 'duration': duration,
  289. 'stimulation': is_stimulation,
  290. 'sampling_interval': interval
  291. }
  292. parsed_loops.append(parsed_loop)
  293. # increase the time offset
  294. time_offset += duration
  295. return parsed_loops
  296. def get_duration_from_interval_and_loops(self, duration, interval, loop):
  297. """Infers the duration of the loop from the number of measurements and the interval
  298. Args:
  299. duration: loop duration in milliseconds
  300. duration: measurement interval in milliseconds
  301. loop: loop dictionary
  302. Returns:
  303. float: the loop duration in milliseconds
  304. """
  305. if duration == 0 and interval > 0:
  306. number_of_loops = get_from_dict_if_exists('uiCount', loop)
  307. number_of_loops = number_of_loops if number_of_loops is not None and number_of_loops > 0 else 1
  308. duration = interval * number_of_loops
  309. return duration
  310. def _parse_events(self):
  311. """Extract events
  312. """
  313. # list of event names manually extracted from an ND2 file that contains all manually
  314. # insertable events from NIS-Elements software (4.60.00 (Build 1171) Patch 02)
  315. event_names = {
  316. 1: 'Autofocus',
  317. 7: 'Command Executed',
  318. 9: 'Experiment Paused',
  319. 10: 'Experiment Resumed',
  320. 11: 'Experiment Stopped by User',
  321. 13: 'Next Phase Moved by User',
  322. 14: 'Experiment Paused for Refocusing',
  323. 16: 'External Stimulation',
  324. 33: 'User 1',
  325. 34: 'User 2',
  326. 35: 'User 3',
  327. 36: 'User 4',
  328. 37: 'User 5',
  329. 38: 'User 6',
  330. 39: 'User 7',
  331. 40: 'User 8',
  332. 44: 'No Acquisition Phase Start',
  333. 45: 'No Acquisition Phase End',
  334. 46: 'Hardware Error',
  335. 47: 'N-STORM',
  336. 48: 'Incubation Info',
  337. 49: 'Incubation Error'
  338. }
  339. self._metadata_parsed['events'] = []
  340. events = read_metadata(read_chunk(self._fh, self._label_map.image_events), 1)
  341. if events is None or six.b('RLxExperimentRecord') not in events:
  342. return
  343. events = events[six.b('RLxExperimentRecord')][six.b('pEvents')]
  344. if len(events) == 0:
  345. return
  346. for event in events[six.b('')]:
  347. event_info = {
  348. 'index': event[six.b('I')],
  349. 'time': event[six.b('T')],
  350. 'type': event[six.b('M')],
  351. }
  352. if event_info['type'] in event_names.keys():
  353. event_info['name'] = event_names[event_info['type']]
  354. self._metadata_parsed['events'].append(event_info)
  355. @property
  356. def image_text_info(self):
  357. """Textual image information
  358. Returns:
  359. dict: containing the textual image info
  360. """
  361. return read_metadata(read_chunk(self._fh, self._label_map.image_text_info), 1)
  362. @property
  363. def image_metadata_sequence(self):
  364. """Image metadata of the sequence
  365. Returns:
  366. dict: containing the metadata
  367. """
  368. return read_metadata(read_chunk(self._fh, self._label_map.image_metadata_sequence), 1)
  369. @property
  370. def image_calibration(self):
  371. """The amount of pixels per micron.
  372. Returns:
  373. dict: pixels per micron
  374. """
  375. return read_metadata(read_chunk(self._fh, self._label_map.image_calibration), 1)
  376. @property
  377. def image_attributes(self):
  378. """Image attributes
  379. Returns:
  380. dict: containing the image attributes
  381. """
  382. return read_metadata(read_chunk(self._fh, self._label_map.image_attributes), 1)
  383. @property
  384. def x_data(self):
  385. """X data
  386. Returns:
  387. dict: x_data
  388. """
  389. return read_array(self._fh, 'double', self._label_map.x_data)
  390. @property
  391. def y_data(self):
  392. """Y data
  393. Returns:
  394. dict: y_data
  395. """
  396. return read_array(self._fh, 'double', self._label_map.y_data)
  397. @property
  398. def z_data(self):
  399. """Z data
  400. Returns:
  401. dict: z_data
  402. """
  403. try:
  404. return read_array(self._fh, 'double', self._label_map.z_data)
  405. except ValueError:
  406. # Depending on the file format/exact settings, this value is
  407. # sometimes saved as float instead of double
  408. return read_array(self._fh, 'float', self._label_map.z_data)
  409. @property
  410. def roi_metadata(self):
  411. """Contains information about the defined ROIs: shape, position and type (reference/background/stimulation).
  412. Returns:
  413. dict: ROI metadata dictionary
  414. """
  415. return read_metadata(read_chunk(self._fh, self._label_map.roi_metadata), 1)
  416. @property
  417. def pfs_status(self):
  418. """Perfect focus system (PFS) status
  419. Returns:
  420. dict: Perfect focus system (PFS) status
  421. """
  422. return read_array(self._fh, 'int', self._label_map.pfs_status)
  423. @property
  424. def pfs_offset(self):
  425. """Perfect focus system (PFS) offset
  426. Returns:
  427. dict: Perfect focus system (PFS) offset
  428. """
  429. return read_array(self._fh, 'int', self._label_map.pfs_offset)
  430. @property
  431. def camera_exposure_time(self):
  432. """Exposure time information
  433. Returns:
  434. dict: Camera exposure time
  435. """
  436. return read_array(self._fh, 'double', self._label_map.camera_exposure_time)
  437. @property
  438. def lut_data(self):
  439. """LUT information
  440. Returns:
  441. dict: LUT information
  442. """
  443. return xmltodict.parse(read_chunk(self._fh, self._label_map.lut_data))
  444. @property
  445. def grabber_settings(self):
  446. """Grabber settings
  447. Returns:
  448. dict: Acquisition settings
  449. """
  450. return xmltodict.parse(read_chunk(self._fh, self._label_map.grabber_settings))
  451. @property
  452. def custom_data(self):
  453. """Custom user data
  454. Returns:
  455. dict: custom user data
  456. """
  457. return xmltodict.parse(read_chunk(self._fh, self._label_map.custom_data))
  458. @property
  459. def app_info(self):
  460. """NIS elements application info
  461. Returns:
  462. dict: (Version) information of the NIS Elements application
  463. """
  464. return xmltodict.parse(read_chunk(self._fh, self._label_map.app_info))
  465. @property
  466. def camera_temp(self):
  467. """Camera temperature
  468. Yields:
  469. float: the temperature
  470. """
  471. camera_temp = read_array(self._fh, 'double', self._label_map.camera_temp)
  472. if camera_temp:
  473. for temp in map(lambda x: round(x * 100.0, 2), camera_temp):
  474. yield temp
  475. @property
  476. def acquisition_times(self):
  477. """Acquisition times
  478. Yields:
  479. float: the acquisition time
  480. """
  481. acquisition_times = read_array(self._fh, 'double', self._label_map.acquisition_times)
  482. if acquisition_times:
  483. for acquisition_time in map(lambda x: x / 1000.0, acquisition_times):
  484. yield acquisition_time
  485. @property
  486. def image_metadata(self):
  487. """Image metadata
  488. Returns:
  489. dict: Extra image metadata
  490. """
  491. if self._label_map.image_metadata:
  492. return read_metadata(read_chunk(self._fh, self._label_map.image_metadata), 1)
  493. @property
  494. def image_events(self):
  495. """Image events
  496. Returns:
  497. dict: Image events
  498. """
  499. if self._label_map.image_metadata:
  500. for event in self._metadata_parsed["events"]:
  501. yield event