Exemplo n.º 1
0
    def test_t1_calculation_shape(self):
        tr = 1
        alphas = np.array(range(1, 6))
        dwi = np.zeros((1, 2, 3, 5))
        t1 = scalar_analysis.calculate_t1(dwi, alphas, tr)

        expected = np.zeros((1, 2, 3))
        npt.assert_array_equal(expected, t1)
Exemplo n.º 2
0
    def test_t1_calculation(self):
        tr = 3.7220  # repetition time taken from sample t1 weighted dicom files

        # test with 1.1 log weight
        alphas = np.array([
            np.deg2rad(flip_angle_degrees)
            for flip_angle_degrees in [43, 22, 15, 8, 5]
        ])
        dwi = np.array([[[[155, 290, 405, 600, 800]]]])

        t1 = scalar_analysis.calculate_t1(dwi, alphas, tr)
        npt.assert_approx_equal(t1[0][0][0], 1572.275, significant=6)
Exemplo n.º 3
0
    def test_t1_calculation_slice(self):
        demo_file_dir = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            "test_data/breast_single_t1_slice/")
        dcm_dir = dicom_util.read_dicomdir(demo_file_dir)
        t1_array = scalar_analysis.calculate_t1(dcm_dir["dwi"],
                                                dcm_dir["alphas"],
                                                dcm_dir["rep_time_seconds"])

        expected_t1 = np.fromfile(os.path.join(demo_file_dir,
                                               't1_test_slice.txt'),
                                  sep=' ').reshape(1, 256, 256)
        npt.assert_allclose(t1_array, expected_t1, rtol=1e-07, atol=1e-07)
Exemplo n.º 4
0
def baseline_format(expected, dir_output, voi_output, phantom_definition):
    baseline = defaultdict(lambda: defaultdict)  # 2 keys deep
    center_distances = []
    volume_differences = []
    scalar_median_differences = []
    scalar_mean_differences = []
    total_clamped_pixels = 0

    # Calculate scalar maps and VOI stats
    scalar_type = dir_output["scalar_type"]
    label_map = voi_output["label_map"]
    if scalar_type == "ADC":
        scalar_map = scalar_analysis.calculate_adc(dir_output["dwi"],
                                                   dir_output["bvalues"]) * 1e6
        voi_stats = scalar_analysis.voi_stats(
            label_map, scalar_map, dir_output["image_coordinate_system"],
            phantom_definition)
    elif scalar_type == "T1":
        scalar_map = scalar_analysis.calculate_t1(
            dir_output["dwi"],
            dir_output["alphas"],
            dir_output["rep_time_seconds"],
            use_pool=not WINDOWS,
            clamp=CLAMP,
            threshold=5)
        voi_stats = scalar_analysis.voi_stats(
            label_map,
            scalar_map,
            dir_output["image_coordinate_system"],
            phantom_definition,
            clamp=CLAMP)
    else:
        # Shouldn't ever get here
        return baseline

    # Analyze stats for each VOI
    vois = voi_output["found_vois"]
    for voi in vois:
        expected_voi_dict = expected[voi]
        baseline[voi] = {}

        # Compare center coordinates and variation in center
        if "center" in vois[voi]:
            center_info = {
                "center_left_cm": vois[voi]["center"][0],
                "center_posterior_cm": vois[voi]["center"][1],
                "center_superior_cm": vois[voi]["center"][2],
                "std_dev_of_coronal_center": {
                    "coronal x": vois[voi]["coronal_center_std_dev"][0],
                    "coronal y": vois[voi]["coronal_center_std_dev"][1]
                }
            }
            baseline[voi].update(center_info)

            # Compare found center to expected (if known)
            if "center_left_cm" in expected_voi_dict:
                center_distance_cm = np.linalg.norm(
                    np.array([
                        expected_voi_dict["center_left_cm"],
                        expected_voi_dict["center_posterior_cm"],
                        expected_voi_dict["center_superior_cm"]
                    ]) - vois[voi]["center"])
                center_distances.append(center_distance_cm)
                baseline[voi].update(
                    {"center_distance_cm": center_distance_cm})

        # Compare scalar stats to expected values for this dataset (ADC/T1)
        scalar_type = scalar_type.lower()
        if voi in voi_stats:
            median_diff = expected_voi_dict["{}_median".format(
                scalar_type)] - voi_stats[voi]["median"]
            mean_diff = expected_voi_dict["{}_mean".format(
                scalar_type)] - voi_stats[voi]["mean"]
            scalar_median_differences.append(np.abs(median_diff))
            scalar_mean_differences.append(np.abs(mean_diff))
            total_clamped_pixels += voi_stats[voi]["clamped_pixels"]
            volume_difference_percent = (
                voi_stats[voi]["volume"] - expected_voi_dict["volume_cm3"]
            ) / expected_voi_dict["volume_cm3"] * 100
            volume_differences.append(volume_difference_percent)

            baseline[voi].update({
                "{}_median".format(scalar_type):
                voi_stats[voi]["median"],
                "{}_median_difference".format(scalar_type):
                median_diff,
                "{}_mean".format(scalar_type):
                voi_stats[voi]["mean"],
                "{}_mean_difference".format(scalar_type):
                mean_diff,
                "{}_max".format(scalar_type):
                voi_stats[voi]["max"],
                "{}_min".format(scalar_type):
                voi_stats[voi]["min"],
                "{}_std_dev".format(scalar_type):
                voi_stats[voi]["std_dev"],
                "number_clamped_pixels":
                voi_stats[voi]["clamped_pixels"],
                "volume_cm3":
                voi_stats[voi]["volume"],
                "volume_difference_percent":
                volume_difference_percent
            })

    # Gather information about entire dataset
    missing_voi_count = len(expected.keys()) - len(voi_stats.keys())
    baseline.update({
        "center_max_distance_cm":
        max(center_distances) if center_distances else 0.0,
        "center_average_distance_cm":
        np.mean(center_distances) if center_distances else 0.0,
        "volume_max_difference_percent":
        max(volume_differences) if volume_differences else 0.0,
        "volume_average_difference_percent":
        np.mean(volume_differences) if volume_differences else 0.0,
        "missing_voi_count":
        missing_voi_count,
        "{}_median_max_difference".format(scalar_type):
        max(scalar_median_differences) if scalar_median_differences else 0.0,
        "{}_median_average_difference".format(scalar_type):
        np.mean(scalar_median_differences)
        if scalar_median_differences else 0.0,
        "{}_mean_max_difference".format(scalar_type):
        max(scalar_mean_differences) if scalar_mean_differences else 0.0,
        "{}_mean_average_difference".format(scalar_type):
        np.mean(scalar_mean_differences) if scalar_mean_differences else 0.0,
        "total_clamped_pixels":
        total_clamped_pixels,
        "error_in_voi_finding_for_dataset":
        voi_output["total_error"]
    })

    return baseline
Exemplo n.º 5
0
        "../phantom_data/4493056_4493_T1_NIST_PHANTOM_NoPE/E7217/7")
    #demo_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "test_data/voi/adc/breast_131_spheres_adc_20190124_19")
    dcm_dir = dicom_util.read_dicomdir(demo_dir)

    cs = dcm_dir["image_coordinate_system"]
    logging.info("generating vois")
    coronal_dwi = np.transpose(deepcopy(dcm_dir["dwi"]), (3, 1, 0, 2))

    voi_list = voi_analysis.get_vois(dcm_dir["dwi"], cs,
                                     phantom_definitions.BREAST_131_CYLS_T1)

    #for v in voi_list["found_vois"]:
    #print("voi {} has height of {} cm".format(v, voi_list["found_vois"][v]["height"]))

    scalar_map = scalar_analysis.calculate_t1(dcm_dir["dwi"],
                                              dcm_dir["alphas"],
                                              dcm_dir["rep_time_seconds"],
                                              use_pool=not WINDOWS,
                                              clamp=CLAMP,
                                              threshold=5)
    #scalar_map = scalar_analysis.calculate_adc(dcm_dir["dwi"], dcm_dir["bvalues"]) * 1e6
    stats = scalar_analysis.voi_stats(voi_list["label_map"],
                                      scalar_map,
                                      cs,
                                      phantom_definitions.BREAST_131_CYLS_T1,
                                      clamp=CLAMP)

    for v in stats:
        #print("voi: {}, num clamped pixels: {}".format(v, stats[v]["clamped_pixels"]))
        #print("voi: {}, mean scalar value: {}, expected value: {}".format(v, stats[v]["mean"], phantom_definitions.NEW_ADC["vois"][v]["expected_value"]))
        print(stats[v])