common.py 4.49 KB
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
2
import copy
from dataclasses import dataclass, field
Wei Shoulin's avatar
Wei Shoulin committed
3
4
5

def from_proto_model_list(clazz, records):
    return [clazz().from_proto_model(r) for r in records]
Wei Shoulin's avatar
Wei Shoulin committed
6

Wei Shoulin's avatar
mc3    
Wei Shoulin committed
7
8
def from_dict_list(clazz, records):
    return [clazz().from_dict(r) for r in records]
Wei Shoulin's avatar
Wei Shoulin committed
9

Wei Shoulin's avatar
Wei Shoulin committed
10
11
def to_proto_model_list(protoObjClazz, records):
    return [r.to_proto_model(protoObjClazz) for r in records]
Wei Shoulin's avatar
Wei Shoulin committed
12
13
14
15

def default_field(obj):
    return field(default_factory=lambda: copy.copy(obj))    
@dataclass
Wei Shoulin's avatar
Wei Shoulin committed
16
17
18
19
20
21
22
23
class BaseModel:

    def from_dict(self, data = {}):
        for k in self.__dataclass_fields__.keys():
            self.__setattr__(k, data.get(k, None))
        return self

    def from_proto_model(self, record):
Wei Shoulin's avatar
Wei Shoulin committed
24
25
26
        for k in self.__dataclass_fields__.keys():
            self.__setattr__(k, record.__getattribute__(k))
        return self
Wei Shoulin's avatar
Wei Shoulin committed
27
28
29
30
31
32
    
    def to_proto_model(self, protoObjClazz):
        obj = protoObjClazz()
        for k in self.__dataclass_fields__.keys():
            v = self.__getattribute__(k)
            if v is not None:
Wei Shoulin's avatar
Wei Shoulin committed
33
                if isinstance(v, list) or isinstance(v, tuple):
Wei Shoulin's avatar
Wei Shoulin committed
34
                    obj.__getattribute__(k).extend(v)
Wei Shoulin's avatar
Wei Shoulin committed
35
                elif isinstance(v, dict):
Wei Shoulin's avatar
Wei Shoulin committed
36
                    obj.__getattribute__(k).update(v)                    
Wei Shoulin's avatar
Wei Shoulin committed
37
38
39
                else:
                    obj.__setattr__(k, v)
        return obj
Wei Shoulin's avatar
Wei Shoulin committed
40

Wei Shoulin's avatar
Wei Shoulin committed
41
@dataclass
Wei Shoulin's avatar
Wei Shoulin committed
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
class Gaia3Record(BaseModel):
    SourceId: int  = -1
    RandomIndex: int  = -1
    RefEpoch: float = -1
    Ra: float = -1
    RaError: float = -1
    Dec : float = -1
    DecError: float = -1
    Parallax: float = -1
    ParallaxError: float = -1
    ParallaxOverError: float = -1
    Pm: float = -1
    Pmra: float = -1
    PmraError: float = -1
    Pmdec: float = -1
    PmdecError: float = -1
    RaDecCorr : float = -1
    RaParallaxCorr: float = -1
    RaPmraCorr: float = -1
    RaPmdecCorr: float = -1
    DecParallaxCorr: float = -1
    DecPmraCorr: float = -1
    DecPmdecCorr: float = -1
    ParallaxPmraCorr: float = -1
    ParallaxPmdecCorr: float = -1
    PmraPmdecCorr: float = -1
    AstrometricNObsAl: int = -1
    AstrometricNObsAc: int = -1
    AstrometricNGoodObsAl: int = -1
    AstrometricNBadObsAl: int = -1
    AstrometricGofAl: float = -1
    AstrometricChi2Al: float = -1
    AstrometricExcessNoise: float = -1
Wei Shoulin's avatar
bug    
Wei Shoulin committed
75
    AstrometricExcessNoiseSig: float = -1
Wei Shoulin's avatar
Wei Shoulin committed
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
    AstrometricParamsSolved: int = -1
    AstrometricPrimaryFlag: int = -1
    NuEffUsedInAstrometry: float = -1
    Pseudocolour: float = -1
    PseudocolourError: float = -1
    RaPseudocolourCorr: float = -1
    DecPseudocolourCorr: float = -1
    ParallaxPseudocolourCorr: float = -1
    PmraPseudocolourCorr : float = -1
    PmdecPseudocolourCorr: float = -1
    AstrometricMatchedTransits: int = -1
    VisibilityPeriodsUsed: int = -1
    AstrometricSigma5dMax : float = -1
    MatchedTransits: int = -1
    NewMatchedTransits: int = -1
    MatchedTransitsRemoved: int = -1
    IpdGofHarmonicAmplitude: float = -1
    IpdGofHarmonicPhase: float = -1
    IpdFracMultiPeak: int = -1
    IpdFracOddWin: int = -1
    Ruwe: float = -1
    ScanDirectionStrengthK1: float = -1
    ScanDirectionStrengthK2: float = -1
    ScanDirectionStrengthK3: float = -1
    ScanDirectionStrengthK4: float = -1
    ScanDirectionMeanK1: float = -1
    ScanDirectionMeanK2: float = -1
    ScanDirectionMeanK3: float = -1
    ScanDirectionMeanK4: float = -1
    DuplicatedSource: int = -1
    PhotGNObs: int = -1
    PhotGMeanFlux: float = -1
    PhotGMeanFluxError: float = -1
    PhotGMeanFluxOverError: float = -1
    PhotGMeanMag: float = -1
    PhotBpNObs: int = -1
    PhotBpMeanFlux: float = -1
    PhotBpMeanFluxError: float = -1
    PhotBpMeanFluxOverError: float = -1
    PhotBpMeanMag: float = -1
    PhotRpNObs: int = -1
    PhotRpMeanFlux: float = -1
    PhotRpMeanFluxError: float = -1
    PhotRpMeanFluxOverError: float = -1
    PhotRpMeanMag: float = -1
    PhotBpNContaminatedTransits: float = -1
    PhotBpNBlendedTransits: float = -1
    PhotRpNContaminatedTransits: float = -1
    PhotRpNBlendedTransits: float = -1
    PhotProcMode : float = -1
    PhotBpRpExcessFactor: float = -1
    BpRp: float = -1
    BpG : float = -1
    GRp : float = -1
    Dr2RadialVelocity: float = -1
    Dr2RadialVelocityError: float = -1
    Dr2RvNbTransits: int = -1
    Dr2RvTemplateTeff: float = -1
    Dr2RvTemplateLogg: float = -1
    Dr2RvTemplateFeH : float = -1
    L: float = -1
    B: float = -1
    EclLon: float = -1
    EclLat: float = -1
    NS8HIdx: int = -1
    NS16HIdx: int = -1
    NS32HIdx: int = -1
    NS64HIdx: int = -1

    def __init__(self):
        self.AstrometricNObsAc = 0