castalign 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Maxwell Shinn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.4
2
+ Name: castalign
3
+ Version: 0.2.0
4
+ Summary: Coppafish, Antibody Staining, and Two-photon alignment: 3D nonlinear alignment for microscopy and in vivo imaging
5
+ Home-page: https://github.com/mwshinn/castalign
6
+ Author: Max Shinn
7
+ Author-email: m.shinn@ucl.ac.uk
8
+ Maintainer: Max Shinn
9
+ Maintainer-email: m.shinn@ucl.ac.uk
10
+ License: MIT
11
+ Classifier: Topic :: Scientific/Engineering
12
+ Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
13
+ Classifier: Topic :: Scientific/Engineering :: Visualization
14
+ Classifier: Topic :: Multimedia :: Graphics
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Development Status :: 4 - Beta
18
+ Requires-Python: >=3.7
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE.txt
21
+ Requires-Dist: numpy
22
+ Requires-Dist: scipy
23
+ Requires-Dist: napari
24
+ Requires-Dist: magicgui
25
+ Requires-Dist: scikit-image
26
+ Requires-Dist: imageio
27
+ Requires-Dist: imageio-ffmpeg
28
+ Requires-Dist: threadpoolctl
29
+ Dynamic: author
30
+ Dynamic: author-email
31
+ Dynamic: classifier
32
+ Dynamic: description
33
+ Dynamic: description-content-type
34
+ Dynamic: home-page
35
+ Dynamic: license
36
+ Dynamic: license-file
37
+ Dynamic: maintainer
38
+ Dynamic: maintainer-email
39
+ Dynamic: requires-dist
40
+ Dynamic: requires-python
41
+ Dynamic: summary
42
+
43
+ # CASTalign: Coppafish, Antibody Staining, and Two-photon alignment
44
+
45
+ CASTalign allows you to register 3D microscopy images to each other and to *in
46
+ vivo* two-photon imaging. It is optimised to allow registration between
47
+ CoppaFISH 3D, antibody staining (e.g., immunofluorescence), and *in vivo*
48
+ imaging.
49
+
50
+ # Installation
51
+
52
+ To install, you need the following packages:
53
+
54
+ > pip install numpy scipy napari magicgui vispy scikit-image imageio imageio-ffmpeg
55
+
56
+ # Usage
57
+
58
+ See the [tutorial](https://castalign.readthedocs.io/en/latest/tutorial/) in the [documentation](https://castalign.readthedocs.io/en/latest/).
@@ -0,0 +1,16 @@
1
+ # CASTalign: Coppafish, Antibody Staining, and Two-photon alignment
2
+
3
+ CASTalign allows you to register 3D microscopy images to each other and to *in
4
+ vivo* two-photon imaging. It is optimised to allow registration between
5
+ CoppaFISH 3D, antibody staining (e.g., immunofluorescence), and *in vivo*
6
+ imaging.
7
+
8
+ # Installation
9
+
10
+ To install, you need the following packages:
11
+
12
+ > pip install numpy scipy napari magicgui vispy scikit-image imageio imageio-ffmpeg
13
+
14
+ # Usage
15
+
16
+ See the [tutorial](https://castalign.readthedocs.io/en/latest/tutorial/) in the [documentation](https://castalign.readthedocs.io/en/latest/).
@@ -0,0 +1,7 @@
1
+ from .base import *
2
+ from .graph import Graph, load
3
+ from .graph import TransformGraph # Backward compatibility
4
+ from ._version import __version__
5
+
6
+ import PIL.Image as _PILI
7
+ _PILI.MAX_IMAGE_PIXELS = 1000000000
@@ -0,0 +1,242 @@
1
+ from . import base as transform
2
+ import numpy as np
3
+ from . import ndarray_shifted as ndarray_shifted
4
+ from . import utils
5
+ import os
6
+ import tempfile
7
+ from webmzarr import WebM
8
+
9
+ class TransformGraph:
10
+ def __init__(self, name, use_zarr=False):
11
+ # NOTE: If you change the constructor or internal data structure, you also need to change the load and save methods.
12
+ self.name = name
13
+ self.nodes = [] # List of node names
14
+ self.edges = {} # Dictionary of dictonaries, edges[node1][node2] = transform
15
+ self.node_images = {} # If node has an associated image, node name is key and image is value
16
+ self.compressed_node_images = {} # If a node has an associated image, the compressed version is stored here and loaded dynamically into node_images
17
+ self.node_notes = {}
18
+ self.filename = None
19
+ self._zarr_mode = use_zarr
20
+ self._zarr_image_buffer = {}
21
+ self._zarr_object = None
22
+ def __eq__(self, other):
23
+ return (self.name == other.name) and \
24
+ self.nodes == other.nodes and \
25
+ self.edges == other.edges and \
26
+ len(self.compressed_node_images) == len(other.compressed_node_images) and \
27
+ all(np.allclose(self.compressed_node_images[ni1][0],other.compressed_node_images[ni2][0]) for ni1,ni2 in zip(self.compressed_node_images.keys(), other.compressed_node_images.keys()))
28
+ def save(self, filename=None):
29
+ if filename is None:
30
+ filename = self.filename
31
+ if self._zarr_mode: # zarr
32
+ if self._zarr_object is None:
33
+ store = zarr.DirectoryStore(filename)
34
+ self._zarr_object = zarr.group(store=store, overwrite=True)
35
+ self._zarr_object.create_group('node_images')
36
+ self._zarr_object['name'] = self.name
37
+ self._zarr_object['nodes'] = self.nodes
38
+ self._zarr_object['edges'] = repr(self.edges)
39
+ self._zarr_object['notes'] = repr(self.node_notes)
40
+ for k,v in self._zarr_image_buffer.items():
41
+ if isinstance(v[0], str):
42
+ self._zarr_object['node_images'][k] = v[0]
43
+ else:
44
+ if v[0].shape[0] == 1:
45
+ self._zarr_object['node_images'][k] = zarr.creation.array(v[0], compressor="zstd")
46
+ else:
47
+ self._zarr_object['node_images'][k] = zarr.creation.array(v, compressor=WebM(**v[1]), chunks=(v.shape[0], 128, 128)) # TODO tune the chunk size to something more optimal
48
+ self._zarr_image_buffer = {}
49
+ else: # npz
50
+ # Note to future self: If I ende up not using image arrays, I could rewrite this to save in text format.
51
+ node_images_keys = list(sorted(self.compressed_node_images.keys()))
52
+ node_images_values = [self.compressed_node_images[k] for k in node_images_keys]
53
+ node_image_arrays_compressed = {f"nodeimage_{i}": node_images_values[i][0] for i in range(0, len(node_images_values))}
54
+ node_image_arrays_info = {f"nodeimageinfo_{i}": node_images_values[i][1] for i in range(0, len(node_images_values))}
55
+ np.savez_compressed(filename, name=self.name, nodes=self.nodes, nodeimage_keys=node_images_keys, **node_image_arrays_compressed, **node_image_arrays_info, edges=repr(self.edges), notes=repr(self.node_notes))
56
+ @classmethod
57
+ def load(cls, filename):
58
+ if self._zarr_mode: # zarr
59
+ store = zarr.DirectoryStore(filename)
60
+ f = zarr.group(store=store, overwrite=True)
61
+ g = cls(str(zarr_object['name']))
62
+ g.nodes = list(map(str, f['nodes']))
63
+ g.edges = eval(str(f['edges']), transform.__dict__, transform.__dict__)
64
+ if "notes" in f.keys():
65
+ g.node_notes = eval(str(f['notes']))
66
+ g.filename = filename
67
+ return g
68
+ else: # npz
69
+ f = np.load(filename)
70
+ g = cls(str(f['name']))
71
+ g.nodes = list(map(str, f['nodes']))
72
+ g.edges = eval(str(f['edges']), transform.__dict__, transform.__dict__)
73
+ for i,n in enumerate(f['nodeimage_keys']):
74
+ n = str(n)
75
+ g.compressed_node_images[n] = (f[f'nodeimage_{i}'], f[f'nodeimageinfo_{i}'])
76
+ if "notes" in f.keys():
77
+ g.node_notes = eval(str(f['notes']))
78
+ g.filename = filename
79
+ return g
80
+ @classmethod
81
+ def load_old(cls, filename):
82
+ f = np.load(filename)
83
+ g = cls(str(f['name']))
84
+ g.nodes = list(map(str, f['nodes']))
85
+ g.edges = eval(str(f['edges']), transform.__dict__, transform.__dict__)
86
+ for i,n in enumerate(f['nodeimage_keys']):
87
+ n = str(n)
88
+ g.node_images[n] = f[f'nodeimage_{i}']
89
+ return g
90
+ def add_node(self, name, image=None, compression="normal", notes=""):
91
+ # Image can either be a 3-dimensional ndarray or a string of another node
92
+ assert name not in self.nodes, f"Node '{name}' already exists"
93
+ if image is not None: # Do this first because it may fail due to a memory error, and we don't want the node half-added
94
+ if self._zarr_mode:
95
+ if isinstance(image, str):
96
+ self._zarr_image_buffer[name] = (image, {})
97
+ else:
98
+ if image.ndim == 2:
99
+ image = image[None]
100
+ transform_id = _image_detect_transform(image)
101
+ maxval = np.quantile(image, .999)
102
+ minval = np.min(image)
103
+ self._zarr_image_buffer[name] = (image, {"maxval": maxval, "minval": minval, "transform": "log10" if transform_id == 1 else "none"})
104
+ else:
105
+ if isinstance(image, str):
106
+ self.compressed_node_images[name] = (image, [])
107
+ else:
108
+ if image.ndim == 2:
109
+ image = image[None]
110
+ self.compressed_node_images[name] = utils.compress_image(image, level=compression)
111
+ self.node_images[name] = image
112
+ self.node_notes[name] = notes
113
+ self.nodes.append(name)
114
+ self.edges[name] = {}
115
+ # TODO this doesn't handle the case where other node images refer to the given node
116
+ def remove_node(self, name):
117
+ if name in self.compressed_node_images:
118
+ del self.compressed_node_images[name]
119
+ if name in self.node_images:
120
+ del self.node_images[name]
121
+ if name in self.node_notes:
122
+ del self.node_notes[name]
123
+ for n in list(self.edges[name]):
124
+ del self.edges[name][n]
125
+ if name in self.edges[n]:
126
+ del self.edges[n][name]
127
+ self.nodes.remove(name)
128
+ def replace_node_image(self, name, image=None, compression="normal"):
129
+ """Replace or remove a node's image without impacting its other connections"""
130
+ # Mostly copied from add_node
131
+ assert name in self.nodes, f"Node '{name}' doesn't exist"
132
+ if name in self.node_images:
133
+ del self.node_images[name]
134
+ if image is not None: # Do this first because it may fail due to a memory error, and we don't want the node half-added
135
+ if isinstance(image, str):
136
+ self.compressed_node_images[name] = (image, [])
137
+ else:
138
+ if image.ndim == 2:
139
+ image = image[None]
140
+ self.compressed_node_images[name] = utils.compress_image(image, level=compression)
141
+ self.node_images[name] = image
142
+ else:
143
+ if name in self.compressed_node_images.keys():
144
+ del self.compressed_node_images[name]
145
+ def add_edge(self, frm, to, transform, update=False):
146
+ assert frm in self.nodes, f"Node '{frm}' doesn't exist"
147
+ assert to in self.nodes, f"Node '{to}' doesn't exist"
148
+ if update is False:
149
+ assert to not in self.edges[frm].keys(), "Edge already exists"
150
+ else:
151
+ assert to in self.edges[frm].keys(), "Edge doesn't exist"
152
+ self.edges[frm][to] = transform
153
+ try:
154
+ inv = transform.invert()
155
+ self.edges[to][frm] = inv
156
+ except NotImplementedError:
157
+ pass
158
+ def remove_edge(self, frm, to):
159
+ assert frm in self.nodes, f"Node '{frm}' doesn't exist"
160
+ assert to in self.nodes, f"Node '{to}' doesn't exist"
161
+ assert to in self.edges[frm].keys(), "Edge doesn't exist"
162
+ del self.edges[frm][to]
163
+ if frm in self.edges[to].keys():
164
+ del self.edges[to][frm]
165
+ def connected_components(self):
166
+ """Find connected components in the graph.
167
+
168
+ This does not yet support directed graphs, i.e., graphs which contain
169
+ non-invertable transforms.
170
+
171
+ """
172
+ components = []
173
+ for n in self.nodes:
174
+ # Make sure n isn't accounted for already
175
+ if any([n in c for c in components]):
176
+ continue
177
+ # Find all nodes reachable from n and add to current_component.
178
+ # Only search through those that haven't been searched through yet.
179
+ current_component = set([n])
180
+ to_search = [n]
181
+ while len(to_search) > 0:
182
+ node = to_search.pop()
183
+ connected = list(self.edges[node].keys())
184
+ to_search.extend([c for c in connected if c not in current_component])
185
+ current_component = current_component.union(set(connected))
186
+ components.append(current_component)
187
+ return components
188
+ def unload(self):
189
+ """Clear memory by unloading the node images, keeping only the compressed forms"""
190
+ keys = list(self.node_images.keys())
191
+ for k in keys:
192
+ del self.node_images[k]
193
+ def get_transform(self, frm, to):
194
+ assert frm in self.nodes, f"Node {frm} not found"
195
+ assert to in self.nodes, f"Node {to} not found"
196
+ def _get_transform_from_chain(chain):
197
+ cur = frm
198
+ tform = None
199
+ for c in chain:
200
+ tform = self.edges[cur][c] if tform is None else tform + self.edges[cur][c]
201
+ cur = c
202
+ return tform
203
+ candidates = list(map(lambda x : (x,) if isinstance(x, str) else tuple(x), self.edges[frm].keys()))
204
+ seen = [frm]
205
+ while len(candidates) > 0:
206
+ if to in [l[-1] for l in candidates]:
207
+ chain = next(l for l in candidates if to == l[-1])
208
+ return _get_transform_from_chain(chain)
209
+ c0 = candidates.pop(0)
210
+ seen.append(c0[-1])
211
+ to_append = [tuple(list(c0)+[n]) for n in self.edges[c0[-1]] if n not in seen]
212
+ candidates.extend(to_append)
213
+ raise RuntimeError(f"Path from '{frm}' to '{to}' not found")
214
+ def get_image(self, node):
215
+ if node not in self.node_images.keys():
216
+ if len(self.compressed_node_images[node][1]) == 0: # First element is a string of a node
217
+ imnode = str(self.compressed_node_images[node][0])
218
+ self.node_images[node] = self.get_transform(imnode, node).transform_image(self.get_image(imnode), relative=True)
219
+ else:
220
+ self.node_images[node] = utils.decompress_image(*self.compressed_node_images[node])
221
+ return self.node_images[node]
222
+ def visualise(self, filename=None, nearby=None):
223
+ fn = filename
224
+ if fn is None:
225
+ fn = tempfile.mkstemp()[1]
226
+ try:
227
+ import graphviz
228
+ except ImportError:
229
+ raise ImportError("Please install graphviz package to visualise")
230
+ g = graphviz.Digraph(self.name, filename=filename)
231
+ for e1 in self.edges.keys():
232
+ for e2 in self.edges[e1].keys():
233
+ if nearby is not None and e1 != nearby and e2 != nearby:
234
+ continue
235
+ if e1 in self.edges[e2].keys() and self.edges[e1][e2].__class__.__name__ == self.edges[e2][e1].__class__.__name__:
236
+ if e1 > e2:
237
+ g.edge(e1, e2, label=self.edges[e1][e2].__class__.__name__, dir="both")
238
+ else:
239
+ g.edge(e1, e2, label=self.edges[e1][e2].__class__.__name__)
240
+ g.view()
241
+ if filename is None: # Temporary file
242
+ os.unlink(fn)
@@ -0,0 +1 @@
1
+ __version__ = "0.2.0"