Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## \[Unreleased\]

### Added

- Schema maps
- Added ZTF 'lite' schema map

### Fixed

- `Topic.publish()`: Message attributes were improperly defined
Expand Down
16 changes: 12 additions & 4 deletions pittgoogle/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class Schemas:

@staticmethod
def get(
schema_name: Literal["elasticc", "lsst", "lvk", "ztf", "default", None] = "default",
schema_name: Literal[
"elasticc", "lsst", "lvk", "ztf", "ztf.lite", "default", None
] = "default",
alert_bytes: bytes | None = None,
) -> schema.Schema:
"""Return the schema with name matching `schema_name`.
Expand All @@ -88,8 +90,9 @@ def get(
schema_name = "default"

for yaml_dict in SCHEMA_MANIFEST:
name = yaml_dict["name"].split(".")[0] # [FIXME] This is a hack for elasticc.
if name == schema_name:
name = yaml_dict["name"]
name_prefix = yaml_dict["name"].split(".")[0] # [FIXME] This is a hack for elasticc.
if name == schema_name or name_prefix == schema_name:
_Schema = Schemas._get_class(schema_name)
return _Schema._from_yaml(yaml_dict=yaml_dict, alert_bytes=alert_bytes)

Expand All @@ -113,7 +116,12 @@ def _get_class(schema_name: str) -> Type[schema.Schema]:
exceptions.SchemaError:
If a schema named `schema_name` is not found in the registry or cannot be loaded.
"""
class_name = schema_name[0].upper() + schema_name[1:] + "Schema"
schema_name_parts = schema_name.split(".")
class_name = (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointing out that both:
class_name = schema_name[0].upper() + schema_name[1:] + "Schema" and

schema_name_parts = schema_name.split(".")
class_name = (
    schema_name_parts[0].capitalize()
    + "".join(part for part in schema_name_parts[1:])
    + "Schema"
)

will not work if schema_name = "elasticc.v0_9_1.brokerClassification" or schema_name = "elasticc.v0_9_1.alert".

schema_name_parts[0].capitalize()
+ "".join(part for part in schema_name_parts[1:])
+ "Schema"
)
err_msg = (
f"{class_name} not found for schema_name='{schema_name}'. ",
"For valid names, see `pittgoogle.Schemas().names`.",
Expand Down
10 changes: 10 additions & 0 deletions pittgoogle/registry_manifests/schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@
1: g
2: r
3: i
#
# ZTF lite alerts
- name: 'ztf.lite'
description: 'Schema for ZTF lite alerts.'
origin: 'https://zwickytransientfacility.github.io/ztf-avro-alert/schema.html'
path: null
filter_map:
1: g
2: r
3: i
8 changes: 7 additions & 1 deletion pittgoogle/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
LsstSchema
LvkSchema
ZtfSchema
ZtfliteSchema

----
"""
Expand Down Expand Up @@ -324,7 +325,7 @@ def survey(self) -> str:
def map(self) -> dict:
"""Mapping of Pitt-Google's generic field names to survey-specific field names."""
if self._map is None:
yml = __package_path__ / "schemas" / "maps" / f"{self.survey}.yml"
yml = __package_path__ / "schemas" / "maps" / f"{self.survey.replace('.', '-')}.yml"
try:
self._map = yaml.safe_load(yml.read_text())
except FileNotFoundError:
Expand Down Expand Up @@ -619,3 +620,8 @@ class ZtfSchema(DefaultSchema):
deserializer: Literal["json", "avro"] = attrs.field(default="avro")
"""Whether to use a Avro (default) or JSON to deserialize when decoding `alert_bytes` -> `alert_dict`.
If "avro", this `pittgoogle.Schema` will expect the Avro schema to be attached to `alert_bytes` in the header."""


@attrs.define(kw_only=True)
class ZtfliteSchema(DefaultSchema):
"""Schema for ZTF lite alerts."""
14 changes: 14 additions & 0 deletions pittgoogle/schemas/maps/ztf-lite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FILTER_MAP:
1: g
2: r
3: i
prv_sources: prv_sources
source: source_dict
objectid: [alertIds, objectId]
sourceid: [alertIds, sourceId]
filter: [source_dict, filter]
mag: [source_dict, mag]
mag_err: [source_dict, magerr]
mag_zp: [source_dict, magzp]
xmatch: xmatch
jd: [source_dict, jd]