core_lens.base.view#
Lazy, immutable view of a scoped entity pending materialisation.
Classes#
Module Contents#
- class core_lens.base.view.Season(*args, **kwds)#
Bases:
enum.EnumSeason enum.
Enumerates standard agronomic seasons used across Core Lens for temporal filtering.
- KHARIF = 'kharif'#
- RABI = 'rabi'#
- ZAID = 'zaid'#
- CURRENT = 'current'#
- class core_lens.base.view.View(keys: polars.DataFrame | polars.LazyFrame, entity: core_lens.base.entity.BaseEntity, entity_name: str, time_filter: dict[str, Any] | None = None, join_spec: dict[str, Any] | None = None, season_config: core_lens.aoi.SeasonConfig | None = None)#
Lazy, immutable snapshot of a scoped entity pending materialisation.
A
Viewis produced by the filter methods onBaseEntity(where(),spatial_filter(),spatial_join()). It records what to load without touching any Parquet file. Data is only scanned when one of the three materialisation properties is accessed, and is materialised when.df()is called on the returnedResult.Viewis immutable. Every method that would mutate state instead returns a newViewwith the updated field, leaving the original untouched. This makes it safe to branch a view and materialise it in different ways:view = aoi.mws.where(tehsil="Pangi") annual = view.between("2010-01-01", "2023-12-31").annual static = view.static
- Variables:
keys – A
pl.LazyFramecontaining the resolved key column(s) for all entity instances that passed the spatial/attribute filters. This is the index slice — no data columns, only IDs.entity – Reference to the parent
BaseEntitythat produced this view. Used during materialisation to resolve file paths and schema.entity_name – Human-readable name for the entity (e.g.
"mws"). Used to namespace joined columns and populateentity_name.time_filter – A dict encoding the pending time constraint set by
between(), orNoneif no time filter has been applied. The materialisation step reads this dict to build predicate pushdown expressions.join_spec – A dict encoding a deferred
spatial_join()request, orNone. Evaluated during materialisation after the primary scan is complete.
- keys#
- entity#
- entity_name#
- time_filter = None#
- join_spec = None#
- where(**kwargs: Any) View#
Return a new View further filtered by attributes.
Each keyword argument is interpreted as
column=valueapplied to the static file’s attribute columns. Multiple arguments are AND-ed.
- spatial_filter(geometry: shapely.Geometry | None = None, bbox: tuple[float, float, float, float] | None = None, relationship: str = 'centroid', threshold: float = 0.5) View#
Return a new View further filtered by geometry.
Uses the in-memory bbox index for a fast rectangular pre-filter, then refines with a Shapely STRtree exact-relationship check.
- Parameters:
geometry (shapely.Geometry | None, optional) – A Shapely geometry representing the spatial extent.
bbox (tuple[float, float, float, float] | None, optional) – Bounding box as
(minx, miny, maxx, maxy)in WGS-84.relationship (str, optional) –
"centroid"(default) or"area"mode.threshold (float, optional) – Area coverage threshold for
"area"mode. Default 0.5.
- Returns:
A new lazy
Viewscoped to the given spatial extent.- Return type:
- Raises:
ValueError – If neither
geometrynorbboxis provided.
- spatial_join(other: core_lens.base.entity.BaseEntity, agg: dict[str, str]) View#
Return a new View with a cross-entity join pending.
The join is recorded in the View’s
join_specand computed only at materialisation time (.static,.annual, or.sub_annual).- Parameters:
other (BaseEntity) – The secondary
BaseEntitywhose columns will be joined and aggregated ontoself.agg (dict[str, str]) – Mapping of
{column: aggregation}specifying which columns fromotherto bring in and how to aggregate them.
- Returns:
A new lazy
Viewwith the join spec recorded.- Return type:
- between(start: str | None = None, end: str | None = None, *, season: Season | None = None, year: int | tuple[int, int] | None = None) View#
Return a new
Viewwith a pending time filter applied.Two mutually exclusive modes are supported:
Date range mode — pass
startandendas ISO-8601 strings:view.between("2010-01-01", "2023-12-31")Season mode — pass
seasonas a keyword argument.yearis optional and may be a single year or an inclusive(from, to)tuple:from core_lens.base.view import Season view.between(season=Season.KHARIF) view.between(season=Season.KHARIF, year=2020) view.between(season=Season.KHARIF, year=(2018, 2023)) view.between(season=Season.CURRENT)
The returned
Viewcarries the filter intime_filterbut does not execute any I/O. The filter is applied during materialisation via predicate pushdown on the Parquet scan.- Parameters:
start (str | None, optional) – Start of the date range (ISO-8601). Required in date range mode; must be
Nonein season mode.end (str | None, optional) – End of the date range (ISO-8601). Required in date range mode; must be
Nonein season mode.season (Season | None, optional) – A
Seasonenum value. Activates season mode.year (int | tuple[int, int] | None, optional) – Year or inclusive year range to restrict the season filter. Only valid in season mode.
"current"season ignores this.
- Returns:
A new
Viewwithtime_filterset.- Return type:
- Raises:
ValueError – If the arguments are inconsistent (e.g. mixing date range and season arguments, omitting required args, or supplying
yearwithseason="current").
- property static: core_lens.base.result.Result#
Resolve the static GeoParquet file scan and return a
Result.The result always has
has_geometry=Truebecause the static file is a GeoParquet carrying geometry for every entity instance.
- property annual: core_lens.base.result.Result#
Resolve the annual Parquet file scan and return a
Result.
- property sub_annual: core_lens.base.result.Result#
Resolve the sub_annual Parquet file scan and return a
Result.