Exemple #1
0
def initialize_preferences(settings):
    if settings.has_option('preferences', 'memory'):
        nxsetmemory(settings.get('preferences', 'memory'))
    else:
        settings.set('preferences', 'memory', nxgetmemory())
    if settings.has_option('preferences', 'maxsize'):
        nxsetmaxsize(settings.get('preferences', 'maxsize'))
    else:
        settings.set('preferences', 'maxsize', nxgetmaxsize())
    if settings.has_option('preferences', 'compression'):
        nxsetcompression(settings.get('preferences', 'compression'))
    else:
        settings.set('preferences', 'compression', nxgetcompression())
    if settings.has_option('preferences', 'encoding'):
        nxsetencoding(settings.get('preferences', 'encoding'))
    else:
        settings.set('preferences', 'encoding', nxgetencoding())
    if settings.has_option('preferences', 'lock'):
        nxsetlock(settings.get('preferences', 'lock'))
    else:
        settings.set('preferences', 'lock', nxgetlock())
    if settings.has_option('preferences', 'lockexpiry'):
        nxsetlockexpiry(settings.get('preferences', 'lockexpiry'))
    else:
        settings.set('preferences', 'lockexpiry', nxgetlockexpiry())
    if settings.has_option('preferences', 'recursive'):
        nxsetrecursive(settings.getboolean('preferences', 'recursive'))
    else:
        settings.set('preferences', 'recursive', nxgetrecursive())
    if settings.has_option('preferences', 'style'):
        set_style(settings.get('preferences', 'style'))
    else:
        settings.set('preferences', 'style', 'default')
    settings.save()
Exemple #2
0
def peak_search(data_file, data_path, i, j, k, threshold):
    """Identify peaks in the slab of raw data

    Parameters
    ----------
    data_file : str
        File path to the raw data file
    data_path : str
        Internal path to the raw data
    i : int
        Index of first z-value of output peaks
    j : int
        Index of first z-value of processed slab
    k : int
        Index of last z-value of processed slab
    threshold : float
        Peak threshold

    Returns
    -------
    list of NXBlobs
        Peak locations and intensities stored in NXBlob instances
    """
    global saved_blobs
    nxsetlock(600)

    def save_blobs(lio, blobs):
        for b in blobs:
            if b[0] < 0.1:
                continue
            lio.spot3d_id += 1
            blob = NXBlob(b)
            if blob.np > 0.0:
                saved_blobs.append(blob)
        if lio.onfirst > 0:
            lio.onfirst = 0

    data_root = nxload(data_file, 'r')
    with data_root.nxfile:
        data = data_root[data_path][j:k].nxvalue

    labelimage.outputpeaks = save_blobs
    lio = labelimage(data.shape[-2:], flipper=flip1, fileout=os.devnull)
    nframes = data.shape[0]
    saved_blobs = []
    for z in range(nframes):
        lio.peaksearch(data[z], threshold, z)
        lio.mergelast()
    lio.finalise()
    for blob in saved_blobs:
        blob.z += j
    return i, saved_blobs
Exemple #3
0
def symmetrize_data(symm_function, data_type, data_file, data_path):
    nxsetlock(60)
    data_root = nxload(data_file, 'r')
    if data_type == 'signal':
        data = data_root[data_path].nxvalue
    else:
        signal = data_root[data_path].nxvalue
        data = np.zeros(signal.shape, signal.dtype)
        data[np.where(signal > 0)] = 1
    result = symm_function(data)
    root = nxload(tempfile.mkstemp(suffix='.nxs')[1], mode='w')
    root['data'] = result
    return data_type, root.nxfilename
Exemple #4
0
def symmetrize_entries(symm_function, data_type, data_file, data_path):
    nxsetlock(60)
    data_root = nxload(data_file, 'r')
    data_path = os.path.basename(data_path)
    for i, entry in enumerate([e for e in data_root if e != 'entry']):
        if i == 0:
            if data_type == 'signal':
                data = data_root[entry][data_path].nxsignal.nxvalue
            elif data_root[entry][data_path].nxweights:
                data = data_root[entry][data_path].nxweights.nxvalue
            else:
                signal = data_root[entry][data_path].nxsignal.nxvalue
                data = np.zeros(signal.shape, dtype=signal.dtype)
                data[np.where(signal > 0)] = 1
        else:
            if data_type == 'signal':
                data += data_root[entry][data_path].nxsignal.nxvalue
            elif data_root[entry][data_path].nxweights:
                data += data_root[entry][data_path].nxweights.nxvalue
    result = symm_function(data)
    root = nxload(tempfile.mkstemp(suffix='.nxs')[1], mode='w')
    root['data'] = result
    return data_type, root.nxfilename
Exemple #5
0
def mask_volume(data_file,
                data_path,
                mask_file,
                mask_path,
                i,
                j,
                k,
                pixel_mask,
                threshold_1=2,
                horiz_size_1=11,
                threshold_2=0.8,
                horiz_size_2=51):
    """Generate a 3D mask around Bragg peaks.

    Parameters
    ----------
    data_file : str
        File path to the raw data file
    data_path : str
        Internal path to the raw data
    mask_file : str
        File path to the mask file
    mask_path : str
        Internal path to the mask array
    i : int
        Index of first z-value of output mask
    j : int
        Index of first z-value of processed slab
    k : int
        Index of last z-value of processed slab
    pixel_mask : array-like
        2D detector mask. This has to be the same shape as the last two
        dimensions of the 3D slab. Values of 1 represent masked pixels.
    horiz_size_1 : int, optional
        Size of smaller convolution rectangles, by default 11
    threshold_1 : int, optional
        Threshold for performing the smaller convolution, by default 2
    horiz_size_2 : int, optional
        Size of larger convolution rectangles, by default 51
    threshold_2 : float, optional
        Threshold for performing the larger convolution, by default 0.8
    queue : Queue, optional
        Queue used in multiprocessing, by default None
    """

    nxsetlock(600)
    data_root = nxload(data_file, 'r')
    volume = data_root[data_path][j:k].nxvalue

    horiz_size_1, horiz_size_2 = int(horiz_size_1), int(horiz_size_2)
    sum1, sum2 = horiz_size_1**2, horiz_size_2**2
    horiz_kern_1 = np.ones((1, horiz_size_1, horiz_size_1))
    horiz_kern_2 = np.ones((1, horiz_size_2, horiz_size_2))

    diff_volume = volume[1:] - volume[:-1]

    padded_length = [0, horiz_size_1, horiz_size_1]
    vol_smoothed = local_sum_same(
        np.pad(diff_volume,
               pad_width=((0, 0), (horiz_size_1, horiz_size_1),
                          (horiz_size_1, horiz_size_1)),
               mode='edge'), horiz_kern_1, padded_length)
    vol_smoothed /= sum1

    vol_smoothed[np.abs(vol_smoothed) < threshold_1] = 0

    vol_smoothed[vol_smoothed < 0] = 1
    vol_smoothed[vol_smoothed > 0] = 1

    vol_smoothed = fill_gaps(vol_smoothed, pixel_mask)

    padded_length = [0, horiz_size_2, horiz_size_2]
    vol_smoothed = local_sum_same(
        np.pad(vol_smoothed,
               pad_width=((0, 0), (horiz_size_2, horiz_size_2),
                          (horiz_size_2, horiz_size_2)),
               mode='edge'), horiz_kern_2, padded_length)
    vol_smoothed /= sum2
    vol_smoothed[vol_smoothed < threshold_2] = 0
    vol_smoothed[vol_smoothed > threshold_2] = 1
    mask_root = nxload(mask_file, 'rw')
    mask_root[mask_path][j + 1:k - 1] = (np.maximum(vol_smoothed[0:-1],
                                                    vol_smoothed[1:]))
    return i