def run(input_dir, phantom, suffix=".raw", output_dir="../data/simresults"): """ Compute the variance in three ROIs of several volumes in the given input directory. Pass a suffix if desired. Run this in in scripts/ if no output_dir specified. phantom | string, prefix to output filename """ from constants import NOISELEVEL_FILE OUTPUT_FNAME = NOISELEVEL_FILE.format(phantom) raw_files = [f for f in os.listdir(input_dir) if f.endswith(suffix)] d = OrderedDict() for f in tqdm(sorted(raw_files)): img = np.fromfile( os.path.join(input_dir, f), dtype=np.float16).reshape((250, 512, 311)) variances = compute_var_from_volume(img) # Fetch frame and flux info from filename. filtermode = find_filtermode(f) frms = f[:3] flux = f[8:13] d[f] = (filtermode, frms, flux) + variances if d: outpath = os.path.join(output_dir, OUTPUT_FNAME) # output file will be sorted because first loop runs over sorted filenames. write_header = not os.path.exists(outpath) with open(outpath, 'a+') as f: if write_header: f.write("#fmode,fmrs,flux,var1,var2,var3\n") for vtuple in d.values(): # CSV style f.write("{},{},{},{},{},{}\n".format(*vtuple))
def run(input_dir, phantom, gt_path, mask_path=None, dtypes=(np.float32, np.float16, np.uint8), suffix=".raw", output_dir="../data/simresults" ): """ Compute RMSE on a set of volumes in the given `input_dir`. Only files ending with `suffix` are considered. Specify `gt_path` and `mask_path` to create ground truth and mask arrays prior to looping over files. `dtypes` order: ground truth, experiment, mask """ import os files = [f for f in os.listdir(input_dir) if f.endswith(suffix)] import re tv_grad_pattern = re.compile(".*-(\d+\.\d+)tv") from utils import find_filtermode, find_recomode results = [] ground_truth = np.fromfile(gt_path, dtype=dtypes[0]) mask = None if mask_path is None else np.fromfile(mask_path, dtype=dtypes[2]) for f in files: rmse = compute_rmse_from_exp_path( os.path.join(input_dir, f), ground_truth, mask, dtype=dtypes[1] ) filMode = find_filtermode(f) frms = f[:3] flux = f[8:13] tvGrad = tv_grad_pattern.search(f).group(1) recoMode = find_recomode(f) results.append(( int(filMode), int(frms), int(flux), float(tvGrad), float(rmse), int(recoMode), # still hardcoded... 3 )) from constants import RMSE_FILE rmse_filepath = os.path.join(output_dir, RMSE_FILE.format(phantom)) write_header = not os.path.exists(rmse_filepath) with open(rmse_filepath, 'a+') as rf: if write_header: rf.write("#fmode,frms,flux,tvGrad,rmse,recoMode,numIter\n") for result in results: rf.write( ','.join([str(r) for r in result]) + '\n' )
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')