Skip to content

Commit

Permalink
Merge branch 'protos' of https://github.com/ISISNeutronMuon/MDANSE in…
Browse files Browse the repository at this point in the history
…to maciej/reactivate-atom-trace
  • Loading branch information
MBartkowiakSTFC committed Feb 17, 2025
2 parents 2ad58df + d14e44a commit 4244dda
Show file tree
Hide file tree
Showing 172 changed files with 929 additions and 901 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/black.yml → .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Black
name: Lint

on:
push:
Expand All @@ -16,6 +16,15 @@ jobs:
options: "--check --verbose"
src: "MDANSE/Src"

lint_check_ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
src: "./MDANSE/Src"
args: "check"

lint-mdanse-gui:
runs-on: ubuntu-latest
steps:
Expand All @@ -24,3 +33,12 @@ jobs:
with:
options: "--check --verbose"
src: "MDANSE_GUI/Src"

lint_check_ruff_gui:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/ruff-action@v3
with:
src: "./MDANSE_GUI/Src"
args: "check"
37 changes: 18 additions & 19 deletions MDANSE/Src/MDANSE/Chemistry/Databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __getitem__(self, item: str) -> dict:
return copy.deepcopy(self._data[item])
except KeyError:
raise AtomsDatabaseError(
"The element {} is not registered in the database.".format(item)
f"The element {item} is not registered in the database."
)

def _load(self, user_database: str = None, default_database: str = None) -> None:
Expand Down Expand Up @@ -253,7 +253,7 @@ def add_atom(self, atom: str) -> None:

if atom in self._data:
raise AtomsDatabaseError(
"The atom {} is already stored in the database".format(atom)
f"The atom {atom} is already stored in the database"
)

self._data[atom] = {}
Expand All @@ -272,11 +272,11 @@ def add_property(self, pname: str, ptype: str) -> None:

if pname in self._properties:
raise AtomsDatabaseError(
"The property {} is already registered in the database.".format(pname)
f"The property {pname} is already registered in the database."
)

if ptype not in AtomsDatabase._TYPES:
raise AtomsDatabaseError("The property type {} is unknown".format(ptype))
raise AtomsDatabaseError(f"The property type {ptype} is unknown")

self._properties[pname] = ptype
ptype = AtomsDatabase._TYPES[ptype]
Expand Down Expand Up @@ -307,7 +307,7 @@ def get_isotopes(self, atom: str) -> list[str]:
"""

if atom not in self._data:
raise AtomsDatabaseError("The atom {} is unknown".format(atom))
raise AtomsDatabaseError(f"The atom {atom} is unknown")

# The isotopes are searched according to |symbol| property
symbol = self._data[atom]["symbol"]
Expand Down Expand Up @@ -340,7 +340,7 @@ def get_property(self, pname: str) -> dict[str, Union[str, int, float, list]]:

if pname not in self._properties:
raise AtomsDatabaseError(
"The property {} is not registered in the database".format(pname)
f"The property {pname} is not registered in the database"
)

ptype = AtomsDatabase._TYPES[self._properties[pname]]
Expand All @@ -365,11 +365,11 @@ def get_value(self, atom: str, pname: str) -> Union[str, int, float, list]:
"""

if atom not in self._data:
raise AtomsDatabaseError("The atom {} is unknown".format(atom))
raise AtomsDatabaseError(f"The atom {atom} is unknown")

if pname not in self._properties:
raise AtomsDatabaseError(
"The property {} is not registered in the database".format(pname)
f"The property {pname} is not registered in the database"
)

ptype = self._properties[pname]
Expand Down Expand Up @@ -397,12 +397,12 @@ def get_values_for_multiple_atoms(

if not all(atom in self._data for atom in atoms):
raise AtomsDatabaseError(
"One or more of the provided atoms {} are unknown".format(atoms)
f"One or more of the provided atoms {atoms} are unknown"
)

if prop not in self._properties:
raise AtomsDatabaseError(
"The property {} is not registered in the database".format(prop)
f"The property {prop} is not registered in the database"
)

values = {name: self._data[name][prop] for name in unique_atoms}
Expand All @@ -425,11 +425,11 @@ def set_value(
"""

if atom not in self._data:
raise AtomsDatabaseError("The element {} is unknown".format(atom))
raise AtomsDatabaseError(f"The element {atom} is unknown")

if pname not in self._properties:
raise AtomsDatabaseError(
"The property {} is not registered in the database".format(pname)
f"The property {pname} is not registered in the database"
)

try:
Expand All @@ -438,7 +438,7 @@ def set_value(
)
except ValueError:
raise AtomsDatabaseError(
"Can not coerce {} to {} type".format(value, self._properties[pname])
f"Can not coerce {value} to {self._properties[pname]} type"
)

def has_atom(self, atom: str) -> bool:
Expand Down Expand Up @@ -480,19 +480,18 @@ def info(self, atom: str) -> str:

# A delimiter line.
delimiter = "-" * 70
tab_fmt = " {:<20}{!s:>50}"

info = [
delimiter,
"%s" % atom.center(70),
"%s" % " {0:<20}{1:>50}".format("property", "value"),
f"{atom:^70}",
tab_fmt.format("property", "value"),
delimiter,
]

# The values for all element's properties
for pname in sorted(self._properties):
info.append(
" {0:<20}{1:>50}".format(pname, str(self._data[atom].get(pname, None)))
)
info.append(tab_fmt.format(pname, self._data[atom].get(pname, None)))

info.append(delimiter)
info = "\n".join(info)
Expand Down Expand Up @@ -525,7 +524,7 @@ def match_numeric_property(
)
except KeyError:
raise AtomsDatabaseError(
"The property {} is not registered in the database".format(pname)
f"The property {pname} is not registered in the database"
)

tolerance = abs(tolerance)
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Core/SubclassFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def recursive_keys(parent_class: type) -> list:
"""
try:
results = parent_class.subclasses()
except:
except Exception:
return []
else:
for child in parent_class.subclasses():
Expand All @@ -117,7 +117,7 @@ def recursive_dict(parent_class: type) -> dict:
ckey: parent_class._registered_subclasses[ckey]
for ckey in parent_class.subclasses()
}
except:
except Exception:
return {}
else:
for child in parent_class.subclasses():
Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Framework/AtomMapping/atom_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, atm_label: str, **kwargs):
# methods as of writing e.g. re.sub
translation = str.maketrans("", "", ";=")
self.atm_label = atm_label.translate(translation)
self.grp_label = f""
self.grp_label = ""
if kwargs:
for k, v in kwargs.items():
self.grp_label += f"{k}={str(v).translate(translation)};"
Expand Down
24 changes: 20 additions & 4 deletions MDANSE/Src/MDANSE/Framework/AtomSelector/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import json
import copy
import json
from typing import Union

from MDANSE.Chemistry.ChemicalSystem import ChemicalSystem
from MDANSE.MolecularDynamics.Trajectory import Trajectory
from MDANSE.Framework.AtomSelector.all_selector import select_all
from MDANSE.Framework.AtomSelector.atom_selectors import *
from MDANSE.Framework.AtomSelector.group_selectors import *
from MDANSE.Framework.AtomSelector.molecule_selectors import *
from MDANSE.Framework.AtomSelector.atom_selectors import (
select_atom_fullname,
select_atom_name,
select_dummy,
select_element,
select_hs_on_element,
select_hs_on_heteroatom,
select_index,
)
from MDANSE.Framework.AtomSelector.group_selectors import (
select_hydroxy,
select_methyl,
select_phosphate,
select_primary_amine,
select_sulphate,
select_thiol,
)
from MDANSE.Framework.AtomSelector.molecule_selectors import select_water


class Selector:
Expand Down
68 changes: 20 additions & 48 deletions MDANSE/Src/MDANSE/Framework/Configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ class ConfigurationError(Error):
class Configurable(object):
"""
This class allows any object that derives from it to be configurable within the MDANSE framework.
Within that framework, to be configurable, a class must:
#. derive from this class
#. implement the "configurators" class attribute as a list of 3-tuple whose:
#.. 0-value is the type of the configurator that will be used to fetch the corresponding \
MDANSE.Framework.Configurators.IConfigurator.IConfigurator derived class from the configurators registry
#.. 1-value is the name of the configurator that will be used as the key of the _configuration attribute.
#.. 2-value is the dictionary of the keywords used when initializing the configurator.
#.. 2-value is the dictionary of the keywords used when initializing the configurator.
"""

enabled = True
Expand Down Expand Up @@ -93,8 +93,8 @@ def build_configuration(self):
typ, name, configurable=self, **kwds
)
# Any kind of error has to be caught
except:
raise ConfigurationError("Could not set %r configuration item" % name)
except Exception:
raise ConfigurationError(f"Could not set {name!r} configuration item")

def set_settings(self, settings):
self.settings = settings
Expand Down Expand Up @@ -228,10 +228,10 @@ def build_doc_example(cls):
docstring += ">>> \n"
docstring += ">>> \n"
docstring += ">>> parameters = {}\n"
for k, v in list(cls.get_default_parameters().items()):
docstring += ">>> parameters[%r]=%r\n" % (k, v)
for k, v in cls.get_default_parameters().items():
docstring += f">>> parameters[{k!r}]={v!r}\n"
docstring += ">>> \n"
docstring += ">>> job = IJob.create(%r)\n" % cls.__name__
docstring += f">>> job = IJob.create({cls.__name__!r})\n"
docstring += ">>> job.setup(parameters)\n"
docstring += ">>> job.run()\n"
return docstring
Expand All @@ -249,53 +249,25 @@ def build_doc_texttable(cls, doclist):
sizes[1] = max(sizes[1], len(v["Default value"]))
# Case of Description field: has to be splitted and parsed for inserting sphinx "|" keyword for multiline
v["Description"] = v["Description"].strip()
v["Description"] = v["Description"].split("\n")
v["Description"] = v["Description"].splitlines()
v["Description"] = ["| " + vv.strip() for vv in v["Description"]]
sizes[2] = max(sizes[2], max([len(d) for d in v["Description"]]))

docstring += "+%s+%s+%s+\n" % (
"-" * (sizes[0] + 1),
"-" * (sizes[1] + 1),
"-" * (sizes[2] + 1),
)
docstring += "| %-*s| %-*s| %-*s|\n" % (
sizes[0],
columns[0],
sizes[1],
columns[1],
sizes[2],
columns[2],
)
docstring += "+%s+%s+%s+\n" % (
"=" * (sizes[0] + 1),
"=" * (sizes[1] + 1),
"=" * (sizes[2] + 1),
)
sizes[2] = max(sizes[2], max(map(len, v["Description"])))

data_line = "| " + "| ".join(f"{{}}:<{size}" for size in sizes) + "|\n"
sep_line = "+" + "+".join("-" * (size + 1) for size in sizes) + "+\n"

docstring += sep_line
docstring += data_line.format(*columns)
docstring += sep_line.replace("-", "=")

for v in doclist:
docstring += "| %-*s| %-*s| %-*s|\n" % (
sizes[0],
v["Configurator"],
sizes[1],
v["Default value"],
sizes[2],
v["Description"][0],
docstring += data_line.format(
v["Configurator"], v["Default value"], v["Description"][0]
)
if len(v["Description"]) > 1:
for descr in v["Description"][1:]:
docstring += "| %-*s| %-*s| %-*s|\n" % (
sizes[0],
"",
sizes[1],
"",
sizes[2],
descr,
)
docstring += "+%s+%s+%s+\n" % (
"-" * (sizes[0] + 1),
"-" * (sizes[1] + 1),
"-" * (sizes[2] + 1),
)
data_line.format("", "", descr)
docstring += sep_line

docstring += "\n"
return docstring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse(self):

try:
self._input = ASETrajectory(self["filename"])
except:
except Exception:
self._input = iread(self["filename"], index="[:]")
first_frame = read(self["filename"], index=0)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def configure(self, values):
if file_format == "guess":
file_format = None

if file_format is not None and not file_format in self._allowed_formats:
if file_format is not None and file_format not in self._allowed_formats:
LOG.error(f"WRONG FORMAT in {self._name}")
self.error_status = f"The ASE file format {file_format} is not supported"
return
Expand Down Expand Up @@ -99,10 +99,10 @@ def get_information(self):
:rtype: str
"""
try:
val = self["value"]
self["value"]
except KeyError:
result = f"No VALUE in {self._name}"
LOG.error(result)
return result
else:
return "Input file: %r\n" % self["value"]
return f"Input file: {self['value']!r}\n"
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def get_information(self) -> str:
return "Not configured yet\n"

info = []
info.append("Number of selected atoms:%d" % self["selection_length"])
info.append("Selected elements:%s" % self["unique_names"])
info.append(f"Number of selected atoms:{self['selection_length']:d}")
info.append(f"Selected elements:{self['unique_names']}")

return "\n".join(info) + "\n"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def get_information(self) -> str:
if self["value"] is None:
return "No atoms selected for transmutation\n"

return "Number of transmuted atoms:%d\n" % self._nTransmutedAtoms
return f"Number of transmuted atoms:{self._nTransmutedAtoms:d}\n"

def get_transmuter(self) -> AtomTransmuter:
"""
Expand Down
Loading

0 comments on commit 4244dda

Please sign in to comment.