Beispiel #1
0
def mapfile_threshold(input_filename,
                      output_filename,
                      threshold=0,
                      normalize=False,
                      zero=True):
    """
    Threshold the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        threshold (float): The threshold value
        normalize (bool): Normalize the map before thresholding
        zero (bool): Shift the data to zero

    """

    # Open the input file
    infile = read(input_filename)

    # Get data
    data = infile.data.copy()

    # Apply the threshold
    data = array_threshold(data,
                           threshold=threshold,
                           normalize=normalize,
                           zero=zero)

    # Write the output file
    write(output_filename, data, infile=infile)
Beispiel #2
0
def mapfile_cc(input_map_filename1, input_map_filename2=None, output_map_filename=None):
    """
    Compute the CC between two maps

    Args:
        input_map_filename1 (str): The input map filename
        input_map_filename2 (str): The input map filename
        output_map_filename (str): The output cc filename

    """

    # Open the input file
    infile1 = read(input_map_filename1)

    # Get the data
    data1 = infile1.data
    if input_map_filename2 is not None:
        infile2 = read(input_map_filename2)
        data2 = infile2.data
        data2 = reorder(data2, read_axis_order(infile2), read_axis_order(infile1))
    else:
        data2 = None

    # Compute the cc
    cc = array_cc(data1, data2)

    # Write the output file
    write(output_map_filename, cc.astype("float32"), infile=infile1)
def mapfile_segment(
    input_map_filename,
    output_map_filename=None,
    output_mask_filename=None,
    num_objects=1,
):
    """
    Segment the map

    Args:
        input_map_filename (str): The input map filename
        output_map_filename (str): The output map filename
        output_mask_filename (str): The output mask filename
        num_objects (int): The number of objects

    """

    # Open the input file
    infile = read(input_map_filename)

    # Get data
    data = infile.data

    # Segment the data
    mask = array_segment(data, num_objects=num_objects)

    # Write the output file
    if output_mask_filename is not None:
        write(output_mask_filename, mask, infile=infile)
    if output_map_filename is not None:
        write(output_map_filename, maptools.mask(data, mask), infile=infile)
Beispiel #4
0
def crop(
    input_filename,
    output_filename,
    roi=None,
):
    """
    Crop the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        roi (list): The region of interest

    """

    # Open the input file
    infile = read(input_filename)

    # Get the roi
    if roi is not None:
        z0, y0, x0, z1, y1, x1 = roi
    else:
        z0, y0, x0 = 0, 0, 0
        z1, y1, x1 = infile.data.shape
    assert z1 > z0
    assert y1 > y0
    assert x1 > x0

    # Get the subset of data
    logger.info("Cropping map with roi: %s" % list(roi))
    data = infile.data[z0:z1, y0:y1, x0:x1]

    # Write the output file
    write(output_filename, data, infile=infile)
Beispiel #5
0
def mapfile_rebin(input_map_filename, output_map_filename, shape=None):
    """
    Rebin the map

    Args:
        input_map_filename (str): The input map filename
        output_map_filename (str): The output map filename
        shape (tuple): The new shape of the map

    """

    # Open the input file
    infile = read(input_map_filename)

    # Get the data
    data = infile.data

    # Get the subset of data
    logger.info("Resampling map from shape %s to %s" %
                (data.shape, tuple(shape)))
    data = array_rebin(data, shape)

    # Write the output file
    outfile = write(output_map_filename, data, infile=infile)

    # Update the voxel size
    outfile.voxel_size = (
        outfile.voxel_size["z"] * infile.data.shape[0] // shape[0],
        outfile.voxel_size["y"] * infile.data.shape[1] // shape[1],
        outfile.voxel_size["x"] * infile.data.shape[2] // shape[2],
    )
Beispiel #6
0
def mapfile_reorder(input_filename, output_filename, axis_order=None):
    """
    Reorder the data axes

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        axis_order (list): The axis order

    """

    # Open the input file
    infile = read(input_filename)

    # Get the axis order
    original_order = read_axis_order(infile)
    assert tuple(sorted(axis_order)) == (0, 1, 2)

    # Reorder the axes
    data = array_reorder(infile.data, original_order, axis_order)

    # Write the output file
    outfile = write(output_filename, data, infile=infile)
    write_axis_order(outfile, axis_order)
    outfile.update_header_stats()
Beispiel #7
0
def fft(input_map_filename,
        output_map_filename,
        mode=None,
        shift=True,
        normalize=True):
    """
    Compute the FFT of the map

    Args:
        input_map_filename (str): The input map filename
        output_map_filename (str): The output map filename
        mode (str): The component to output
        shift (bool): Shift the fourier components
        normalize (bool): Normalize before computing FFT

    """

    # Open the input file
    infile = read(input_map_filename)

    # Get the subset of data
    logger.info("Computing FFT (%s)" % mode)

    # The data
    data = infile.data

    # Normalize if necessary
    if normalize:
        data = (data - numpy.mean(data)) / numpy.std(data)

    # Compute FFT
    data = {
        "real": lambda x: numpy.real(x),
        "imaginary": lambda x: numpy.imag(x),
        "amplitude": lambda x: numpy.abs(x),
        "phase": lambda x: numpy.angle(x),
        "power": lambda x: numpy.abs(x)**2,
    }[mode](numpy.fft.fftn(data).astype("complex64"))

    # Shift if necessary
    if shift:
        data = numpy.fft.fftshift(data)

    # Write the output file
    write(output_map_filename, data, infile=infile)
Beispiel #8
0
def mapfile_filter(
    input_filename,
    output_filename,
    filter_type="lowpass",
    filter_shape="gaussian",
    resolution=None,
):
    """
    Filter the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        filter_type (str): The filter type
        filter_shape (str): The filter shape
        resolution (list): The resolution

    """

    # Check the input
    assert filter_type in ["lowpass", "highpass", "bandpass", "bandstop"]
    assert filter_shape in ["square", "gaussian"]

    # Open the input file
    infile = read(input_filename)

    # Get the voxel size
    voxel_size = (
        infile.voxel_size["z"],
        infile.voxel_size["y"],
        infile.voxel_size["x"],
    )

    # Filter the data
    data = array_filter(
        infile.data,
        filter_type=filter_type,
        filter_shape=filter_shape,
        resolution=resolution,
        voxel_size=voxel_size,
    )

    # Write the output file
    write(output_filename, data, infile=infile)
Beispiel #9
0
def mapfile_rescale(
    input_filename,
    output_filename,
    mean=None,
    sdev=None,
    vmin=None,
    vmax=None,
    scale=None,
    offset=None,
):
    """
    Rescale the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        mean (float): The desired mean value
        sdev (float): The desired sdev value
        vmin (float): The desired min value
        vmax (float): The desired max value
        scale (float): The scale
        offset (float): The offset

    """

    # Open the input file
    infile = read(input_filename)

    # Get the data
    data = infile.data

    # Rescale the map
    data = array_rescale(data,
                         mean=mean,
                         sdev=sdev,
                         vmin=vmin,
                         vmax=vmax,
                         scale=scale,
                         offset=offset)

    # Write the output file
    write(output_filename, data, infile=infile)
Beispiel #10
0
def mapfile_transform(
    input_filename,
    output_filename,
    offset=None,
    rotation=(0, 0, 0),
    translation=(0, 0, 0),
    deg=False,
):
    """
    Transform the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        offset = (array): The offset to rotate about
        rotation (array): The rotation vector
        translation (array): The translation vector
        deg (bool): Is the rotation in degrees

    """

    # Open the input file
    infile = read(input_filename)

    # Get the axis order
    axis_order = read_axis_order(infile)

    # Get data
    data = infile.data

    # Do the transform
    data = array_transform(
        data,
        axis_order=axis_order,
        offset=offset,
        rotation=rotation,
        translation=translation,
        deg=deg,
    )

    # Write the output file
    write(output_filename, data, infile=infile)
Beispiel #11
0
def mapfile_fsc3d(
    input_filename1, input_filename2, output_filename=None, kernel=9, resolution=None
):
    """
    Compute the local FSC of the map

    Args:
        input_filename1 (str): The input map filename
        input_filename2 (str): The input map filename
        output_filename (str): The output map filename
        kernel (int): The kernel size
        resolution (float): The resolution limit

    """

    # Open the input files
    infile1 = read(input_filename1)
    infile2 = read(input_filename2)

    # Get the data
    data1 = infile1.data
    data2 = infile2.data

    # Reorder input arrays
    data1 = reorder(data1, read_axis_order(infile1), (0, 1, 2))
    data2 = reorder(data2, read_axis_order(infile2), (0, 1, 2))

    # Compute the local FSC
    fsc = fsc3d(
        data1,
        data2,
        kernel=kernel,
        resolution=resolution,
        voxel_size=infile1.voxel_size,
    )

    # Reorder output array
    fsc = reorder(fsc, (0, 1, 2), read_axis_order(infile1))

    # Write the output file
    write(output_filename, fsc.astype("float32"), infile=infile1)
Beispiel #12
0
def mapfile_segment(input_filename, output_filename, num_objects=1):
    """
    Segment the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        num_objects (int): The number of objects

    """

    # Open the input file
    infile = read(input_filename)

    # Get data
    data = infile.data.copy()

    # Apply the segment
    data = array_segment(data, num_objects=num_objects)

    # Write the output file
    write(output_filename, data, infile=infile)
Beispiel #13
0
def mapfile_mask(
    input_filename,
    output_filename,
    mask_filename=None,
    fourier_space=False,
    shift=False,
):
    """
    Mask the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        mask_filename (str): The mask filename
        space (str): Apply in real space or fourier space
        shift (bool): Shift the mask

    """

    # Open the input file
    infile = read(input_filename)

    # Open the mask file
    maskfile = read(mask_filename)

    # Apply the mask
    data = infile.data
    mask = maskfile.data

    # Reorder the maskfile axes to match the data
    mask = reorder(mask, read_axis_order(maskfile), read_axis_order(infile))

    # Apply the mask
    data = array_mask(data, mask, fourier_space=fourier_space, shift=shift)

    # Write the output file
    write(output_filename, data.astype("float32"), infile=infile)
Beispiel #14
0
def rotate(input_filename, output_filename, axes=(0, 1), num=1):
    """
    Rotate the map

    Args:
        input_filename (str): The input map filename
        output_filename (str): The output map filename
        axes (tuple): The axis to rotate around
        num (int): The number of times to rotate by 90 degrees

    """

    # Open the input file
    infile = read(input_filename)

    # Get data
    data = infile.data.copy()

    # Apply the threshold
    logger.info("Rotating %d times around axes %s" % (num, axes))
    data = numpy.rot90(data, k=num, axes=axes)

    # Write the output file
    write(output_filename, data, infile=infile)
def mapfile_dilate(input_map_filename,
                   output_map_filename,
                   kernel=3,
                   num_iter=1):
    """
    Dilate the map

    Args:
        input_map_filename (str): The input map filename
        output_map_filename (str): The output map filename
        kernel (tuple): The kernel size
        num_iter (int): The number of iterations

    """

    # Open the input file
    infile = read(input_map_filename)

    # Get the subset of data
    logger.info("Dilating map")
    data = array_dilate(infile.data, kernel=kernel, num_iter=num_iter)

    # Write the output file
    write(output_map_filename, data.astype("uint8"), infile=infile)