schrodinger.math.mathutils module

Contains math-related utility functions

Copyright Schrodinger, LLC. All rights reserved.

schrodinger.math.mathutils.round_value(val, precision=3, significant_figures=None)[source]

Return val as a string with the required precision or significant figures.

Either precision or significant_figures should be provided. Uses scientific notation for very large or small values, if the precision allows.

Parameters
  • val (float) – The value to round

  • precision (int) – The precision needed after rounding. A precision of 2 means two decimal places should be kept after rounding. A negative precision indicates how many digits can be replaced with zeros before the decimal point. -5 means that 123456 can be shown as 1e5, -3 means it can be shown as 1.23e5.

  • significant_figures (int) – The number of significant figures that should remain after rounding. If provided, determines the rounding precision.

Return type

str or None

Returns

A string with the required precision, or None if the input is a string

schrodinger.math.mathutils.deduplicate_xy_data(x_vals, y_vals)[source]

Remove duplicate x values by averaging the y values for them.

Parameters
  • x_vals (list) – The x values

  • y_vals (list) – The y values

Return type

list, list

Returns

The deduplicated xy data

class schrodinger.math.mathutils.Interpolate1D(source_vals, target_vals, log_interp=False)[source]

Bases: object

Creates a map between values in a source and a target axis, allowing to get the equivalent target point for each source point.

Wrapper around scipy.interpolate.interp1d to allow logarithmic interpolation or extrapolation.

__init__(source_vals, target_vals, log_interp=False)[source]

Create an instance.

Parameters
  • source_vals (tuple) – The values of points in the source range

  • target_vals (tuple) – The values of points in the target range

  • log_interp (bool) – Whether the interpolation is logarithmic. If False, linear interpolation will be used.

class schrodinger.math.mathutils.Interpolate2D(x_source_vals, x_target_vals, y_source_vals, y_target_vals, x_log_interp=False, y_log_interp=False)[source]

Bases: object

Creates two instances of Interpolate1D to map values between two source axes and two target axes. Example use case is mapping QGraphicsScene/QChart XY coordinates to a XY coordinate system being displayed in the scene/chart.

The two axes need to be independent.

__init__(x_source_vals, x_target_vals, y_source_vals, y_target_vals, x_log_interp=False, y_log_interp=False)[source]

Create an instance.

Parameters
  • x_source_vals (tuple) – The values of points in the X source range

  • x_target_vals (tuple) – The values of points in the X target range

  • y_source_vals (tuple) – The values of points in the Y source range

  • y_target_vals (tuple) – The values of points in the Y target range

  • x_log_interp (bool) – Whether the X axis interpolation is logarithmic

  • y_log_interp (bool) – Whether the Y axis interpolation is logarithmic

schrodinger.math.mathutils.roundup(inp)[source]

Round away from zero (to +/-infinity).

Parameters

inp (float or numpy.array) – value to be rounded

Return type

float or numpy.array

Returns

Rounded value

schrodinger.math.mathutils.sig_fig_round(value, significant_figure=5)[source]
Parameters
  • value (float) – change the significant figures of this value

  • significant_figure (int) – significant figure of the displayed value

Return type

str

Returns

str representation of a number with proper significant figures

schrodinger.math.mathutils.get_significant_figures(number)[source]

Get significant digits of a number

Parameters

number (float or int or str) – number to find significant digits

Return type

int

Returns

number of significant digits

schrodinger.math.mathutils.set_significant_figures(number, sig_fig)[source]

Set significant digits of a number

Parameters
  • number (float or int) – number to set significant digits

  • sig_fig (int) – number of significant digits

Return type

float

Returns

number with desired significant digits

schrodinger.math.mathutils.polyfit(xdata, ydata, degree)[source]

Fit a polynomial of rang degree to data.

Parameters
  • xdata (numpy.array) – X data

  • ydata (numpy.array) – Y data

  • degree (int) – Degree of the fitting polynomial

Return type

numpy.array, float

Returns

Fitting coefficients, coefficient of determination (R^2)

schrodinger.math.mathutils.dbscan_cluster(objects, eps, key=None)[source]

Cluster the given objects by numbers obtained via the given key function using a specified precision. Uses simple interface to sklearn.cluster.DBSCAN (density based spatial clustering).

Parameters
  • objects (list) – the objects to cluster

  • eps (float) – the precision that controls the size of the clusters, for example if objects is [102, 104, 307, 309, 99, 919, 918] an eps of 6 will produce 3 clusters, see sklearn.cluster.DBSCAN documentation for more details

  • key (function or None) – the function to get a number from the object, if None is the identity function

Raises

ValueError – if there is an issue

Return type

dict

Returns

clustered objects, keys are arbitrary cluster indices, values are lists of objects for the given cluster