climate_ref.models.metric_value
#
MetricValue
#
Bases: DimensionMixin, CreatedUpdatedMixin, Base
Represents a single metric value
This is a base class for different types of metric values (e.g. scalar, series) which are stored in a single table using single table inheritance.
This value has a number of dimensions which are used to query the diagnostic values. These dimensions describe aspects such as the type of statistic being measured, the region of interest or the model from which the statistic is being measured.
The columns in this table are not known statically because the REF can track an arbitrary
set of dimensions depending on the controlled vocabulary that will be used.
A call to register_cv_dimensions must be made before using this class.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
type = mapped_column(index=True)
class-attribute
instance-attribute
#
Type of metric value
This value is used to determine how the metric value should be interpreted.
MetricValueType
#
Bases: Enum
Type of metric value
This is used to determine how the metric value should be interpreted.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
ScalarMetricValue
#
Bases: MetricValue
A scalar value with an associated dimensions
This is a subclass of MetricValue that is used to represent a scalar value.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
build(*, execution_id, value, dimensions, attributes)
classmethod
#
Build a MetricValue from a collection of dimensions and a value
This is a helper method that validates the dimensions supplied and provides an interface similar to climate_ref_core.metric_values.ScalarMetricValue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execution_id
|
int
|
Execution that created the diagnostic value |
required |
value
|
float
|
The value of the diagnostic |
required |
dimensions
|
dict[str, str]
|
Dimensions that describe the diagnostic execution result |
required |
attributes
|
dict[str, Any] | None
|
Optional additional attributes to describe the value, but are not in the controlled vocabulary. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If an unknown dimension was supplied. Dimensions must exist in the controlled vocabulary. |
Returns:
| Type | Description |
|---|---|
Newly created MetricValue
|
|
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
SeriesIndex
#
Bases: Base
A shared 1-d index axis for series metric values
Many series share the same index (for example a common monthly time axis),
so the index is stored once here and referenced by
SeriesMetricValue.index_id
rather than duplicated on every row. Axes are deduplicated by hash.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
hash = mapped_column(unique=True, index=True)
class-attribute
instance-attribute
#
Content hash of (name, values).
The axes are deduplicated by this hash, so identical axes will share the same row and be referenced by id.
length = mapped_column()
class-attribute
instance-attribute
#
Number of points in the index; used to validate series lengths.
name = mapped_column(nullable=True)
class-attribute
instance-attribute
#
Name of the index (e.g. "time"). Used for presentation.
values = mapped_column()
class-attribute
instance-attribute
#
The 1-d array of index values.
bulk_get_or_create(session, axes_by_hash)
classmethod
#
Resolve many axes at once, returning a {hash: id} map.
Existing axes are fetched in a single query and any missing axes are bulk-inserted,
so a batch of series values costs two queries rather than one get_or_create per row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Active database session. |
required |
axes_by_hash
|
Mapping[str, tuple[str | None, Sequence[float | int | str]]]
|
Content hash (see compute_hash)
mapped to the axis |
required |
Returns:
| Type | Description |
|---|---|
The shared axis id for every hash in ``axes_by_hash``.
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If any key does not equal |
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
compute_hash(name, values)
staticmethod
#
Compute the content hash used to deduplicate identical axes.
The hash covers both the name and the ordered values, so two axes are only shared when they are genuinely identical.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
get_or_create(session, name, values)
classmethod
#
Return the existing axis with this content, or create and flush a new one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Session
|
Active database session. |
required |
name
|
str | None
|
Name of the index. |
required |
values
|
Sequence[float | int | str]
|
1-d array of index values. |
required |
Returns:
| Type | Description |
|---|---|
The shared [SeriesIndex][climate_ref.models.metric_value.SeriesIndex] row.
|
|
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
SeriesMetricValue
#
Bases: MetricValue
A 1d series with associated dimensions
This is a subclass of MetricValue that is used to represent a series. This can be used to represent time series, vertical profiles or other 1d data.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
index
property
#
The 1-d index values, resolved from the shared axis.
index_name
property
#
The name of the index, resolved from the shared axis.
reference_id = mapped_column(nullable=True, index=True)
class-attribute
instance-attribute
#
Content hash of the reference payload, for reference (observation) series only.
Two reference series with an identical payload share the same reference_id,
so observations can be deduplicated deterministically across executions.
It is None for model series. See
compute_reference_id.
build(*, execution_id, values, index_axis, dimensions, attributes)
classmethod
#
Build a database object from a series
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
execution_id
|
int
|
Execution that created the diagnostic value |
required |
values
|
list[float | int]
|
1-d array of values |
required |
index_axis
|
SeriesIndex
|
The shared index axis for this series, obtained via SeriesIndex.get_or_create |
required |
dimensions
|
dict[str, str]
|
Dimensions that describe the diagnostic execution result |
required |
attributes
|
dict[str, Any] | None
|
Optional additional attributes to describe the value, but are not in the controlled vocabulary. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If an unknown dimension was supplied. Dimensions must exist in the controlled vocabulary. |
ValueError
|
If the length of values and index do not match. |
Returns:
| Type | Description |
|---|---|
Newly created MetricValue
|
|
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
compute_reference_id(values, index, reference_source_id)
staticmethod
#
Compute the content hash that deduplicates an identical reference payload.
The hash covers the values, the index and the reference source, so two reference series are only treated as the same observation when their payloads are genuinely identical. Keep this payload stable: it is the deduplication key used downstream.
Source code in packages/climate-ref/src/climate_ref/models/metric_value.py
validate_series_lengths(mapper, connection, target)
#
Validate that values and the referenced index axis have matching lengths
This is done on insert and update to ensure that the database is consistent.