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]) user = key[20:]
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))