foscat 3.0.9__py3-none-any.whl → 3.6.0__py3-none-any.whl
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.
- foscat/CNN.py +151 -0
- foscat/CircSpline.py +102 -34
- foscat/FoCUS.py +2363 -1052
- foscat/GCNN.py +239 -0
- foscat/Softmax.py +29 -20
- foscat/Spline1D.py +86 -36
- foscat/Synthesis.py +335 -262
- foscat/alm.py +690 -0
- foscat/alm_tools.py +11 -0
- foscat/backend.py +933 -588
- foscat/backend_tens.py +63 -0
- foscat/loss_backend_tens.py +48 -38
- foscat/loss_backend_torch.py +35 -41
- foscat/scat.py +1639 -1015
- foscat/scat1D.py +1256 -774
- foscat/scat2D.py +9 -7
- foscat/scat_cov.py +3067 -1541
- foscat/scat_cov1D.py +11 -1467
- foscat/scat_cov2D.py +9 -7
- foscat/scat_cov_map.py +77 -51
- foscat/scat_cov_map2D.py +79 -49
- foscat-3.6.0.dist-info/LICENCE +13 -0
- foscat-3.6.0.dist-info/METADATA +184 -0
- foscat-3.6.0.dist-info/RECORD +27 -0
- {foscat-3.0.9.dist-info → foscat-3.6.0.dist-info}/WHEEL +1 -1
- foscat/GetGPUinfo.py +0 -36
- foscat-3.0.9.dist-info/METADATA +0 -23
- foscat-3.0.9.dist-info/RECORD +0 -22
- {foscat-3.0.9.dist-info → foscat-3.6.0.dist-info}/top_level.txt +0 -0
foscat/CNN.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import pickle
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
import foscat.scat_cov as sc
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class CNN:
|
|
9
|
+
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
scat_operator=None,
|
|
13
|
+
nparam=1,
|
|
14
|
+
nscale=1,
|
|
15
|
+
chanlist=[],
|
|
16
|
+
in_nside=1,
|
|
17
|
+
n_chan_in=1,
|
|
18
|
+
nbatch=1,
|
|
19
|
+
SEED=1234,
|
|
20
|
+
filename=None,
|
|
21
|
+
):
|
|
22
|
+
|
|
23
|
+
if filename is not None:
|
|
24
|
+
outlist = pickle.load(open("%s.pkl" % (filename), "rb"))
|
|
25
|
+
self.scat_operator = sc.funct(KERNELSZ=outlist[3], all_type=outlist[7])
|
|
26
|
+
self.KERNELSZ = self.scat_operator.KERNELSZ
|
|
27
|
+
self.all_type = self.scat_operator.all_type
|
|
28
|
+
self.npar = outlist[2]
|
|
29
|
+
self.nscale = outlist[5]
|
|
30
|
+
self.chanlist = outlist[0]
|
|
31
|
+
self.in_nside = outlist[4]
|
|
32
|
+
self.nbatch = outlist[1]
|
|
33
|
+
self.n_chan_in = outlist[8]
|
|
34
|
+
self.x = self.scat_operator.backend.bk_cast(outlist[6])
|
|
35
|
+
self.out_nside = self.in_nside // (2**self.nscale)
|
|
36
|
+
else:
|
|
37
|
+
self.nscale = nscale
|
|
38
|
+
self.nbatch = nbatch
|
|
39
|
+
self.npar = nparam
|
|
40
|
+
self.n_chan_in = n_chan_in
|
|
41
|
+
self.scat_operator = scat_operator
|
|
42
|
+
if len(chanlist) != nscale + 1:
|
|
43
|
+
print(
|
|
44
|
+
"len of chanlist (here %d) should of nscale+1 (here %d)"
|
|
45
|
+
% (len(chanlist), nscale + 1)
|
|
46
|
+
)
|
|
47
|
+
return None
|
|
48
|
+
|
|
49
|
+
self.chanlist = chanlist
|
|
50
|
+
self.KERNELSZ = scat_operator.KERNELSZ
|
|
51
|
+
self.all_type = scat_operator.all_type
|
|
52
|
+
self.in_nside = in_nside
|
|
53
|
+
self.out_nside = self.in_nside // (2**self.nscale)
|
|
54
|
+
|
|
55
|
+
np.random.seed(SEED)
|
|
56
|
+
self.x = scat_operator.backend.bk_cast(
|
|
57
|
+
np.random.randn(self.get_number_of_weights())
|
|
58
|
+
/ (self.KERNELSZ * self.KERNELSZ)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def save(self, filename):
|
|
62
|
+
|
|
63
|
+
outlist = [
|
|
64
|
+
self.chanlist,
|
|
65
|
+
self.nbatch,
|
|
66
|
+
self.npar,
|
|
67
|
+
self.KERNELSZ,
|
|
68
|
+
self.in_nside,
|
|
69
|
+
self.nscale,
|
|
70
|
+
self.get_weights().numpy(),
|
|
71
|
+
self.all_type,
|
|
72
|
+
self.n_chan_in,
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
myout = open("%s.pkl" % (filename), "wb")
|
|
76
|
+
pickle.dump(outlist, myout)
|
|
77
|
+
myout.close()
|
|
78
|
+
|
|
79
|
+
def get_number_of_weights(self):
|
|
80
|
+
totnchan = 0
|
|
81
|
+
for i in range(self.nscale):
|
|
82
|
+
totnchan = totnchan + self.chanlist[i] * self.chanlist[i + 1]
|
|
83
|
+
return (
|
|
84
|
+
self.npar * 12 * self.out_nside**2 * self.chanlist[self.nscale]
|
|
85
|
+
+ totnchan * self.KERNELSZ * self.KERNELSZ
|
|
86
|
+
+ self.KERNELSZ * self.KERNELSZ * self.n_chan_in * self.chanlist[0]
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
def set_weights(self, x):
|
|
90
|
+
self.x = x
|
|
91
|
+
|
|
92
|
+
def get_weights(self):
|
|
93
|
+
return self.x
|
|
94
|
+
|
|
95
|
+
def eval(self, im, indices=None, weights=None):
|
|
96
|
+
|
|
97
|
+
x = self.x
|
|
98
|
+
ww = self.scat_operator.backend.bk_reshape(
|
|
99
|
+
x[0 : self.KERNELSZ * self.KERNELSZ * self.n_chan_in * self.chanlist[0]],
|
|
100
|
+
[self.KERNELSZ * self.KERNELSZ, self.n_chan_in, self.chanlist[0]],
|
|
101
|
+
)
|
|
102
|
+
nn = self.KERNELSZ * self.KERNELSZ * self.n_chan_in * self.chanlist[0]
|
|
103
|
+
|
|
104
|
+
im = self.scat_operator.healpix_layer(im, ww)
|
|
105
|
+
im = self.scat_operator.backend.bk_relu(im)
|
|
106
|
+
|
|
107
|
+
for k in range(self.nscale):
|
|
108
|
+
ww = self.scat_operator.backend.bk_reshape(
|
|
109
|
+
x[
|
|
110
|
+
nn : nn
|
|
111
|
+
+ self.KERNELSZ
|
|
112
|
+
* self.KERNELSZ
|
|
113
|
+
* self.chanlist[k]
|
|
114
|
+
* self.chanlist[k + 1]
|
|
115
|
+
],
|
|
116
|
+
[self.KERNELSZ * self.KERNELSZ, self.chanlist[k], self.chanlist[k + 1]],
|
|
117
|
+
)
|
|
118
|
+
nn = (
|
|
119
|
+
nn
|
|
120
|
+
+ self.KERNELSZ
|
|
121
|
+
* self.KERNELSZ
|
|
122
|
+
* self.chanlist[k]
|
|
123
|
+
* self.chanlist[k + 1]
|
|
124
|
+
)
|
|
125
|
+
if indices is None:
|
|
126
|
+
im = self.scat_operator.healpix_layer(im, ww)
|
|
127
|
+
else:
|
|
128
|
+
im = self.scat_operator.healpix_layer(
|
|
129
|
+
im, ww, indices=indices[k], weights=weights[k]
|
|
130
|
+
)
|
|
131
|
+
im = self.scat_operator.backend.bk_relu(im)
|
|
132
|
+
im = self.scat_operator.ud_grade_2(im, axis=0)
|
|
133
|
+
|
|
134
|
+
ww = self.scat_operator.backend.bk_reshape(
|
|
135
|
+
x[
|
|
136
|
+
nn : nn
|
|
137
|
+
+ self.npar * 12 * self.out_nside**2 * self.chanlist[self.nscale]
|
|
138
|
+
],
|
|
139
|
+
[12 * self.out_nside**2 * self.chanlist[self.nscale], self.npar],
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
im = self.scat_operator.backend.bk_matmul(
|
|
143
|
+
self.scat_operator.backend.bk_reshape(
|
|
144
|
+
im, [1, 12 * self.out_nside**2 * self.chanlist[self.nscale]]
|
|
145
|
+
),
|
|
146
|
+
ww,
|
|
147
|
+
)
|
|
148
|
+
im = self.scat_operator.backend.bk_reshape(im, [self.npar])
|
|
149
|
+
im = self.scat_operator.backend.bk_relu(im)
|
|
150
|
+
|
|
151
|
+
return im
|
foscat/CircSpline.py
CHANGED
|
@@ -1,51 +1,119 @@
|
|
|
1
|
+
import numpy as np
|
|
1
2
|
|
|
2
|
-
import math
|
|
3
3
|
|
|
4
4
|
class CircSpline:
|
|
5
5
|
def __init__(self, nodes, degree=3):
|
|
6
6
|
"""
|
|
7
|
-
|
|
7
|
+
Initializes the Spline1D instance.
|
|
8
|
+
|
|
9
|
+
Parameters:
|
|
10
|
+
- nodes (int): The number of nodes in the spline.
|
|
11
|
+
- degree (int): The degree of the spline. Default is 3.
|
|
8
12
|
"""
|
|
9
13
|
self.degree = degree
|
|
10
14
|
self.nodes = nodes
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def cubic_spline_function(self,x):
|
|
14
18
|
"""
|
|
15
|
-
|
|
19
|
+
Evaluate the cubic spline basis function.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
x (float or array): Input value(s) to evaluate the spline basis function.
|
|
23
|
+
|
|
24
|
+
Returns:
|
|
25
|
+
float or array: Result of the cubic spline basis function.
|
|
16
26
|
"""
|
|
17
|
-
return
|
|
27
|
+
return -2 * x**3 + 3 * x**2
|
|
18
28
|
|
|
19
|
-
|
|
29
|
+
|
|
30
|
+
def eval(self,x):
|
|
20
31
|
"""
|
|
21
|
-
Compute
|
|
32
|
+
Compute a 3rd-degree cubic spline with 4-point support.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
x (float or array): Input value(s) to compute the spline.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
indices (array): Indices of the spline support points.
|
|
39
|
+
coefficients (array): Normalized spline coefficients.
|
|
22
40
|
"""
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
N=self.nodes
|
|
42
|
+
|
|
43
|
+
if isinstance(x, float):
|
|
44
|
+
# Single scalar input
|
|
45
|
+
base_idx = int(x * (N))
|
|
46
|
+
indices = np.zeros([4], dtype="int")
|
|
47
|
+
coefficients = np.zeros([4])
|
|
48
|
+
else:
|
|
49
|
+
# Array input
|
|
50
|
+
base_idx = (x * (N)).astype("int")
|
|
51
|
+
indices = np.zeros([4, x.shape[0]], dtype="int")
|
|
52
|
+
coefficients = np.zeros([4, x.shape[0]])
|
|
53
|
+
|
|
54
|
+
# Compute the fractional part of the input
|
|
55
|
+
fractional_part = x * (N) - base_idx
|
|
56
|
+
|
|
57
|
+
# Compute spline coefficients for 4 support points
|
|
58
|
+
coefficients[3] = self.cubic_spline_function(fractional_part / 2) / 2
|
|
59
|
+
coefficients[2] = self.cubic_spline_function(0.5 + fractional_part / 2) / 2
|
|
60
|
+
coefficients[1] = self.cubic_spline_function(1 - fractional_part / 2) / 2
|
|
61
|
+
coefficients[0] = self.cubic_spline_function(0.5 - fractional_part / 2) / 2
|
|
62
|
+
|
|
63
|
+
# Assign indices for the support points
|
|
64
|
+
indices[3] = (base_idx + 2+N)%N
|
|
65
|
+
indices[2] = (base_idx + 1+N)%N
|
|
66
|
+
indices[1] = (base_idx + N )%N
|
|
67
|
+
indices[0] = (base_idx + N-1)%N
|
|
28
68
|
|
|
29
|
-
|
|
69
|
+
# Square coefficients and normalize
|
|
70
|
+
coefficients = coefficients * coefficients
|
|
71
|
+
coefficients /= np.sum(coefficients, axis=0)
|
|
72
|
+
|
|
73
|
+
return indices, coefficients
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def eval_N(self,x,N):
|
|
30
77
|
"""
|
|
31
|
-
|
|
78
|
+
Compute a 3rd-degree cubic spline with 4-point support.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
x (float or array): Input value(s) to compute the spline.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
indices (array): Indices of the spline support points.
|
|
85
|
+
coefficients (array): Normalized spline coefficients.
|
|
32
86
|
"""
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
87
|
+
|
|
88
|
+
if isinstance(x, float):
|
|
89
|
+
# Single scalar input
|
|
90
|
+
base_idx = int(x * (N))
|
|
91
|
+
indices = np.zeros([4], dtype="int")
|
|
92
|
+
coefficients = np.zeros([4])
|
|
93
|
+
else:
|
|
94
|
+
# Array input
|
|
95
|
+
base_idx = (x * (N)).astype("int")
|
|
96
|
+
indices = np.zeros([4, x.shape[0]], dtype="int")
|
|
97
|
+
coefficients = np.zeros([4, x.shape[0]])
|
|
98
|
+
|
|
99
|
+
# Compute the fractional part of the input
|
|
100
|
+
fractional_part = x * (N) - base_idx
|
|
101
|
+
|
|
102
|
+
# Compute spline coefficients for 4 support points
|
|
103
|
+
coefficients[3] = self.cubic_spline_function(fractional_part / 2) / 2
|
|
104
|
+
coefficients[2] = self.cubic_spline_function(0.5 + fractional_part / 2) / 2
|
|
105
|
+
coefficients[1] = self.cubic_spline_function(1 - fractional_part / 2) / 2
|
|
106
|
+
coefficients[0] = self.cubic_spline_function(0.5 - fractional_part / 2) / 2
|
|
107
|
+
|
|
108
|
+
# Assign indices for the support points
|
|
109
|
+
indices[3] = (base_idx + 2+N)%N
|
|
110
|
+
indices[2] = (base_idx + 1+N)%N
|
|
111
|
+
indices[1] = (base_idx + N )%N
|
|
112
|
+
indices[0] =( base_idx + N-1)%N
|
|
113
|
+
|
|
114
|
+
# Adjust indices to start from 0
|
|
115
|
+
# Square coefficients and normalize
|
|
116
|
+
coefficients = coefficients * coefficients
|
|
117
|
+
coefficients /= np.sum(coefficients, axis=0)
|
|
51
118
|
|
|
119
|
+
return indices, coefficients
|