Skip to content

Commit

Permalink
update plugin structure (#112)
Browse files Browse the repository at this point in the history
* updated plugin structure

* corrected toml

* added pynxtools dependency

* update nomad-lab version

* Apply suggestions from Sarthak's code review

Co-authored-by: Sarthak Kapoor <[email protected]>

* fixed typo in readme

* entry points name

* added entry point in general schema

* ruff linting

* Ruff linting 2

* Apply suggestions from code review

Co-authored-by: Sarthak Kapoor <[email protected]>
Co-authored-by: Hampus Näsström <[email protected]>

* changed xrd parser folder

* added autolinting on save

* last fixes and descriptions

* description of general schema

* fixed general SchemaPackage

* changed general package into a module

* ruff

* typo in entry point

* added aliases

* reverted nomad-lab version in toml dependencies

* relaxed pynxtools version in toml

* Change nomad-lab to 1.3.4dev

* Moved RawFileXRDData to schema.py and added alias

* Ruff formatting

* Moved RawFileTransmissionData to schema file

* Ruff

* Ruff

---------

Co-authored-by: Sarthak Kapoor <[email protected]>
Co-authored-by: Sarthak Kapoor <[email protected]>
Co-authored-by: Hampus Näsström <[email protected]>
  • Loading branch information
4 people authored Sep 3, 2024
1 parent 5550cd3 commit 9df037a
Show file tree
Hide file tree
Showing 22 changed files with 430 additions and 334 deletions.
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"python.defaultInterpreterPath": ".pyenv/bin/python",
"editor.rulers": [
90
88
],
"editor.renderWhitespace": "all",
"editor.tabSize": 4
"editor.tabSize": 4,
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true
}
}
Empty file added MANIFEST.in
Empty file.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ pip install nomad-measurements --index-url https://gitlab.mpcdf.mpg.de/api/v4/pr
### Setting up your OASIS
Read the [NOMAD plugin documentation](https://nomad-lab.eu/prod/v1/staging/docs/plugins/plugins.html#add-a-plugin-to-your-nomad) for all details on how to deploy the plugin on your NOMAD instance.

You need to modify the ```nomad.yaml``` configuration file of your NOMAD instance.
To include, for example, the XRD plugin you need to add the following lines: .
You don't need to modify the ```nomad.yaml``` configuration file of your NOMAD instance, beacuse the package is pip installed and all the available modules (entry points) are loaded.
To include, instead, only some of the entry points, you need to specify them in the ```include``` section of the ```nomad.yaml```. In the following lines, a list of all the available entry points:

```yaml
plugins:
include:
- 'parsers/nomad_measurements/xrd'
options:
parsers/nomad_measurements/xrd:
python_package: nomad_measurements.xrd.parser
- "nomad_measurements.general:schema"
- "nomad_measurements.xrd:schema"
- "nomad_measurements.xrd.parser:parser"
- "nomad_measurements.transmission:schema"
- "nomad_measurements.transmission:parser"
```
### Development
Expand All @@ -44,5 +45,5 @@ cd nomad-measurements

And install the package in editable mode with the development ('dev') dependencies:
```sh
pip install -e .[dev]
pip install -e .[dev] --index-url https://gitlab.mpcdf.mpg.de/api/v4/projects/2187/packages/pypi/simple
```
10 changes: 0 additions & 10 deletions nomad.yaml

This file was deleted.

116 changes: 76 additions & 40 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,103 @@ requires = [
build-backend = "setuptools.build_meta"

[project]
classifiers = [
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
]
name = "nomad-measurements"
dynamic = ["version"]
description = "A plugin for NOMAD containing base sections for material processing."
readme = "README.md"
requires-python = ">=3.9"
authors = [
{ name = "Hampus Näsström", email = '[email protected]' },
{ name = "Andrea Albino", email = '[email protected]' },
{ name = "Sebastian Brückner", email = '[email protected]' },
{ name = "Sarthak Kapoor", email = '[email protected]' },
]
requires-python = ">=3.9"
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"License :: OSI Approved :: Apache Software License",
]
license = { file = "LICENSE" }
dependencies = [
"nomad-lab>=1.3.3.dev86",
"nomad-lab>=1.3.4dev",
"xmltodict==0.13.0",
"fairmat-readers-xrd~=0.0.3"
"fairmat-readers-xrd~=0.0.3",
"pynxtools",
]
[project.optional-dependencies]
dev = [
"pytest",
"ruff",
"structlog==22.3.0",
]

[project.license]
file = "LICENSE"

[project.urls]
"Homepage" = "https://github.com/FAIRmat-NFDI/nomad-measurements"
"Bug Tracker" = "https://github.com/FAIRmat-NFDI/nomad-measurements/issues"

[tool.uv]
index-url = "https://gitlab.mpcdf.mpg.de/api/v4/projects/2187/packages/pypi/simple"

[project.optional-dependencies]
dev = [
"pytest",
"ruff",
"structlog",
]

[tool.ruff]
include = ["src/*.py", "tests/*.py"]
exclude = ["dependencies"]
# Exclude a variety of commonly ignored directories.
exclude = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".mypy_cache",
".nox",
".pants.d",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
]

# Same as Black.
line-length = 88
indent-width = 4

[tool.ruff.lint]
select = [
"E", # pycodestyle
"W", # pycodestyle
"PL", # pylint
# pycodestyle
"E",
# Pyflakes
"F",
# pyupgrade
"UP",
# isort
"I",
# pylint
"PL",
]

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
"PLR0911", # Too many return statements
"PLR0912", # Too many branches
"PLR0913", # Too many arguments in function definition
"PLR0915", # Too many statements
"PLR2004", # Magic value used instead of constant
"PLW0603", # Using the global statement
"PLW2901", # redefined-loop-name
"PLR1714", # consider-using-in
"PLR5501", # else-if-used
"F403", # 'from module import *' used; unable to detect undefined names
]

fixable = ["ALL"]

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# this is entirely optional, you can remove this if you wish to
[tool.ruff.format]
# use single quotes for strings.
quote-style = "single"
Expand All @@ -89,9 +116,18 @@ skip-magic-trailing-comma = false
# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

[tool.setuptools]
package-dir = { "" = "src" }

[tool.setuptools.packages.find]
where = [
"src",
]
where = ["src"]

[project.entry-points.'nomad.plugin']

general_schema = "nomad_measurements:schema"
xrd_schema = "nomad_measurements.xrd:schema"
xrd_parser = "nomad_measurements.xrd:parser"
transmission_schema = "nomad_measurements.transmission:schema"
transmission_parser = "nomad_measurements.transmission:parser"

[tool.setuptools_scm]
[tool.setuptools_scm]
4 changes: 0 additions & 4 deletions setup.py

This file was deleted.

142 changes: 9 additions & 133 deletions src/nomad_measurements/__init__.py
Original file line number Diff line number Diff line change
@@ -1,138 +1,14 @@
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import TYPE_CHECKING
from nomad.metainfo.metainfo import (
Category,
)
from nomad.datamodel.data import (
EntryDataCategory,
)
from nomad.datamodel.metainfo.basesections import (
SectionReference,
Activity,
Process,
Measurement,
)
from nomad.metainfo import (
Quantity,
SubSection,
)
from nomad.datamodel.metainfo.annotations import (
ELNAnnotation,
ELNComponentEnum,
)

if TYPE_CHECKING:
from structlog.stdlib import (
BoundLogger,
)


class NOMADMeasurementsCategory(EntryDataCategory):
"""
A category for all measurements defined in the `nomad-measurements` plugin.
"""

m_def = Category(label='NOMAD Measurements', categories=[EntryDataCategory])
from nomad.config.models.plugins import SchemaPackageEntryPoint


class ActivityReference(SectionReference):
"""
A section used for referencing an Activity.
"""
class GeneralSchemaPackageEntryPoint(SchemaPackageEntryPoint):
def load(self):
from nomad_measurements.general import m_package

reference = Quantity(
type=Activity,
description='A reference to a NOMAD `Activity` entry.',
a_eln=ELNAnnotation(
component=ELNComponentEnum.ReferenceEditQuantity,
label='Activity Reference',
),
)
lab_id = Quantity(
type=str,
description="""
The readable identifier for the activity.
""",
a_eln=ELNAnnotation(
component=ELNComponentEnum.StringEditQuantity,
),
)
return m_package

def normalize(self, archive, logger: 'BoundLogger') -> None:
"""
The normalizer for the `EntityReference` class.
Will attempt to fill the `reference` from the `lab_id` or vice versa.

Args:
archive (EntryArchive): The archive containing the section that is being
normalized.
logger ('BoundLogger'): A structlog logger.
"""
super(ActivityReference, self).normalize(archive, logger)
if self.reference is None and self.lab_id is not None:
from nomad.search import search, MetadataPagination

query = {'results.eln.lab_ids': self.lab_id}
search_result = search(
owner='all',
query=query,
pagination=MetadataPagination(page_size=1),
user_id=archive.metadata.main_author.user_id,
)
if search_result.pagination.total > 0:
entry_id = search_result.data[0]['entry_id']
upload_id = search_result.data[0]['upload_id']
self.reference = f'../uploads/{upload_id}/archive/{entry_id}#data'
if search_result.pagination.total > 1:
logger.warn(
f'Found {search_result.pagination.total} entries with lab_id: '
f'"{self.lab_id}". Will use the first one found.'
)
else:
logger.warn(f'Found no entries with lab_id: "{self.lab_id}".')
elif self.lab_id is None and self.reference is not None:
self.lab_id = self.reference.lab_id
if self.name is None and self.lab_id is not None:
self.name = self.lab_id


class ProcessReference(ActivityReference):
"""
A section used for referencing a Process.
"""

reference = Quantity(
type=Process,
description='A reference to a NOMAD `Process` entry.',
a_eln=ELNAnnotation(
component=ELNComponentEnum.ReferenceEditQuantity,
label='Process Reference',
),
)


class InSituMeasurement(Measurement):
"""
A section used for a measurement performed in-situ during a process.
"""

process = SubSection(
section_def=ProcessReference,
description='A reference to the process during which the measurement occurred.',
)
schema = GeneralSchemaPackageEntryPoint(
name='General Schema',
description='Schema package containing basic classes used around in the plugin.',
)
Loading

0 comments on commit 9df037a

Please sign in to comment.