-
Notifications
You must be signed in to change notification settings - Fork 2
/
maths.py
executable file
·1185 lines (950 loc) · 34.8 KB
/
maths.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Module maths provides useful mathematic tools.
"""
import numpy as np
import math
from operator import itemgetter
from scipy import interpolate
from copy import deepcopy
from itertools import product
from functools import partial
from multiprocessing import Pool
def relative_positions(positions, point, box_size):
"""
Returns relative positions to point in box of extent
(-box_size/2, box_size) in both dimensions of space.
Parameters
----------
positions : float array
Position of single point or array of positions.
point : float array
Position of the new centre.
box_size : float or array
Length of the box in one dimension or all dimensions.
Returns
-------
rel_positions : float array
Relative positions.
"""
return (np.array(positions) - np.array(point)
+ np.array(box_size)/2)%np.array(box_size) - np.array(box_size)/2
def wo_mean(arr):
"""
Returns deviation of values in array with respect to mean of array.
Parameters
----------
arr : array like
Array of values.
Returns
-------
dev_arr : array like
Deviations from mean of array.
"""
return np.array(arr) - np.mean(arr, axis=0)
class DictList(dict):
"""
Custom hash table class to give value [] to uninitialised keys.
"""
def __init__(self):
super().__init__()
def __getitem__(self, key):
try:
return super().__getitem__(key)
except KeyError:
return []
def g2Dto1D(g2D, L):
"""
Returns cylindrical average of 2D grid.
Parameters
----------
g2D : 2D array
2D grid.
NOTE: g2D[0, 0] is considered the r=0 point on the grid, and we
consider periodic boundaries.
L : float or float array
Length of the box represented by the grid in one dimension or all
dimensions.
Returns
-------
g1D : Numpy array
Array of (r, g1D(r)) with g1D(r) the averaged 2D grid at radius r.
"""
g2D = np.array(g2D)
dL = np.array(L)/np.array(g2D.shape) # boxes separation in each direction
r_max = np.min(L)/2 # maximum radius to be calculated in number of boxes
g1D_dic = DictList() # hash table of radii and values at radii
for i in range(g2D.shape[0]):
for j in range(g2D.shape[1]):
radius = np.sqrt(np.sum((np.array((i, j))*dL)**2)) # radius corresponding to coordinates [i, j], [-i, j], [i, -j], [-i, -j]
if radius <= r_max:
g1D_dic[radius] += [g2D[i, j], g2D[-i, j], g2D[i, -j],
g2D[-i, -j]]
return np.array(list(map(
lambda radius: [radius, np.mean(g1D_dic[radius])],
sorted(g1D_dic))))
def g2Dto1Dsquare(g2D, L):
"""
Returns cylindrical average of square 2D grid.
Parameters
----------
g2D : 2D array
Square 2D grid.
NOTE: g2D[0, 0] is considered the r=0 point on the grid, and we
consider periodid boundaries.
L : float
Length of the box represented by the grid in one dimension.
Returns
-------
g1D : Numpy array
Array of (r, g1D(r)) with g1D(r) the averaged 2D grid at radius r.
"""
g2D = np.array(g2D)
dL = L/g2D.shape[0] # boxes separation in each direction
sq_r_max = (g2D.shape[0]/2)**2 # maximum radius to be calculated in number of boxes
g1D_dic = DictList() # hash table of radii and values at radii
for i in range(g2D.shape[0]):
for j in range(g2D.shape[1]):
sqradius = i**2 + j**2 # radius corresponding to coordinates [i, j], [-i, j], [i, -j], [-i, -j]
if sqradius <= sq_r_max:
g1D_dic[sqradius] += [g2D[i, j], g2D[-i, j], g2D[i, -j],
g2D[-i, -j]]
return np.array(list(map(
lambda sqradius: [dL*np.sqrt(sqradius), np.mean(g1D_dic[sqradius])],
sorted(g1D_dic))))
def g2Dto1Dgrid(g2D, grid, average_grid=False):
"""
Returns cylindrical average of square 2D grid with values of radius given
by other parameter grid.
Parameters
----------
g2D : 2D array
Square 2D grid.
grid : 2D array
Array of radii.
average_grid : bool
Return g2D grid with cylindrically averaged values.
Returns
-------
g1D : Numpy array
Array of (r, g1D(r)) with g1D(r) the averaged 2D grid at radius r.
g2D_cylindrical [average_grid] : Numpy array
Cylindrically averaged g2D.
"""
g2D = np.array(g2D)
grid = np.array(grid)
g1D_dic = DictList() # hash table of radii and values at radii
for i in range(g2D.shape[0]):
for j in range(g2D.shape[1]):
g1D_dic[grid[i, j]] += [g2D[i, j]]
g1D = np.array(list(map(
lambda radius: [radius, np.mean(g1D_dic[radius])],
sorted(g1D_dic))))
if not(average_grid): return g1D
g2D_cylindrical = np.zeros(grid.shape)
for radius, mean_g in zip(*np.transpose(g1D)):
for i, j in zip(*np.where(grid == radius)):
g2D_cylindrical[i, j] = mean_g
return g1D, g2D_cylindrical
def normalise1D(*vector):
"""
Returs 1D vector of unitary norm with same direction.
Parameters
----------
vector : 1D array-like or coordinates as positional arguments
Vector to normalise.
Returns
-------
u_vector : 1D Numpy array
Unitary vector with same direction.
"""
vector = np.array(vector).flatten() # 1D vector
norm = np.linalg.norm(vector) # vector norm
if norm == 0: return vector # vector is 0
return vector/norm
def amplogwidth(arr, factor=2):
"""
Calculates the amplitudes of elements in array arr and, excluding the
zeros, returns the mean of the logarithms of these amplitudes plus and
minus factor times their standard deviation.
Parameters
----------
arr : array like
Array.
factor : float
Width factor. (default: 2)
Returns
-------
min : float
E(log(||arr||)) - factor*V(log(||arr||))
max : float
E(log(||arr||)) + factor*V(log(||arr||))
"""
log = np.ma.log10(np.sqrt(np.sum(arr**2, axis=-1))) # logarithms of amplitudes
mean = log.mean() # means of logarithms of amplitudes
std = log.std() # standard deviation of logarithms of amplitudes
return mean - factor*std, mean + factor*std
def mean_sterr(values):
"""
Returns mean and standard error of values.
Parameters
----------
values : float array
Values.
Returns
-------
mean : float
Mean of values.
sterr : float
Standard error of values.
"""
values = np.array(values)
if values.size == 0: return None, None
return np.mean(values), np.std(values)/np.sqrt(np.prod(values.shape))
class Grid:
"""
Manipulate 2D grids, in which we consider the values to correspond to a
variable at uniformly distributed positions in space.
"""
def __init__(self, grid, extent=(-1, 1, -1, 1)):
"""
Sets the grid.
Parameters
----------
grid : array-like
2D grid.
extent : scalars (left, right, bottom, top)
Values of space variables at corners. (default: (-1, 1, -1, 1))
"""
self.grid = np.array(grid)
self.shape = self.grid.shape # shape of the grid
self.extent = extent
self.box_size = np.array([
self.extent[1] - self.extent[0],
self.extent[-1] - self.extent[-2]])
self.sep_boxes = self.sep_boxes_x, self.sep_boxes_y =\
self.box_size/self.shape[:2] # distance between consecutive boxes in each direction
def __getitem__(self, *key):
"""
Associates Grid[key] to Grid.grid[key].
Parameters
----------
key : *
Key to access.
Returns
-------
value : *
Grid.grid[key]
"""
return self.grid.__getitem__(*key)
def get_grid_indexes(self):
"""
Returns grid of self.grid indexes and saves them in attributes
self.grid_indexes.
Returns
-------
grid_index : Numpy array
Grid of self.grid indexes.
"""
self.grid_indexes = vector_vector_grid(
range(self.shape[0]), range(self.shape[1]), dtype=int)
return self.grid_indexes
def get_grid_coordinates(self):
"""
Returns grid of self.grid cartesiancoordinates and saves them in
attributes self.grid_coordinates.
Returns
-------
grid_coordinates : Numpy array
Grid of self.grid cartesian coordinates.
"""
self.grid_coordinates = np.transpose(vector_vector_grid(
self.extent[0] + self.sep_boxes_x/2
+ np.arange(self.sep_boxes_x*self.shape[0],
step=self.sep_boxes_x),
(self.extent[-2] + self.sep_boxes_y/2
+ np.arange(self.sep_boxes_y*self.shape[0],
step=self.sep_boxes_y))[::-1]),
(1, 0, 2))
return self.grid_coordinates
def get_grid_coordinates_polar(self):
"""
Returns grid of self.grid cartesiancoordinates and saves them in
attributes self.grid_coordinates.
Returns
-------
grid_coordinates_polar : Numpy array
Grid of self.grid cartesian coordinates.
"""
self.get_grid_coordinates()
radii = np.sqrt(np.sum(self.grid_coordinates**2, axis=-1))
angles = np.reshape(list(map(lambda x, y: math.atan2(y, x),
*np.transpose(np.reshape(self.grid_coordinates,
(np.prod(self.grid_coordinates.shape[:2]), 2))))),
self.grid_coordinates.shape[:2])
self.grid_coordinates_polar = np.concatenate((
np.reshape(radii, (*radii.shape, 1)),
np.reshape(angles, (*angles.shape, 1))
), axis=-1)
return self.grid_coordinates_polar
def in_grid(self, x, y):
"""
Indicates if point (x, y) in cartesian coordinates is in grid.
Parameters
----------
x : float
x-coordinate
y : float
y-coordinate
Returns
-------
is_in_grid : bool
(x, y) in grid.
"""
return (x >= self.extent[0] and x <= self.extent[1]
and y >= self.extent[2] and y <= self.extent[3])
def get_value_cartesian(self, x, y, linear_interpolation=False):
"""
Get value of grid at position in cartesian coordinates.
Parameters
----------
x : float
x-coordinate
y : float
y-coordinate
linear_interpolation : bool
Get value by linear interpolation of neighbouring grid boxes.
(default: False)
Returns
-------
value : *
Value at (x, y) with or without linear interpolation.
"""
if not(self.in_grid(x, y)): return None # point not on grid
index_y = int((x - self.extent[0])//self.sep_boxes_x)%self.shape[0] # index correponding to second axis of grid
index_x = -1-int((y - self.extent[2])//self.sep_boxes_y)%self.shape[1] # index correponding to first axis of grid
if not(linear_interpolation): return self.grid[index_x, index_y]
try:
nearest_box_pos = self.grid_coordinates[index_x, index_y] # nearest box position
except AttributeError:
nearest_box_pos = self.get_grid_coordinates()[index_x, index_y] # nearest box position
neighbouring_boxes = neighbouring_boxes_2D(
(index_x, index_y), self.shape)
neighbours_values = itemgetter(*neighbouring_boxes)(self.grid)
neighbours_relative_positions = (nearest_box_pos[::-1] +
(np.array(neighbouring_boxes_2D((1, 1), 3)) - 1)
*self.sep_boxes[::-1])[:, ::-1] # positions of neighbouring boxes
return interpolate.interp2d(
*np.transpose(neighbours_relative_positions), neighbours_values,
kind='linear')(x, y)[0]
def get_value_polar(self, r, angle, centre=(0, 0),
linear_interpolation=False):
"""
Get value of grid at position in polar coordinates.
Parameters
----------
r : float
Radius from centre.
angle : float
Angle from x-direction.
centre : float tuple
Origin for calculation. (default: (0, 0))
linear_interpolation : bool
Get value by linear interpolation of neighbouring grid boxes.
(default: False)
Returns
-------
value : *
Value at (r, angle) from centre with or without linear
interpolation.
"""
x = centre[0] + r*np.cos(angle) # corresponding cartesian x-coordinate
y = centre[1] + r*np.sin(angle) # corresponding cartesian y-coordinate
return self.get_value_cartesian(x, y,
linear_interpolation=linear_interpolation)
class GridFFT(Grid):
"""
Manipulate 2D grids, in which we consider the values to correspond to a
variable at uniformly distributed positions in space, and their fast Fourier
transforms.
"""
def __init__(self, grid, d=1):
"""
Sets the grid and its Fourier transform.
Parameters
----------
grid : array-like
2D grid.
d : float
Sample spacing. (default: 1)
"""
self.d = d
self.shape = np.array(grid).shape
super().__init__(grid, extent=
(-self.d*self.shape[0]/2, self.d*self.shape[0]/2,
-self.d*self.shape[1]/2, self.d*self.shape[1]/2)) # initialises superclass
self.gridFFT = np.fft.fft2(self.grid, axes=(0, 1)) # grid FFT
self.FFT2Dfilter = FFT2Dfilter(self.gridFFT, d=self.d) # signal filter
def gaussian_filter(self, sigma):
"""
Apply a Gaussian filter on the 2D grid.
(see active_particles.maths.FFT2Dfilter.gaussian_filter)
Parameters
----------
sigma : float
Standard deviation \\sigma of the convoluting Gaussian function.
Returns
-------
filteredGrid : Numpy array
Filtered grid.
"""
return self.FFT2Dfilter.gaussian_filter(sigma).get_signal()
def vector_vector_grid(vector1, vector2, dtype=None):
"""
From vector1 = (v1_i)_i and vector2 = (v2_i)_i, returns matrix
M = (M_{i, j})_{i, j} = ((v1_i, v2_j))_{i, j}.
Parameters
----------
vector1 : 1D array-like
Vector 1.
vector2 : 1D array-like
Vector 2.
dtype : Numpy array dtype
Data type of the Numpy array to return. (default: None)
NOTE: if dtype == None, then the array is not converted to any type.
Returns
-------
M : 2D array-like
Matrix M.
"""
M = np.zeros((len(vector1), len(vector2), 2))
M[:, :, 0] = vector1
M = np.transpose(M, (1, 0, 2))
M[:, :, 1] = vector2
if dtype != None: return M.astype(dtype)
else: return M
def wave_vectors_2D(nx, ny, d=1):
"""
Returns wave vectors for 2D signals with window lengths nx and ny in the
two directions and sample spacing d.
Parameters
----------
nx : int
Window length in first direction.
ny : int
Window length in second direction.
d : float
Sample spacing. (default: 1)
Returns
-------
wave_vectors : (nx, ny, 2) Numpy array
Grid of wave vectors.
"""
return 2*np.pi*vector_vector_grid(
np.fft.fftfreq(nx, d=d),
np.fft.fftfreq(ny, d=d))
def kFFTgrid(grid):
"""
Calculates the Fast Fourier Transform (FFT) of 2D grid and returns its dot
and cross product with corresponding normalised wave vector.
Parameters
----------
grid : array-like
2D grid of 2D vectors (i.e., (_, _, 2) grid).
Returns
-------
k_cross_grid : grid.shape Numpy array
Grid of cross products between normalised wave vectors and grid Fourier
transform.
k_dot_grid : grid.shape Numpy array
Grid of dot products between normalised wave vectors and grid Fourier
transform.
"""
FFTgrid = np.fft.fft2(grid, axes=(0, 1)) # Fourier transform of grid
wave_vectors = wave_vectors_2D(*grid.shape[:2]) # grid of wave vectors
wave_vectors_norm = np.sqrt(np.sum(wave_vectors**2, axis=-1)) # grid of wave vectors norm
k_cross_grid = np.cross(wave_vectors, FFTgrid) # k cross FFTgrid
k_dot_grid = np.zeros(FFTgrid.shape[:2], dtype=np.complex128)
for i in range(FFTgrid.shape[0]):
for j in range(FFTgrid.shape[1]):
k_dot_grid[i, j] = np.dot(wave_vectors[i, j],
FFTgrid[i, j]) # k dot FFTgrid
return (divide_arrays(k_cross_grid, wave_vectors_norm),
divide_arrays(k_dot_grid, wave_vectors_norm))
def divide_arrays(array1, array2):
"""
Divide array1 by array2, and outputs 0 values where array2 is equal to 0.
NOTE: array1, array2 and out must have the same shapes.
Parameters
----------
array1 : array-like
Numerator array.
array2 : array-like
Denominator array.
Returns
-------
array : array-like
Quotient array.
"""
if not(isinstance(array1, np.ndarray)): array1 = np.array(array1)
if not(isinstance(array2, np.ndarray)): array2 = np.array(array2)
return np.divide(array1, array2,
out=np.zeros(array1.shape, dtype=array1.dtype), where=array2!=0)
def grid_from_function(grid_values, function, dimension=None):
"""
Returns grid of dimension dimension from function evaluated at
grid_values.
NOTE: in 1D, use list(map(function, grid_values)).
Parameters
----------
grid_values : array-like
Grid of values at which to evaluate function.
function : function
Function of grid values variables.
dimension : int or None
Dimension of the grid to return. (default: None)
NOTE: None is considered as dimension equal to dimension of
grid_values.
NOTE : dimension can be lesser than grid_values dimension, in this case
values in grid_values remaining dimensions are passed as positional
parameters to function.
Returns
-------
grid : Numpy array
Grid created from function evaluated at grid_values.
"""
grid_values = np.array(grid_values)
grid_values_dimension = len(grid_values.shape) # dimension of grid_values
if dimension == None or dimension > grid_values_dimension:
dimension = grid_values_dimension
grid_shape = grid_values.shape[:dimension] # shape of grid
values_length = np.prod(grid_shape) # number of elements in grid
grid = np.array(list(map(
lambda value: function(*np.array(value).flatten()),
np.reshape(grid_values,
(values_length, *grid_values.shape[dimension:])))))
return np.reshape(grid, grid_shape + grid.shape[1:])
def step_function(X, Y):
"""
Returns step function f from array-likes X and Y, such that
| Y[0] if x <= X[0]
f(x) = | Y[i + 1] if X[i] < x <= X[i + 1]
| Y[-1] if x >= X[-1]
NOTE: X and Y must have the same shape.
Parameters
----------
X : 1D array-like
x-coordinates.
Y : 1D array-like
y-coordinates.
Returns
-------
f : lambda function
f function.
"""
return lambda x: Y[next((i for i in range(len(X)) if X[i] >= x), -1)]
class FFT2Dfilter:
"""
Filter 2D signal from Fourier components obtained via fast Fourier
transform.
/!\\ WARNING /!\\
Using bandlimiting low-pass filters self.cut_high_wave_frequencies and
self.cut_low_wave_lengths may cause ringing artifacts (see
https://en.wikipedia.org/wiki/Ringing_artifacts).
"""
def __init__(self, signalFFT, d=1, **kwargs):
"""
Defines wave vectors corresponding to input FFT.
NOTE: It is assumed that the order of Fourier components has not been
altered from the np.fft.fft2 function.
Parameters
----------
signalFFT : 2D array-like
Fast Fourier transform of signal.
d : float
Sample spacing. (default: 1)
Optional keyword arguments
--------------------------
wave_vectors : (*signalFFT.shape, 2) array-like
Wave vectors corresponding to signalFFT components.
"""
self.signalFFT = np.array(signalFFT) # signal fast Fourier transform
if not('wave_vectors' in kwargs): # no input wave vectors
self.wave_vectors = wave_vectors_2D(*self.signalFFT.shape[:2], d=d) # wave vectors corresponding to signalFFT components
else: self.wave_vectors = np.array(kwargs['wave_vectors'])
self.wave_vectors_norm = np.sqrt(np.sum(self.wave_vectors**2, axis=-1)) # wave vectors norm
def get_signal(self):
"""
Gets signal defined by the Fourier components in self.signalFFT.
Returns
-------
signal : Numpy array
Signal from inverse fast Fourier transform.
"""
return np.fft.ifft2(self.signalFFT, axes=(0, 1))
def cut_low_wave_frequencies(self, threshold):
"""
Sets Fourier components corresponding to wave vectors corresponding to
frequencies lower than threshold to 0.
Parameters
----------
threshold : float
Threshold frequency for cutting.
Returns
-------
filteredFFT : active_particles.maths.FFT2Dfilter
Filtered Fourier components.
"""
filteredFFT = deepcopy(self)
filteredFFT.signalFFT[filteredFFT.wave_vectors_norm < threshold] = 0 # cut low wave frequencies
return filteredFFT
def cut_high_wave_frequencies(self, threshold):
"""
Sets Fourier components corresponding to wave vectors corresponding to
frequencies higher than threshold to 0.
Parameters
----------
threshold : float
Threshold frequency for cutting.
Returns
-------
filteredFFT : active_particles.maths.FFT2Dfilter
Filtered Fourier components.
"""
filteredFFT = deepcopy(self)
filteredFFT.signalFFT[filteredFFT.wave_vectors_norm > threshold] = 0 # cut high wave frequencies
return filteredFFT
def cut_low_wave_lengths(self, threshold):
"""
Sets Fourier components corresponding to wave vectors corresponding to
lengths lower than threshold to 0.
Parameters
----------
threshold : float
Threshold length for cutting.
Returns
-------
filteredFFT : active_particles.maths.FFT2Dfilter
Filtered Fourier components.
"""
return self.cut_high_wave_frequencies(np.divide(2*np.pi, threshold))
def cut_high_wave_lengths(self, threshold):
"""
Sets Fourier components corresponding to wave vectors corresponding to
lengths higher than threshold to 0.
Parameters
----------
threshold : float
Threshold length for cutting.
Returns
-------
filteredFFT : active_particles.maths.FFT2Dfilter
Filtered Fourier components.
"""
return self.cut_low_wave_frequencies(np.divide(2*np.pi, threshold))
def gaussian_filter(self, sigma):
"""
Multiply signal FFT with Gaussian function
exp(-1/2 \\sigma^2\\vec{k^2}) of wave vectors \\vec{k}, such that the
resulting signal is a convolution of the original signal with the
normalised Gaussian function
1/(2\\pi\\sigma^2) exp(-\\vec{r}^2/(2\\sigma^2)) of space variable
\\vec{r}.
Parameters
----------
sigma : float
Standard deviation \\sigma of the convoluting Gaussian function.
Returns
-------
filteredFFT : active_particles.maths.FFT2Dfilter
Filtered Fourier components.
"""
filteredFFT = deepcopy(self)
if sigma == 0: return filteredFFT # no filtering
gaussian_coefficients = np.exp(
-1/2*(sigma**2)*np.sum(self.wave_vectors**2, axis=-1))
try:
filteredFFT.signalFFT *= gaussian_coefficients
except ValueError:
filteredFFT.signalFFT *= np.reshape(gaussian_coefficients,
gaussian_coefficients.shape + (1,))
return filteredFFT
def count(arrays, max_norm):
"""
Returns number of arrays in array which norm is lesser than or equal to
max_norm in infite norm.
Parameters
----------
arrays : 2D array-like
Arrays.
max_norm : float
Maximum norm.
Returns
-------
N : int
Number of arrays which norm is lesser than or equal to max_norm in
infinite norm.
"""
return np.sum((abs(np.array(arrays)) <= max_norm).all(axis=-1))
def gaussian_smooth_1D(X, Y, sigma, *x):
"""
From y-coordinates Y at corresponding x-coordinates X, this function
returns smoothed y-coordinates with smoothing function exp(-(x/sigma)^2)
at x-coordinates x.
Parameters
----------
X : array-like
Input x-coordinates.
Y : array-like
Input y-coordinates.
sigma : float
Smoothing length scale.
NOTE: if sigma == 0 or None, a linear interpolation is performed.
x : float
Output x-coordinates.
NOTE: if no x is passed, then smoothed y-coordinates are returned at X.
Returns
-------
smoothedY : array-like
Smoothed y-coordinates.
"""
X = np.array(X)
Y = np.array(Y)
if x == (): x = X
else: x = np.array(x)
if sigma == 0 or sigma == None: # perform linear interpolation
return interpolate.interp1d(X, Y,
kind='linear', fill_value='extrapolate')(x)
smoothing_function = lambda x: np.exp(-(x/sigma)**2)
smoothedY = np.empty(len(x))
for index in range(len(x)):
smoothing_coefficients = list(map(smoothing_function, X - x[index]))
smoothedY[index] =\
np.sum(Y*smoothing_coefficients)/np.sum(smoothing_coefficients)
return smoothedY
def gaussian_smooth_2D(X, Y, Z, sigma, *xy):
"""
From z-coordinates Z at corresponding pairs of x-coordinates X and
y-coordinates Y, this function returns smoothed z-coordinates with
smoothing function exp(-(x^2 + y^2)/sigma^2) at xy-coordinates xy.
Parameters
----------
X : array-like
Input x-coordinates.
Y : array-like
Input y-coordinates.
Z : array-like
Input z-coordinates.
sigma : float
Smoothing length scale.
NOTE: if sigma == 0 or None, a linear interpolation is performed.
xy : 2-uple of float
Output xy-coordinates as 2-uples (x, y).
NOTE: if no xy are passed, then smoothed xy-coordinates are returned
at X and Y.
Returns
-------
smoothedZ : array-like
Smoothed z-coordinates.
"""
XY = np.vstack((X, Y)).T
Z = np.array(Z)
if xy == (): xy = XY
else: xy = np.array(xy)
if sigma == 0 or sigma == None: # perform linear interpolation
return np.array(list(map(
lambda x, y: interpolate.griddata(XY, Z, [(x, y)],
method='linear')[0],
*xy.T)))
smoothing_function = lambda x, y: np.exp(-(x**2 + y**2)/(sigma**2))
smoothedZ = np.empty(len(xy))
for index in range(len(xy)):
smoothing_coefficients = list(map(smoothing_function,
*(XY - xy[index]).T))
smoothedZ[index] =\
np.sum(Z*smoothing_coefficients)/np.sum(smoothing_coefficients)
return smoothedZ
def neighbouring_boxes_2D(index, shape):
"""
In a 2D grid of shape shape with periodic boundaries, this function returns
the 9 neighbouring boxes indexes of box with index index, including index.
Parameters
----------
index : 2-uple of int
Index of the box to get neighbours of.
shape : 2-uple of int or int
Number of boxes in all or one direction.
Returns
-------
neighbours : list of 2-uple
List of indexes of neighbouring boxes.
"""
index = np.array(index, dtype=int)
shape = (lambda shape: np.array([shape[0], shape[-1]], dtype=int))(
np.array(shape, ndmin=1))
neighbours = []
for inc_x in [-1, 0, 1]: # increment in x index
for inc_y in [-1, 0, 1]: # increment in y index
inc = np.array([inc_x, inc_y], dtype=int)
neighbours += [tuple((index + inc + shape)%shape)]
return neighbours
class Histogram:
"""
Make histogram from lists of float values.
"""
def __init__(self, Nbins, vmin, vmax, log=False):
"""
Parameters
----------
Nbins : int
Number of histogram bins.