-
Notifications
You must be signed in to change notification settings - Fork 1
/
_normalisation.py
67 lines (52 loc) · 1.95 KB
/
_normalisation.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
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
"""Mass spectrometry spectra normalization
This module should be imported and contains the following:
* NormalizerInterface - Interface for a spectra normalizer.
* TICNormalizer - Class for total ion count normalization.
"""
import numpy as np
from typing import Tuple
from abc import ABC, abstractmethod
class NormalizerInterface(ABC):
"""Interface for a spectra normalizer
"""
@classmethod
@abstractmethod
def normalize(
cls, spectra: Tuple[np.ndarray, np.ndarray], epsilon: float = 0.001
) -> Tuple[np.ndarray, np.ndarray]:
"""Method to normalize a spectra
Args:
spectra (Tuple[np.ndarray, np.ndarray]): First element is the mz values
array of spectra and second element is the intensity values array
of spectra.
epsilon (float): Small float added to denominator to avoid zero
division.
Returns:
Tuple[np.ndarray, np.ndarray]: First element is the mz values array of
spectra and second element is the normalized intensity values array
of spectra.
"""
raise NotImplementedError
class TICNormalizer(NormalizerInterface):
"""Total Ion Count normalizer.
"""
@classmethod
def normalize(
cls, spectra: Tuple[np.ndarray, np.ndarray], epsilon: float = 0.001
) -> Tuple[np.ndarray, np.ndarray]:
"""Method to normalize a spectra
Args:
spectra (Tuple[np.ndarray, np.ndarray]): First element is the mz values
array of spectra and second element is the intensity values array
of spectra.
epsilon (float): Small float added to denominator to avoid zero
division.
Returns:
Tuple[np.ndarray, np.ndarray]: First element is the mz values array of
spectra and second element is the normalized intensity values array
of spectra.
"""
# Unpack spectra
mzs, intensities = np.copy(spectra)
# Return tic normalized
return (mzs, intensities / (intensities.sum() + epsilon))