@@ -55,12 +55,10 @@ def Fields2Components(results):
55
55
comps2 = _split_comps (key2 )
56
56
for i , comp1 in enumerate (comps1 ):
57
57
for j , comp2 in enumerate (comps2 ):
58
- #if i <= j:
59
- # Only save the upper triangle
60
58
_a1 , _b1 , _i1 , _j1 = comp1
61
59
_a2 , _b2 , _i2 , _j2 = comp2
62
- covkey = (_a1 , _b1 , _a2 , _b2 , _i1 , _j1 , _i2 , _j2 )
63
60
_r = r .array
61
+ covkey = (_a1 , _b1 , _a2 , _b2 , _i1 , _j1 , _i2 , _j2 )
64
62
_results [covkey ] = Result (_r [..., i , j , :, :], ell )
65
63
else :
66
64
raise ValueError (
@@ -69,6 +67,64 @@ def Fields2Components(results):
69
67
return _results
70
68
71
69
70
+ def Components2Fields (results ):
71
+ _results = {}
72
+ keys = list (results .keys ())
73
+ naxis = np .unique ([len (result .axis ) for result in results .values ()])
74
+ if len (naxis ) != 1 :
75
+ raise ValueError ("Different types of results in the same dictionary." )
76
+ naxis = naxis [0 ]
77
+ if naxis == 1 :
78
+ # We are dealing with Cls
79
+ # find unique fields in comps
80
+ keys = set ([_unsplit_comps (key ) for key in keys ])
81
+ keys = sorted (keys )
82
+ for key in keys :
83
+ # get comps of each unique field
84
+ comps = _split_comps (key )
85
+ cls = np .array ([results [format_key (comp )] for comp in comps ])
86
+ _results [key ] = cls
87
+ elif naxis == 2 :
88
+ # We are dealing with Covariance matrices
89
+ keys = list (set ([(k [0 ], k [1 ], k [4 ], k [5 ]) for k in keys ]))
90
+ keys = set ([_unsplit_comps (key ) for key in keys ])
91
+ keys = sorted (keys )
92
+ for key1 , key2 in itertools .combinations_with_replacement (keys , 2 ):
93
+ a1 , b1 , i1 , j1 = key1
94
+ a2 , b2 , i2 , j2 = key2
95
+ field_covkey = (a1 , b1 , a2 , b2 , i1 , j1 , i2 , j2 )
96
+ comps1 = _split_comps (key1 )
97
+ comps2 = _split_comps (key2 )
98
+ fields_cov = []
99
+ for _key1 , _key2 in itertools .product (comps1 , comps2 ):
100
+ _a1 , _b1 , _i1 , _j1 = _key1
101
+ _a2 , _b2 , _i2 , _j2 = _key2
102
+ comp_covkey = (_a1 , _b1 , _a2 , _b2 , _i1 , _j1 , _i2 , _j2 )
103
+ if comp_covkey in results .keys ():
104
+ comp_cov = results [comp_covkey ]
105
+ fields_cov .append (comp_cov )
106
+ if len (fields_cov ) == 0 :
107
+ # Happens if the BBAA exists but not AABB
108
+ comps1 , comps2 = comps2 , comps1
109
+ field_covkey = (a2 , b2 , a1 , b1 , i2 , j2 , i1 , j1 )
110
+ for _key1 , _key2 in itertools .product (comps1 , comps2 ):
111
+ _a1 , _b1 , _i1 , _j1 = _key1
112
+ _a2 , _b2 , _i2 , _j2 = _key2
113
+ comp_covkey = (_a1 , _b1 , _a2 , _b2 , _i1 , _j1 , _i2 , _j2 )
114
+ if comp_covkey in results .keys ():
115
+ comp_cov = results [comp_covkey ]
116
+ fields_cov .append (comp_cov )
117
+ # format the covariance
118
+ first , * rest = fields_cov
119
+ ell = first .ell
120
+ axis = first .axis
121
+ nell1 , nell2 = first .shape
122
+ n , m = len (comps1 ), len (comps2 )
123
+ fields_cov = np .reshape (fields_cov , (n , m , nell1 , nell2 ))
124
+ _results [field_covkey ] = Result (fields_cov , ell , axis = axis )
125
+ return _results
126
+
127
+
72
128
def Components2Data (results ):
73
129
keys = list (results .keys ())
74
130
ells = [results [key ].ell for key in keys ]
@@ -138,66 +194,6 @@ def Data2Components(cov):
138
194
return Cl_cov_dict
139
195
140
196
141
- def Components2Fields (results ):
142
- _results = {}
143
- keys = list (results .keys ())
144
- naxis = np .unique ([len (result .axis ) for result in results .values ()])
145
- if len (naxis ) != 1 :
146
- raise ValueError ("Different types of results in the same dictionary." )
147
- naxis = naxis [0 ]
148
- if naxis == 1 :
149
- # We are dealing with Cls
150
- # find unique fields in comps
151
- keys = set ([_unsplit_comps (key ) for key in keys ])
152
- keys = sorted (keys )
153
- for key in keys :
154
- # get comps of each unique field
155
- comps = _split_comps (key )
156
- cls = np .array ([results [format_key (comp )] for comp in comps ])
157
- _results [key ] = cls
158
- elif naxis == 2 :
159
- # We are dealing with Covariance matrices
160
- keys = [(k [0 ], k [1 ], k [4 ], k [5 ]) for k in keys ]
161
- keys = list (set (keys ))
162
- for key1 , key2 in itertools .combinations_with_replacement (keys , 2 ):
163
- a1 , b1 , i1 , j1 = key1
164
- a2 , b2 , i2 , j2 = key2
165
- covkey = (a1 , b1 , a2 , b2 , i1 , j1 , i2 , j2 )
166
- ells1 , ells2 = results [covkey ].ell
167
- nells1 , nells2 = len (ells1 ), len (ells2 )
168
- # Writes the covkeys of the spin components associated with covkey
169
- # it also returns what fields go in what axis
170
- # comps1/comp2 (E, E) (B, B) (E,B)
171
- # (POS, E)
172
- # (POS, B)
173
- comps1 = _split_comps (key1 )
174
- comps2 = _split_comps (key2 )
175
- # Save comps in dtype metadata
176
- dt = np .dtype (
177
- float ,
178
- metadata = {
179
- "fields1" : comps1 ,
180
- "fields2" : comps2 ,
181
- },
182
- )
183
- _cov = np .zeros ((len (comps1 ), len (comps2 ), nells1 , nells2 ), dtype = dt )
184
- for i in range (len (comps1 )):
185
- for j in range (len (comps2 )):
186
- comp1 = comps1 [i ]
187
- comp2 = comps2 [j ]
188
- _a1 , _b1 , _ , _ = comp1
189
- _a2 , _b2 , _ , _ = comp2
190
- _covkey = _a1 , _b1 , _a2 , _b2 , i1 , j1 , i2 , j2
191
- if _covkey not in results .keys ():
192
- # This triggers if the element doesn't exist
193
- # but the symmetrical term does
194
- _cov [i , j , :, :] = np .zeros ((nells2 , nells1 ))
195
- else :
196
- _cov [i , j , :, :] = results [_covkey ]
197
- _results [covkey ] = Result (_cov , ell = (ells1 , ells2 ))
198
- return _results
199
-
200
-
201
197
def _split_comps (key ):
202
198
_key = copy .deepcopy (key )
203
199
a , b , i , j = _key
0 commit comments