pysdic.SDICAnalysis#

The pysdic.SDICAnalysis stores the data of a stereo digital image correlation analysis: a reference mesh, the views (camera and image) acquired at each frame, the displacement of the vertices at each frame and, optionally, the acquisition times of the frames.

SDICAnalysis
├── mesh                     (reference configuration, not copied but locked)
├── times                    (N_f,) strictly increasing, or None
├── frame 0
│   ├── displacement         (N_v, E), read-only (not set by default)
│   └── views                [View 0, View 1, ...] (possibly empty)
├── frame 1
│   └── ...
└── frame N_f - 1

The views are linked by pysdic.Pairing objects, used by the workflows and the terms of the analysis (see pysdic.SDICCorrelationTerm). The unknowns of an optimization (displacements of some frames, parameters of some transformations) are described by a pysdic.SDICUnknowns, validated by the analysis, which builds the vector of unknowns (see Unknowns): its displacements are always total displacements from the reference mesh.

SDICAnalysis class#

class SDICAnalysis(mesh, views, *, times=None, displacements=None)[source]#

Bases: object

The data of a stereo digital image correlation (SDIC) analysis: a reference mesh, the views (camera and image) acquired at each frame and the displacement of the vertices of the mesh at each frame.

  • The mesh is the reference (undeformed) configuration. It is not copied but locked (see pysdic.core.lockable.LockableMixin): its vertices and elements cannot be modified while it is used by the analysis. A mesh that is already locked (for example by another analysis) is accepted and shared.

  • The views of each frame are given at the creation, in their index order. Several views can share the same camera or the same image (see display_structure()). A frame can have no view (for example a frame only used by a temporal regularization).

  • The displacements are stored in the analysis (not in the mesh), one array of shape \((N_v, E)\) per frame. A frame without displacement raises an error when its displacement is used. Once set, a displacement can be replaced but not removed.

  • The times of acquisition of the frames (optional) are strictly increasing with the frame index: the frames are in chronological order. They are needed by the temporal operators (see pysdic.SDICTemporalRegularizationTerm).

Important

The structure of the analysis (mesh, number of frames, views) is fixed at the creation: the views, cameras and images are immutable, and the mesh is locked. Only the displacements (set_displacement()) and the parameters of the transformations of the cameras (set_transformation_parameters()) can be modified. Use with_mesh(), with_views(), with_frame_views() and with_times() to get a new analysis with another mesh, other views or other times: the new analysis shares the other data (no copy).

The views are linked by pysdic.Pairing objects, used by the workflows and the operators of the analysis. The unknowns of an optimization are described by a pysdic.SDICUnknowns (which does not know the analysis): the analysis validates it (validate_unknowns()) and builds the vector of unknowns from its current state (pack_unknowns(), unpack_unknowns(), apply_unknowns()).

Parameters:
  • mesh (pysdic.Mesh) – The reference mesh, with an element type. It is not copied: it is locked.

  • views (Sequence[Sequence[pysdic.View]]) – The views of each frame, views[f] being the views of the frame \(f\) in their index order (possibly empty). The number of frames \(N_f\) is len(views) (at least 1). The views are not copied (they are immutable). Their cameras must have the dimension of the mesh as input dimension.

  • times (Optional[ArrayLike], optional (default: None)) – The times of acquisition of the frames, of shape \((N_f,)\), finite and strictly increasing (copied), in any unit. If None, the times are not defined (the temporal operators raise an error).

  • displacements (Optional[Mapping[Integral, ArrayLike]], optional (default: None)) – The initial displacements, as a dictionary {frame: displacement} with arrays of shape \((N_v, E)\) (copied). The other frames have no displacement. Equivalent to calling set_displacement() for each frame.

Raises:
  • TypeError – If an input has a wrong type.

  • ValueError – If the mesh has no element type, there is no frame, the input dimension of a camera is not the dimension of the mesh, the times are invalid, a displacement is invalid, or the transformations of the cameras are not independent (see check_consistency()).

  • IndexError – If a frame of displacements is out of range.

Examples

from pysdic import SDICAnalysis, View

views = [
    [View(left_camera, left_images[f]), View(right_camera, right_images[f])]
    for f in range(n_frames)
]
analysis = SDICAnalysis(mesh, views, displacements={0: numpy.zeros((mesh.n_vertices, 3))})

mesh.coordinates = ...   # RuntimeError: the mesh is used by the analysis

Instantiation#

The SDICAnalysis class is instantiated with its constructor SDICAnalysis(mesh, views, *, times=None, displacements=None):

  • mesh: the reference mesh, with an element type. It is not copied but locked (see below).

  • views: the views of each frame, views[f] being the list of the views of the frame f (a frame can have no view). The number of frames is len(views).

  • times: the acquisition times of the frames, strictly increasing (required by pysdic.SDICTemporalRegularizationTerm), or None.

  • displacements: the initial displacements, a dictionary {frame: (N_v, E) array}. The other frames have no displacement.

from pysdic import SDICAnalysis, View

views = [[View(left, images[f, 0]), View(right, images[f, 1])] for f in range(2)]
analysis = SDICAnalysis(mesh, views, times=[0.0, 0.1])
analysis.set_zero_displacement(0)

The usual acquisition setups can be built directly with the constructors functions (see SDIC analysis):

Important

The structure of the analysis (mesh, views, times) is fixed at its creation: the structures of the shared objects are computed once and checked by SDICAnalysis.check_consistency().

  • The mesh is locked: its vertices and its connectivity cannot be modified (its properties can). Work on a copy (mesh.copy()) and use SDICAnalysis.with_mesh() to analyse another mesh. A mesh already locked (e.g. by another analysis) is accepted and shared.

  • The with_* methods create a new analysis sharing everything else (images, cameras, displacements).

  • The displacements and the parameters of the transformations remain modifiable.

  • The terms and parametrizations built on an analysis keep this analysis: an analysis created with a with_* method needs new terms (the same pysdic.SDICUnknowns can be reused).

Implemented methods and attributes#

The following sections provide an overview of the attributes and methods defined in the pysdic.SDICAnalysis class.

Attributes#

SDICAnalysis.mesh

[Get] The reference (undeformed) mesh of the analysis.

SDICAnalysis.n_frames

[Get] The number of frames of the analysis.

SDICAnalysis.times

[Get] The times of acquisition of the frames, strictly increasing.

New analyses#

A new analysis with another mesh, other views or other times. The other objects are shared (not copied), and the displacements are kept when it is possible (keep_displacements).

SDICAnalysis.with_mesh(mesh, *[, ...])

Create a new analysis with another mesh and the same views.

SDICAnalysis.with_views(views, *[, times, ...])

Create a new analysis with the same mesh and other views for all the frames.

SDICAnalysis.with_frame_views(frame, views)

Create a new analysis where the views of one frame are replaced.

SDICAnalysis.with_times(times)

Create a new analysis with other times of acquisition of the frames.

Displacements#

The displacements are stored in the analysis (not in the mesh), one read-only array of shape \((N_v, E)\) per frame. A frame without displacement raises an error when its displacement is used, and a displacement cannot be removed once set (set_displacement(frame, None) raises an error).

SDICAnalysis.set_displacement(frame, ...)

Set (or replace) the displacement of the vertices of the mesh at a frame.

SDICAnalysis.set_zero_displacement(frame)

Set a zero displacement of shape \((N_v, E)\) at a frame: the vertices are at the coordinates of the mesh.

SDICAnalysis.has_displacement(frame)

Check if the displacement of a frame is set.

SDICAnalysis.get_displacement(frame)

Get the displacement of the vertices of the mesh at a frame.

SDICAnalysis.get_deformed_coordinates(frame)

Get the coordinates of the vertices of the mesh deformed at a frame: \(\vec{X} + \vec{u}(\text{frame})\).

SDICAnalysis.get_deformed_mesh(frame)

Get a copy of the mesh deformed at a frame (same connectivity and properties).

Views#

The views of a frame are indexed in the order of the construction. The views are not copied, and the input dimension of their camera must be the dimension of the mesh.

SDICAnalysis.get_view(frame, index)

Get a view of a frame.

SDICAnalysis.get_views(frame)

Get the views of a frame.

SDICAnalysis.n_views(frame)

Get the number of views of a frame.

Pairings#

SDICAnalysis.validate_pairing(pairing)

Check that the two views of a pairing exist in the analysis and that their images have the same number of channels.

Unknowns#

The unknowns of an optimization are described by a pysdic.SDICUnknowns, which does not know the analysis (as a pysdic.Pairing). The analysis validates it and builds the vector of unknowns \(\vec{p} = [\vec{p}_U, \vec{p}_T]\) from its current state: the total displacements of the optimized frames (Fortran order) and the parameters of the optimized transformations.

unknowns = SDICUnknowns([1], [("extrinsic", 1)])
analysis.validate_unknowns(unknowns)

p0 = analysis.pack_unknowns(unknowns)                                 # Current state -> p
displacements, parameters = analysis.unpack_unknowns(unknowns, p)     # p -> copies
analysis.apply_unknowns(unknowns, result.parameters)                  # p -> analysis (replaced, not added)

Warning

With a parametrization, the solver works on a reduced vector \(\vec{q}\): use parametrization.reparametrized_pack/unpack/apply (see pysdic.SDICFixedParametrization).

SDICAnalysis.validate_unknowns(unknowns)

Check that a pysdic.SDICUnknowns can be used with the analysis: its optimized frames exist, its optimized transformations exist, are distinct objects, have parameters and are not locked.

SDICAnalysis.n_unknowns(unknowns)

The size of the vector of unknowns \(N_f N_v E + \sum n_p\) of a pysdic.SDICUnknowns in the analysis.

SDICAnalysis.pack_unknowns(unknowns)

The complete vector of unknowns \(\vec{p}\) of the current state of the analysis (e.g. the initial parameters p0 without parametrization): the total displacements of the optimized frames, then the parameters of the optimized transformations.

SDICAnalysis.unpack_unknowns(unknowns, ...)

Split a complete vector of unknowns (inverse of pack_unknowns()).

SDICAnalysis.apply_unknowns(unknowns, parameters)

Write a complete vector of unknowns (e.g. result.parameters without parametrization) in the analysis: the displacements of the optimized frames (set_displacement(), replaced, not added) and the parameters of the optimized transformations (set_transformation_parameters(), all the cameras using them are updated).

SDICAnalysis.get_unknowns_frame_slice(...)

The slice of the displacement \((N_v E,)\) (Fortran order) of an optimized frame in the vector of unknowns.

SDICAnalysis.get_unknowns_transformation_slice(...)

The slice of the parameters of an optimized transformation in the vector of unknowns.

SDICAnalysis.compute_unknowns_indices(...[, ...])

The indices in the vector of unknowns of the displacements of selected vertices and components of all the optimized frames, followed by the indices of all the parameters of the optimized transformations.

Structure of the analysis#

Several views can share the same image, camera or transformation of camera. The structure of a kind of object ("view", "image", "camera", "extrinsic", "distortion" or "intrinsic") gives, for each view, the index of its object. The objects are compared by identity and numbered in the order of their first occurrence along the frames and the views.

analysis.display_structure("extrinsic")
# Structure of the extrinsics (index of the shared extrinsic of each view):
#   Frame 0  : [0, 1]
#   Frame 1  : [0, 1]
p = analysis.get_transformation_parameters("extrinsic", 1)
analysis.set_transformation_parameters("extrinsic", 1, p)  # All the views using the extrinsic 1

SDICAnalysis.compute_structure(kind)

Compute the sharing of an object between the views of the analysis.

SDICAnalysis.display_structure(kind)

Print the sharing of an object between the views of the analysis (see compute_structure()).

SDICAnalysis.get_transformation(kind, index)

Get a transformation of the cameras of the analysis by its index in the structure of its kind (see compute_structure() and display_structure()).

SDICAnalysis.get_transformation_parameters(...)

Get a copy of the parameters of a transformation of the cameras (see get_transformation()).

SDICAnalysis.set_transformation_parameters(...)

Set the parameters of a transformation of the cameras (see get_transformation()).

SDICAnalysis.check_consistency()

Check that the transformations of the cameras of the analysis are independent of each other.

Note

Each transformation (kind, index) of the structure owns its own parameters. SDICAnalysis.check_consistency() (called at the creation) checks that:

  • the extrinsics, the distortions and the intrinsics are disjoint sets (an object cannot be used as two kinds);

  • each unit transformation appears only once: the composed and reparametrized transformations are explored down to their unit transformations, and an error is raised if two entries (or two steps of the same entry) share a unit transformation (their parameters would be coupled without being declared as one unknown).

Save and load#

An analysis is saved in a folder (or a zip archive of the same files): the mesh, each image and each transformation once (even if shared), the displacements, the times and the structure of the analysis, with a SHA-256 manifest. No pickle is used. The writing is atomic (temporary folder renamed at the end), and overwrite=True only replaces a previous save of an analysis. At the loading, the integrity of the files is checked (check_integrity=True), then the summary of the metadata (numbers of frames, vertices, elements, views, images, cameras and transformations, frames with a displacement, times) and the structures of the loaded analysis are compared with the saved ones (“Inconsistent loading” error if they differ).

folder/
├── metadata.json        format, pysdic version, date, title, description, summary
├── README.txt           the same information, readable by a human
├── manifest.json        SHA-256 of each file
├── structure.json       cameras, views, frames, structures
├── mesh/mesh.npz        (+ mesh.vtk with export_vtk=True, never read)
├── images/image_00.npz ...
├── extrinsics/, distortions/, intrinsics/   one JSON file per transformation
├── displacements.npz    frame_00, ... (only the frames with a displacement)
└── times.npz            (only if the times are set)
analysis.to_folder("results/test_42", title="Tensile test 42")
same_analysis = SDICAnalysis.from_folder("results/test_42")

SDICAnalysis.to_folder(folder, *[, ...])

Save the analysis in a folder, one file per atom, to reload it exactly with from_folder().

SDICAnalysis.from_folder(folder, *[, ...])

Load an analysis saved with to_folder().

SDICAnalysis.to_archive(file_path, *[, ...])

Save the analysis in a single ZIP archive, to reload it exactly with from_archive().

SDICAnalysis.from_archive(file_path, *[, ...])

Load an analysis saved with to_archive().

Note

The integration points and the pairings are not part of the analysis: save them separately (e.g. pysdic.IntegrationPoints.to_npz_file()).

Saves of previous versions#

The compatibility of a save is given by the version of pysdic that wrote it (pysdic_version in metadata.json):

  • 2.1.0 and later: the current format, loaded directly.

  • 0.0.1 to 2.1.0 (including the saves of the former pysdic.SDIC class): converted to the current format while loading, with a pysdic.PreviousSaveWarning displayed in the console. The files are not modified: save the loaded analysis again (to_folder or to_archive) to update the save and remove the warning.

  • Before 0.0.1: deprecated, a pysdic.DeprecatedError is raised (a subclass of ValueError).

import warnings
from pysdic import PreviousSaveWarning, SDICAnalysis

analysis = SDICAnalysis.from_folder("results/old_test")   # PreviousSaveWarning
analysis.to_folder("results/old_test", overwrite=True)     # Updated: no more warning

warnings.simplefilter("error", PreviousSaveWarning)        # Or: refuse the saves of previous versions

PreviousSaveWarning

The SDIC save was written by a previous version of pysdic: it is converted to the current format while loading.

DeprecatedError

The SDIC save was written by a version of pysdic whose format can no longer be loaded.