qewton.data.dataloaders.sampler package

Module contents

Submodules

qewton.data.dataloaders.sampler.grid_sampler module

class qewton.data.dataloaders.sampler.grid_sampler.GridSampler(geometry: Geometry, n_points: int | DiscreteHyperparameter | CategoricalHyperparameter, filter_fn: Callable | None = None, compute_normals: bool = False, normal_name: str | Variable = 'normals', name: str = 'PointSampler', state: NodeState = NodeState.FIXED, backend: type[ComputingBackend[TensorType]] = <class 'qewton.backends.base.DeepLearningBackend'>)[source]

Bases: PointSampler[TensorType]

Samples points in a grid-like fashion from a geometry.

Parameters:
  • geometry (Geometry) – The geometry in which points should be sampled.

  • n_points (int | Hyperparameter) – The number of points that should be sampled.

  • filter_fn (callable, optional) – An additional filter_fn that specifies at which location points should be sampled. Internally a rejection sampling strategy is used. Default is None.

  • compute_normals (bool, optional) – Whether to compute normals to each sampled points. This is only possible for BoundaryGeometries. Defaults to False.

  • normal_name (str | Variable) – The name for the output port of the normals. Defaults to “normals”.

  • name (str, optional) – The name of the node. Defaults to “PointSampler”.

  • state (NodeState, optional) – The state of the node. Defaults to NodeState.FIXED.

sample_points()[source]

Sample one batch of points.

Returns:

The points and the normals, or None for

the normals if compute_normals is not set.

Return type:

tuple of TensorType

qewton.data.dataloaders.sampler.point_sampler module

Base class for samplers that draw points from a geometry.

qewton.data.dataloaders.sampler.point_sampler.discretization_mode(samplers, max_vertex_distance, device: Device | str | None = None)[source]

Context manager that switches samplers to mesh mode.

Inside the context, every sampler outputs the vertices of a mesh of its geometry, see PointSampler.set_mesh_mode().

Parameters:
  • samplers (iterable of PointSampler) – The samplers to switch.

  • max_vertex_distance (float or dict or None) – The mesh resolution, one value or one per geometry variable.

  • device – The device the mesh points are built on. Defaults to each sampler’s own device. The samplers themselves are not moved.

qewton.data.dataloaders.sampler.point_sampler.active_discretization_mode(samplers, geometry)[source]

Context manager that makes samplers output a geometry’s points.

Inside the context, every sampler outputs the discretization points of geometry instead of sampling or meshing, see PointSampler.active_discretization. All samplers get the same geometry.

Parameters:
  • samplers (iterable of PointSampler) – The samplers to switch.

  • geometry (Geometry) – A discrete geometry with discretization_points.

class qewton.data.dataloaders.sampler.point_sampler.PointSampler(geometry: Geometry, n_points: int | DiscreteHyperparameter | CategoricalHyperparameter, filter_fn: Callable | None = None, compute_normals: bool = False, normal_name: str | Variable = 'normals', name: str = 'PointSampler', state: NodeState = NodeState.FIXED, backend: type[ComputingBackend[TensorType]] = <class 'qewton.backends.base.DeepLearningBackend'>)[source]

Bases: DataNode[TensorType]

Base class for data nodes that sample points from a geometry.

The node has one output port for the points, named after the geometry variable, and optionally one for the normals. Each forward pass returns, in this order of precedence: the points of active_discretization, the vertices of a mesh in mesh mode, the next cached batch, or newly sampled points. The current points are also stored in sampled_geometry, e.g. for plotting.

Parameters:
  • geometry – The geometry to sample from. Must use the same backend as the node.

  • n_points – The number of points sampled per batch.

  • filter_fn – Restricts where points are sampled, by rejection sampling. Its parameters are annotated with the variables (parts of the geometry variable) whose coordinates they receive; it returns a boolean mask of accepted points.

  • compute_normals – Whether to also output the normal vector at each point. Only possible for a BoundaryGeometry.

  • normal_name – The variable, or the name of the variable, of the normal output port.

  • name – The name of the node.

  • state – The state of the node.

  • backend – The backend used for computations.

Raises:

ValueError – If compute_normals is set for a geometry that is not a boundary.

geometry

The geometry to sample from.

Type:

Geometry

sampled_geometry

Holds the points of the current forward pass, see SampledGeometry.

Type:

SampledGeometry

compute_normals

Whether normals are output.

Type:

bool

normal_name

The variable of the normal output port.

Type:

str or Variable

filter_fn

The rejection filter.

Type:

callable or None

point_cache

Cached batches of points, see cache().

Type:

TensorType or None

normal_cache

Cached batches of normals, see cache().

Type:

TensorType or None

mesh_mode

Whether mesh mode is active, see set_mesh_mode().

Type:

bool

active_discretization

If set, the sampler outputs the discretization points of this geometry, overriding sampling and mesh mode. Setting it validates the geometry and raises ValueError if its dimension differs from the sampler’s, its bounding box does not overlap the sampler’s domain on some axis, or any of its points lies outside the domain.

Type:

Geometry or None

Notes

Subclasses must implement sample_points().

property active_discretization: Geometry | None
visualization_geometry(max_vertex_distance: float | dict[Variable, float] | None = None, device: Device | str | None = None) → Geometry[source]

Return this sampler’s geometry meshed for visualization.

The mesh is built once per distinct (max_vertex_distance, device) pair and cached.

Parameters:
  • max_vertex_distance – The maximum vertex distance of the mesh. A dict gives one value per geometry variable: a ProductGeometry receives it as-is to mesh each factor, any other geometry uses the entry of its own variable.

  • device – The device of the mesh. None uses the default device of create_mesh().

Returns:

The meshed geometry.

Return type:

Geometry

set_mesh_mode(max_vertex_distance: float | dict[Variable, float] | None = None, device: Device | str | None = None)[source]

Switch the sampler to mesh mode.

In mesh mode, the forward pass outputs the vertices of a mesh of the geometry (see visualization_geometry()) and stores the cells in sampled_geometry.

Parameters:
  • max_vertex_distance – The mesh resolution, one value or one per geometry variable.

  • device – The device the mesh is built on. Defaults to the sampler’s current device.

unset_mesh_mode()[source]

Leave mesh mode and return to normal sampling.

cache(n_batches=1)[source]

Sample a number of batches once and serve them from memory.

Later forward passes cycle through the cached batches.

Parameters:

n_batches (int, optional) – The number of batches to cache (at least one). Defaults to 1.

clear_cache()[source]

Clear the cache and return to sampling on every forward pass.

provides_data_in_phase(phase: EvaluationPhase) → bool[source]

Return whether the sampler provides data in phase.

Parameters:

phase – The evaluation phase.

Returns:

Always True.

Return type:

bool

abstractmethod sample_points() → tuple[TensorType, TensorType | None][source]

Sample one batch of points.

Returns:

The points and the normals, or None for

the normals if compute_normals is not set.

Return type:

tuple of TensorType

forward() → TensorType | tuple[TensorType, TensorType | None][source]

Return the points (and normals) for one batch.

Returns:

The points, or

(points, normals) if compute_normals is set. Points of active_discretization are flattened to shape (n_points, dim) and moved to the sampler’s device; mesh mode points are on the device set by set_mesh_mode.

Return type:

TensorType or tuple of TensorType

Raises:

NotImplementedError – If normals are requested together with active_discretization or mesh mode.

property sampled_geometries: list[SampledGeometry]

Every SampledGeometry this sampler updates, e.g. to convert all of them for plotting.

to(device: str | Device)[source]

Move the node, its geometry and its cached points to device.

Parameters:

device – The target device.

qewton.data.dataloaders.sampler.product_sampler module

Sampler for the Cartesian product of two samplers.

class qewton.data.dataloaders.sampler.product_sampler.ProductSampler(sampler_a: PointSampler, sampler_b: PointSampler, name: str = 'ProductSampler')[source]

Bases: PointSampler[TensorType]

Sampler that combines every point of one sampler with every point of another.

Usually created with sampler_a * sampler_b. The geometry is the product of both geometries. The point output has the geometry axes of sampler_a followed by those of sampler_b, and one feature axis holding the concatenated coordinates.

Parameters:
  • sampler_a – The first factor sampler.

  • sampler_b – The second factor sampler. Must use the same backend and a different variable than sampler_a.

  • name – The name of the node.

sampler_a

The first factor sampler.

Type:

PointSampler

sampler_b

The second factor sampler.

Type:

PointSampler

Notes

Normals are output if one of the factor samplers computes normals; that sampler’s normals are broadcast over the other factor.

set_mesh_mode(max_vertex_distance: float | dict[Variable, float] | None = None, device: Device | str | None = None)[source]

Switch this sampler and both factor samplers to mesh mode.

Parameters:
  • max_vertex_distance – The mesh resolution, one value or one per geometry variable.

  • device – The device the meshes are built on.

unset_mesh_mode()[source]

Leave mesh mode in this sampler and both factor samplers.

forward()[source]

Return the product points (and normals) for one batch.

In mesh mode, the vertices of both factor meshes are combined.

Returns:

The points, or

(points, normals) if normals are computed.

Return type:

TensorType or tuple of TensorType

sample_points() → tuple[TensorType, TensorType | None][source]

Sample both factors and combine every pair of points.

Returns:

The points of shape

(*geometry_shape_a, *geometry_shape_b, dim_a + dim_b) and the broadcast normals, or None.

Return type:

tuple of TensorType

property sampled_geometries: list[SampledGeometry]

This sampler’s SampledGeometry and those of both factor samplers.

to(device: str | Device)[source]

Move this sampler and both factor samplers to device.

Parameters:

device – The target device.

qewton.data.dataloaders.sampler.random_sampler module

class qewton.data.dataloaders.sampler.random_sampler.RandomUniformSampler(geometry: Geometry, n_points: int | DiscreteHyperparameter | CategoricalHyperparameter, filter_fn: Callable | None = None, compute_normals: bool = False, normal_name: str | Variable = 'normals', name: str = 'PointSampler', state: NodeState = NodeState.FIXED, backend: type[ComputingBackend[TensorType]] = <class 'qewton.backends.base.DeepLearningBackend'>)[source]

Bases: PointSampler

Samples points uniformly at random from a geometry.

sample_points()[source]

Sample one batch of points.

Returns:

The points and the normals, or None for

the normals if compute_normals is not set.

Return type:

tuple of TensorType