Exemplo n.º 1
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)
Exemplo n.º 2
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()
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
0
def mapfile_fsc(
    input_map_filename1,
    input_map_filename2,
    output_plot_filename=None,
    output_data_filename=None,
    nbins=20,
    resolution=None,
    axis=None,
    method="binned",
):
    """
    Compute the local FSC of the map

    Args:
        input_map_filename1 (str): The input map filename
        input_map_filename2 (str): The input map filename
        output_plot_filename (str): The output map filename
        output_data_filename (str): The output data filename
        nbins (int): The number of bins
        resolution (float): The resolution limit
        axis (tuple): The axis of the plane to compute the FSC
        method (str): Method to use (binned or averaged)

    """
    # Check the axis
    if axis is None or type(axis) == int:
        axis = [axis]
    elif axis[0] is None or type(axis[0]) == int:
        axis = [axis]

    # Open the input files
    infile1 = read(input_map_filename1)
    infile2 = read(input_map_filename2)

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

    # Reorder data2 to match data1
    data1 = reorder(data1, read_axis_order(infile1), (0, 1, 2))
    data2 = reorder(data2, read_axis_order(infile2), (0, 1, 2))

    # Loop through all axes
    results = []
    for current_axis in axis:

        # Compute the FSC
        bins, num, fsc = array_fsc(
            data1,
            data2,
            voxel_size=tuple(infile1.voxel_size[a] for a in ["z", "y", "x"]),
            nbins=nbins,
            resolution=resolution,
            axis=current_axis,
            method=method,
        )

        # Compute the FSC average
        fsc_average = float(numpy.sum(num * fsc) / numpy.sum(num))
        logger.info("FSC average: %g" % fsc_average)

        # Compute the resolution
        bin_index, bin_value, fsc_value = resolution_from_fsc(bins, fsc)
        logger.info("Estimated resolution = %f A" % (1 / sqrt(bin_value)))

        # Write the results dictionary
        results.append({
            "axis": current_axis,
            "table": {
                "bin": list(map(float, bins)),
                "fsc": list(map(float, fsc))
            },
            "resolution": {
                "bin_index": int(bin_index),
                "bin_value": float(bin_value),
                "fsc_value": float(fsc_value),
                "estimate": float(1 / sqrt(bin_value)),
            },
            "fsc_average": fsc_average,
        })

    # Write the FSC curve
    if output_plot_filename is not None:
        fig, ax = pylab.subplots(figsize=(8, 6))
        for r in results:
            bins = r["table"]["bin"]
            fsc = r["table"]["fsc"]
            axis = r["axis"]
            resolution = r["resolution"]["bin_value"]
            if axis == None:
                axis == (0, 1, 2)
            ax.plot(r["table"]["bin"],
                    r["table"]["fsc"],
                    label="axis - %s" % str(axis))
            ax.set_xlabel("Resolution (A)")
            ax.set_ylabel("FSC")
            ax.set_ylim(0, 1)
            ax.axvline(resolution, color="black")
            ax.xaxis.set_major_formatter(
                ticker.FuncFormatter(lambda x, p: "%.1f" % (1 / sqrt(x))
                                     if x > 0 else None))
        ax.legend()
        fig.savefig(output_plot_filename, dpi=300, bbox_inches="tight")
        pylab.close(fig)

    # Write a data file
    if output_data_filename is not None:
        with open(output_data_filename, "w") as outfile:
            yaml.safe_dump(results, outfile)

    # Return the results
    return results
Exemplo n.º 7
0
def mapfile_fsc(
    input_filename1,
    input_filename2,
    output_filename=None,
    output_data_filename=None,
    nbins=20,
    resolution=None,
    axis=None,
    method="binned",
):
    """
    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
        nbins (int): The number of bins
        resolution (float): The resolution limit
        axis (tuple): The axis of the plane to compute the FSC
        method (str): Method to use (binned or averaged)

    """
    # Check the axis
    if type(axis) in [int, float]:
        axis = (axis, )

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

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

    # Reorder data2 to match data1
    data1 = reorder(data1, read_axis_order(infile1), (0, 1, 2))
    data2 = reorder(data2, read_axis_order(infile2), (0, 1, 2))

    # Compute the FSC
    bins, fsc = array_fsc(
        data1,
        data2,
        voxel_size=tuple(infile1.voxel_size[a] for a in ["z", "y", "x"]),
        nbins=nbins,
        resolution=resolution,
        axis=axis,
        method=method,
    )

    # Compute the resolution
    bin_index, bin_value, fsc_value = resolution_from_fsc(bins, fsc)
    logger.info("Estimated resolution = %f A" % (1 / sqrt(bin_value)))

    # Write the FSC curve
    fig, ax = pylab.subplots(figsize=(8, 6))
    ax.plot(bins, fsc)
    ax.set_xlabel("Resolution (A)")
    ax.set_ylabel("FSC")
    ax.set_ylim(0, 1)
    ax.axvline(bin_value, color="black")
    ax.xaxis.set_major_formatter(
        ticker.FuncFormatter(lambda x, p: "%.1f" % (1 / sqrt(x))
                             if x > 0 else None))
    fig.savefig(output_filename, dpi=300, bbox_inches="tight")
    pylab.close(fig)

    # Write a data file
    if output_data_filename is not None:
        with open(output_data_filename, "w") as outfile:
            yaml.safe_dump(
                {
                    "table": {
                        "bin": list(map(float, bins)),
                        "fsc": list(map(float, fsc)),
                    },
                    "resolution": {
                        "bin_index": int(bin_index),
                        "bin_value": float(bin_value),
                        "fsc_value": float(fsc_value),
                        "estimate": float(1 / sqrt(bin_value)),
                    },
                },
                outfile,
            )