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))
# Find brain region that each channel is located in brain_regions = ephysalign.get_brain_locations(xyz_channels) # Add extra keys to store all useful information as one bunch object brain_regions['xyz'] = xyz_channels brain_regions['lateral'] = chn_coords[:, 0] brain_regions['axial'] = chn_coords[:, 1] # Store brain regions result in channels dict with same key as in alignment channel_info = {key: brain_regions} channels.update(channel_info) # For plotting -> extract the boundaries of the brain regions, as well as CCF label and colour region, region_label, region_colour, _ = ephysalign.get_histology_regions( xyz_channels, depths) channel_depths_track = (ephysalign.feature2track(depths, feature, track) - ephysalign.track_extent[0]) # Make plot that shows the brain regions that channels pass through ax_regions = fig.axes[iK * 2] for reg, col in zip(region, region_colour): height = np.abs(reg[1] - reg[0]) bottom = reg[0] color = col / 255 ax_regions.bar(x=0.5, height=height, width=1, color=color, bottom=reg[0], edgecolor='w') ax_regions.set_yticks(region_label[:, 0].astype(int))