1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
"""
imodfile — Read, write, and inspect IMOD model files.
Provides a Pythonic interface to the `.mod` file format used by the
`IMOD <https://bio3d.colorado.edu/imod/>`_ electron microscopy toolkit.
Basic usage::
import imodfile
import numpy as np
# ── Load ──
model = imodfile.load("cells.mod")
print(f"{len(model)} objects, image size {model.image_size}")
# ── Read points ──
for obj in model.objects:
for cont in obj.contours:
pts = cont.points # ndarray (N, 3), float32
xs, ys, zs = pts[:, 0], pts[:, 1], pts[:, 2]
# ── Create ──
obj = model.add_object()
obj.name = "mitochondria"
obj.color = (1.0, 0.0, 0.0)
cont = obj.add_contour()
cont.points = np.array([[0,0,0], [1,0,0], [0,1,0]], dtype=np.float32)
cont.surface = 0
cont.time = 1
# ── Save ──
model.save("output.mod")
model.save("output.txt") # ASCII format
model.save_points("points.txt") # point dump
"""
# Re-exported types
=
# ── type aliases ──
=
= # shape (N, 3)
# ═════════════════════════════════════════════════════════════════════════════
# Contour
# ═════════════════════════════════════════════════════════════════════════════
"""A contour — a series of 3-D points with optional surface/time metadata.
Points are stored as an (N, 3) float32 numpy array and can be both read
and written::
cont = obj.contours[0]
# read
pts = cont.points # ndarray (N, 3)
x = pts[:, 0]
# write (must be (N, 3) float32)
cont.points = np.array([[1,2,3],[4,5,6]], dtype=np.float32)
"""
"""(N, 3) float32 array of point coordinates.
Setting this replaces all points in the contour.
The array must have shape ``(N, 3)`` and dtype ``float32``.
Example::
cont.points = np.array([
[10.0, 20.0, 30.0],
[40.0, 50.0, 60.0],
], dtype=np.float32)
"""
...
...
"""Number of points (read-only, derived from ``.points``)."""
...
"""Surface index this contour belongs to.
Used to group contours within an object into separate surfaces.
Default is 0."""
...
...
"""Time index for time-series data. Default is 0.
Numbered from 1 when set; 0 means no time set."""
...
...
"""Bit flags. Common values::
``0x10`` — contour has scan-line pairs (not open/closed)
``0x08`` — draw on all planes regardless of Z
``0x04`` — wild contour (not in one Z plane)
"""
...
...
"""e.g. ``Contour(points=42, surface=0)``"""
...
# ═════════════════════════════════════════════════════════════════════════════
# Mesh
# ═════════════════════════════════════════════════════════════════════════════
"""A triangle/quad mesh with vertices and an index list.
Vertices are stored as an (N, 3) float32 array. The index list
contains vertex indices plus special sentinel values:
* ``-1`` — end of list
* ``-21`` — begin polygon (vertices only)
* ``-22`` — end polygon
* ``-23`` — begin vertex/normal polygon pair
"""
"""(N, 3) float32 array of vertex coordinates. Read/write.
Example::
mesh.vertices = np.array([
[0,0,0], [1,0,0], [1,1,0], [0,1,0],
], dtype=np.float32)
"""
...
...
"""Index list into the vertex array.
Contains vertex indices and negative sentinels
(``-1`` = end, ``-22`` = polygon end, etc.)
Example::
mesh.indices = [-23, 0, 0, 1, 0, 2, 0, -22] # triangle
"""
...
...
"""e.g. ``Mesh(vertices=128, indices=384)``"""
...
# ═════════════════════════════════════════════════════════════════════════════
# Object
# ═════════════════════════════════════════════════════════════════════════════
"""A model object — a named container for contours and meshes.
Each object has its own colour, transparency, material properties,
and display settings::
obj = model.objects[0]
obj.name = "membrane"
obj.color = (0.0, 1.0, 0.0) # green
obj.flags |= 0x100 # turn off display
"""
"""Object name (up to 64 characters). Read/write."""
...
...
"""RGB colour as ``(red, green, blue)``, each in [0, 1].
Example::
obj.color = (1.0, 0.5, 0.0) # orange
"""
...
...
"""Bit flags for display and drawing properties.
Selected flags::
``0x001`` — open contours (not closed)
``0x002`` — fill polygons
``0x004`` — scattered points
``0x008`` — inside-out surface normals
``0x010`` — draw mesh in 3-D
``0x020`` — no lines in 3-D
``0x040`` — light both sides
``0x080`` — use fill colour
``0x100`` — off (do not display)
``0x200`` — draw label
``0x400`` — scale line width
``0x800`` — has time indices
"""
...
...
"""List of contours belonging to this object.
Use ``.add_contour()`` and ``.remove_contour()`` to modify."""
...
"""List of meshes belonging to this object.
Use ``.add_mesh()`` to create new meshes."""
...
"""Add a new empty contour and return it.
After creation, set its ``.points``, ``.surface``, etc.::
cont = obj.add_contour()
cont.points = np.array([...], dtype=np.float32)
cont.surface = 1
"""
...
"""Remove a contour by its index.
Raises ``IndexError`` if the index is out of range."""
...
"""Add a new empty mesh and return it."""
...
"""All contour points in this object, concatenated into a single
(N, 3) float32 array.
This is a zero-allocation view when used from Rust, but returns
a copied numpy array to Python."""
...
"""Number of contours (same as ``len(obj.contours)``)."""
...
"""e.g. ``Object(name='membrane', contours=12, meshes=1)``"""
...
# ═════════════════════════════════════════════════════════════════════════════
# Model
# ═════════════════════════════════════════════════════════════════════════════
"""Top-level IMOD model — the root container.
A model holds a list of objects, image dimensions, coordinate
transforms, and view settings::
model = imodfile.load("cells.mod")
print(model.name) # "IMOD-NewModel"
print(model.image_size) # (1024, 1024, 300)
print(model.pixel_size) # 0.868
# iterate all points
for obj in model.objects:
for cont in obj.contours:
...
"""
"""Model name (up to 128 characters). Read/write."""
...
...
"""Image dimensions as ``(xmax, ymax, zmax)``.
These are typically the pixel dimensions of the tomogram or image
volume the model was drawn on. Read/write.
Example::
model.image_size = (2048, 2048, 600)
"""
...
...
"""Pixel size in physical units (e.g. nanometres).
The ``units`` field indicates the scale (not exposed yet).
Default is 1.0. Read/write."""
...
...
"""List of objects in this model.
Use ``.add_object()`` and ``.remove_object()`` to modify."""
...
"""Load a model file.
Automatically detects binary vs ASCII format from file contents.
Args:
path: Path to the ``.mod`` file (or ``.txt`` for ASCII).
Returns:
A new ``Model`` instance.
Raises:
FileNotFoundError: The file does not exist.
IOError: The file is not a valid IMOD model.
Example::
model = Model.load("ribosome.mod")
"""
...
"""Save the model to a file.
Uses binary format by default. If the path ends with ``.txt``
or ``.ascii``, uses ASCII format instead.
Args:
path: Output file path.
Raises:
IOError: Failed to write the file.
Example::
model.save("output.mod") # binary
model.save("output.txt") # ASCII
"""
...
"""Write all contour points to a plain-text file.
Each line is ``x y z``. Contours are separated by a blank line.
Useful for piping into external plotting tools.
Args:
path: Output text file path.
Example::
model.save_points("points.txt")
# → 632.34 442.14 404.11
# 649.41 558.57 420.98
# <blank>
# 431.70 538.67 372.98
# ...
"""
...
"""Add a new empty object and return it.
The new object has default properties (grey colour, no contours,
no meshes)::
obj = model.add_object()
obj.name = "new structure"
obj.color = (1.0, 0.0, 0.0)
"""
...
"""Remove an object by its index.
Raises ``IndexError`` if the index is out of range."""
...
"""All points from all objects and contours, concatenated
into a single (N, 3) float32 numpy array.
This is the simplest way to get all coordinates for analysis::
all_pts = model.points() # shape (N, 3)
xs, ys, zs = all_pts.T
"""
...
"""Number of objects (same as ``len(model.objects)``)."""
...
"""e.g. ``Model(name='IMOD-NewModel', objects=3)``"""
...
# ═════════════════════════════════════════════════════════════════════════════
# Top-level convenience
# ═════════════════════════════════════════════════════════════════════════════
"""Load a model file.
This is a convenience alias for ``Model.load()``::
import imodfile
model = imodfile.load("cells.mod")
Args:
path: Path to the IMOD model file.
Returns:
A ``Model`` instance.
Raises:
FileNotFoundError: The file does not exist.
IOError: The file is not a valid IMOD model.
"""
...