You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/1-concepts/5-pixel_size/python.md
+32-34Lines changed: 32 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,11 @@ jupytext:
5
5
extension: .md
6
6
format_name: myst
7
7
format_version: 0.13
8
-
jupytext_version: 1.14.5
8
+
jupytext_version: 1.17.2
9
9
kernelspec:
10
+
name: python3
10
11
display_name: Python 3 (ipykernel)
11
12
language: python
12
-
name: python3
13
13
---
14
14
15
15
# Python: Pixel size & dimensions
@@ -34,12 +34,10 @@ The situation is improving though.
34
34
35
35
Here, we'll look at accessing pixel size information using two popular image-reading libraries:
36
36
*[`imageio`](https://pypi.org/project/imageio/) - which very commonly used, and makes reading lots of common image types straightforward
37
-
*[`AICSImageIO`](https://pypi.org/project/aicsimageio/3.2.1/) - which is a bit more complex, but has some *extremely* useful features for working with scientific images
37
+
*[`bioio`](https://bioio-devs.github.io/bioio) - which is a bit more complex, but has some *extremely* useful features for working with scientific images
38
38
39
39
+++
40
40
41
-
42
-
43
41
### ImageIO
44
42
45
43
To explore pixel sizes with `imageio`, let's return to the neuron image used in the 'Channels & colors' chapter.
@@ -98,41 +96,41 @@ So while `imageio` is excellent for reading images easily - generally just a qui
98
96
99
97
+++
100
98
101
-
### Using AICSImageIO
99
+
### Using BioIO
102
100
103
-
The best alternative I know for working with scientific (especially biomedical) images is [**AICSImageIO**](https://github.com/AllenCellModeling/aicsimageio).
101
+
The best alternative I know for working with scientific (especially biomedical) images is [**BioIO**](https://bioio-devs.github.io/bioio) (the successor to [AICSImageIO](https://github.com/AllenCellModeling/aicsimageio)).
104
102
This is a really useful Python library that standardizes reading and writing multiple file formats - and, depending upon how it's installed, can even access lots more awkward proprietary file formats with the help of [Bio-Formats](http://www.openmicroscopy.org/bio-formats/).
105
103
106
-
Although it's possible to use a version of `imread` with AICSImageIO, it's worth learning the alternative way of doing things by creating an `AICSImage` object.
104
+
Although it's possible to use a version of `imread` with BioIO, it's worth learning the alternative way of doing things by creating an `BioImage` object.
107
105
This provides us with a way to access pixels and lots of other useful things whenever we need them.
108
106
109
107
```{code-cell} ipython3
110
-
from aicsimageio.aics_image import AICSImage
108
+
from bioio import BioImage
111
109
112
-
# Create an AICSImage
113
-
img_aics = AICSImage(path)
110
+
# Create a BioImage
111
+
img_bio = BioImage(path)
114
112
115
113
# Print its main attributes
116
-
print(img_aics)
117
-
for d in dir(img_aics):
114
+
print(img_bio)
115
+
for d in dir(img_bio):
118
116
if not d.startswith('_'):
119
117
print(d)
120
118
```
121
119
122
120
From this, we can immediately see the attribute that will provide us with pixel sizes directly.
123
121
124
122
```{code-cell} ipython3
125
-
print(img_aics.physical_pixel_sizes)
123
+
print(img_bio.physical_pixel_sizes)
126
124
```
127
125
128
-
One perhaps non-obvious thing to know when using AICSImageIO is that the `AICSImage` isn't a regular NumPy array of the kind that `imageio.imread` would return.
126
+
One perhaps non-obvious thing to know when using BioIO is that the `BioImage` isn't a regular NumPy array of the kind that `imageio.imread` would return.
129
127
Rather, if you want that, you need to request the data.
130
128
131
129
Using this knowledge, we can check that we have the same mean pixel value for both - as a quick way to ascertain that the actual pixel values are likely to match.
132
130
133
131
```{code-cell} ipython3
134
132
print(f'Mean pixel value from imageio: {im_iio.mean():.2f} (total pixel count {im_iio.size})')
135
-
print(f'Mean pixel value from AICSImageIO: {img_aics.data.mean():.2f} (total pixel count {img_aics.data.size})')
133
+
print(f'Mean pixel value from BioIO: {img_bio.data.mean():.2f} (total pixel count {img_bio.data.size})')
136
134
```
137
135
138
136
## Dimensions
@@ -146,7 +144,7 @@ We might well expect that the NumPy arrays representing the pixel values are the
146
144
NumPy is incredibly flexible when it comes to handling multidimensional arrays.
147
145
And while that flexibility can be really helpful, it can also complicate things.
148
146
149
-
To see it in action, let's check the dimensions of the images we read using imageio and AICSImageIO.
147
+
To see it in action, let's check the dimensions of the images we read using imageio and BioIO.
print(f'Shape of image read by imageio: {im_iio.shape}')
157
155
158
-
# Print shape of image read by AICSImageIO
159
-
from aicsimageio.aics_image import AICSImage
160
-
im_aics = AICSImage(path).data
161
-
print(f'Shape of image read by AICSImageIO: {im_aics.shape}')
156
+
# Print shape of image read by bioio
157
+
from bioio import BioImage
158
+
im_bio = BioImage(path).data
159
+
print(f'Shape of image read by BioIO: {im_bio.shape}')
162
160
163
-
print(f'Arrays the same? {np.array_equal(im_aics, im_iio)}')
161
+
print(f'Arrays the same? {np.array_equal(im_bio, im_iio)}')
164
162
```
165
163
166
-
We can see the number of pixels are the same, but there are some extra 'singleton' dimensions stuck into the results from AICSImageIO (i.e. with length `1`).
164
+
We can see the number of pixels are the same, but there are some extra 'singleton' dimensions stuck into the results from BioIO (i.e. with length `1`).
167
165
168
166
Fortunately, we can easily remove them with an `np.squeeze` - and end up with the same arrays.
169
167
170
168
```{code-cell} ipython3
171
-
im_aics_squeezed = np.squeeze(im_aics)
172
-
print(f'Shape of image read by AICSImageIO & squeezed: {im_aics_squeezed.shape}')
169
+
im_bio_squeezed = np.squeeze(im_bio)
170
+
print(f'Shape of image read by BioIO & squeezed: {im_bio_squeezed.shape}')
173
171
174
-
print(f'Arrays the same? {np.array_equal(im_aics_squeezed, im_iio)}')
172
+
print(f'Arrays the same? {np.array_equal(im_bio_squeezed, im_iio)}')
175
173
```
176
174
177
-
So a natural question is: **why has AICSImageIO snuck in some extra dimensions?**
175
+
So a natural question is: **why has BioIO snuck in some extra dimensions?**
178
176
179
177
Before answering that, we should ask ourselves something else.
180
178
**What exactly do we _have_ along the dimension of length `5`?**
@@ -201,12 +199,12 @@ for ii in range(n_slices):
201
199
To me, that looks very much like we have 5 different channels.
202
200
I'm making some assumptions there... but they seem pretty safe assumptions.
203
201
204
-
However AICSImageIO removes this ambiguity in a couple of ways.
205
-
1. You can expect `AICSImage` to return a 5D array, with the dimensions in a consistent order: `TCZYX` (although there is at least one caveat in the next section!)
202
+
However BioIO removes this ambiguity in a couple of ways.
203
+
1. You can expect `BioIO` to return a 5D array, with the dimensions in a consistent order: `TCZYX` (although there is at least one caveat in the next section!)
206
204
2. You can easily query the dimensions and order to be sure
207
205
208
206
```{code-cell} ipython3
209
-
image = AICSImage(path)
207
+
image = BioImage(path)
210
208
print(image.dims)
211
209
print(f'Shape: {image.dims.shape}')
212
210
print(f'Order: {image.dims.order}')
@@ -266,19 +264,19 @@ except Exception as ex:
266
264
So imageio might get channels at the start or the end.
267
265
For RGB, it seems to prefer the end.
268
266
269
-
What does AICSImageIO do?
267
+
What does BioIO do?
270
268
271
-
Since I said AICSImageIO is consistent, I'd like to say it puts the channels in the same place for the RGB and 5-channel image... but no.
269
+
Since I said BioIO is consistent, I'd like to say it puts the channels in the same place for the RGB and 5-channel image... but no.
272
270
It also treats RGB as a special case.
273
271
274
272
```{code-cell} ipython3
275
-
image = AICSImage(path)
273
+
image = BioImage(path)
276
274
277
275
print(image.shape)
278
276
print(image.dims.order)
279
277
```
280
278
281
-
It's a little hard to find, but the AICSImageIO documentation mentions that [you can expect 5 dimensions for non-RGB images, but RGB images have 6 dimensions](https://allencellmodeling.github.io/aicsimageio/aicsimageio.readers.html#aicsimageio.readers.bioformats_reader.BioFile.to_numpy) - where the sixth is called `S` for `Samples`.
279
+
It's a little hard to find, but the BioIO documentation mentions that [you can expect 5 dimensions for non-RGB images, but RGB images have 6 dimensions](https://bioio-devs.github.io/bioio/bioio.html#bioio.bio_image.BioImage.get_image_data) - where the sixth is called `S` for `Samples`.
282
280
283
281
The good thing is that, assuming you don't have anything else going on with the first 3 dimensions - i.e. they are just `(1, 1, 1)` - a simple `np.squeeze` is enough to convert the pixel array into a matplotlib-friendly channels-last RGB format.
Copy file name to clipboardExpand all lines: chapters/1-concepts/6-files/python.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,11 @@ jupytext:
5
5
extension: .md
6
6
format_name: myst
7
7
format_version: 0.13
8
-
jupytext_version: 1.14.5
8
+
jupytext_version: 1.17.2
9
9
kernelspec:
10
+
name: python3
10
11
display_name: Python 3 (ipykernel)
11
12
language: python
12
-
name: python3
13
13
---
14
14
15
15
# Python: Files & file formats
@@ -97,9 +97,9 @@ im = tifffile.imread(path)
97
97
print(f'Print the mean for comparison: {im.mean()}')
98
98
```
99
99
100
-
### AICSImageIO
100
+
### BioIO
101
101
102
-
[AICSImageIO](https://github.com/AllenCellModeling/aicsimageio) is an excellent package for reading lots of image formats in Python - and is particularly strong when it comes to reading multidimensional images and metadata.
102
+
[BioIO](https://bioio-devs.github.io/bioio/) is an excellent package for reading lots of image formats in Python - and is particularly strong when it comes to reading multidimensional images and metadata.
103
103
104
104
It can even handle a variety of microscopy formats, and optionally use [Bio-Formats](https://www.openmicroscopy.org/bio-formats/).
105
105
@@ -126,4 +126,4 @@ Dask isn't an image reading package, but [dask-image](https://image.dask.org/) i
126
126
127
127
Finally, [Napari](https://napari.org) isn't an image reading library either; rather, it's a fantastic open-source, extensible, multidimensional image viewer for Python.
128
128
129
-
Napari can bring everything together - working with dask arrays and reading images with plugins, such as [napari-aicsimageio](https://github.com/AllenCellModeling/napari-aicsimageio) and [napari-lazy-openslide](https://github.com/manzt/napari-lazy-openslide).
129
+
Napari can bring everything together - working with dask arrays and reading images with plugins, such as [napari-lazy-openslide](https://github.com/manzt/napari-lazy-openslide).
0 commit comments