forgi 2.0.0 documentation

«  forgi.projection package   ::   Contents   ::   forgi.projection.projection2d module  »

forgi.projection.hausdorff module

forgi.projection.hausdorff.offsets(skip=0, to_iterate=[], toskip={})[source]

An iterator over offsets and their length ((dx,dy), norm((dx,dy))) in the order of increasing norm((dx,dy)) dx and dy are integers, starting at (0,0).

Parameters:
  • skip – A float. The iterator may skip cell up to SKIP away from (0,0) before returning the first value.
  • to_iterate – Do not use this. The mutable default parameter is used as a hack for caching.
  • toskip – Do not use this. The mutable default parameter is used as a hack for caching.
Yields:

A tuple ((dx,dy), n) where dx and dy are integers and n=norm((dx,dy)).

forgi.projection.hausdorff.modified_hausdorff_distance(img, ref_img, _=None)[source]

Return the grid-based Modified Hausdorff distance between two aligned boolean matrices. This distance was proposed in the following paper: TODO It uses the mean of all distances instead of the max. The current implementation is not optimized

forgi.projection.hausdorff.hausdorff_distance(img, ref_img, cutoff=inf)[source]

Return the grid-based Hausdorff distance between two aligned boolean matrices.

Parameters:
  • ref_img (img,) – Two aligned boolean 2D matrices with the same shape.
  • cutoff – A float. If the distance is greater than cutoff, return float(“inf”). (Used to increase execution speed in certain cases)
forgi.projection.hausdorff.locally_minimal_distance(ref_img, scale, cg, start_rot=None, offset=None, proj_dir=None, maxiter=50, advanced=False, virtual_atoms=True, distance=<function hausdorff_distance>)[source]

Local optimization of the Hausdorff distance between a image and a CoarseGrainRNA

For a given reference image and a given CoarseGrainRNA, the distance is a function of continuouse variables (projection direction, offset and rotation), but is a stepfunction, because it is grid-based. Therefor traditional optimization approaches, which rely on a gradient, fail. This implementation tests, whether the distance is increasing or decreasing in different directions, no matter how far one has to walk in that direction before the distance changes. It then follows the deepest decrease (not the steepest decrease!)

Parameters:
  • ref_img – The reference image. A boolean square matrix.
  • scale – The edge length in Angstrom of the reference image.
  • cg – The coarse grain RNA to match to the projection.
  • start_rot – The in-plane rotation of the projection.
  • offset – The offset that will be applied to the projection in Angstrom. A np.array([x,y]).
  • proj_dir – The starting projection direction in spherical polar coordinates. If this is not given: uses cg.project_from.
  • maxiter – Maximal number of iterations. An int.
  • distance – a function with signature like hausdorff_distance
  • advanced – Try steps in more directions (takes longer)
  • virtual_atoms – Whether or not to project the virtual atoms. A boolean.
Returns:

A triple: (distance, image, parameters) Where distance is a float,image a matrix and params is a triple: np.array([theta, phi]), degrees, np.array([x_offset, y_offset])

forgi.projection.hausdorff.globally_minimal_distance(ref_img, scale, cg, start_points=40, starting_rotations=(0, 180), starting_offsets=(array([0, 0]), ), local_maxiter=5, use_heuristic=True, virtual_atoms=True, verbose=False, distance=<function hausdorff_distance>)[source]

Global minimization of Hausdorff distance.

Uses several Heuristics to speed up the process.

Parameters:
  • ref_img – The reference image. A boolean square matrix.
  • scale – The edge length in Angstrom of the reference image.
  • cg – The coarse grain RNA to match to the projection.
  • start_points – Number of starting projection directions. For each, local optimization with maxiter steps is performed.
  • starting_rotations – A list of in-plane rotations in degrees.
  • starting_offsets – A list/array of np.arrays of the type np.array([x,y]).
  • local_maxiter – Maximal iteration for each local optimization
  • use_heuristic – A Boolean
  • virtual_atoms – Boolean. If False, do not project virtual atoms (faster)
  • verbose – If True, print a summary at the end.
Returns:

A triple: (best_distance, best_image, best_parameters) Where best_distance is a float, best_image a matrix and best params is a triple: np.array([theta, phi]), degrees, np.array([x_offset, y_offset])

forgi.projection.hausdorff.get_box(projection, width, offset=array([0, 0]))[source]
forgi.projection.hausdorff.get_longest_img_diameter(img, scale)[source]
forgi.projection.hausdorff.try_parameters(ref_img, scale, cg, rotations=(0, 180), offsets=(array([0, 0]), ), proj_directions=None, virtual_atoms=True)[source]

Try all combinations of the given starting parameters (offset, in-plane rotation and projection direction) and find the ones with the shorthest Huasdorff distance.

Parameters:
  • ref_img – The reference image. A boolean square matrix.
  • scale – The edge length in Angstrom of the reference image.
  • cg – The coarse grain RNA to match to the projection.
  • rotations – A list of in-plane rotations in degrees.
  • offsets – A list/array of np.arrays of the type np.array([x,y]).
  • proj_directions – A list of projection_directions (tuples theta, phi) or None. If None, uses cg.project_from.
  • virtual_atoms – Boolean. If False, do not project virtual atoms (faster)
Returns:

A triple: (best_distance, best_image, best_parameters) Where best_distance is a float, best_image a matrix and best params is a triple: np.array([theta, phi]), degrees, np.array([x_offset, y_offset])

«  forgi.projection package   ::   Contents   ::   forgi.projection.projection2d module  »