Exemple #1
0
 def test_merge_microstructures(self):
     m1 = Microstructure(os.path.join(PYMICRO_EXAMPLES_DATA_DIR, 'm1_data.h5'))
     m2 = Microstructure(os.path.join(PYMICRO_EXAMPLES_DATA_DIR, 'm2_data.h5'))
     # merge the two microstructures
     m1m2 = Microstructure.merge_microstructures([m1, m2], overlap=16)
     m1m2.autodelete = True
     h5_file = m1m2.h5_file
     xdmf_file = m1m2.xdmf_file
     dims = (64, 64, 64)
     for i in range(3):
         self.assertEqual(m1m2.get_grain_map().shape[i], dims[i])
     self.assertEqual(m1m2.get_number_of_grains(), 27)
     del m1m2
     self.assertTrue(not os.path.exists(h5_file))
     self.assertTrue(not os.path.exists(xdmf_file))
     del m1, m2
Exemple #2
0
def merge_dct_scans(scan_list,
                    samtz_list,
                    use_mask=False,
                    overlap=-1,
                    root_dir='.',
                    write_to_h5=True):
    """Merge two DCT scans.

    This function build a `Microstructure` instance for each DCT scan and calls merge_microstructures.
    The overlap can be deduced from the samtz values or specified directly.

    :param list scan_list: a list with the two DCT scan names.
    :param list samtz_list: a list with the two samtz value (the order should match the scan names).
    :param bool use_mask: a flag to also merge the absorption masks.
    :param int overlap: the value to use for the overlap if not computed automatically.
    :param str root_dir: the root data folder.
    :param bool write_to_h5: flag to write the result of the merging operation to an HDF5 file.
    :return: A new `Microstructure` instance of the 2 merged scans.
    """
    from pymicro.crystal.microstructure import Microstructure
    import numpy as np
    import os
    import h5py

    scan_shapes = []  # warning, shapes will be in (z, y, x) form
    micros = []

    for scan in scan_list:
        scan_path = os.path.join(root_dir, scan, '5_reconstruction',
                                 'phase_01_vol.mat')
        with h5py.File(scan_path) as f:
            scan_shapes.append(f['vol'].shape)
            print(f['vol'].shape)

    # figure out the maximum cross section
    max_shape = np.array(scan_shapes).max(axis=0)[[2, 1, 0]]

    for scan in scan_list:
        # read microstructure for this scan
        dct_analysis_dir = os.path.join(root_dir, scan)
        print('processing scan %s' % scan)
        micro = Microstructure.from_dct(data_dir=dct_analysis_dir)
        print('voxel_size is {}'.format(micro.voxel_size))

        # pad both grain map and mask
        print('max shape is {}'.format(max_shape))
        print('vol shape is {}'.format(micro.grain_map.shape))
        offset = max_shape - micro.grain_map.shape
        offset[2] = 0  # do not pad along Z
        padding = [(o // 2, max_shape[0] - micro.grain_map.shape[0] - o // 2)
                   for o in offset]
        print('padding is {}'.format(padding))
        micro.grain_map = np.pad(micro.grain_map, padding, mode='constant')
        print('has mask ? {}'.format(hasattr(micro, 'mask')))
        if use_mask:
            micro.mask = np.pad(micro.mask, padding, mode='constant')
        elif hasattr(micro, 'mask'):
            print('deleting mask attribute since we do not want to use it')
            delattr(micro, 'mask')
        micros.append(micro)

    # find out the overlap region (based on the difference in samtz)
    overlap_from_samtz = int(
        (samtz_list[1] + scan_shapes[1][0] // 2 * micros[1].voxel_size) /
        micros[1].voxel_size -
        (samtz_list[0] - scan_shapes[0][0] // 2 * micros[0].voxel_size) /
        micros[0].voxel_size)
    print('vertical overlap deduced from samtz positions is %d voxels' %
          overlap_from_samtz)
    if overlap < 0:
        overlap = overlap_from_samtz
    print('using an actual overlap of %d voxels' % overlap)

    # we have prepared the 2 microstructures, now merge them
    merged_micro = Microstructure.merge_microstructures(micros,
                                                        overlap,
                                                        plot=True)

    if write_to_h5:
        # write the result
        merged_micro.to_h5()

    return merged_micro