Skip to content

Commit

Permalink
made explicit projection types
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Dec 18, 2024
1 parent 5f1d747 commit fda7e64
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 308 deletions.
17 changes: 16 additions & 1 deletion src/event_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@
from .documents.partial_event_page import PartialEventPage
from .documents.partial_resource import PartialResource
from .documents.resource import Resource
from .documents.run_start import Calculation, Hints, Projection, Projections, RunStart
from .documents.run_start import (
CalculatedEventProjection,
Calculation,
ConfigurationProjection,
Hints,
LinkedEventProjection,
Projections,
RunStart,
StaticProjection,
)
from .documents.run_stop import RunStop
from .documents.stream_datum import StreamDatum, StreamRange
from .documents.stream_resource import StreamResource
Expand Down Expand Up @@ -79,6 +88,10 @@
"Resource",
"Calculation",
"Hints",
"LinkedEventProjection",
"StaticProjection",
"CalculatedEventProjection",
"ConfigurationProjection",
"Projection",
"Projections",
"RunStart",
Expand Down Expand Up @@ -1822,6 +1835,8 @@ class MismatchedDataKeys(InvalidData):
DocumentNames.resource: "jsonschemas/resource.json",
DocumentNames.stream_datum: "jsonschemas/stream_datum.json",
DocumentNames.stream_resource: "jsonschemas/stream_resource.json",
DocumentNames.bulk_datum: "jsonschemas/bulk_datum.json",
DocumentNames.bulk_events: "jsonschemas/bulk_events.json",
}
schemas = {}
for name, filename in SCHEMA_NAMES.items():
Expand Down
148 changes: 75 additions & 73 deletions src/event_model/basemodels/run_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,101 +39,92 @@ class Calculation(BaseModel):
]


class Projection(BaseModel):
"""Where to get the data from"""

model_config = ConfigDict(extra="forbid")
class ConfigurationProjection(BaseModel):
location: Annotated[
Literal["configuration"],
Field(
description="Projection comes from configuration "
"fields in the event_descriptor document",
),
]

calculation: Annotated[
Optional[Calculation],
type: Annotated[
Literal["linked"],
Field(
description="required fields if type is calculated",
title="calculation properties",
default=None,
description=(
"Projection is of type linked, a value linked from the data set."
)
),
]
config_index: Annotated[Optional[int], Field(default=None)]
config_device: Annotated[Optional[str], Field(default=None)]
field: Annotated[Optional[str], Field(default=None)]
config_index: Annotated[int, Field()]
config_device: Annotated[str, Field()]
field: Annotated[str, Field()]
stream: Annotated[str, Field()]


class LinkedEventProjection(BaseModel):
location: Annotated[
Optional[Literal["start", "event", "configuration"]],
Literal["event"],
Field(description="Projection comes and event"),
]

type: Annotated[
Literal["linked"],
Field(
description="start comes from metadata fields in the start document, "
"event comes from event, configuration comes from configuration "
"fields in the event_descriptor document",
default=None,
description=(
"Projection is of type linked, a value linked from the data set."
)
),
]
stream: Annotated[Optional[str], Field(default=None)]
field: Annotated[str, Field()]
stream: Annotated[str, Field()]


class CalculatedEventProjection(BaseModel):
location: Annotated[
Literal["event"],
Field(description="Projection comes and event"),
]

type: Annotated[
Optional[Literal["linked", "calculated", "static"]],
Literal["calculated"],
Field(
description="linked: a value linked from the data set, "
"calculated: a value that requires calculation, "
"static: a value defined here in the projection ",
default=None,
description=(
"Projection is of type calculated, a value that requires calculation."
)
),
]
value: Annotated[
Optional[Any],
field: Annotated[str, Field()]
stream: Annotated[str, Field()]
calculation: Annotated[
Calculation,
Field(
description="value explicitely defined in the projection "
"when type==static.",
default=None,
description="required fields if type is calculated",
title="calculation properties",
),
]


class StaticProjection(BaseModel):
type: Annotated[
Literal["static"],
Field(
description=(
"Projection is of type static, a value defined here in the projection"
)
),
]
value: Annotated[
Any, Field(description="value explicitely defined in the static projection")
]


RUN_START_EXTRA_SCHEMA = {
"$defs": {
"DataType": {
"patternProperties": {"^([^./]+)$": {"$ref": "#/$defs/DataType"}},
"additionalProperties": False,
},
"Projection": {
"allOf": [
{
"if": {
"allOf": [
{"properties": {"location": {"enum": ["configuration"]}}},
{"properties": {"type": {"enum": ["linked"]}}},
]
},
"then": {
"required": [
"type",
"location",
"config_index",
"config_device",
"field",
"stream",
]
},
},
{
"if": {
"allOf": [
{"properties": {"location": {"enum": ["event"]}}},
{"properties": {"type": {"enum": ["linked"]}}},
]
},
"then": {"required": ["type", "location", "field", "stream"]},
},
{
"if": {
"allOf": [
{"properties": {"location": {"enum": ["event"]}}},
{"properties": {"type": {"enum": ["calculated"]}}},
]
},
"then": {"required": ["type", "field", "stream", "calculation"]},
},
{
"if": {"properties": {"type": {"enum": ["static"]}}},
"then": {"required": ["type", "value"]},
},
],
},
},
"properties": {
"hints": {
Expand All @@ -155,7 +146,18 @@ class Projections(BaseModel):
name: Annotated[
Optional[str], Field(description="The name of the projection", default=None)
]
projection: Annotated[Dict[Any, Projection], Field(description="")]
projection: Annotated[
Dict[
Any,
Union[
ConfigurationProjection,
LinkedEventProjection,
CalculatedEventProjection,
StaticProjection,
],
],
Field(description=""),
]
version: Annotated[
str,
Field(
Expand Down
71 changes: 54 additions & 17 deletions src/event_model/documents/run_start.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# ruff: noqa
# generated by datamodel-codegen:
# filename: run_start.json
# timestamp: 2024-12-18T11:01:13+00:00
# timestamp: 2024-12-18T12:11:28+00:00

from __future__ import annotations

from typing import Any, Dict, List, Literal, Optional, TypedDict, Union
from typing import Any, Dict, List, Optional, TypedDict, Union

from typing_extensions import NotRequired

Expand All @@ -22,6 +22,21 @@ class Calculation(TypedDict):
"""


class ConfigurationProjection(TypedDict):
config_device: str
config_index: int
field: str
location: NotRequired[str]
"""
Projection comes from configuration fields in the event_descriptor document
"""
stream: str
type: NotRequired[str]
"""
Projection is of type linked, a value linked from the data set.
"""


DataType = Any


Expand All @@ -36,30 +51,44 @@ class Hints(TypedDict):
"""


class Projection(TypedDict):
class LinkedEventProjection(TypedDict):
field: str
location: NotRequired[str]
"""
Projection comes and event
"""
stream: str
type: NotRequired[str]
"""
Where to get the data from
Projection is of type linked, a value linked from the data set.
"""

calculation: NotRequired[Optional[Calculation]]

class StaticProjection(TypedDict):
type: NotRequired[str]
"""
required fields if type is calculated
Projection is of type static, a value defined here in the projection
"""
config_device: NotRequired[Optional[str]]
config_index: NotRequired[Optional[int]]
field: NotRequired[Optional[str]]
location: NotRequired[Optional[Literal['start', 'event', 'configuration']]]
value: Any
"""
start comes from metadata fields in the start document, event comes from event, configuration comes from configuration fields in the event_descriptor document
value explicitely defined in the static projection
"""


class CalculatedEventProjection(TypedDict):
calculation: Calculation
"""
required fields if type is calculated
"""
stream: NotRequired[Optional[str]]
type: NotRequired[Optional[Literal['linked', 'calculated', 'static']]]
field: str
location: NotRequired[str]
"""
linked: a value linked from the data set, calculated: a value that requires calculation, static: a value defined here in the projection
Projection comes and event
"""
value: NotRequired[Any]
stream: str
type: NotRequired[str]
"""
value explicitely defined in the projection when type==static.
Projection is of type calculated, a value that requires calculation.
"""


Expand All @@ -76,7 +105,15 @@ class Projections(TypedDict):
"""
The name of the projection
"""
projection: Dict[str, Projection]
projection: Dict[
str,
Union[
ConfigurationProjection,
LinkedEventProjection,
CalculatedEventProjection,
StaticProjection,
],
]
version: str
"""
The version of the projection spec. Can specify the version of an external specification.
Expand Down
27 changes: 27 additions & 0 deletions src/event_model/jsonschemas/bulk_datum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"properties": {
"datum_kwarg_list": {
"type": "array",
"items": {"type": "object"},
"description": "Array of arguments to pass to the Handler to retrieve one quanta of data"
},
"resource": {
"type": "string",
"description": "UID of the Resource to which all these Datum documents belong"
},
"datum_ids": {
"type": "array",
"items": {"type": "string"},
"description": "Globally unique identifiers for each Datum (akin to 'uid' for other Document types), typically formatted as '<resource>/<integer>'"
}
},
"required": [
"datum_kwarg_list",
"resource",
"datum_ids"
],
"additionalProperties": false,
"type": "object",
"title": "bulk_datum",
"description": "Document to reference a quanta of externally-stored data"
}
Loading

0 comments on commit fda7e64

Please sign in to comment.