Skip to content

Commit

Permalink
Reformat Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
Luthaf committed Oct 23, 2023
1 parent 6d3b53e commit 29b7874
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
4 changes: 2 additions & 2 deletions python/chemiscope/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from .structures import ( # noqa
all_atomic_environments,
arrow_from_vector,
ase_tensors_to_ellipsoids,
ase_vectors_to_arrows,
center_shape,
composition_properties,
ellipsoid_from_tensor,
ase_vectors_to_arrows,
ase_tensors_to_ellipsoids,
extract_properties,
librascal_atomic_environments,
)
Expand Down
54 changes: 34 additions & 20 deletions python/chemiscope/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,30 @@ def create_input(
Each of these can contain some or all of the parameters associated with each shape,
and the parameters for each shape are obtained by combining the parameters from the
most general to the most specific, i.e., if there is a duplicate key in the `global` and `atom`
fields, the value within the `atom` field will supersede the `global` field for that atom.
The parameters for atom `k` that is part of structure `j` are obtained as
most general to the most specific, i.e., if there is a duplicate key in the
`global` and `atom` fields, the value within the `atom` field will supersede the
`global` field for that atom. The parameters for atom `k` that is part of structure
`j` are obtained as
.. code-block:: python
global_parameters.update(structure_j).update(atom_k)
If given, the `structure` parameters list should contain one entry per structure, and
the `atom` parameters list should be a flat list corresponding to the atoms of each consecutive structure.
All shapes accept a few general parameters, and some specific ones
If given, the `structure` parameters list should contain one entry per structure,
and the `atom` parameters list should be a flat list corresponding to the atoms of
each consecutive structure. All shapes accept a few general parameters, and some
specific ones
.. code-block:: python
# general parameters
{
"position" : [float, float, float], # centering (defaults to origin for structure, atom position for atom)
"scale" : float, # scaling of the size of the shape
"orientation" [float, float, float, float], # optional, given as quaternion in (x, y, z, w) format
# centering (defaults to origin for structure, atom position for atom)
"position" : [float, float, float],
# scaling of the size of the shape
"scale" : float,
# optional, given as quaternion in (x, y, z, w) format
"orientation" [float, float, float, float],
"color" : string | hex code # e.g. 0xFF0000
}
Expand All @@ -226,7 +231,9 @@ def create_input(
"vector" : [float, float, float], # orientation and shape of the arrow
"baseRadius" : float,
"headRadius" : float,
"headLength" : float, # the tip of the arrow is at the end of the segment. it'll extend past the base point if the arrow is not long enough
# the tip of the arrow is at the end of the segment.
# It will extend past the base point if the arrow is not long enough
"headLength" : float,
}
# "kind" : "custom"
Expand All @@ -235,7 +242,9 @@ def create_input(
[float, float, float],
...
],
"simplices": [ # mesh triangulation (optional); computed via convex triangulation where omitted
# mesh triangulation (optional); computed via convex triangulation
# where omitted
"simplices": [
[int, int, int], # indices refer to the list of vertices
...
],
Expand Down Expand Up @@ -853,7 +862,8 @@ def _validate_shapes(structures, shapes):
for shape_key in shapes_for_key:
if shape_key not in ["kind", "parameters"]:
raise ValueError(
f"Invalid entry `{shape_key}` in the specifications for shape `{key}`"
f"Invalid entry `{shape_key}` in the specification "
f"for shape `{key}`"
)

base_shape = shapes_for_key["parameters"].get("global", {})
Expand All @@ -867,14 +877,14 @@ def _validate_shapes(structures, shapes):
and len(structure_parameters) <= structure_i
):
raise TypeError(
f"structure_parameters must be a list with length {(len(structures))}, "
f"got length={len(structure_parameters)} instead"
f"structure_parameters must be a list with {(len(structures))} "
f"elements, got {len(structure_parameters)}"
)
for _ in range(structures[structure_i]["size"]):
if atom_parameters is not None and len(atom_parameters) <= atom_counter:
raise TypeError(
f"atom_parameters must be a list coinciding to the atomic environments, "
f"got length={len(atom_parameters)} instead"
f"atom_parameters must be a list coinciding to the atomic "
f"environments, got {len(atom_parameters)} elements"
)

shape = {
Expand Down Expand Up @@ -938,7 +948,8 @@ def _check_valid_shape(shape):

if not isinstance(parameters["radius"], float):
raise TypeError(
f"sphere shape 'radius' must be a float, got {type(parameters['radius'])}"
"sphere shape 'radius' must be a float, got "
f"{type(parameters['radius'])}"
)

elif shape["kind"] == "ellipsoid":
Expand Down Expand Up @@ -985,15 +996,18 @@ def _check_valid_shape(shape):
elif shape["kind"] == "arrow":
if not isinstance(parameters["baseRadius"], float):
raise TypeError(
f"sphere shape 'baseRadius' must be a float, got {type(parameters['baseRadius'])}"
"sphere shape 'baseRadius' must be a float, "
f"got {type(parameters['baseRadius'])}"
)
if not isinstance(parameters["headRadius"], float):
raise TypeError(
f"sphere shape 'headRadius' must be a float, got {type(parameters['headRadius'])}"
"sphere shape 'headRadius' must be a float, "
f"got {type(parameters['headRadius'])}"
)
if not isinstance(parameters["headLength"], float):
raise TypeError(
f"sphere shape 'headLength' must be a float, got {type(parameters['headLength'])}"
"sphere shape 'headLength' must be a float, "
f"got {type(parameters['headLength'])}"
)

vector_array = np.asarray(parameters["vector"]).astype(
Expand Down
18 changes: 13 additions & 5 deletions python/chemiscope/jupyter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-
import gzip
import json
import warnings

import ipywidgets
from traitlets import Bool, Dict, Unicode

from .input import create_input
from .version import __version__

import warnings

# this needs to match the version/name defined in
# python/jupyter/src/labextension.ts
PACKAGE_NAME = "chemiscope"
Expand Down Expand Up @@ -62,9 +61,18 @@ def save(self, path):
def _repr_html_(self):
if not _is_running_in_notebook():
return """
<div style="width: 100%; background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; padding: 15px; font-size: 16px; box-sizing: border-box; text-align: center;">
Interactive chemiscope widgets can only be visualized inside a jupyter notebook.
</div>
<div
style="width: 100%;
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
padding: 15px;
font-size: 16px;
box-sizing: border-box;
text-align: center;"
>
Interactive chemiscope widgets can only be visualized inside a jupyter notebook.
</div>
"""
else:
return self.__repr__()
Expand Down
3 changes: 2 additions & 1 deletion python/chemiscope/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json

from .input import write_input
from .structures import all_atomic_environments, extract_properties
import json


def _chemiscope_input_parser():
Expand Down
7 changes: 1 addition & 6 deletions python/chemiscope/structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@
_ase_to_json,
_ase_valid_structures,
)
from ._shapes import arrow_from_vector, center_shape, ellipsoid_from_tensor # noqa

from ._ase import ( # noqa isort: skip
ase_vectors_to_arrows,
ase_tensors_to_ellipsoids,
)

from ._shapes import ( # noqa
arrow_from_vector,
center_shape,
ellipsoid_from_tensor,
)


def _guess_adapter(frames):
"""
Expand Down
3 changes: 2 additions & 1 deletion python/chemiscope/structures/_ase.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import warnings
from collections import Counter

from ._shapes import ellipsoid_from_tensor, arrow_from_vector
import numpy as np

from ._shapes import arrow_from_vector, ellipsoid_from_tensor

try:
import ase

Expand Down

0 comments on commit 29b7874

Please sign in to comment.