forked from RecordEvolution/IMCtermite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_numpy_chunks.py
More file actions
85 lines (65 loc) · 2.6 KB
/
usage_numpy_chunks.py
File metadata and controls
85 lines (65 loc) · 2.6 KB
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
import imctermite
import os
# Path to a sample file
# Using sampleB.raw because it has integer data with scaling (factor=0.01, offset=327.68)
raw_file = "samples/sampleB.raw"
if not os.path.exists(raw_file):
print(f"Sample file {raw_file} not found.")
exit(1)
print(f"Loading {raw_file}")
try:
imcraw = imctermite.ImcTermite(raw_file)
except RuntimeError as e:
print(f"Failed to load/parse raw-file: {e}")
exit(1)
# Get channels metadata
channels = imcraw.get_channels(False)
if not channels:
print("No channels found.")
exit(0)
# Pick the first channel
# For sampleB.raw, channel 347 is the interesting one
target_uuid = "347"
channel_info = next((ch for ch in channels if ch['uuid'] == target_uuid), channels[0])
first_channel_uuid = channel_info['uuid']
print(f"Iterating over channel {first_channel_uuid} ({channel_info.get('name', 'unnamed')})")
# Check native datatype
if 'datatype' in channel_info:
print(f"Native IMC datatype ID: {channel_info['datatype']}")
# Example 1: Scaled mode (default) - returns floats (physical units)
print("\n--- Scaled Mode (Physical Units) ---")
total_rows = 0
chunk_size = 1000
for chunk in imcraw.iter_channel_numpy(first_channel_uuid, include_x=True, chunk_rows=chunk_size, mode="scaled"):
start = chunk['start']
y = chunk['y']
x = chunk.get('x')
count = len(y)
total_rows += count
if total_rows <= chunk_size * 2: # Print only first few chunks
print(f"Chunk start={start}, count={count}, y_shape={y.shape}, y_dtype={y.dtype}")
if x is not None:
print(f" x_shape={x.shape}, x_dtype={x.dtype}")
if count > 0:
print(f" First y value: {y[0]}")
print(f"Total rows read (scaled): {total_rows}")
# Example 2: Raw mode - returns native types (e.g. integers)
print("\n--- Raw Mode (Native Types) ---")
# Get scaling factors
factor = float(channel_info.get('factor', 1.0))
offset = float(channel_info.get('offset', 0.0))
print(f"Scaling: factor={factor}, offset={offset}")
total_rows = 0
for chunk in imcraw.iter_channel_numpy(first_channel_uuid, include_x=True, chunk_rows=chunk_size, mode="raw"):
start = chunk['start']
y = chunk['y']
count = len(y)
total_rows += count
if total_rows <= chunk_size * 2:
print(f"Chunk start={start}, count={count}, y_shape={y.shape}, y_dtype={y.dtype}")
if count > 0:
raw_val = y[0]
scaled_val = raw_val * factor + offset
print(f" First y value (raw): {raw_val}")
print(f" First y value (manually scaled): {scaled_val}")
print(f"Total rows read (raw): {total_rows}")