Skip to content

Commit

Permalink
Adding isort and other rules for formatting (#99)
Browse files Browse the repository at this point in the history
* Added more rules for linting

Added organizeImports on save on settings.json

* Formatted all files

* Fix utils format
  • Loading branch information
JosePizarro3 authored Jul 8, 2024
1 parent 0e5707f commit bd2f695
Show file tree
Hide file tree
Showing 40 changed files with 344 additions and 366 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"files.trimTrailingWhitespace": true,
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "charliermarsh.ruff"
},
"python.testing.pytestArgs": [
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ select = [
"E", # pycodestyle
"W", # pycodestyle
"PL", # pylint
"F", # Pyflakes
"UP", # pyupgrade
"I", # isort
]

ignore = [
"E501", # Line too long ({width} > {limit} characters)
"E701", # Multiple statements on one line (colon)
"E731", # Do not assign a lambda expression, use a def
"E402", # Module level import not at top of file
"F401",
"PLR0911", # Too many return statements
"PLR0912", # Too many branches
"PLR0913", # Too many arguments in function definition
Expand Down
7 changes: 3 additions & 4 deletions src/nomad_simulations/schema_packages/atoms_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
# limitations under the License.
#

from typing import TYPE_CHECKING, Any, Dict, Optional, Union
from typing import TYPE_CHECKING, Any, Optional, Union

import ase
import numpy as np
import pint

from nomad.datamodel.data import ArchiveSection
from nomad.datamodel.metainfo.annotations import ELNAnnotation
from nomad.datamodel.metainfo.basesections import Entity
from nomad.metainfo import MEnum, Quantity, SubSection
from nomad.units import ureg

if TYPE_CHECKING:
from nomad.metainfo import Section, Context
from nomad.datamodel.datamodel import EntryArchive
from nomad.metainfo import Context, Section
from structlog.stdlib import BoundLogger

from nomad_simulations.schema_packages.utils import RussellSaundersState
Expand Down Expand Up @@ -154,7 +153,7 @@ def __init__(self, m_def: 'Section' = None, m_context: 'Context' = None, **kwarg
)
),
}
self._orbitals_map: Dict[str, Any] = {
self._orbitals_map: dict[str, Any] = {
'l_symbols': self._orbitals[-1],
'ml_symbols': {i: self._orbitals[i] for i in range(4)},
'ms_symbols': dict(zip((-0.5, 0.5), ('down', 'up'))),
Expand Down
13 changes: 6 additions & 7 deletions src/nomad_simulations/schema_packages/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
# limitations under the License.
#

from typing import TYPE_CHECKING, List
from typing import TYPE_CHECKING

import numpy as np

from nomad.config import config
from nomad.datamodel.data import Schema
from nomad.datamodel.metainfo.annotations import ELNAnnotation
Expand Down Expand Up @@ -204,15 +203,15 @@ def resolve_composition_formula(self, system_parent: ModelSystem) -> None:
"""

def set_composition_formula(
system: ModelSystem, subsystems: List[ModelSystem], atom_labels: List[str]
system: ModelSystem, subsystems: list[ModelSystem], atom_labels: list[str]
) -> None:
"""Determine the composition formula for `system` based on its `subsystems`.
If `system` has no children, the atom_labels are used to determine the formula.
Args:
system (ModelSystem): The system under consideration.
subsystems (List[ModelSystem]): The children of system.
atom_labels (List[str]): The global list of atom labels corresponding
subsystems (list[ModelSystem]): The children of system.
atom_labels (list[str]): The global list of atom labels corresponding
to the atom indices stored in system.
"""
if not subsystems:
Expand All @@ -236,13 +235,13 @@ def set_composition_formula(
children_names=subsystem_labels
)

def get_composition_recurs(system: ModelSystem, atom_labels: List[str]) -> None:
def get_composition_recurs(system: ModelSystem, atom_labels: list[str]) -> None:
"""Traverse the system hierarchy downward and set the branch composition for
all (sub)systems at each level.
Args:
system (ModelSystem): The system to traverse downward.
atom_labels (List[str]): The global list of atom labels corresponding
atom_labels (list[str]): The global list of atom labels corresponding
to the atom indices stored in system.
"""
subsystems = system.model_system
Expand Down
35 changes: 17 additions & 18 deletions src/nomad_simulations/schema_packages/model_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
#

import re
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, Optional

import numpy as np

from nomad.datamodel.data import ArchiveSection
from nomad.datamodel.metainfo.annotations import ELNAnnotation
from nomad.metainfo import MEnum, Quantity, SubSection, URL, Section
from nomad.metainfo import URL, MEnum, Quantity, Section, SubSection

if TYPE_CHECKING:
from nomad.metainfo import Context
from nomad.datamodel.datamodel import EntryArchive
from nomad.metainfo import Context
from structlog.stdlib import BoundLogger

from nomad_simulations.schema_packages.atoms_state import CoreHole, OrbitalsState
Expand Down Expand Up @@ -302,16 +301,16 @@ def __init__(self, m_def: 'Section' = None, m_context: 'Context' = None, **kwarg
}

def resolve_libxc_names(
self, xc_functionals: List[XCFunctional]
) -> Optional[List[str]]:
self, xc_functionals: list[XCFunctional]
) -> Optional[list[str]]:
"""
Resolves the `libxc_names` and sorts them from the list of `XCFunctional` sections.
Args:
xc_functionals (List[XCFunctional]): The list of `XCFunctional` sections.
xc_functionals (list[XCFunctional]): The list of `XCFunctional` sections.
Returns:
(Optional[List[str]]): The resolved and sorted `libxc_names`.
(Optional[list[str]]): The resolved and sorted `libxc_names`.
"""
return sorted(
[
Expand All @@ -323,13 +322,13 @@ def resolve_libxc_names(

def resolve_jacobs_ladder(
self,
libxc_names: List[str],
libxc_names: list[str],
) -> str:
"""
Resolves the `jacobs_ladder` from the `libxc_names`. The mapping (libxc -> NOMAD) is set in `self._jacobs_ladder_map`.
Args:
libxc_names (List[str]): The list of `libxc_names`.
libxc_names (list[str]): The list of `libxc_names`.
Returns:
(str): The resolved `jacobs_ladder`.
Expand All @@ -356,14 +355,14 @@ def resolve_jacobs_ladder(
return self._jacobs_ladder_map.get(highest_rung_abbrev, 'unavailable')

def resolve_exact_exchange_mixing_factor(
self, xc_functionals: List[XCFunctional], libxc_names: List[str]
self, xc_functionals: list[XCFunctional], libxc_names: list[str]
) -> Optional[float]:
"""
Resolves the `exact_exchange_mixing_factor` from the `xc_functionals` and `libxc_names`.
Args:
xc_functionals (List[XCFunctional]): The list of `XCFunctional` sections.
libxc_names (List[str]): The list of `libxc_names`.
xc_functionals (list[XCFunctional]): The list of `XCFunctional` sections.
libxc_names (list[str]): The list of `libxc_names`.
Returns:
(Optional[float]): The resolved `exact_exchange_mixing_factor`.
Expand All @@ -373,7 +372,7 @@ def resolve_exact_exchange_mixing_factor(
if functional.name == 'hybrid':
return functional.parameters.get('exact_exchange_mixing_factor')

def _scan_patterns(patterns: List[str], xc_name: str) -> bool:
def _scan_patterns(patterns: list[str], xc_name: str) -> bool:
return any(x for x in patterns if re.search('_' + x + '$', xc_name))

for xc_name in libxc_names:
Expand Down Expand Up @@ -496,20 +495,20 @@ def resolve_type(self) -> Optional[str]:

def resolve_orbital_references(
self,
model_systems: List[ModelSystem],
model_systems: list[ModelSystem],
logger: 'BoundLogger',
model_index: int = -1,
) -> Optional[List[OrbitalsState]]:
) -> Optional[list[OrbitalsState]]:
"""
Resolves the references to the `OrbitalsState` sections from the child `ModelSystem` section.
Args:
model_systems (List[ModelSystem]): The list of `ModelSystem` sections.
model_systems (list[ModelSystem]): The list of `ModelSystem` sections.
logger (BoundLogger): The logger to log messages.
model_index (int, optional): The `ModelSystem` section index from which resolve the references. Defaults to -1.
Returns:
Optional[List[OrbitalsState]]: The resolved references to the `OrbitalsState` sections.
Optional[list[OrbitalsState]]: The resolved references to the `OrbitalsState` sections.
"""
try:
model_system = model_systems[model_index]
Expand Down
18 changes: 7 additions & 11 deletions src/nomad_simulations/schema_packages/model_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
#

import re
from typing import TYPE_CHECKING, Optional, Tuple
from typing import TYPE_CHECKING, Optional

import ase
import numpy as np

from matid import Classifier, SymmetryAnalyzer # pylint: disable=import-error
from matid.classification.classifications import (
Atom,
Expand All @@ -32,18 +31,17 @@
Material2D,
Surface,
)

from nomad.config import config
from nomad.atomutils import Formula, get_normalized_wyckoff, search_aflow_prototype
from nomad.config import config
from nomad.datamodel.data import ArchiveSection
from nomad.datamodel.metainfo.annotations import ELNAnnotation
from nomad.datamodel.metainfo.basesections import Entity, System
from nomad.metainfo import MEnum, Quantity, SectionProxy, SubSection
from nomad.units import ureg

if TYPE_CHECKING:
from nomad.metainfo import Section, Context
from nomad.datamodel.datamodel import EntryArchive
from nomad.metainfo import Context, Section
from structlog.stdlib import BoundLogger

from nomad_simulations.schema_packages.atoms_state import AtomsState
Expand Down Expand Up @@ -541,7 +539,7 @@ def resolve_analyzed_atomic_cell(

def resolve_bulk_symmetry(
self, original_atomic_cell: AtomicCell, logger: 'BoundLogger'
) -> Tuple[Optional[AtomicCell], Optional[AtomicCell]]:
) -> tuple[Optional[AtomicCell], Optional[AtomicCell]]:
"""
Resolves the symmetry of the material being simulated using MatID and the
originally parsed data under original_atomic_cell. It generates two other
Expand All @@ -553,8 +551,7 @@ def resolve_bulk_symmetry(
uses to in MatID.SymmetryAnalyzer().
logger (BoundLogger): The logger to log messages.
Returns:
primitive_atomic_cell (Optional[AtomicCell]): The primitive `AtomicCell` section.
conventional_atomic_cell (Optional[AtomicCell]): The standarized `AtomicCell` section.
primitive_atomic_cell, conventional_atomic_cell (tuple[Optional[AtomicCell], Optional[AtomicCell]]): The primitive and standardized `AtomicCell` sections.
"""
symmetry = {}
try:
Expand Down Expand Up @@ -945,7 +942,7 @@ class ModelSystem(System):

def resolve_system_type_and_dimensionality(
self, ase_atoms: ase.Atoms, logger: 'BoundLogger'
) -> Tuple[str, int]:
) -> tuple[str, int]:
"""
Resolves the `ModelSystem.type` and `ModelSystem.dimensionality` using `MatID` classification analyzer:
Expand All @@ -954,8 +951,7 @@ def resolve_system_type_and_dimensionality(
Args:
ase.Atoms: The ASE Atoms structure to analyse.
Returns:
system_type (str): The system type as determined by MatID.
dimensionality (str): The system dimensionality as determined by MatID.
system_type, dimensionality (tuple[str]): The system type and dimensionality as determined by MatID.
"""
classification = None
system_type, dimensionality = self.type, self.dimensionality
Expand Down
Loading

4 comments on commit bd2f695

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py14286%53–55
   atoms_state.py1902189%31–33, 219–222, 246, 301–302, 370–371, 373, 555, 567–568, 629–633, 648–652, 659
   general.py75791%29–30, 98, 162, 272–273, 283
   model_method.py2657771%28–30, 189–192, 195–202, 294–295, 315, 336–355, 371–397, 400–417, 771, 782, 824–831, 869, 888, 968, 1025, 1100, 1214
   model_system.py2612292%43–45, 512–515, 562–569, 743–744, 965–969, 975–976, 984–985, 990, 1013
   numerical_settings.py2636575%30–32, 164, 234, 236–237, 240–243, 247–248, 255–258, 267–270, 274–277, 279–282, 287–290, 296–299, 470–497, 572, 607–610, 634, 637, 682, 684–687, 691, 695, 742, 746–767, 822–823, 890, 899–901, 904
   outputs.py1151091%27–28, 247–250, 290–293, 318, 320, 357, 376
   physical_property.py102793%38–40, 220, 349–351
   variables.py651183%26–28, 116, 139, 159–160, 163, 185, 208, 228
src/nomad_simulations/schema_packages/properties
   band_gap.py51590%26–28, 153–154
   band_structure.py1112280%27–29, 249–282, 295, 302, 338–339, 342
   energies.py36975%25–27, 54, 75, 100, 121, 131, 140
   fermi_surface.py17476%25–27, 58
   forces.py22673%26–28, 55, 75, 98
   hopping_matrix.py29583%25–27, 75, 110
   permittivity.py48883%25–27, 115–123
   spectral_profile.py25612452%27–29, 75–78, 113–116, 217–318, 374–386, 411–414, 434, 439–442, 484–520, 544, 591–594, 610–611, 616–622
   thermodynamics.py751876%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 195, 211, 236, 252, 277
src/nomad_simulations/schema_packages/utils
   utils.py681479%26–29, 79–88, 97–98, 103, 106
TOTAL208844179% 

Tests Skipped Failures Errors Time
321 0 💤 19 ❌ 0 🔥 2.769s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py14286%53–55
   atoms_state.py1902189%31–33, 219–222, 246, 301–302, 370–371, 373, 555, 567–568, 629–633, 648–652, 659
   general.py75791%29–30, 98, 162, 272–273, 283
   model_method.py2657771%28–30, 189–192, 195–202, 294–295, 315, 336–355, 371–397, 400–417, 771, 782, 824–831, 869, 888, 968, 1025, 1100, 1214
   model_system.py2612292%43–45, 512–515, 562–569, 743–744, 965–969, 975–976, 984–985, 990, 1013
   numerical_settings.py2636575%30–32, 164, 234, 236–237, 240–243, 247–248, 255–258, 267–270, 274–277, 279–282, 287–290, 296–299, 470–497, 572, 607–610, 634, 637, 682, 684–687, 691, 695, 742, 746–767, 822–823, 890, 899–901, 904
   outputs.py1151091%27–28, 247–250, 290–293, 318, 320, 357, 376
   physical_property.py102793%38–40, 220, 349–351
   variables.py651183%26–28, 116, 139, 159–160, 163, 185, 208, 228
src/nomad_simulations/schema_packages/properties
   band_gap.py51590%26–28, 153–154
   band_structure.py1112280%27–29, 249–282, 295, 302, 338–339, 342
   energies.py36975%25–27, 54, 75, 100, 121, 131, 140
   fermi_surface.py17476%25–27, 58
   forces.py22673%26–28, 55, 75, 98
   hopping_matrix.py29583%25–27, 75, 110
   permittivity.py48883%25–27, 115–123
   spectral_profile.py25612452%27–29, 75–78, 113–116, 217–318, 374–386, 411–414, 434, 439–442, 484–520, 544, 591–594, 610–611, 616–622
   thermodynamics.py751876%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 195, 211, 236, 252, 277
src/nomad_simulations/schema_packages/utils
   utils.py681479%26–29, 79–88, 97–98, 103, 106
TOTAL208844179% 

Tests Skipped Failures Errors Time
321 0 💤 28 ❌ 0 🔥 5.386s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py14286%53–55
   atoms_state.py1902189%31–33, 219–222, 246, 301–302, 370–371, 373, 555, 567–568, 629–633, 648–652, 659
   general.py75791%29–30, 98, 162, 272–273, 283
   model_method.py2657771%28–30, 189–192, 195–202, 294–295, 315, 336–355, 371–397, 400–417, 771, 782, 824–831, 869, 888, 968, 1025, 1100, 1214
   model_system.py2612292%43–45, 512–515, 562–569, 743–744, 965–969, 975–976, 984–985, 990, 1013
   numerical_settings.py2636575%30–32, 164, 234, 236–237, 240–243, 247–248, 255–258, 267–270, 274–277, 279–282, 287–290, 296–299, 470–497, 572, 607–610, 634, 637, 682, 684–687, 691, 695, 742, 746–767, 822–823, 890, 899–901, 904
   outputs.py1151091%27–28, 247–250, 290–293, 318, 320, 357, 376
   physical_property.py102793%38–40, 220, 349–351
   variables.py651183%26–28, 116, 139, 159–160, 163, 185, 208, 228
src/nomad_simulations/schema_packages/properties
   band_gap.py51590%26–28, 153–154
   band_structure.py1112280%27–29, 249–282, 295, 302, 338–339, 342
   energies.py36975%25–27, 54, 75, 100, 121, 131, 140
   fermi_surface.py17476%25–27, 58
   forces.py22673%26–28, 55, 75, 98
   hopping_matrix.py29583%25–27, 75, 110
   permittivity.py48883%25–27, 115–123
   spectral_profile.py25612452%27–29, 75–78, 113–116, 217–318, 374–386, 411–414, 434, 439–442, 484–520, 544, 591–594, 610–611, 616–622
   thermodynamics.py751876%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 195, 211, 236, 252, 277
src/nomad_simulations/schema_packages/utils
   utils.py681479%26–29, 79–88, 97–98, 103, 106
TOTAL208844179% 

Tests Skipped Failures Errors Time
321 0 💤 28 ❌ 0 🔥 5.542s ⏱️

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/nomad_simulations
   __init__.py4250%3–4
   _version.py11282%5–6
src/nomad_simulations/schema_packages
   __init__.py14286%53–55
   atoms_state.py1902189%31–33, 219–222, 246, 301–302, 370–371, 373, 555, 567–568, 629–633, 648–652, 659
   general.py75791%29–30, 98, 162, 272–273, 283
   model_method.py2657771%28–30, 189–192, 195–202, 294–295, 315, 336–355, 371–397, 400–417, 771, 782, 824–831, 869, 888, 968, 1025, 1100, 1214
   model_system.py2612292%43–45, 512–515, 562–569, 743–744, 965–969, 975–976, 984–985, 990, 1013
   numerical_settings.py2636575%30–32, 164, 234, 236–237, 240–243, 247–248, 255–258, 267–270, 274–277, 279–282, 287–290, 296–299, 470–497, 572, 607–610, 634, 637, 682, 684–687, 691, 695, 742, 746–767, 822–823, 890, 899–901, 904
   outputs.py1151091%27–28, 247–250, 290–293, 318, 320, 357, 376
   physical_property.py102793%38–40, 220, 349–351
   variables.py651183%26–28, 116, 139, 159–160, 163, 185, 208, 228
src/nomad_simulations/schema_packages/properties
   band_gap.py51590%26–28, 153–154
   band_structure.py1112280%27–29, 249–282, 295, 302, 338–339, 342
   energies.py36975%25–27, 54, 75, 100, 121, 131, 140
   fermi_surface.py17476%25–27, 58
   forces.py22673%26–28, 55, 75, 98
   hopping_matrix.py29583%25–27, 75, 110
   permittivity.py48883%25–27, 115–123
   spectral_profile.py25612452%27–29, 75–78, 113–116, 217–318, 374–386, 411–414, 434, 439–442, 484–520, 544, 591–594, 610–611, 616–622
   thermodynamics.py751876%25–27, 53, 74, 90, 99, 108, 119, 128, 155, 165, 175, 195, 211, 236, 252, 277
src/nomad_simulations/schema_packages/utils
   utils.py681479%26–29, 79–88, 97–98, 103, 106
TOTAL208844179% 

Tests Skipped Failures Errors Time
321 0 💤 28 ❌ 0 🔥 5.462s ⏱️

Please sign in to comment.