|
| 1 | +import numpy as np |
| 2 | +import fastplotlib as fpl |
| 3 | +from sklearn.decomposition import FastICA |
| 4 | +from scipy.spatial import ConvexHull |
| 5 | + |
| 6 | + |
| 7 | +def generate_time( |
| 8 | + n_timepoints: int, |
| 9 | + n_components: int, |
| 10 | + firing_prop = 0.05, |
| 11 | +) -> np.ndarray: |
| 12 | + """ |
| 13 | + Generate some time series data using an AR process: |
| 14 | +
|
| 15 | + x_(t+1) = a * x_t |
| 16 | +
|
| 17 | + One distinct time series component is generated per row. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + n_timepoints: int |
| 22 | +
|
| 23 | + n_components: int |
| 24 | +
|
| 25 | + noise_sigma: float |
| 26 | + add random gaussian noise with this sigma value |
| 27 | +
|
| 28 | + firing_prop: float |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + np.ndarray, np.ndarray |
| 33 | + data [n_components, n_timepoints] |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + x = np.zeros((n_components, n_timepoints)) + 0.01 |
| 38 | + |
| 39 | + a = 0.7 |
| 40 | + |
| 41 | + spikes = list() |
| 42 | + |
| 43 | + for i in range(n_components): |
| 44 | + spikes.append((np.random.rand(n_timepoints) < firing_prop).astype(bool)) |
| 45 | + |
| 46 | + for c_ix in range(n_components): |
| 47 | + for i in range(1, n_timepoints): |
| 48 | + x[c_ix, i] = (a * x[c_ix, i - 1]) + (1 * spikes[c_ix][i]) |
| 49 | + |
| 50 | + return x |
| 51 | + |
| 52 | + |
| 53 | +def gaussian_2d(x=0, y=0, mx=0, my=0, sx=1, sy=1): |
| 54 | + """generate a 2D gaussian kernel""" |
| 55 | + return 1. / (2. * np.pi * sx * sy) * np.exp(-((x - mx)**2. / (2. * sx**2.) + (y - my)**2. / (2. * sy**2.))) |
| 56 | + |
| 57 | + |
| 58 | +def generate_movie(time_components: np.ndarray, dims: tuple[int, int] = (50, 50), noise_sigma=0.1) -> np.ndarray: |
| 59 | + n_timepoints, n_components = time_components.shape |
| 60 | + |
| 61 | + centers = np.random.rand(n_components, 2) |
| 62 | + centers[:, 0] *= dims[0] |
| 63 | + centers[:, 1] *= dims[1] |
| 64 | + centers = centers.clip(0, max=min(dims) - 20) |
| 65 | + centers = centers.astype(int) |
| 66 | + |
| 67 | + r = -20, 20 |
| 68 | + r = np.linspace(*r) |
| 69 | + x, y = np.meshgrid(r, r) |
| 70 | + space_component = gaussian_2d(x, y, sx=2, sy=2)[18:-18, 18:-18] |
| 71 | + space_component /= space_component.max() |
| 72 | + |
| 73 | + space_shape = space_component.shape |
| 74 | + |
| 75 | + movie = np.zeros(shape=[n_components, *dims]) |
| 76 | + |
| 77 | + for time_component, center in zip(time_components, centers): |
| 78 | + space_time = np.outer(space_component, time_component).reshape(*space_component.shape, time_components.shape[1]).transpose(2, 0, 1) |
| 79 | + row_ix, col_ix = center |
| 80 | + |
| 81 | + movie[:, row_ix:row_ix + space_shape[0], col_ix:col_ix + space_shape[1]] += space_time |
| 82 | + movie += np.random.normal(loc=0, scale=noise_sigma, size=movie.size).reshape(movie.shape) |
| 83 | + return movie |
| 84 | + |
| 85 | + |
| 86 | +def decomposition(movie, n_components=5): |
| 87 | + n_timepoints = movie.shape[0] |
| 88 | + X = movie.reshape(n_timepoints, np.prod(movie.shape[1:])).T |
| 89 | + |
| 90 | + ica = FastICA(n_components=n_components, fun="exp", random_state=0) |
| 91 | + |
| 92 | + spatial_components = np.abs(ica.fit_transform(X).reshape(*dims, n_components).T) |
| 93 | + temporal_components = np.abs(ica.mixing_) |
| 94 | + |
| 95 | + contours = list() |
| 96 | + for index in range(n_components): |
| 97 | + points = np.array(np.where(spatial_components[index] > np.percentile(spatial_components[index], 98))).T |
| 98 | + hull = ConvexHull(points) |
| 99 | + vertices = np.vstack([hull.points[hull.vertices], hull.points[hull.vertices][0]]) |
| 100 | + contours.append(vertices) |
| 101 | + |
| 102 | + return contours, temporal_components |
| 103 | + |
| 104 | + |
| 105 | +n_components = 5 |
| 106 | +n_timepoints = 100 |
| 107 | +dims = (50, 50) |
| 108 | + |
| 109 | +np.random.seed(0) |
| 110 | +time_components = generate_time( |
| 111 | + n_timepoints=n_timepoints, |
| 112 | + n_components=n_components, |
| 113 | +) |
| 114 | + |
| 115 | +np.random.seed(10) |
| 116 | +movie = generate_movie(time_components, dims=dims, noise_sigma=0.1) |
| 117 | + |
| 118 | +contours, time_series = decomposition(movie, n_components=n_components) |
| 119 | + |
0 commit comments