def _set_metadata_from_mapping( omd: dict, md: DictionaryTreeBrowser, mapping: dict, ): """Update metadata dictionary inplace from original metadata dictionary via a mapping. Parameters ---------- omd Dictionary with original metadata. md Dictionary with metadata to update. mapping Mapping between `omd` and `md`. """ for key_out, key_in in mapping.items(): try: if isinstance(key_in, list): value = _get_nested_dictionary(omd, key_in) else: value = omd[key_in] md.set_item(key_out, value) except KeyError: warnings.warn(f"Could not read {key_in} from file.")
def kikuchipyheader2dicts( scan_group: h5py.Group, md: DictionaryTreeBrowser ) -> Tuple[DictionaryTreeBrowser, DictionaryTreeBrowser, DictionaryTreeBrowser]: """Return scan metadata dictionaries from a kikuchipy h5ebsd file. Parameters ---------- scan_group : h5py:Group HDF group of scan data and header. md Dictionary with empty fields from kikuchipy's metadata. Returns ------- md kikuchipy ``metadata`` elements available in kikuchipy file. omd All metadata available in kikuchipy file. scan_size Scan, image, step and detector pixel size available in kikuchipy file. """ # Data sets to not read via hdf5group2dict pattern_dset_names = list(manufacturer_pattern_names().values()) omd = DictionaryTreeBrowser() sem_node, ebsd_node = metadata_nodes(["sem", "ebsd"]) md.set_item( ebsd_node, hdf5group2dict( group=scan_group["EBSD/Header"], data_dset_names=pattern_dset_names, ), ) md = _delete_from_nested_dictionary(md, "Phases") phase_node = "Sample.Phases" md.set_item( sem_node, hdf5group2dict( group=scan_group["SEM/Header"], data_dset_names=pattern_dset_names, ), ) md.set_item( phase_node, hdf5group2dict( group=scan_group["EBSD/Header/Phases"], data_dset_names=pattern_dset_names, recursive=True, ), ) # Get and remove scan info values from metadata mapping = { "sx": "pattern_width", "sy": "pattern_height", "nx": "n_columns", "ny": "n_rows", "step_x": "step_x", "step_y": "step_y", "delta": "detector_pixel_size", } scan_size = DictionaryTreeBrowser() for k, v in mapping.items(): scan_size.set_item(k, _get_nested_dictionary(md, ebsd_node + "." + v)) md = _delete_from_nested_dictionary(md, mapping.values()) return md, omd, scan_size