Skip to content

Commit e86feb1

Browse files
authored
Merge pull request Quasars#33 from ngergihun/fixes
[FIX] - Backward compatibility and IFG processing
2 parents 984a369 + df72aac commit e86feb1

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

pySNOM/images.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,14 @@ def calculate(self, data, mask=None):
297297
return norm
298298

299299

300-
class BackgroundPolyFit(MaskedTransformation):
300+
class BackgroundPolyFit(Transformation):
301301
def __init__(self, xorder=1, yorder=1, datatype=DataTypes.Phase):
302302
self.xorder = int(xorder)
303303
self.yorder = int(yorder)
304304
self.datatype = datatype
305305

306-
def calculate(self, data, mask = None):
307-
""" Calculates and returns the fitted polynomial background using mask (if given) without applying it to the data"""
308-
if mask is not None:
309-
data = mask*data
306+
def calculate(self, data):
307+
"""Calculates and returns the fitted polynomial background using mask (if given) without applying it to the data"""
310308

311309
Z = copy.deepcopy(data)
312310
x = list(range(0, Z.shape[1]))
@@ -340,6 +338,32 @@ def get_basis(x, y, max_order_x=1, max_order_y=1):
340338
print("X and Y order must be integer!")
341339

342340
return background
341+
342+
def transform(self, data):
343+
"""Calculates and applies the corrections to the data taking into account the mask if given"""
344+
background = self.calculate(data)
345+
346+
if self.datatype == DataTypes["Amplitude"]:
347+
return data / background, background
348+
else:
349+
return data - background, background
350+
351+
352+
class MaskedBackgroundPolyFit(BackgroundPolyFit,MaskedTransformation):
353+
def __init__(self, xorder=1, yorder=1, datatype=DataTypes.Phase):
354+
self.xorder = int(xorder)
355+
self.yorder = int(yorder)
356+
self.datatype = datatype
357+
358+
def calculate(self, data, mask=None):
359+
"""Calculates and returns the fitted polynomial background using mask (if given) without applying it to the data"""
360+
if mask is not None:
361+
data = mask * data
362+
363+
return BackgroundPolyFit.calculate(self,data)
364+
365+
def transform(self, data, mask=None):
366+
return MaskedTransformation.transform(self, data, mask=mask)
343367

344368

345369
# TODO: Helper functions to create masks or turn other types of masks into 1/Nan mask

pySNOM/interferograms.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ def transform(self, neaifg):
336336
pointifg_params["Scan"] = "Fourier Scan"
337337
pointifg_params["Averaging"] = neaifg.parameters["Averaging"]
338338

339-
spectra = NeaSpectrum({}, {}, scantype=neaifg.scantype)
339+
spectra_params = dict()
340+
spectra_params["Scan"] = "Fourier Scan"
341+
spectra_params["PixelArea"] = pixel_area
342+
spectra = NeaSpectrum({}, spectra_params, scantype=neaifg.scantype)
340343

341344
allchannels = list(neaifg.data.keys())
342345
optical_channels = [
@@ -385,8 +388,6 @@ def transform(self, neaifg):
385388
spectra.data[channelP] = phiFullData
386389
spectra.data["Wavenumber"] = fFullData
387390

388-
spectra._parameters["PixelArea"] = pixel_area
389-
390391
return spectra
391392

392393

pySNOM/tests/test_transform.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
import numpy as np
44

5-
from pySNOM.images import LineLevel, BackgroundPolyFit, SimpleNormalize, DataTypes, AlignImageStack, mask_from_datacondition, dict_from_imagestack
5+
from pySNOM.images import (
6+
LineLevel,
7+
BackgroundPolyFit,
8+
MaskedBackgroundPolyFit,
9+
SimpleNormalize,
10+
DataTypes,
11+
AlignImageStack,
12+
mask_from_datacondition,
13+
dict_from_imagestack,
14+
)
615

716

817
class TestLineLevel(unittest.TestCase):
@@ -98,31 +107,47 @@ def test_withmask(self):
98107
mask = np.ones([10,10])
99108
mask[4:8,4:8] = np.nan
100109

101-
t = BackgroundPolyFit(xorder=1,yorder=1,datatype=DataTypes.Phase)
102-
out = t.transform(d,mask=mask)
103-
np.testing.assert_almost_equal(out[0,0], 0.0)
104-
np.testing.assert_almost_equal(out[9,9], 0.0)
110+
t = MaskedBackgroundPolyFit(xorder=1, yorder=1, datatype=DataTypes.Phase)
111+
out = t.transform(d, mask=mask)
112+
np.testing.assert_almost_equal(out[0, 0], 0.0)
113+
np.testing.assert_almost_equal(out[9, 9], 0.0)
105114

106-
t = BackgroundPolyFit(xorder=1,yorder=1,datatype=DataTypes.Amplitude)
107-
out = t.transform(d,mask=mask)
108-
np.testing.assert_almost_equal(out[0,0], 1.0)
109-
np.testing.assert_almost_equal(out[9,9], 1.0)
115+
t = MaskedBackgroundPolyFit(xorder=1, yorder=1, datatype=DataTypes.Amplitude)
116+
out = t.transform(d, mask=mask)
117+
np.testing.assert_almost_equal(out[0, 0], 1.0)
118+
np.testing.assert_almost_equal(out[9, 9], 1.0)
110119

111120
def test_withoutmask(self):
112121
d = np.ones([10,10])
113122
d[4:8,4:8] = 10
114123
mask = np.ones([10,10])
115124
mask[4:8,4:8] = np.nan
116125

117-
t = BackgroundPolyFit(xorder=1,yorder=1,datatype=DataTypes.Phase)
126+
t = MaskedBackgroundPolyFit(xorder=1, yorder=1, datatype=DataTypes.Phase)
118127
out = t.transform(d)
119128
np.testing.assert_almost_equal(out[0,0], -0.2975206611570238)
120129
np.testing.assert_almost_equal(out[9,9], -3.439338842975202)
121130

122-
t = BackgroundPolyFit(xorder=1,yorder=1,datatype=DataTypes.Amplitude)
131+
t = MaskedBackgroundPolyFit(xorder=1, yorder=1, datatype=DataTypes.Amplitude)
123132
out = t.transform(d)
124-
np.testing.assert_almost_equal(out[0,0], 0.7707006369426758)
125-
np.testing.assert_almost_equal(out[9,9], 0.22525876833718098)
133+
np.testing.assert_almost_equal(out[0, 0], 0.7707006369426758)
134+
np.testing.assert_almost_equal(out[9, 9], 0.22525876833718098)
135+
136+
def test_old_version(self):
137+
d = np.ones([10, 10])
138+
d[4:8, 4:8] = 10
139+
mask = np.ones([10, 10])
140+
mask[4:8, 4:8] = np.nan
141+
142+
t = BackgroundPolyFit(xorder=1, yorder=1, datatype=DataTypes.Phase)
143+
out, _ = t.transform(d)
144+
np.testing.assert_almost_equal(out[0, 0], -0.2975206611570238)
145+
np.testing.assert_almost_equal(out[9, 9], -3.439338842975202)
146+
147+
t = BackgroundPolyFit(xorder=1, yorder=1, datatype=DataTypes.Amplitude)
148+
out, _ = t.transform(d)
149+
np.testing.assert_almost_equal(out[0, 0], 0.7707006369426758)
150+
np.testing.assert_almost_equal(out[9, 9], 0.22525876833718098)
126151

127152
class TestHelperFunction(unittest.TestCase):
128153

0 commit comments

Comments
 (0)