def run(input_dir, phantom, roi_name, suffix=".raw", 
        output_dir="../data/simresults", **kwargs):
    """
    Script to compute the ROI edge sharpness of several volumes in the given
    input directory. Pass a suffix if desired.
    Results are written to file. Manually specify parameters!
    Run this in in scripts/ if no output_dir specified.
        phantom | string, prefix to output filename
        width | int, pixel width of region selected for ES computation, DEPRECATED
        input_dir, output_dir | string 
        roi_name | name of ROI, taken from EDGESHARPNESS_ROIS
        kwargs | passed to `compute_es_from_volume`: prefilter_sigma 
                 passed to `compute_es_from_profile`: p0
    """

    #input_dir = "/media/metzner/Z/Data/Head/forbild-phantoms/simresults/1noise"
    raw_files = [f for f in os.listdir(input_dir) if f.endswith(suffix)]

    prefilter_sigma = kwargs.get("prefilter_sigma", 0)
    kwargs.pop("return_fig", None)
    from constants import EDGESHARPNESS_FILE
    output_fname = EDGESHARPNESS_FILE.format(
            phantom, 
            roi_name,
            prefilter_sigma
            )

    results = []

    for fname in tqdm(sorted(raw_files)):
        #fname = "479frms_5e+05flux_1noise_nofilter_fdk.raw"
        volume = np.fromfile(os.path.join(input_dir, fname), dtype=np.float16).reshape((250,512,311))

        edge_sharpness, edge_sharpness_delta = compute_es_from_volume(volume,
                roi_name=roi_name, return_fig=False, **kwargs)

        frms = fname[:3]
        flux = fname[8:13]
        filtermode = find_filtermode(fname)

        results.append((filtermode, frms, flux, edge_sharpness, edge_sharpness_delta))

    if results:
        outpath = os.path.join(output_dir, output_fname)
        write_header = not os.path.exists(outpath)
        with open(outpath, 'a+') as f:
            if write_header:
                f.write("#fmode,frms,flux,shrpns,uncrty\n")
            for result in results:
                f.write(','.join([str(i) for i in result]) + '\n')
EXECUTABLE = os.path.join(BUILD_DIR, "bin/./ImFusionConsole")

PHANTOM_FILE = PHANTOMS[PHANTOM_NAME]["rawfile"]
PHANTOM_FILEPATH = os.path.join(PHANTOM_INPUT_DIR, PHANTOM_FILE)
IR_SCALING = PHANTOMS[PHANTOM_NAME]["ir_scaling"]

FDK_SCALING_FILE = "fdk_postscaling.dat"
FDK_SCALING_FILEPATH = os.path.join(VOLUME_OUTPUT_DIR, FDK_SCALING_FILE)

EDGESHARPNESS_INPUT_DIR = VOLUME_OUTPUT_DIR
EDGESHARPNESS_OUTPUT_DIR = "../data/simresults"
EDGESHARPNESS_FILEPATH = os.path.join(
        EDGESHARPNESS_OUTPUT_DIR, 
        EDGESHARPNESS_FILE.format(
            PHANTOM_NAME+"-tv",
            EDGESHARPNESS_ROI_NAME,
            EDGESHARPNESS_PREFILTER_SIGMA 
            )
        )

NOISELEVEL_OUTPUT_DIR = "../data/simresults"
NOISELEVEL_FILEPATH = os.path.join(
        NOISELEVEL_OUTPUT_DIR,
        NOISELEVEL_FILE.format(PHANTOM_NAME+"-tv")
        )

RMSE_INPUT_DIR = PHANTOM_INPUT_DIR 
RMSE_OUTPUT_DIR = "../data/simresults"
RMSE_GROUND_TRUTH_PATH = os.path.join(RMSE_INPUT_DIR, RMSE_GROUND_TRUTH_FILE)
RMSE_MASK_PATH = os.path.join(RMSE_INPUT_DIR, RMSE_MASK_FILE)
# Ground truth and mask data needs to be loaded only once.
Beispiel #3
0
def run(input_dir, phantom, save_volume, 
        suffix="_0filter_fdk.raw",
        es_params=None,
        compute_noise_level=False,
        output_dir="../data/simresults",
        **bf_kwargs
        ):
    """ 
    Run postfilter routine on all files in input_dir that have the specified
    suffix. 
        phantom | str, prefix for analysis files 
        save_volume | bool, whether to save the postfiltered volume or not 
        es_params | None or tuple(roi_name, sigma)
        output_dir | Run this in scripts/ if not specified 
        bf_kwargs | sigmaISq, sigmaP, spacing, kernelScaling
    """
    raw_nofilter_files = [f for f in listdir(input_dir) if f.endswith(suffix)]

    COMPUTE_EDGESHARPNESS = es_params is not None 
    COMPUTE_ROI_VARIANCE = compute_noise_level

    if COMPUTE_EDGESHARPNESS:
        EDGESHARPNESS_FILEPATH = os.path.join(output_dir,
                EDGESHARPNESS_FILE.format(phantom, *es_params))
    ROI_VARIANCES_FILEPATH = os.path.join(output_dir, NOISELEVEL_FILE.format(phantom))

    for i,file in tqdm(enumerate(raw_nofilter_files)):
        # convert to float32 because pyopencl cannot handle float16.
        img_in = np.fromfile(
                os.path.join(input_dir, file), 
                dtype=np.float16).reshape((250,512,311)).astype(np.float32)
        img_out = _apply_bilateral_filter(img_in, queue, **bf_kwargs)

        # Analysis
        if COMPUTE_ROI_VARIANCE:
            variances = compute_var_from_volume(img_out)
            write_header = not os.path.exists(ROI_VARIANCES_FILEPATH)
            with open(ROI_VARIANCES_FILEPATH, 'a+') as rvf:
                if write_header:
                    rvf.write("#fmode,fmrs,flux,var1,var2,var3\n")
                rvf.write("{},{},{},{},{},{}\n".format(
                    2,
                    file[:3],
                    file[8:13],
                    *variances 
                    )
                )
        if COMPUTE_EDGESHARPNESS:
            es, esd = compute_es_from_volume(img_out, *es_params[::-1])
            write_header = not os.path.exists(EDGESHARPNESS_FILEPATH)
            with open(EDGESHARPNESS_FILEPATH, 'a+') as esf:
                if write_header:
                    esf.write("#fmode,frms,flux,shrpns,uncrty\n")
                esf.write("{},{},{},{},{}\n".format(
                    2, # post filtermode
                    file[:3],
                    file[8:13],
                    es,
                    esd
                    )
                )
        if save_volume:       
            # volume output directory is the input directory, i.e. on the server.
            img_out.astype(np.float16).tofile(
                    os.path.join(
                        input_dir,
                        file.replace(suffix, "_2filter_fdk.raw")))