xtal2png.utils package

Submodules

xtal2png.utils.data module

xtal2png.utils.data.assert_structures_approximate_match(example_structures, structures, tol_multiplier=1.0)[source]
xtal2png.utils.data.element_wise_scaler(X: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], feature_range: Sequence | None = None, data_range: Sequence | None = None)[source]

Scale parameters according to a prespecified min and max (data_range).

feature_range is preserved from MinMaxScaler

See also

sklearn.preprocessing.MinMaxScaler

Scale each feature to a given range.

Parameters:
  • X (ArrayLike) – Features to be scaled element-wise.

  • feature_range (Sequence) – The scaled values will span the range of feature_range

  • data_range (Sequence) – Expected bounds for the data, e.g. 0 to 117 for periodic elements

Returns:

Element-wise scaled values.

Return type:

X_scaled

Examples

>>> element_wise_scaler([[1, 2], [3, 4]], feature_range=[1, 4], data_range=[0, 8])
array([[1.375, 1.75 ],
    [2.125, 2.5  ]])
xtal2png.utils.data.element_wise_unscaler(X_scaled: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], feature_range: Sequence, data_range: Sequence)[source]

Scale parameters according to a prespecified min and max (data_range).

feature_range is preserved from MinMaxScaler

See also

sklearn.preprocessing.MinMaxScaler

Scale each feature to a given range.

Parameters:
  • X (ArrayLike) – Element-wise scaled values.

  • feature_range (Sequence) – The scaled values will span the range of feature_range

  • data_range (Sequence) – Expected bounds for the data, e.g. 0 to 117 for periodic elements

Returns:

Element-wise unscaled values.

Return type:

X

Examples

>>> element_wise_unscaler(
...     [[1.375, 1.75], [2.125, 2.5]], feature_range=[1, 4], data_range=[0, 8]
... )
array([[1., 2.],
   [3., 4.]])
xtal2png.utils.data.get_image_mode(d: ndarray) str[source]

Get the image mode (i.e. “RGB” vs. grayscale (“L”)) for an image array.

Parameters:

d (np.ndarray) – A NumPy array with 3 dimensions, where the first dimension corresponds to the of image channels and the second and third dimensions correspond to the height and width of the image.

Returns:

mode – “RGB” for 3-channel images and “L” for grayscale images.

Return type:

str

Raises:
  • ValueError – “expected an array with 3 dimensions, received {d.ndim} dims”

  • ValueError – “Expected a single-channel or 3-channel array, but received a {d.ndim}-channel array.”

Examples

>>> d = np.zeros((1, 64, 64), dtype=np.uint8) # grayscale image
>>> mode = get_image_mode(d)
"L"
xtal2png.utils.data.rgb_scaler(X: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], data_range: Sequence | None = None)[source]

Scale parameters according to RGB scale (0 to 255).

feature_range is fixed to [0, 255], data_range is either specified

See also

sklearn.preprocessing.MinMaxScaler

Scale each feature to a given range.

Parameters:
  • X (ArrayLike) – Features to be scaled element-wise.

  • data_range (Optional[Sequence]) – Range to use in place of np.min(X) and np.max(X) as in MinMaxScaler.

Returns:

Element-wise scaled values.

Return type:

X_scaled

Examples

>>> rgb_scaler([[1, 2], [3, 4]], data_range=[0, 8])
array([[ 32,  64],
    [ 96, 128]], dtype=uint8)
xtal2png.utils.data.rgb_unscaler(X: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], data_range: Sequence)[source]

Unscale parameters from their RGB scale (0 to 255).

feature_range is fixed to [0, 255], data_range is either specified or calculated based on min and max.

See also

sklearn.preprocessing.MinMaxScaler

Scale each feature to a given range.

Parameters:
  • X (ArrayLike) – Element-wise scaled values.

  • data_range (Optional[Sequence]) – Range to use in place of np.min(X) and np.max(X) as in class:MinMaxScaler.

Returns:

Unscaled features.

Return type:

X

Examples

>>> rgb_unscaler([[32, 64], [96, 128]], data_range=[0, 8])
array([[1, 2],
      [3, 4]])
xtal2png.utils.data.unit_cell_converter(s: Structure, cell_type: str | None = None, symprec=0.1, angle_tolerance=5.0)[source]

Convert from the original unit cell type to another unit cell via pymatgen.

Parameters:
  • s (Structure) – a pymatgen Structure.

  • cell_type (Optional[str], optional) – The cell type as a str or None if leaving the structure as-is. Possible options are “primitive_standard”, “conventional_standard”, “refined”, “reduced”, and None. By default None

Returns:

s – The converted Structure.

Return type:

Structure

Raises:

ValueError – “Expected one of ‘primitive_standard’, ‘conventional_standard’, ‘refined’, ‘reduced’ or None, got {cell_type}”

Examples

>>> s = unit_cell_converter(s, cell_type="reduced")

xtal2png.utils.plotting module

xtal2png.utils.plotting.matplotlibify(fig: Figure, size: int = 24, width_inches: float | int = 3.5, height_inches: float | int = 3.5, dpi: int = 142, return_scale: bool = False) Figure[source]

Make plotly figures look more like matplotlib for academic publishing.

modified from: https://medium.com/swlh/fa56ddd97539

Parameters:
  • fig (go.Figure) – Plotly figure to be matplotlibified

  • size (int, optional) – Font size for layout and axes, by default 24

  • width_inches (Union[float, int], optional) – Width of matplotlib figure in inches, by default 3.5

  • height_inches (Union[float, int], optional) – Height of matplotlib figure in Inches, by default 3.5

  • dpi (int, optional) – Dots per inch (resolution) of matplotlib figure, by default 142. Leave as default unless you’re willing to verify nothing strange happens with the output.

  • return_scale (bool, optional) – If true, then return scale which is a quantity that helps with creating a high-resolution image at the specified absolute width and height in inches. More specifically: >>> width_default_px = fig.layout.width >>> targ_dpi = 300 >>> scale = width_inches / (width_default_px / dpi) * (targ_dpi / dpi) Feel free to ignore this parameter.

Returns:

fig – The matplotlibified plotly figure.

Return type:

go.Figure

Examples

>>> import plotly.express as px
>>> df = px.data.tips()
>>> fig = px.histogram(df, x="day")
>>> fig.show()
>>> fig = matplotlibify(fig, size=24, width_inches=3.5, height_inches=3.5, dpi=142)
>>> fig.show()

Note the difference between https://user-images.githubusercontent.com/45469701/171044741-35591a2c-dede-4df1-ae47-597bbfdb89cf.png # noqa: E501 and https://user-images.githubusercontent.com/45469701/171044746-84a0deb0-1e15-40bf-a459-a5a7d3425b20.png, # noqa: E501 which are both static exports of interactive plotly figures.

xtal2png.utils.plotting.plot_and_save(fig_path, fig, mpl_kwargs={}, show=False, update_legend=False)[source]

Module contents