Пример #1
0
class TestsEphysAlignment(unittest.TestCase):
    def setUp(self):
        self.ephysalign = EphysAlignment(xyz_picks)
        self.feature = self.ephysalign.feature_init
        self.track = self.ephysalign.track_init

    def test_no_scaling(self):
        xyz_channels = self.ephysalign.get_channel_locations(self.feature,
                                                             self.track,
                                                             depths=depths)
        coords = np.r_[[xyz_picks[-1, :]], [xyz_channels[0, :]]]
        dist_to_fist_electrode = np.around(_cumulative_distance(coords)[-1], 5)
        assert np.isclose(dist_to_fist_electrode, (TIP_SIZE_UM + 20) / 1e6)

    def test_offset(self):
        feature_val = 500 / 1e6
        track_val = 1000 / 1e6

        tracks = np.sort(np.r_[self.track[[0, -1]], track_val])
        track_new = self.ephysalign.feature2track(tracks, self.feature,
                                                  self.track)
        feature_new = np.sort(np.r_[self.feature[[0, -1]], feature_val])
        track_new = self.ephysalign.adjust_extremes_uniform(
            feature_new, track_new)

        xyz_channels = self.ephysalign.get_channel_locations(feature_new,
                                                             track_new,
                                                             depths=depths)
        coords = np.r_[[xyz_picks[-1, :]], [xyz_channels[0, :]]]
        dist_to_fist_electrode = np.around(_cumulative_distance(coords)[-1], 5)
        assert np.isclose(dist_to_fist_electrode,
                          ((TIP_SIZE_UM + 20) / 1e6 + feature_val))
        track_val = self.ephysalign.track2feature(track_val, feature_new,
                                                  track_new)
        self.assertTrue(np.all(np.isclose(track_val, feature_val)))

        region_new, _ = self.ephysalign.scale_histology_regions(
            feature_new, track_new)
        _, scale_factor = self.ephysalign.get_scale_factor(region_new)
        self.assertTrue(np.all(np.isclose(scale_factor, 1)))

    def test_uniform_scaling(self):
        feature_val = np.array([500, 700, 2000]) / 1e6
        track_val = np.array([1000, 1300, 2700]) / 1e6

        tracks = np.sort(np.r_[self.track[[0, -1]], track_val])
        track_new = self.ephysalign.feature2track(tracks, self.feature,
                                                  self.track)
        feature_new = np.sort(np.r_[self.feature[[0, -1]], feature_val])
        track_new = self.ephysalign.adjust_extremes_uniform(
            feature_new, track_new)

        region_new, _ = self.ephysalign.scale_histology_regions(
            feature_new, track_new)
        _, scale_factor = self.ephysalign.get_scale_factor(region_new)
        self.assertTrue(np.isclose(scale_factor[0], 1))
        self.assertTrue(np.isclose(scale_factor[-1], 1))

    def test_linear_scaling(self):
        feature_val = np.array([500, 700, 2000]) / 1e6
        track_val = np.array([1000, 1300, 2700]) / 1e6

        tracks = np.sort(np.r_[self.track[[0, -1]], track_val])
        track_new = self.ephysalign.feature2track(tracks, self.feature,
                                                  self.track)
        feature_new = np.sort(np.r_[self.feature[[0, -1]], feature_val])

        fit = np.polyfit(feature_new[1:-1], track_new[1:-1], 1)
        linear_fit = np.around(1 / fit[0], 3)

        feature_new, track_new = self.ephysalign.adjust_extremes_linear(
            feature_new, track_new, extend_feature=1)

        region_new, _ = self.ephysalign.scale_histology_regions(
            feature_new, track_new)
        _, scale_factor = self.ephysalign.get_scale_factor(region_new)

        self.assertTrue(np.isclose(np.around(scale_factor[0], 3), linear_fit))
        self.assertTrue(np.isclose(np.around(scale_factor[-1], 3), linear_fit))
Пример #2
0
        for reg, col in zip(region_scaled, scale_factor):
            height = np.abs(reg[1] - reg[0])
            color = np.array(mapper.to_rgba(col, bytes=True)) / 255
            ax.bar(x=1.1, height=height, width=0.2, color=color, bottom=reg[0], edgecolor='w')

        sec_ax = ax.secondary_yaxis('right')
        sec_ax.set_yticks(np.mean(region, axis=1))
        sec_ax.set_yticklabels(np.around(scale, 2))
        sec_ax.tick_params(axis="y", direction="in")
        sec_ax.set_ylim([20, 3840])

    fig, ax = plt.subplots(1, len(alignments) + 1, figsize=(15, 15))
    ephysalign = EphysAlignment(xyz_picks, depths, brain_atlas=brain_atlas)
    feature, track, _ = ephysalign.get_track_and_feature()
    channels_orig = ephysalign.get_channel_locations(feature, track)
    region, region_label = ephysalign.scale_histology_regions(feature, track)
    region_scaled, scale_factor = ephysalign.get_scale_factor(region)
    region_colour = ephysalign.region_colour

    norm = matplotlib.colors.Normalize(vmin=0.5, vmax=1.5, clip=True)
    mapper = matplotlib.cm.ScalarMappable(norm=norm, cmap=matplotlib.cm.seismic)

    ax_i = fig.axes[0]
    plot_regions(region, region_label, region_colour, ax_i)
    plot_scaling(region_scaled, scale_factor, mapper, ax_i)
    ax_i.set_title('Original')

    for iK, key in enumerate(alignments):
        # Location of reference lines used for alignmnet
        feature = np.array(alignments[key][0])
        track = np.array(alignments[key][1])
Пример #3
0
                              name=traj['probe_name'])
    xyz_picks = np.array(insertion[0]['json']['xyz_picks']) / 1e6

    session_info = traj['session']['start_time'][:10] + '_' + traj['probe_name']

    for iK, key in enumerate(alignments):
        # Location of reference lines used for alignmnet
        feature = np.array(alignments[key][0])
        track = np.array(alignments[key][1])
        user = key[:19]
        # Instantiate EphysAlignment object
        ephysalign = EphysAlignment(xyz_picks,
                                    depths,
                                    track_prev=track,
                                    feature_prev=feature)
        region_scaled, _ = ephysalign.scale_histology_regions(feature, track)
        _, scale_factor = ephysalign.get_scale_factor(region_scaled)

        if np.all(np.round(np.diff(scale_factor), 3) == 0):
            # Case where there is no scaling but just an offset
            scale_factor = np.array([1])
            avg_sf = 1
        else:
            if feature.size > 4:
                # Case where 3 or more reference lines have been placed so take gradient of
                # linear fit to represent average scaling factor
                avg_sf = scale_factor[0]
            else:
                # Case where 2 reference lines have been used. Only have local scaling between
                # two reference lines, everywhere else scaling is 1. Use the local scaling as the
                # average scaling factor