surfacetopography 0.91.2__py3.6-linux-x86_64.egg

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. EGG-INFO/PKG-INFO +11 -0
  2. EGG-INFO/SOURCES.txt +96 -0
  3. EGG-INFO/dependency_links.txt +1 -0
  4. EGG-INFO/native_libs.txt +1 -0
  5. EGG-INFO/not-zip-safe +1 -0
  6. EGG-INFO/requires.txt +6 -0
  7. EGG-INFO/top_level.txt +3 -0
  8. SurfaceTopography/ChangeLog.md +358 -0
  9. SurfaceTopography/Converters.py +197 -0
  10. SurfaceTopography/Generation.py +440 -0
  11. SurfaceTopography/HeightContainer.py +315 -0
  12. SurfaceTopography/IO/DI.py +261 -0
  13. SurfaceTopography/IO/FromFile.py +802 -0
  14. SurfaceTopography/IO/H5.py +79 -0
  15. SurfaceTopography/IO/IBW.py +176 -0
  16. SurfaceTopography/IO/MI.py +307 -0
  17. SurfaceTopography/IO/Matlab.py +106 -0
  18. SurfaceTopography/IO/NC.py +228 -0
  19. SurfaceTopography/IO/NPY.py +139 -0
  20. SurfaceTopography/IO/OPDx.py +885 -0
  21. SurfaceTopography/IO/Reader.py +389 -0
  22. SurfaceTopography/IO/__init__.py +241 -0
  23. SurfaceTopography/IO/__pycache__/DI.cpython-36.pyc +0 -0
  24. SurfaceTopography/IO/__pycache__/FromFile.cpython-36.pyc +0 -0
  25. SurfaceTopography/IO/__pycache__/H5.cpython-36.pyc +0 -0
  26. SurfaceTopography/IO/__pycache__/IBW.cpython-36.pyc +0 -0
  27. SurfaceTopography/IO/__pycache__/MI.cpython-36.pyc +0 -0
  28. SurfaceTopography/IO/__pycache__/Matlab.cpython-36.pyc +0 -0
  29. SurfaceTopography/IO/__pycache__/NC.cpython-36.pyc +0 -0
  30. SurfaceTopography/IO/__pycache__/NPY.cpython-36.pyc +0 -0
  31. SurfaceTopography/IO/__pycache__/OPDx.cpython-36.pyc +0 -0
  32. SurfaceTopography/IO/__pycache__/Reader.cpython-36.pyc +0 -0
  33. SurfaceTopography/IO/__pycache__/__init__.cpython-36.pyc +0 -0
  34. SurfaceTopography/Interpolation.py +30 -0
  35. SurfaceTopography/Nonuniform/Autocorrelation.py +161 -0
  36. SurfaceTopography/Nonuniform/Detrending.py +87 -0
  37. SurfaceTopography/Nonuniform/PowerSpectrum.py +178 -0
  38. SurfaceTopography/Nonuniform/ScalarParameters.py +140 -0
  39. SurfaceTopography/Nonuniform/VariableBandwidth.py +155 -0
  40. SurfaceTopography/Nonuniform/__init__.py +28 -0
  41. SurfaceTopography/Nonuniform/__pycache__/Autocorrelation.cpython-36.pyc +0 -0
  42. SurfaceTopography/Nonuniform/__pycache__/Detrending.cpython-36.pyc +0 -0
  43. SurfaceTopography/Nonuniform/__pycache__/PowerSpectrum.cpython-36.pyc +0 -0
  44. SurfaceTopography/Nonuniform/__pycache__/ScalarParameters.cpython-36.pyc +0 -0
  45. SurfaceTopography/Nonuniform/__pycache__/VariableBandwidth.cpython-36.pyc +0 -0
  46. SurfaceTopography/Nonuniform/__pycache__/__init__.cpython-36.pyc +0 -0
  47. SurfaceTopography/Nonuniform/__pycache__/common.cpython-36.pyc +0 -0
  48. SurfaceTopography/Nonuniform/common.py +95 -0
  49. SurfaceTopography/NonuniformLineScan.py +310 -0
  50. SurfaceTopography/Special.py +225 -0
  51. SurfaceTopography/Tools/__init__.py +28 -0
  52. SurfaceTopography/Tools/__pycache__/__init__.cpython-36.pyc +0 -0
  53. SurfaceTopography/Tools/__pycache__/common.cpython-36.pyc +0 -0
  54. SurfaceTopography/Tools/common.py +75 -0
  55. SurfaceTopography/Uniform/Autocorrelation.py +211 -0
  56. SurfaceTopography/Uniform/Detrending.py +155 -0
  57. SurfaceTopography/Uniform/Filtering.py +226 -0
  58. SurfaceTopography/Uniform/Interpolation.py +159 -0
  59. SurfaceTopography/Uniform/PowerSpectrum.py +197 -0
  60. SurfaceTopography/Uniform/ScalarParameters.py +173 -0
  61. SurfaceTopography/Uniform/VariableBandwidth.py +169 -0
  62. SurfaceTopography/Uniform/__init__.py +28 -0
  63. SurfaceTopography/Uniform/__pycache__/Autocorrelation.cpython-36.pyc +0 -0
  64. SurfaceTopography/Uniform/__pycache__/Detrending.cpython-36.pyc +0 -0
  65. SurfaceTopography/Uniform/__pycache__/Filtering.cpython-36.pyc +0 -0
  66. SurfaceTopography/Uniform/__pycache__/Interpolation.cpython-36.pyc +0 -0
  67. SurfaceTopography/Uniform/__pycache__/PowerSpectrum.cpython-36.pyc +0 -0
  68. SurfaceTopography/Uniform/__pycache__/ScalarParameters.cpython-36.pyc +0 -0
  69. SurfaceTopography/Uniform/__pycache__/VariableBandwidth.cpython-36.pyc +0 -0
  70. SurfaceTopography/Uniform/__pycache__/__init__.cpython-36.pyc +0 -0
  71. SurfaceTopography/Uniform/__pycache__/common.cpython-36.pyc +0 -0
  72. SurfaceTopography/Uniform/common.py +260 -0
  73. SurfaceTopography/UniformLineScanAndTopography.py +887 -0
  74. SurfaceTopography/__init__.py +62 -0
  75. SurfaceTopography/__pycache__/Converters.cpython-36.pyc +0 -0
  76. SurfaceTopography/__pycache__/Generation.cpython-36.pyc +0 -0
  77. SurfaceTopography/__pycache__/HeightContainer.cpython-36.pyc +0 -0
  78. SurfaceTopography/__pycache__/Interpolation.cpython-36.pyc +0 -0
  79. SurfaceTopography/__pycache__/NonuniformLineScan.cpython-36.pyc +0 -0
  80. SurfaceTopography/__pycache__/Special.cpython-36.pyc +0 -0
  81. SurfaceTopography/__pycache__/UniformLineScanAndTopography.cpython-36.pyc +0 -0
  82. SurfaceTopography/__pycache__/__init__.cpython-36.pyc +0 -0
  83. SurfaceTopography/__pycache__/common.cpython-36.pyc +0 -0
  84. SurfaceTopography/common.py +111 -0
  85. _SurfaceTopography.cpython-36m-x86_64-linux-gnu.so +0 -0
  86. _SurfaceTopography.py +8 -0
  87. __pycache__/_SurfaceTopography.cpython-36.pyc +0 -0
  88. test/IO/__init__.py +28 -0
  89. test/IO/__pycache__/__init__.cpython-36.pyc +0 -0
  90. test/IO/__pycache__/test_ibw.cpython-36.pyc +0 -0
  91. test/IO/__pycache__/test_io.cpython-36.pyc +0 -0
  92. test/IO/__pycache__/test_mi.cpython-36.pyc +0 -0
  93. test/IO/__pycache__/test_netcdf.cpython-36.pyc +0 -0
  94. test/IO/__pycache__/test_npy.cpython-36.pyc +0 -0
  95. test/IO/__pycache__/test_opdx.cpython-36.pyc +0 -0
  96. test/IO/test_ibw.py +178 -0
  97. test/IO/test_io.py +401 -0
  98. test/IO/test_mi.py +103 -0
  99. test/IO/test_netcdf.py +134 -0
  100. test/IO/test_npy.py +135 -0
  101. test/IO/test_opdx.py +441 -0
  102. test/__init__.py +26 -0
  103. test/__pycache__/__init__.cpython-36.pyc +0 -0
  104. test/__pycache__/compare-orientation-with-gwyddion.cpython-36.pyc +0 -0
  105. test/__pycache__/conftest.cpython-36.pyc +0 -0
  106. test/__pycache__/test_autocorrelation.cpython-36.pyc +0 -0
  107. test/__pycache__/test_bicubic.cpython-36.pyc +0 -0
  108. test/__pycache__/test_filtering.cpython-36.pyc +0 -0
  109. test/__pycache__/test_generation.cpython-36.pyc +0 -0
  110. test/__pycache__/test_import.cpython-36.pyc +0 -0
  111. test/__pycache__/test_make_sphere.cpython-36.pyc +0 -0
  112. test/__pycache__/test_power_spectrum.cpython-36.pyc +0 -0
  113. test/__pycache__/test_scalar_parameters.cpython-36.pyc +0 -0
  114. test/__pycache__/test_topography1.cpython-36.pyc +0 -0
  115. test/__pycache__/test_topography2.cpython-36.pyc +0 -0
  116. test/__pycache__/test_variable_bandwidth.cpython-36.pyc +0 -0
  117. test/compare-orientation-with-gwyddion.py +102 -0
  118. test/conftest.py +80 -0
  119. test/test_autocorrelation.py +311 -0
  120. test/test_bicubic.py +190 -0
  121. test/test_filtering.py +69 -0
  122. test/test_generation.py +259 -0
  123. test/test_import.py +54 -0
  124. test/test_make_sphere.py +108 -0
  125. test/test_power_spectrum.py +259 -0
  126. test/test_scalar_parameters.py +176 -0
  127. test/test_topography1.py +1287 -0
  128. test/test_topography2.py +328 -0
  129. test/test_variable_bandwidth.py +107 -0
@@ -0,0 +1,328 @@
1
+ #
2
+ # Copyright 2019-2020 Lars Pastewka
3
+ # 2019-2020 Antoine Sanner
4
+ #
5
+ # ### MIT license
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #
25
+
26
+ import pickle
27
+ import unittest
28
+ import pytest
29
+
30
+ import numpy as np
31
+ from numpy.testing import assert_array_equal
32
+
33
+ from muFFT import FFT
34
+ from NuMPI.Tools import Reduction
35
+
36
+ from SurfaceTopography import Topography
37
+ from SurfaceTopography.Generation import fourier_synthesis
38
+ from SurfaceTopography.UniformLineScanAndTopography import (
39
+ DetrendedUniformTopography, UniformLineScan
40
+ )
41
+
42
+
43
+ def test_positions(comm):
44
+ nx, ny = (12 * comm.Get_size(), 10 * comm.Get_size() + 1)
45
+ sx = 33.
46
+ sy = 54.
47
+ fftengine = FFT((nx, ny), fft='mpi', communicator=comm)
48
+
49
+ surf = Topography(np.zeros(fftengine.nb_subdomain_grid_pts),
50
+ physical_sizes=(sx, sy),
51
+ decomposition='subdomain',
52
+ nb_grid_pts=(nx, ny),
53
+ subdomain_locations=fftengine.subdomain_locations,
54
+ communicator=comm)
55
+
56
+ x, y = surf.positions()
57
+ assert x.shape == fftengine.nb_subdomain_grid_pts
58
+ assert y.shape == fftengine.nb_subdomain_grid_pts
59
+
60
+ assert Reduction(comm).min(x) == 0
61
+ assert abs(Reduction(comm).max(x) - sx * (1 - 1. / nx)) \
62
+ < 1e-8 * sx / nx, "{}".format(x)
63
+ assert Reduction(comm).min(y) == 0
64
+ assert abs(Reduction(comm).max(y) - sy * (1 - 1. / ny)) < 1e-8
65
+
66
+
67
+ class TopographyTest(unittest.TestCase):
68
+
69
+ def test_positions_and_heights(self):
70
+ X = np.arange(3).reshape(1, 3)
71
+ Y = np.arange(4).reshape(4, 1)
72
+ h = X + Y
73
+
74
+ t = Topography(h, (8, 6))
75
+
76
+ self.assertEqual(t.nb_grid_pts, (4, 3))
77
+
78
+ assert_array_equal(t.heights(), h)
79
+ X2, Y2, h2 = t.positions_and_heights()
80
+ assert_array_equal(X2, [
81
+ (0, 0, 0),
82
+ (2, 2, 2),
83
+ (4, 4, 4),
84
+ (6, 6, 6),
85
+ ])
86
+ assert_array_equal(Y2, [
87
+ (0, 2, 4),
88
+ (0, 2, 4),
89
+ (0, 2, 4),
90
+ (0, 2, 4),
91
+ ])
92
+ assert_array_equal(h2, [
93
+ (0, 1, 2),
94
+ (1, 2, 3),
95
+ (2, 3, 4),
96
+ (3, 4, 5)])
97
+
98
+ #
99
+ # After detrending, the position and heights should have again
100
+ # just 3 arrays and the third array should be the same as .heights()
101
+ #
102
+ dt = t.detrend(detrend_mode='slope')
103
+
104
+ np.testing.assert_allclose(dt.heights(), [
105
+ (0, 0, 0),
106
+ (0, 0, 0),
107
+ (0, 0, 0),
108
+ (0, 0, 0)])
109
+
110
+ X2, Y2, h2 = dt.positions_and_heights()
111
+
112
+ assert h2.shape == (4, 3)
113
+ assert_array_equal(X2, [
114
+ (0, 0, 0),
115
+ (2, 2, 2),
116
+ (4, 4, 4),
117
+ (6, 6, 6),
118
+ ])
119
+ assert_array_equal(Y2, [
120
+ (0, 2, 4),
121
+ (0, 2, 4),
122
+ (0, 2, 4),
123
+ (0, 2, 4),
124
+ ])
125
+ np.testing.assert_allclose(h2, [
126
+ (0, 0, 0),
127
+ (0, 0, 0),
128
+ (0, 0, 0),
129
+ (0, 0, 0)])
130
+
131
+ def test_squeeze(self):
132
+ x = np.linspace(0, 4 * np.pi, 101)
133
+ y = np.linspace(0, 8 * np.pi, 103)
134
+ h = np.sin(x.reshape(-1, 1)) + np.cos(y.reshape(1, -1))
135
+ surface = Topography(h, (1.2, 3.2)).scale(2.0)
136
+ surface2 = surface.squeeze()
137
+ self.assertTrue(isinstance(surface2, Topography))
138
+ np.testing.assert_allclose(surface.heights(), surface2.heights())
139
+
140
+ def test_attribute_error(self):
141
+ X = np.arange(3).reshape(1, 3)
142
+ Y = np.arange(4).reshape(4, 1)
143
+ h = X + Y
144
+ t = Topography(h, (8, 6))
145
+
146
+ # nonsense attributes return attribute error
147
+ with self.assertRaises(AttributeError):
148
+ t.ababababababababa
149
+
150
+ #
151
+ # only scaled topographies have coeff
152
+ #
153
+ with self.assertRaises(AttributeError):
154
+ t.coeff
155
+
156
+ st = t.scale(1)
157
+
158
+ self.assertEqual(st.scale_factor, 1)
159
+
160
+ #
161
+ # only detrended topographies have detrend_mode
162
+ #
163
+ with self.assertRaises(AttributeError):
164
+ st.detrend_mode
165
+
166
+ dm = st.detrend(detrend_mode='height').detrend_mode
167
+ self.assertEqual(dm, 'height')
168
+
169
+ #
170
+ # this all should also work after pickling
171
+ #
172
+ t2 = pickle.loads(pickle.dumps(t))
173
+
174
+ with self.assertRaises(AttributeError):
175
+ t2.scale_factor
176
+
177
+ st2 = t2.scale(1)
178
+
179
+ self.assertEqual(st2.scale_factor, 1)
180
+
181
+ with self.assertRaises(AttributeError):
182
+ st2.detrend_mode
183
+
184
+ dm2 = st2.detrend(detrend_mode='height').detrend_mode
185
+ self.assertEqual(dm2, 'height')
186
+
187
+ #
188
+ # this all should also work after scaled+pickled
189
+ #
190
+ t3 = pickle.loads(pickle.dumps(st))
191
+
192
+ with self.assertRaises(AttributeError):
193
+ t3.detrend_mode
194
+
195
+ dm3 = t3.detrend(detrend_mode='height').detrend_mode
196
+ self.assertEqual(dm3, 'height')
197
+
198
+ def test_init_with_lists_calling_scale_and_detrend(self):
199
+ t = Topography(np.array([[1, 1, 1, 1],
200
+ [1, 1, 1, 1],
201
+ [1, 1, 1, 1]]), physical_sizes=(1, 1))
202
+
203
+ # the following commands should be possible without errors
204
+ st = t.scale(1)
205
+ st.detrend(detrend_mode='center')
206
+
207
+ def test_power_spectrum_1D(self):
208
+ X = np.arange(3).reshape(1, 3)
209
+ Y = np.arange(4).reshape(4, 1)
210
+ h = X + Y
211
+
212
+ t = Topography(h, (8, 6))
213
+
214
+ q1, C1 = t.power_spectrum_1D(window='hann')
215
+
216
+ # TODO add check for values
217
+
218
+
219
+ def test_translate(comm_self):
220
+ topography = Topography(np.array([[0, 1, 0], [0, 0, 0]]),
221
+ physical_sizes=(4., 3.))
222
+ print(topography.heights().shape)
223
+
224
+ assert (topography.translate(offset=(1, 0)).heights()
225
+ ==
226
+ np.array([[0, 0, 0],
227
+ [0, 1, 0]])).all()
228
+
229
+ assert (topography.translate(offset=(2, 0)).heights()
230
+ ==
231
+ np.array([[0, 1, 0],
232
+ [0, 0, 0]])).all()
233
+
234
+ assert (topography.translate(offset=(0, -1)).heights()
235
+ ==
236
+ np.array([[1, 0, 0],
237
+ [0, 0, 0]])).all()
238
+
239
+
240
+ def test_pipeline():
241
+ t1 = fourier_synthesis((511, 511), (1., 1.), 0.8, rms_height=1)
242
+ t2 = t1.detrend()
243
+ p = t2.pipeline()
244
+ assert isinstance(p[0], Topography)
245
+ assert isinstance(p[1], DetrendedUniformTopography)
246
+
247
+
248
+ def test_uniform_detrended_periodicity():
249
+ topography = Topography(np.array([[0, 1, 0], [0, 0, 0]]),
250
+ physical_sizes=(4., 3.), periodic=True)
251
+ assert topography.detrend("center").is_periodic
252
+ assert not topography.detrend("height").is_periodic
253
+ assert not topography.detrend("curvature").is_periodic
254
+
255
+
256
+ def test_passing_of_docstring():
257
+ from SurfaceTopography.Uniform.PowerSpectrum import power_spectrum_1D
258
+ topography = Topography(np.array([[0, 1, 0], [0, 0, 0]]),
259
+ physical_sizes=(4., 3.), periodic=True)
260
+ assert topography.power_spectrum_1D.__doc__ == power_spectrum_1D.__doc__
261
+
262
+
263
+ @pytest.mark.parametrize("periodic", (True, False))
264
+ def test_fill_undefined_data_linescan(periodic):
265
+ topography = UniformLineScan(np.array([1, np.nan, 4.]),
266
+ (1.,),
267
+ info=dict(test=1),
268
+ periodic=periodic,
269
+ )
270
+ assert topography.has_undefined_data
271
+
272
+ filled_topography = topography.fill_undefined_data(fill_value=-np.infty)
273
+ assert not filled_topography.has_undefined_data
274
+
275
+ assert filled_topography.physical_sizes == topography.physical_sizes
276
+ assert filled_topography.is_periodic == topography.is_periodic
277
+ assert filled_topography.info["test"] == 1
278
+
279
+
280
+ @pytest.mark.parametrize("periodic", (True, False))
281
+ def test_fill_undefined_data(periodic):
282
+ topography = Topography(np.array([[1, np.nan, 4.],
283
+ [3, 4, 5]]),
284
+ (1., 1.),
285
+ info=dict(test=1),
286
+ periodic=periodic,
287
+ )
288
+ assert topography.has_undefined_data
289
+
290
+ filled_topography = topography.fill_undefined_data(fill_value=-np.infty)
291
+ mask = np.ma.getmask(topography.heights())
292
+ nmask = np.logical_not(mask)
293
+ assert (filled_topography[nmask] == topography[nmask]).all()
294
+ assert (filled_topography[mask] == - np.infty).all()
295
+ assert not filled_topography.has_undefined_data
296
+
297
+ assert filled_topography.physical_sizes == topography.physical_sizes
298
+ assert filled_topography.is_periodic == topography.is_periodic
299
+ assert filled_topography.info["test"] == 1
300
+
301
+
302
+ def test_fill_undefined_data_parallel(comm):
303
+ np.random.seed(comm.rank)
304
+ local_data = np.random.uniform(size=(3, 1))
305
+ local_data[local_data > 0.9] = np.nan
306
+ if comm.rank == 0: # make sure we always have undefined data
307
+ local_data[0, 0] = np.nan
308
+ topography = Topography(local_data,
309
+ (1., 1.),
310
+ info=dict(test=1),
311
+ communicator=comm,
312
+ decomposition="subdomain",
313
+ nb_grid_pts=(3, comm.size),
314
+ subdomain_locations=(0, comm.rank)
315
+ )
316
+
317
+ filled_topography = topography.fill_undefined_data(fill_value=-np.infty)
318
+ assert topography.has_undefined_data
319
+ assert not filled_topography.has_undefined_data
320
+
321
+ mask = np.ma.getmask(topography.heights())
322
+ nmask = np.logical_not(mask)
323
+
324
+ reduction = Reduction(comm)
325
+
326
+ assert reduction.all(filled_topography[nmask] == topography[nmask])
327
+ assert reduction.all(filled_topography[mask] == - np.infty)
328
+ assert not filled_topography.has_undefined_data
@@ -0,0 +1,107 @@
1
+ #
2
+ # Copyright 2017, 2020 Lars Pastewka
3
+ # 2019-2020 Antoine Sanner
4
+ #
5
+ # ### MIT license
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in
15
+ # all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #
25
+ """
26
+ Test tools for variable bandwidth analysis.
27
+ """
28
+
29
+ import unittest
30
+
31
+ import numpy as np
32
+ import pytest
33
+
34
+ from NuMPI import MPI
35
+
36
+ from SurfaceTopography import Topography, UniformLineScan
37
+ from SurfaceTopography.Generation import fourier_synthesis
38
+
39
+ pytestmark = pytest.mark.skipif(
40
+ MPI.COMM_WORLD.Get_size() > 1,
41
+ reason="tests only serial funcionalities, please execute with pytest")
42
+
43
+
44
+ class TestVariableBandwidth(unittest.TestCase):
45
+
46
+ def test_checkerboard_detrend_1d(self):
47
+ arr = np.zeros([4])
48
+ arr[:2] = 1.0
49
+ outarr = UniformLineScan(arr, arr.shape).checkerboard_detrend((2,))
50
+ np.testing.assert_allclose(outarr, np.zeros([4]))
51
+
52
+ def test_checkerboard_detrend_2d(self):
53
+ arr = np.zeros([4, 4])
54
+ arr[:2, :2] = 1.0
55
+ outarr = Topography(arr, arr.shape).checkerboard_detrend((2, 2))
56
+ np.testing.assert_allclose(outarr, np.zeros([4, 4]))
57
+
58
+ arr = np.zeros([4, 4])
59
+ arr[:2, :2] = 1.0
60
+ arr[:2, 1] = 2.0
61
+ outarr = Topography(arr, arr.shape).checkerboard_detrend((2, 2))
62
+ np.testing.assert_allclose(outarr, np.zeros([4, 4]))
63
+
64
+ def test_checkerboard_detrend_with_no_subdivisions(self):
65
+ r = 32
66
+ x, y = np.mgrid[:r, :r]
67
+ h = 1.3 * x - 0.3 * y + 0.02 * x * x + 0.03 * y * y - 0.013 * x * y
68
+ t = Topography(h, (1, 1), periodic=False)
69
+ # This should be the same as a detrend with detrend_mode='height'
70
+ ut1 = t.checkerboard_detrend((1, 1))
71
+ ut2 = t.detrend().heights()
72
+ np.testing.assert_allclose(ut1, ut2)
73
+
74
+ def test_self_affine_topography_1d(self):
75
+ r = 16384
76
+ for H in [0.3, 0.8]:
77
+ t0 = fourier_synthesis((r,), (1,), H, rms_slope=0.1,
78
+ amplitude_distribution=lambda n: 1.0)
79
+
80
+ for t in [t0, t0.to_nonuniform()]:
81
+ mag, bwidth, rms = t.variable_bandwidth(
82
+ nb_grid_pts_cutoff=r // 32)
83
+ self.assertAlmostEqual(rms[0], t.detrend().rms_height())
84
+ np.testing.assert_allclose(bwidth, t.physical_sizes[0] / mag)
85
+ # Since this is a self-affine surface, rms(mag) ~ mag^-H
86
+ b, a = np.polyfit(np.log(mag[1:]), np.log(rms[1:]), 1)
87
+ # The error is huge...
88
+ self.assertTrue(abs(H + b) < 0.1)
89
+
90
+ def test_self_affine_topography_2d(self):
91
+ r = 2048
92
+ res = [r, r]
93
+ for H in [0.3, 0.8]:
94
+ t = fourier_synthesis(res, (1, 1), H, rms_slope=0.1,
95
+ amplitude_distribution=lambda n: 1.0)
96
+ mag, bwidth, rms = t.variable_bandwidth(nb_grid_pts_cutoff=r // 32)
97
+ self.assertAlmostEqual(rms[0], t.detrend().rms_height())
98
+ # Since this is a self-affine surface, rms(mag) ~ mag^-H
99
+ b, a = np.polyfit(np.log(mag[1:]), np.log(rms[1:]), 1)
100
+ # The error is huge...
101
+ self.assertTrue(abs(H + b) < 0.1)
102
+
103
+
104
+ ###
105
+
106
+ if __name__ == '__main__':
107
+ unittest.main()