@@ -64,95 +64,45 @@ class Field(metaclass=ABCMeta):
64
64
# definition of required and optional columns
65
65
__ncol : tuple [int , int ]
66
66
67
- def __init_subclass__ (cls , * , spin : int | None = None ) -> None :
67
+ ''' def __init_subclass__(cls, *, spin: int | None = None) -> None:
68
68
"""Initialise spin weight of field subclasses."""
69
69
super().__init_subclass__()
70
- if spin is not None :
71
- cls .__spin = spin
72
- uses = cls .uses
73
- if uses is None :
74
- uses = ()
75
- elif isinstance (uses , str ):
76
- uses = (uses ,)
77
- ncol = len (uses )
78
- nopt = 0
79
- for u in uses [::- 1 ]:
80
- if u .startswith ("[" ) and u .endswith ("]" ):
81
- nopt += 1
82
- else :
83
- break
84
- cls .__ncol = (ncol - nopt , ncol )
70
+ cls.__spin = spin'''
71
+
85
72
86
73
def __init__ (
87
74
self ,
88
75
mapper : Mapper | None ,
89
- * columns : str ,
76
+ * columns : str ,
77
+ weight : str | None ,
90
78
mask : str | None = None ,
91
79
) -> None :
92
80
"""Initialise the field."""
93
81
super ().__init__ ()
94
82
self .__mapper = mapper
95
- self .__columns = self ._init_columns (* columns ) if columns else None
83
+ self .__columns = columns if columns else None
84
+ self .__weight = weight if weight else None
96
85
self .__mask = mask
97
-
98
- @classmethod
99
- def _init_columns (cls , * columns : str ) -> Columns :
100
- """Initialise the given set of columns for a specific field
101
- subclass."""
102
- nmin , nmax = cls .__ncol
103
- if not nmin <= len (columns ) <= nmax :
104
- uses = cls .uses
105
- if uses is None :
106
- uses = ()
107
- if isinstance (uses , str ):
108
- uses = (uses ,)
109
- count = f"{ nmin } "
110
- if nmax != nmin :
111
- count += f" to { nmax } "
112
- msg = f"field of type '{ cls .__name__ } ' accepts { count } columns"
113
- if uses :
114
- msg += " (" + ", " .join (uses ) + ")"
115
- msg += f", received { len (columns )} "
116
- raise ValueError (msg )
117
- return columns + (None ,) * (nmax - len (columns ))
86
+ self .__spin = 0
118
87
119
88
@property
120
89
def mapper (self ) -> Mapper | None :
121
90
"""Return the mapper used by this field."""
122
91
return self .__mapper
123
-
92
+
124
93
@property
125
- def mapper_or_error (self ) -> Mapper :
126
- """Return the mapper used by this field, or raise a :class:`ValueError`
127
- if not set."""
128
- if self .__mapper is None :
129
- msg = "no mapper for field"
130
- raise ValueError (msg )
131
- return self .__mapper
94
+ def weight (self ) -> str | None :
95
+ """Return the mapper used by this field."""
96
+ return self .__weight
132
97
133
98
@property
134
99
def columns (self ) -> Columns | None :
135
100
"""Return the catalogue columns used by this field."""
136
101
return self .__columns
137
102
138
- @property
139
- def columns_or_error (self ) -> Columns :
140
- """Return the catalogue columns used by this field, or raise a
141
- :class:`ValueError` if not set."""
142
- if self .__columns is None :
143
- msg = "no columns for field"
144
- raise ValueError (msg )
145
- return self .__columns
146
-
147
103
@property
148
104
def spin (self ) -> int :
149
- """Spin weight of field."""
150
- spin = self .__spin
151
- if spin is None :
152
- clsname = self .__class__ .__name__
153
- msg = f"field of type '{ clsname } ' has undefined spin weight"
154
- raise ValueError (msg )
155
- return spin
105
+ return self .__spin
156
106
157
107
@property
158
108
def mask (self ) -> str | None :
@@ -168,7 +118,13 @@ async def __call__(
168
118
) -> ArrayLike :
169
119
"""Implementation for mapping a catalogue."""
170
120
...
171
-
121
+
122
+ def CheckColumns (self , * expected ):
123
+ if (len (expected )!= len (self .columns )):
124
+ error = "Column error. Expected " + str (len (expected )) + " columns"
125
+ error += "with a format " + str (expected ) + ". Received " + str (self .columns )
126
+ raise ValueError (error )
127
+
172
128
173
129
async def _pages (
174
130
catalog : Catalog ,
@@ -190,26 +146,25 @@ async def _pages(
190
146
await coroutines .sleep ()
191
147
192
148
193
- class Positions (Field , spin = 0 ):
149
+ class Positions (Field ):
194
150
"""Field of positions in a catalogue.
195
151
196
152
Can produce both overdensity maps and number count maps, depending
197
153
on the ``overdensity`` property.
198
154
199
155
"""
200
156
201
- uses = "longitude" , "latitude"
202
-
203
157
def __init__ (
204
158
self ,
205
159
mapper : Mapper | None ,
206
160
* columns : str ,
161
+ weight : str | None ,
207
162
overdensity : bool = True ,
208
163
nbar : float | None = None ,
209
164
mask : str | None = None ,
210
165
) -> None :
211
166
"""Create a position field."""
212
- super ().__init__ (mapper , * columns , mask = mask )
167
+ super ().__init__ (mapper , * columns ,weight = weight , mask = mask )
213
168
self .__overdensity = overdensity
214
169
self .__nbar = nbar
215
170
@@ -241,11 +196,15 @@ async def __call__(
241
196
msg = "cannot compute density contrast: no visibility in catalog"
242
197
raise ValueError (msg )
243
198
244
- # get mapper
245
- mapper = self .mapper_or_error
199
+ #get mapper
200
+ mapper = self .mapper
246
201
247
202
# get catalogue column definition
248
- col = self .columns_or_error
203
+ col = self .columns
204
+ self .CheckColumns ("longitude" , "latitude" )
205
+
206
+ #if(len(col)!=2):
207
+ # raise ValueError("Expect 2 colummns, longitude and latitude")
249
208
250
209
# position map
251
210
pos = mapper .create (spin = self .spin )
@@ -259,7 +218,7 @@ async def __call__(
259
218
lon , lat = page .get (* col )
260
219
w = np .ones (page .size )
261
220
262
- mapper .map_values (lon , lat , pos , w )
221
+ self . mapper .map_values (lon , lat , pos , w )
263
222
264
223
ngal += page .size
265
224
@@ -307,11 +266,9 @@ async def __call__(
307
266
return pos
308
267
309
268
310
- class ScalarField (Field , spin = 0 ):
269
+ class ScalarField (Field ):
311
270
"""Field of real scalar values in a catalogue."""
312
271
313
- uses = "longitude" , "latitude" , "value" , "[weight]"
314
-
315
272
async def __call__ (
316
273
self ,
317
274
catalog : Catalog ,
@@ -321,11 +278,13 @@ async def __call__(
321
278
"""Map real values from catalogue to HEALPix map."""
322
279
323
280
# get mapper
324
- mapper = self .mapper_or_error
281
+ mapper = self .mapper
325
282
326
283
# get the column definition of the catalogue
327
- * col , wcol = self .columns_or_error
328
-
284
+ col = self .columns
285
+ self .CheckColumns (self , "longitude" , "latitude" , "value" )
286
+
287
+ wcol = self .__weight
329
288
# scalar field map
330
289
val = mapper .create (spin = self .spin )
331
290
@@ -373,16 +332,14 @@ async def __call__(
373
332
return val
374
333
375
334
376
- class ComplexField (Field , spin = 0 ):
335
+ class ComplexField (Field ):
377
336
"""Field of complex values in a catalogue.
378
337
379
338
The :class:`ComplexField` class has zero spin weight, while
380
339
subclasses such as :class:`Spin2Field` have non-zero spin weight.
381
340
382
341
"""
383
342
384
- uses = "longitude" , "latitude" , "real" , "imag" , "[weight]"
385
-
386
343
async def __call__ (
387
344
self ,
388
345
catalog : Catalog ,
@@ -392,10 +349,14 @@ async def __call__(
392
349
"""Map complex values from catalogue to HEALPix map."""
393
350
394
351
# get mapper
395
- mapper = self .mapper_or_error
352
+ mapper = self .mapper
396
353
397
354
# get the column definition of the catalogue
398
- * col , wcol = self .columns_or_error
355
+ col = self .columns
356
+
357
+ self .CheckColumns (self , "longitude" , "latitude" , "real" , "imag" )
358
+
359
+ wcol = self .weight
399
360
400
361
# complex map with real and imaginary part
401
362
val = mapper .create (2 , spin = self .spin )
@@ -443,7 +404,7 @@ async def __call__(
443
404
return val
444
405
445
406
446
- class Visibility (Field , spin = 0 ):
407
+ class Visibility (Field ):
447
408
"""Copy visibility map from catalogue at given resolution."""
448
409
449
410
async def __call__ (
@@ -455,7 +416,7 @@ async def __call__(
455
416
"""Create a visibility map from the given catalogue."""
456
417
457
418
# get mapper
458
- mapper = self .mapper_or_error
419
+ mapper = self .mapper
459
420
460
421
# make sure that catalogue has a visibility
461
422
visibility = catalog .visibility
@@ -479,11 +440,9 @@ async def __call__(
479
440
return out
480
441
481
442
482
- class Weights (Field , spin = 0 ):
443
+ class Weights (Field ):
483
444
"""Field of weight values from a catalogue."""
484
445
485
- uses = "longitude" , "latitude" , "[weight]"
486
-
487
446
async def __call__ (
488
447
self ,
489
448
catalog : Catalog ,
@@ -493,10 +452,12 @@ async def __call__(
493
452
"""Map catalogue weights."""
494
453
495
454
# get mapper
496
- mapper = self .mapper_or_error
455
+ mapper = self .mapper
497
456
498
457
# get the columns for this field
499
- * col , wcol = self .columns_or_error
458
+ col = self .columns
459
+ self .CheckColumns (self , "longitude" , "latitude" )
460
+ wcol = self .weight
500
461
501
462
# weight map
502
463
wht = mapper .create (spin = self .spin )
@@ -543,8 +504,18 @@ async def __call__(
543
504
return wht
544
505
545
506
546
- class Spin2Field (ComplexField , spin = 2 ):
507
+ class Spin2Field (ComplexField ):
547
508
"""Spin-2 complex field."""
509
+ def __init__ (
510
+ self ,
511
+ mapper : Mapper | None ,
512
+ * columns : str ,
513
+ weight : str | None ,
514
+ mask : str | None = None ,
515
+ ) -> None :
516
+ """Initialise the field."""
517
+ super ().__init__ (mapper , * columns ,weight = weight , mask = mask )
518
+ self .__spin = 2
548
519
549
520
550
521
Shears = Spin2Field
0 commit comments