Exemplo n.º 1
0
    def test_resample_warped_live02(self):
        # corresponds to resampling_test02 for C++
        u_vectors = np.array(
            [[0.0, 0.0, 0.0], [-.5, 0.0, 0.0], [1.5, 0.5, 0.0]],
            dtype=np.float32)

        v_vectors = np.array(
            [[-1.0, 0.0, 0.0], [0.0, 0.0, 0.5], [-1.5, 0.5, -0.5]],
            dtype=np.float32)

        warp_field_template = np.stack((u_vectors, v_vectors), axis=2)
        warp_field = warp_field_template.copy()
        gradient_field_template = warp_field_template * 10
        gradient_field = gradient_field_template.copy()
        warped_live_template = np.array(
            [[1.0, 1.0, 1.0], [0.5, 1.0, 1.0], [0.5, 0.5, -1.0]],
            dtype=np.float32)

        warped_live_field = warped_live_template.copy()
        canonical_field = np.array(
            [[-1.0, 0., 0.0], [0.0, 0., 1.0], [0.0, 0., 1.0]],
            dtype=np.float32)
        expected_new_warped_live_field = np.array(
            [[1.0, 1.0, 1.0], [0.5, 1.0, 1.0], [1.0, 0.125, -1.0]],
            dtype=np.float32)
        expected_u_vectors = np.array(
            [[0.0, 0.0, 0.0], [-0.5, 0.0, 0.0], [0.0, 0.5, 0.0]],
            dtype=np.float32)
        expected_v_vectors = np.array(
            [[-1.0, 0.0, 0.0], [0.0, 0.0, 0.5], [0.0, 0.5, -0.5]],
            dtype=np.float32)
        warped_live_field = ipt.resample_warped_live(canonical_field,
                                                     warped_live_field,
                                                     warp_field,
                                                     gradient_field,
                                                     band_union_only=True,
                                                     known_values_only=False,
                                                     substitute_original=True)
        out_u_vectors = warp_field[:, :, 0]
        out_v_vectors = warp_field[:, :, 1]

        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))
        self.assertTrue(np.allclose(out_u_vectors, expected_u_vectors))
        self.assertTrue(np.allclose(out_v_vectors, expected_v_vectors))

        # re-prep data
        warped_live_field = warped_live_template.copy()

        warped_live_field, (out_u_vectors, out_v_vectors) = \
            cpp_extension.resample(warped_live_field, canonical_field, u_vectors, v_vectors,
                                   band_union_only=True, known_values_only=False, substitute_original=True)

        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))
        self.assertTrue(np.allclose(out_u_vectors, expected_u_vectors))
        self.assertTrue(np.allclose(out_v_vectors, expected_v_vectors))
Exemplo n.º 2
0
    def test_resample_warped_live04(self):
        u_vectors = np.array([[-0., -0., 0.03732542, 0.01575381],
                              [-0., 0.04549519, 0.01572882, 0.00634488],
                              [-0., 0.07203466, 0.01575179, 0.00622413],
                              [-0., 0.05771814, 0.01468342, 0.01397111]],
                             dtype=np.float32)
        v_vectors = np.array([[-0., -0., 0.02127664, 0.01985903],
                              [-0., 0.04313552, 0.02502393, 0.02139519],
                              [-0., 0.02682102, 0.0205336, 0.02577237],
                              [-0., 0.02112256, 0.01908935, 0.02855439]],
                             dtype=np.float32)

        warp_field_template = np.stack((u_vectors, v_vectors), axis=2)
        warp_field = warp_field_template.copy()
        gradient_field_template = warp_field_template * 10
        gradient_field = gradient_field_template.copy()
        warped_live_template = np.array(
            [[1., 1., 0.49999955, 0.42499956],
             [1., 0.44999936, 0.34999937, 0.32499936],
             [1., 0.35000065, 0.25000066, 0.22500065],
             [1., 0.20000044, 0.15000044, 0.07500044]],
            dtype=np.float32)
        warped_live_field = warped_live_template.copy()
        canonical_field = np.array(
            [[1.0000000e+00, 1.0000000e+00, 3.7499955e-01, 2.4999955e-01],
             [1.0000000e+00, 3.2499936e-01, 1.9999936e-01, 1.4999935e-01],
             [1.0000000e+00, 1.7500064e-01, 1.0000064e-01, 5.0000645e-02],
             [1.0000000e+00, 7.5000443e-02, 4.4107438e-07, -9.9999562e-02]],
            dtype=np.float32)
        expected_new_warped_live_field = np.array(
            [[1., 1., 0.49404836, 0.4321034],
             [1., 0.44113636, 0.34710377, 0.32715625],
             [1., 0.3388706, 0.24753733, 0.22598255],
             [1., 0.21407352, 0.16514614, 0.11396749]],
            dtype=np.float32)

        warped_live_field = ipt.resample_warped_live(canonical_field,
                                                     warped_live_field,
                                                     warp_field,
                                                     gradient_field,
                                                     band_union_only=False,
                                                     known_values_only=False,
                                                     substitute_original=False)
        out_u_vectors = warp_field[:, :, 0]
        out_v_vectors = warp_field[:, :, 1]
        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))

        # re-prep data
        warped_live_field = warped_live_template.copy()

        warped_live_field, (out_u_vectors,
                            out_v_vectors) = cpp_extension.resample(
                                warped_live_field, canonical_field, u_vectors,
                                v_vectors)
        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))
Exemplo n.º 3
0
    def test_resample_warped_live05(self):
        u_vectors = np.array([[-0., -0., 0.0334751, 0.01388371],
                              [-0., 0.04041886, 0.0149368, 0.00573045],
                              [-0., 0.06464156, 0.01506416, 0.00579486],
                              [-0., 0.06037777, 0.0144603, 0.01164452]],
                             dtype=np.float32)
        v_vectors = np.array([[-0., -0., 0.019718, 0.02146172],
                              [-0., 0.03823357, 0.02406227, 0.02212186],
                              [-0., 0.02261183, 0.01864575, 0.02234527],
                              [-0., 0.01906347, 0.01756042, 0.02574961]],
                             dtype=np.float32)

        warp_field_template = np.stack((u_vectors, v_vectors), axis=2)
        warp_field = warp_field_template.copy()
        gradient_field_template = warp_field_template * 10
        gradient_field = gradient_field_template.copy()
        warped_live_template = np.array(
            [[1., 1., 0.49404836, 0.4321034],
             [1., 0.44113636, 0.34710377, 0.32715625],
             [1., 0.3388706, 0.24753733, 0.22598255],
             [1., 0.21407352, 0.16514614, 0.11396749]],
            dtype=np.float32)
        warped_live_field = warped_live_template.copy()
        canonical_field = np.array(
            [[1.0000000e+00, 1.0000000e+00, 3.7499955e-01, 2.4999955e-01],
             [1.0000000e+00, 3.2499936e-01, 1.9999936e-01, 1.4999935e-01],
             [1.0000000e+00, 1.7500064e-01, 1.0000064e-01, 5.0000645e-02],
             [1.0000000e+00, 7.5000443e-02, 4.4107438e-07, -9.9999562e-02]],
            dtype=np.float32)
        expected_new_warped_live_field = np.array(
            [[1., 1., 0.48910502, 0.43776682],
             [1., 0.43342987, 0.34440944, 0.3287866],
             [1., 0.33020678, 0.24566805, 0.22797936],
             [1., 0.2261582, 0.17907946, 0.14683424]],
            dtype=np.float32)

        warped_live_field = ipt.resample_warped_live(canonical_field,
                                                     warped_live_field,
                                                     warp_field,
                                                     gradient_field,
                                                     band_union_only=False,
                                                     known_values_only=False,
                                                     substitute_original=False)
        out_u_vectors = warp_field[:, :, 0]
        out_v_vectors = warp_field[:, :, 1]
        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))

        # re-prep data
        warped_live_field = warped_live_template.copy()

        warped_live_field, (out_u_vectors, out_v_vectors) = \
            cpp_extension.resample(warped_live_field, canonical_field, u_vectors, v_vectors)
        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))
Exemplo n.º 4
0
    def __optimization_iteration_direct(self,
                                        warped_live_field,
                                        canonical_field,
                                        warp_field,
                                        data_component_field=None,
                                        smoothing_component_field=None,
                                        level_set_component_field=None,
                                        band_union_only=True):

        self.total_data_energy = 0.
        self.total_smoothing_energy = 0.
        self.total_level_set_energy = 0.

        field_size = warp_field.shape[0]

        live_gradient_y, live_gradient_x = np.gradient(warped_live_field)

        for y in range(0, field_size):
            for x in range(0, field_size):
                if focus_coordinates_match(x, y):
                    print("Point: ", x, ",", y, sep='', end='')

                gradient = 0.0

                live_sdf = warped_live_field[y, x]

                live_is_truncated = value_outside_narrow_band(live_sdf)

                if band_union_only and voxel_is_outside_narrow_band_union(
                        warped_live_field, canonical_field, x, y):
                    continue

                data_gradient, local_data_energy = \
                    dt.compute_local_data_term(warped_live_field, canonical_field, x, y, live_gradient_x,
                                               live_gradient_y, method=self.data_term_method)
                scaled_data_gradient = self.data_term_weight * data_gradient
                self.total_data_energy += self.data_term_weight * local_data_energy
                gradient += scaled_data_gradient
                if focus_coordinates_match(x, y):
                    print(" Data grad: ",
                          BOLD_GREEN,
                          -data_gradient,
                          RESET,
                          sep='',
                          end='')
                if data_component_field is not None:
                    data_component_field[y, x] = data_gradient
                if self.level_set_term_enabled and not live_is_truncated:
                    level_set_gradient, local_level_set_energy = \
                        level_set_term_at_location(warped_live_field, x, y)
                    scaled_level_set_gradient = self.level_set_term_weight * level_set_gradient
                    self.total_level_set_energy += self.level_set_term_weight * local_level_set_energy
                    gradient += scaled_level_set_gradient
                    if level_set_component_field is not None:
                        level_set_component_field[y, x] = level_set_gradient
                    if focus_coordinates_match(x, y):
                        print(" Level-set grad (scaled): ",
                              BOLD_GREEN,
                              -scaled_level_set_gradient,
                              RESET,
                              sep='',
                              end='')

                smoothing_gradient, local_smoothing_energy = \
                    st.compute_local_smoothing_term_gradient(warp_field, x, y, method=self.smoothing_term_method,
                                                             copy_if_zero=False,
                                                             isomorphic_enforcement_factor=
                                                             self.isomorphic_enforcement_factor)
                scaled_smoothing_gradient = self.smoothing_term_weight * smoothing_gradient
                self.total_smoothing_energy += self.smoothing_term_weight * local_smoothing_energy
                gradient += scaled_smoothing_gradient
                if smoothing_component_field is not None:
                    smoothing_component_field[y, x] = smoothing_gradient
                if focus_coordinates_match(x, y):
                    print(" Smoothing grad (scaled): ",
                          BOLD_GREEN,
                          -scaled_smoothing_gradient,
                          RESET,
                          sep='',
                          end='')

                self.gradient_field[y, x] = gradient

        if self.sobolev_smoothing_enabled:
            convolve_with_kernel_preserve_zeros(self.gradient_field,
                                                self.sobolev_kernel, True)

        max_warp = 0.0
        max_warp_location = -1

        # update the warp field based on the gradient
        for y in range(0, field_size):
            for x in range(0, field_size):
                warp_field[y, x] = -self.gradient_field[
                    y, x] * self.gradient_descent_rate
                if focus_coordinates_match(x, y):
                    print(" Warp: ",
                          BOLD_GREEN,
                          warp_field[y, x],
                          RESET,
                          " Warp length: ",
                          BOLD_GREEN,
                          np.linalg.norm(warp_field[y, x]),
                          RESET,
                          sep='')
                warp_length = np.linalg.norm(warp_field[y, x])
                if warp_length > max_warp:
                    max_warp = warp_length
                    max_warp_location = Point2d(x, y)
                if (x, y) in self.focus_neighborhood_log:
                    log = self.focus_neighborhood_log[(x, y)]
                    log.warp_magnitudes.append(warp_length)
                    log.sdf_values.append(warped_live_field[y, x])

        new_warped_live_field = resample_warped_live(canonical_field,
                                                     warped_live_field,
                                                     warp_field,
                                                     self.gradient_field,
                                                     band_union_only=False,
                                                     known_values_only=False,
                                                     substitute_original=False)
        np.copyto(warped_live_field, new_warped_live_field)

        return max_warp, max_warp_location
Exemplo n.º 5
0
    def test_resample_warped_live03(self):
        u_vectors = np.array(
            [[0., 0., 0., 0.],
             [-1.95794283, 1.59443461, -0.80321548, -0.41660499],
             [0.99072356, 0.33884474, -1.16845247, 1.46578561],
             [0., 0., 0., 0.]],
            dtype=np.float32)
        v_vectors = np.array(
            [[0., 0., 0., 0.],
             [-0.89691237, -0.45941665, 1.36006788, -1.05888156],
             [-0.47305308, -1.27971876, -0.38927596, -1.83300769],
             [0., 0., 0., 0.]],
            dtype=np.float32)

        warp_field_template = np.stack((u_vectors, v_vectors), axis=2)
        warp_field = warp_field_template.copy()
        gradient_field_template = warp_field_template * 10
        gradient_field = gradient_field_template.copy()
        warped_live_template = np.array(
            [[0., 0., 0., 0.], [
                0.22921804, 0.04988099, -0.45673641, 0.05888156
            ], [-0.46242488, 0., -0.44900494, 0.92592539], [0., 0., 0., 0.]],
            dtype=np.float32)
        warped_live_field = warped_live_template.copy()
        canonical_field = np.array(
            [[0., 0., 0., 0.], [0.18455172, 0., 0., 0.45854051],
             [0.4508187, 0.8420034, 0.41780566, 0.34275345],
             [0.90931962, 0.20844429, 0.27169698, 0.57087366]],
            dtype=np.float32)
        expected_new_warped_live_field = np.array(
            [[0., 0., 0., 0.], [1., -0.08121467, -0.05654262, 0.05888156],
             [0.02212291, -0.08771848, -0.01639593, 1.], [0., 0., 0., 0.]],
            dtype=np.float32)
        expected_u_vectors = np.array(
            [[0., 0., 0., 0.], [0., 1.59443461, -0.80321548, -0.41660499],
             [0.99072356, 0.33884474, -1.16845247, 0.], [0., 0., 0., 0.]],
            dtype=np.float32)
        expected_v_vectors = np.array(
            [[0., 0., 0., 0.], [0., -0.45941665, 1.36006788, -1.05888156],
             [-0.47305308, -1.27971876, -0.38927596, 0.], [0., 0., 0., 0.]],
            dtype=np.float32)
        warped_live_field = ipt.resample_warped_live(canonical_field,
                                                     warped_live_field,
                                                     warp_field,
                                                     gradient_field,
                                                     band_union_only=False,
                                                     known_values_only=False,
                                                     substitute_original=False)
        out_u_vectors = warp_field[:, :, 0]
        out_v_vectors = warp_field[:, :, 1]
        self.assertTrue(np.allclose(out_u_vectors, expected_u_vectors))
        self.assertTrue(np.allclose(out_v_vectors, expected_v_vectors))
        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))

        # re-prep data
        warped_live_field = warped_live_template.copy()

        warped_live_field, (out_u_vectors,
                            out_v_vectors) = cpp_extension.resample(
                                warped_live_field, canonical_field, u_vectors,
                                v_vectors)

        self.assertTrue(
            np.allclose(warped_live_field, expected_new_warped_live_field))
        self.assertTrue(np.allclose(out_u_vectors, expected_u_vectors))
        self.assertTrue(np.allclose(out_v_vectors, expected_v_vectors))