forked from zaritskylab/SLGC-tissue-similarity-and-grading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
38 lines (32 loc) · 1.13 KB
/
utils.py
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
"""Mass spectrometry utils
This module should be imported and contains the following:
* read_msi - Function to read a continuos msi.
"""
import numpy as np
from typing import Tuple
from pyimzml.ImzMLParser import ImzMLParser
def read_msi(p: ImzMLParser) -> Tuple[np.ndarray, np.ndarray]:
"""
Function to read a continuos imzML parser object into a numpy array.
Args:
p (ImzMLParser): The imzML parser.
Returns:
Tuple[np.ndarray, np.ndarray]: Numpy 3D matrix where y coordinate
(axis=0), x coordinate (axis=1), intensities values (axis=2)
and continuos mzs values.
"""
# Get shape of mzs values
max_z = p.mzLengths[0]
# Get shape of y axis
max_y = p.imzmldict["max count of pixels y"]
# Get shape of x axis
max_x = p.imzmldict["max count of pixels x"]
# Create empty numpy 3D matrix
msi = np.zeros((max_y, max_x, max_z))
# Loop over each coordinate and add to 3D matrix
for i, (x, y, _) in enumerate(p.coordinates):
# Get mzs and intensities
mzs, ints = p.getspectrum(i)
# Add intensities to x,y coordinate
msi[y - 1, x - 1, :] = ints
return mzs, msi