Exemple #1
0
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'
                    )
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.
RMSE_GROUND_TRUTH = np.fromfile(RMSE_GROUND_TRUTH_PATH, dtype=np.float32)
RMSE_MASK = np.fromfile(RMSE_MASK_PATH, dtype=np.uint8)
RMSE_FILEPATH = os.path.join(
        RMSE_OUTPUT_DIR,
        RMSE_FILE.format(PHANTOM_NAME+"-tv")
        )

# For single test run.
"""
PARAMETERS = {
        "CBSIMULATION": {
            "FRAMES": (479,),
            "FLUX": (5e7,),
            "NOISE": (True, )
            },
        "FILTERING": {
            "MODE": (0, )
            },
        "RECO": {
            "MODE": ("MLEM", ), 
NOISELEVEL_OUTPUT_DIR = "../data/simresults"
NOISELEVEL_FILEPATH = os.path.join(
        NOISELEVEL_OUTPUT_DIR,
        NOISELEVEL_FILE.format(PHANTOM_NAME+"-sirt")
        )

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.
RMSE_GROUND_TRUTH = np.fromfile(RMSE_GROUND_TRUTH_PATH, dtype=np.float32)
RMSE_MASK = np.fromfile(RMSE_MASK_PATH, dtype=np.uint8)
RMSE_FILEPATH = os.path.join(
        RMSE_OUTPUT_DIR,
        RMSE_FILE.format(PHANTOM_NAME+"-sirt")
        )

# For single test run.
"""
PARAMETERS = {
        "CBSIMULATION": {
            "FRAMES": (479,),
            "FLUX": (5e7,),
            "NOISE": (True, )
            },
        "FILTERING": {
            "MODE": (0, )
            },
        "RECO": {
            "MODE": ("MLEM", ), 
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.
RMSE_GROUND_TRUTH = np.fromfile(RMSE_GROUND_TRUTH_PATH, dtype=np.float32)
RMSE_MASK = np.fromfile(RMSE_MASK_PATH, dtype=np.uint8)
RMSE_FILEPATH = os.path.join(RMSE_OUTPUT_DIR, RMSE_FILE.format(PHANTOM_NAME + "-tv"))

# For single test run.
"""
PARAMETERS = {
        "CBSIMULATION": {
            "FRAMES": (479,),
            "FLUX": (5e7,),
            "NOISE": (True, )
            },
        "FILTERING": {
            "MODE": (0, )
            },
        "RECO": {
            "MODE": ("MLEM", ), 
            "RUNS": {
NOISELEVEL_OUTPUT_DIR = "../data/simresults"
NOISELEVEL_FILEPATH = os.path.join(
        NOISELEVEL_OUTPUT_DIR,
        NOISELEVEL_FILE.format(PHANTOM_NAME)
        )

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.
RMSE_GROUND_TRUTH = np.fromfile(RMSE_GROUND_TRUTH_PATH, dtype=np.float32)
RMSE_MASK = np.fromfile(RMSE_MASK_PATH, dtype=np.uint8)
RMSE_FILEPATH = os.path.join(
        RMSE_OUTPUT_DIR,
        RMSE_FILE.format(PHANTOM_NAME)
        )

# For single test run.
"""
PARAMETERS = {
        "CBSIMULATION": {
            "FRAMES": (479,),
            "FLUX": (5e7,),
            "NOISE": (True, )
            },
        "FILTERING": {
            "MODE": (0, )
            },
        "RECO": {
            "MODE": ("MLEM", ),