Skip to content

Commit 783cef0

Browse files
authored
#414: remove come more methods (#415)
* remove aggregate method * remove save_data method * remove_from custom * remove helpers * remove couple more methods
1 parent 4392388 commit 783cef0

12 files changed

Lines changed: 35 additions & 313 deletions

File tree

app/data/model/__init__.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
from app.data.model.bibliography import Bibliography
22
from app.data.model.designation import DesignationCatalogObject
3-
from app.data.model.helpers import (
4-
CatalogObjectEncoder,
5-
Layer0CatalogObjectDecoder,
6-
get_catalog_object_type,
7-
new_catalog_object,
8-
)
3+
from app.data.model.helpers import get_catalog_object_type
94
from app.data.model.icrs import ICRSCatalogObject
105
from app.data.model.interface import CatalogObject, MeasuredValue, RawCatalog, get_object
116
from app.data.model.layer2 import Layer2CatalogObject, Layer2Object
@@ -50,14 +45,11 @@
5045
"TableRecord",
5146
"RawCatalog",
5247
"CatalogObject",
53-
"CatalogObjectEncoder",
54-
"Layer0CatalogObjectDecoder",
5548
"DesignationCatalogObject",
5649
"ICRSCatalogObject",
5750
"RedshiftCatalogObject",
5851
"NatureCatalogObject",
5952
"get_catalog_object_type",
60-
"new_catalog_object",
6153
"MeasuredValue",
6254
"Bibliography",
6355
]

app/data/model/designation.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,6 @@ def __eq__(self, value: object) -> bool:
1414

1515
return self.designation == value.designation
1616

17-
@classmethod
18-
def from_custom(cls, design: Any) -> Self:
19-
return cls(str(design))
20-
21-
def layer0_data(self) -> dict[str, Any]:
22-
return {"design": self.designation}
23-
24-
@classmethod
25-
def aggregate(cls, objects: list[Self]) -> Self:
26-
"""
27-
Aggregate designation is selected as the most common designation among all objects.
28-
"""
29-
name_counts = {}
30-
31-
for obj in objects:
32-
name_counts[obj.designation] = name_counts.get(obj.designation, 0) + 1
33-
34-
max_name = ""
35-
36-
for name, count in name_counts.items():
37-
if count > name_counts.get(max_name, 0):
38-
max_name = name
39-
40-
return cls(max_name)
41-
4217
def catalog(self) -> interface.RawCatalog:
4318
return interface.RawCatalog.DESIGNATION
4419

@@ -50,9 +25,6 @@ def layer1_table(cls) -> str:
5025
def layer1_keys(cls) -> list[str]:
5126
return ["design"]
5227

53-
def layer1_data(self) -> dict[str, Any]:
54-
return {"design": self.designation}
55-
5628
@classmethod
5729
def from_layer1(cls, data: dict[str, Any]) -> Self:
5830
return cls(design=data["design"])

app/data/model/helpers.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,6 @@
1-
import json
2-
31
from app.data.model import designation, icrs, interface, nature, redshift
42

53

6-
class CatalogObjectEncoder(json.JSONEncoder):
7-
def default(self, obj):
8-
if not isinstance(obj, interface.CatalogObject):
9-
return json.JSONEncoder.default(self, obj)
10-
11-
data = obj.layer0_data()
12-
data["catalog"] = obj.catalog().value
13-
14-
return data
15-
16-
17-
class Layer0CatalogObjectDecoder(json.JSONDecoder):
18-
def __init__(self, *args, **kwargs):
19-
super().__init__(object_hook=self.object_hook, **kwargs)
20-
21-
def object_hook(self, obj):
22-
if "catalog" not in obj:
23-
return obj
24-
25-
catalog = interface.RawCatalog(obj.pop("catalog"))
26-
27-
return new_catalog_object(catalog, **obj)
28-
29-
304
def get_catalog_object_type(catalog: interface.RawCatalog) -> type[interface.CatalogObject]:
315
if catalog == interface.RawCatalog.DESIGNATION:
326
return designation.DesignationCatalogObject
@@ -38,7 +12,3 @@ def get_catalog_object_type(catalog: interface.RawCatalog) -> type[interface.Cat
3812
return nature.NatureCatalogObject
3913

4014
raise ValueError(f"Unknown catalog: {catalog}")
41-
42-
43-
def new_catalog_object(catalog: interface.RawCatalog, **kwargs) -> interface.CatalogObject:
44-
return get_catalog_object_type(catalog)(**kwargs)

app/data/model/icrs.py

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
from typing import Any, Self, final
22

3-
from astropy import coordinates
4-
from astropy import units as u
5-
63
from app.data.model import interface
7-
from app.lib import astronomy
84

95

106
@final
@@ -21,82 +17,15 @@ def __init__(
2117
self.e_ra = e_ra
2218
self.e_dec = e_dec
2319

24-
@classmethod
25-
def from_custom(
26-
cls,
27-
ra: u.Quantity,
28-
dec: u.Quantity,
29-
e_ra: u.Quantity | None = None,
30-
e_dec: u.Quantity | None = None,
31-
) -> Self:
32-
if not interface.is_nan(ra) and not interface.is_nan(dec):
33-
if ra.unit is not None:
34-
ra_angle = coordinates.Angle(ra, unit=ra.unit)
35-
else:
36-
ra_angle = coordinates.Angle(ra)
37-
38-
if dec.unit is not None:
39-
dec_angle = coordinates.Angle(dec, unit=dec.unit)
40-
else:
41-
dec_angle = coordinates.Angle(dec)
42-
else:
43-
raise ValueError("no ra or dec values")
44-
45-
if e_ra is None or e_dec is None:
46-
raise ValueError("no e_ra or e_dec specified")
47-
48-
coords = coordinates.ICRS(ra=ra_angle, dec=dec_angle)
49-
50-
return cls(
51-
astronomy.to(coords.ra, "deg"),
52-
astronomy.to(coords.dec, "deg"),
53-
astronomy.to(e_ra, "deg"),
54-
astronomy.to(e_dec, "deg"),
55-
)
56-
57-
def layer0_data(self) -> dict[str, Any]:
58-
return {
59-
"ra": self.ra,
60-
"dec": self.dec,
61-
"e_ra": self.e_ra,
62-
"e_dec": self.e_dec,
63-
}
64-
6520
def __eq__(self, value: object) -> bool:
6621
if not isinstance(value, ICRSCatalogObject):
6722
return False
6823

6924
return self.ra == value.ra and self.e_ra == value.e_ra and self.dec == value.dec and self.e_dec == value.e_dec
7025

71-
@classmethod
72-
def aggregate(cls, objects: list[Self]) -> Self:
73-
"""
74-
Aggregate coordinates are computed as the mean of all coordinates.
75-
Errors are computed as the mean of all errors.
76-
"""
77-
ras = [obj.ra for obj in objects]
78-
e_ras = [obj.e_ra for obj in objects]
79-
decs = [obj.dec for obj in objects]
80-
e_decs = [obj.e_dec for obj in objects]
81-
82-
ra = sum(ras) / len(ras)
83-
e_ra = sum(e_ras) / len(e_ras)
84-
dec = sum(decs) / len(decs)
85-
e_dec = sum(e_decs) / len(e_decs)
86-
87-
return cls(ra, dec, e_ra, e_dec)
88-
8926
def catalog(self) -> interface.RawCatalog:
9027
return interface.RawCatalog.ICRS
9128

92-
def layer1_data(self) -> dict[str, Any]:
93-
return {
94-
"ra": self.ra,
95-
"dec": self.dec,
96-
"e_ra": self.e_ra,
97-
"e_dec": self.e_dec,
98-
}
99-
10029
@classmethod
10130
def layer1_table(cls) -> str:
10231
return "icrs.data"

app/data/model/interface.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,10 @@ class CatalogObject(abc.ABC):
4141
Represents an object stored in a particular catalog.
4242
"""
4343

44-
@classmethod
45-
@abc.abstractmethod
46-
def aggregate(cls, objects: list[Self]) -> Self:
47-
pass
48-
4944
@abc.abstractmethod
5045
def catalog(self) -> RawCatalog:
5146
pass
5247

53-
@abc.abstractmethod
54-
def layer0_data(self) -> dict[str, Any]:
55-
pass
56-
57-
@classmethod
58-
@abc.abstractmethod
59-
def from_custom(cls, **kwargs) -> Self:
60-
pass
61-
6248
@classmethod
6349
@abc.abstractmethod
6450
def layer1_table(cls) -> str:
@@ -69,10 +55,6 @@ def layer1_table(cls) -> str:
6955
def layer1_keys(cls) -> list[str]:
7056
pass
7157

72-
@abc.abstractmethod
73-
def layer1_data(self) -> dict[str, Any]:
74-
pass
75-
7658
@classmethod
7759
@abc.abstractmethod
7860
def from_layer1(cls, data: dict[str, Any]) -> Self:

app/data/model/nature.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,6 @@ def __eq__(self, value: object) -> bool:
1414

1515
return self.type_name == value.type_name
1616

17-
@classmethod
18-
def from_custom(cls, type_name: Any) -> Self:
19-
return cls(str(type_name))
20-
21-
def layer0_data(self) -> dict[str, Any]:
22-
return {"type_name": self.type_name}
23-
24-
@classmethod
25-
def aggregate(cls, objects: list[Self]) -> Self:
26-
type_counts: dict[str, int] = {}
27-
for obj in objects:
28-
type_counts[obj.type_name] = type_counts.get(obj.type_name, 0) + 1
29-
30-
max_type = ""
31-
for name, count in type_counts.items():
32-
if count > type_counts.get(max_type, 0):
33-
max_type = name
34-
35-
return cls(max_type)
36-
3717
def catalog(self) -> interface.RawCatalog:
3818
return interface.RawCatalog.NATURE
3919

@@ -45,9 +25,6 @@ def layer1_table(cls) -> str:
4525
def layer1_keys(cls) -> list[str]:
4626
return ["type_name"]
4727

48-
def layer1_data(self) -> dict[str, Any]:
49-
return {"type_name": self.type_name}
50-
5128
@classmethod
5229
def from_layer1(cls, data: dict[str, Any]) -> Self:
5330
return cls(type_name=data["type_name"])

app/data/model/redshift.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from typing import Any, Self, final
22

3-
from astropy import units as u
4-
53
from app.data.model import interface
6-
from app.lib import astronomy
74

85

96
@final
@@ -16,42 +13,6 @@ def __init__(
1613
self.cz = cz
1714
self.e_cz = e_cz
1815

19-
@classmethod
20-
def from_custom(
21-
cls,
22-
cz: u.Quantity | None = None,
23-
z: float | None = None,
24-
e_cz: u.Quantity | None = None,
25-
e_z: float | None = None,
26-
) -> Self:
27-
if not interface.is_nan(cz):
28-
data_cz = astronomy.to(cz, "m/s")
29-
elif not interface.is_nan(z):
30-
data_cz = astronomy.to(z * astronomy.const("c"), "m/s")
31-
else:
32-
raise ValueError("neither z nor cz is specified")
33-
34-
if not interface.is_nan(e_cz):
35-
data_e_cz = astronomy.to(e_cz, "m/s")
36-
elif not interface.is_nan(e_z):
37-
data_e_cz = astronomy.to(e_z * astronomy.const("c"), "m/s")
38-
else:
39-
raise ValueError("neither e_z nor e_cz is specified")
40-
41-
return cls(data_cz, data_e_cz)
42-
43-
def layer0_data(self) -> dict[str, Any]:
44-
return {"cz": self.cz, "e_cz": self.e_cz}
45-
46-
@classmethod
47-
def aggregate(cls, objects: list[Self]) -> Self:
48-
e_cz = [obj.e_cz for obj in objects]
49-
50-
cz = sum(obj.cz for obj in objects) / len(objects)
51-
e_cz = sum(e_cz) / len(e_cz)
52-
53-
return cls(cz, e_cz)
54-
5516
def catalog(self) -> interface.RawCatalog:
5617
return interface.RawCatalog.REDSHIFT
5718

@@ -63,9 +24,6 @@ def layer1_table(cls) -> str:
6324
def layer1_keys(cls) -> list[str]:
6425
return ["cz", "e_cz"]
6526

66-
def layer1_data(self) -> dict[str, Any]:
67-
return {"cz": self.cz, "e_cz": self.e_cz}
68-
6927
@classmethod
7028
def from_layer1(cls, data: dict[str, Any]) -> Self:
7129
return cls(cz=data["cz"], e_cz=data["e_cz"])

app/data/repositories/layer1.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import structlog
55

66
from app.data import model
7-
from app.lib import containers
87
from app.lib.storage import postgres
98

109

@@ -35,47 +34,6 @@ def save_structured_data(self, table: str, columns: list[str], ids: list[str], d
3534
with self.with_tx():
3635
self._storage.execute_batch(query, rows)
3736

38-
def save_data(self, records: list[model.Record]) -> None:
39-
all_catalog_objects = []
40-
for record in records:
41-
for catalog_object in record.data:
42-
all_catalog_objects.append((record.id, catalog_object))
43-
44-
table_objects = containers.group_by(all_catalog_objects, lambda item: item[1].layer1_table())
45-
46-
with self.with_tx():
47-
for table, table_items in table_objects.items():
48-
if not table_items:
49-
continue
50-
51-
columns = ["record_id"]
52-
columns.extend(table_items[0][1].layer1_keys())
53-
54-
params = []
55-
values = []
56-
for record_id, catalog_object in table_items:
57-
data = catalog_object.layer1_data()
58-
data["record_id"] = record_id
59-
60-
params.extend([data[column] for column in columns])
61-
values.append(",".join(["%s"] * len(columns)))
62-
63-
on_conflict_update_statement = ", ".join([f"{column} = EXCLUDED.{column}" for column in columns])
64-
65-
query = f"""
66-
INSERT INTO {table} ({", ".join(columns)})
67-
VALUES {", ".join([f"({value})" for value in values])}
68-
ON CONFLICT (record_id) DO UPDATE SET {on_conflict_update_statement}
69-
"""
70-
71-
self._storage.exec(query, params=params)
72-
73-
self._logger.debug(
74-
"Saved data to layer 1",
75-
table=table,
76-
object_count=len(table_items),
77-
)
78-
7937
def get_new_nature_records(
8038
self, dt: datetime.datetime, limit: int, offset: int
8139
) -> list[model.StructuredData[model.NatureRecord]]:

0 commit comments

Comments
 (0)