Exemple #1
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")))
         )
     with open(EDGESHARPNESS_FILEPATH, 'a+') as esf:
         esf.write(
             "{},{},{},{},{},{},{},{}\n".format(
                 filMode,
                 frms,
                 flux,
                 es,
                 esd,
                 RECO_MODES[recoMode],
                 numRuns,
                 tvGrad
                 )
             )
 if COMPUTE_NOISE_LEVEL:
     var1, var2, var3 = compute_var_from_volume(img_cropped)
     with open(NOISELEVEL_FILEPATH, 'a+') as nlf:
         nlf.write(
                 "{},{},{},{},{},{},{},{},{}\n".format(
                     filMode,
                     frms,
                     flux,
                     var1, var2, var3,
                     RECO_MODES[recoMode],
                     numRuns,
                     tvGrad
                     )
                 )
 if COMPUTE_RMSE:
     rmse = compute_rmse_from_arrays(RMSE_GROUND_TRUTH,
             img_cropped.flatten(), RMSE_MASK)
                    print("Cropping volume and converting to 16bit-float, scaling ...")
                    rawFilePath = "{}.raw".format(tmp_fpath)
                    img = np.fromfile(rawFilePath, dtype=np.float32).reshape((512, 512, 512))

                    scaling = IR_SCALING
                    if recoMode == "FDK":
                        # Scale w.r.t. region that is supposed to be 1
                        scaling = 1.0 / np.mean(img[220:225, 260:360, 150:350])
                        with open(FDK_SCALING_FILEPATH, "a+") as fsf:
                            fsf.write("#{}\n".format(fname))
                            fsf.write("{}\n".format(scaling))
                    img[:, :, :] = scaling * img[:, :, :]
                    #
                    # !!!!!!!!!!!!!!!!!!!11
                    #
                    img_cropped = img[150:400, :, 100:411]

                    if COMPUTE_EDGE_SHARPNESS:
                        es, esd = compute_es_from_volume(
                            img_cropped, EDGESHARPNESS_PREFILTER_SIGMA, EDGESHARPNESS_ROI_NAME
                        )
                        with open(EDGESHARPNESS_FILEPATH, "a+") as esf:
                            esf.write("{},{},{},{},{}\n".format(filMode, frms, flux, es, esd))
                    if COMPUTE_NOISE_LEVEL:
                        variances = compute_var_from_volume(img_cropped)
                        with open(NOISELEVEL_FILEPATH, "a+") as nlf:
                            nlf.write("{},{},{},{},{},{}\n".format(filMode, frms, flux, *variances))
                    if SAVE_VOLUME:
                        img_cropped.astype(np.float16).tofile("{}.raw".format(fpath))
                        os.remove(rawFilePath)