Skip to content

Commit

Permalink
added units
Browse files Browse the repository at this point in the history
  • Loading branch information
aalbino2 committed Jan 14, 2025
1 parent 90e17aa commit ebb2254
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ license = { file = "LICENSE" }
dependencies = [
"nomad-lab>=1.3.0",
"python-magic-bin; sys_platform == 'win32'",
"pdi-nomad-plugin",
]

[project.urls]
Expand Down Expand Up @@ -116,6 +117,7 @@ parser_one_entry_point = "nomad_aa_plugin.parsers:parser_one_entry_point"
parser_two_entry_point = "nomad_aa_plugin.parsers:parser_two_entry_point"
parser_three_entry_point = "nomad_aa_plugin.parsers:parser_three_entry_point"
parser_four_entry_point = "nomad_aa_plugin.parsers:parser_four_entry_point"
parser_five_entry_point = "nomad_aa_plugin.parsers:parser_five_entry_point"

app_entry_point = "nomad_aa_plugin.apps:app_entry_point"

Expand Down
21 changes: 21 additions & 0 deletions src/nomad_aa_plugin/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,24 @@ def load(self):
'__has_comment': '#',
},
)


class MyParserFiveEntryPoint(ParserEntryPoint):
parameter: int = Field(0, description='Custom configuration parameter')

def load(self):
from nomad_aa_plugin.parsers.parser import MyParserFive

return MyParserFive(**self.dict())


parser_five_entry_point = MyParserFiveEntryPoint(
name='MyParserFive',
description='My parser entry point configuration.',
mainfile_name_re=r'.+\.csv',
mainfile_mime_re="(?:text/plain|text/csv)", # 'text/plain',
mainfile_contents_dict={
'__has_all_keys': ['ValueFive', 'ValueFive2'],
'__has_comment': '#',
},
)
64 changes: 60 additions & 4 deletions src/nomad_aa_plugin/parsers/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
EntryArchive,
)

from nomad.units import ureg
from nomad.datamodel.datamodel import EntryArchive
from nomad.parsing import MatchingParser
from nomad.parsing.parser import MatchingParser
Expand Down Expand Up @@ -64,12 +65,14 @@ def parse(

child_archive.data = MyClassTwo()
child_archive.data.my_name = f'{my_name}'
child_archive.data.my_class_one = []

child_archive.data.my_class_one.append(MyClassOne())
my_class_one_subsec = MyClassOne()
my_class_one_subsec.my_value = df_csv["ValueTwo"]
my_class_one_subsec.my_time = df_csv["ValueTwo2"]

child_archive.data.my_class_one[0].my_value = df_csv["ValueTwo"]
child_archive.data.my_class_one[0].my_time = df_csv["ValueTwo2"]
# check which args the function m_add_subsection accepts: packages/nomad-FAIR/nomad/metainfo/metainfo.py
# DO NOT use list.append() to add a subsection to a section!
child_archive.data.m_add_sub_section(MyClassTwo.my_class_one, my_class_one_subsec)

create_archive(
child_archive.m_to_dict(),
Expand Down Expand Up @@ -209,3 +212,56 @@ def parse(
)

archive.data = MyClassOne()


class MyParserFive(MatchingParser):
def parse(
self,
mainfile: str,
archive: EntryArchive,
logger,
) -> None:

df_csv = pd.read_csv(mainfile, sep=',') #, decimal=',', engine='python')

archive.data = MyClassOne()

child_archive = EntryArchive()

my_name = "And"
filetype = 'yaml'

example_filename = f'{my_name}.archive.{filetype}'

child_archive.data = MyClassTwo()
child_archive.data.my_name = f'{my_name}'

my_class_one_subsec = MyClassOne()
# use .to_numpy() method to avoid the error:
# Quantity cannot wrap upcast type <class 'pandas.core.series.Series'>
my_class_one_subsec.my_value = ureg.Quantity(
df_csv["ValueFive"].to_numpy(),
ureg('celsius'),
)
# use .to_numpy() method to avoid the error:
# Quantity cannot wrap upcast type <class 'pandas.core.series.Series'>
my_class_one_subsec.my_time = ureg.Quantity(
df_csv["ValueFive2"].to_numpy(),
ureg('minute'),
)

# use the syntax my_variable.to('hour').magnitude to convert the units

# check which args the function m_add_subsection accepts: packages/nomad-FAIR/nomad/metainfo/metainfo.py
# DO NOT use list.append() to add a subsection to a section!
child_archive.data.m_add_sub_section(MyClassTwo.my_class_one, my_class_one_subsec)

create_archive(
child_archive.m_to_dict(),
archive.m_context,
example_filename,
filetype,
logger,
)

archive.data = MyClassOne()
4 changes: 4 additions & 0 deletions src/nomad_aa_plugin/schema_packages/schema_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,20 @@ class MyClassOne(PlotSection, EntryData):
my_value = Quantity(
type=float,
shape=['*'],
unit = 'K',
a_eln=ELNAnnotation(
component='NumberEditQuantity',
defaultDisplayUnit='celsius',
),
)

my_time = Quantity(
type=float,
shape=['*'],
unit = 's',
a_eln=ELNAnnotation(
component='NumberEditQuantity',
defaultDisplayUnit='minute',
),
)

Expand Down
Empty file removed tests/data/myfile.newmainfilename
Empty file.
18 changes: 18 additions & 0 deletions tests/data/test_five.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Time,ValueFive,ValueFive2
19.03.2018 13:01:47,3,1
19.03.2018 13:02:02,4,2
19.03.2018 13:02:17,5,3
19.03.2018 13:02:32,6,4
19.03.2018 13:02:47,7,5
19.03.2018 13:03:02,8,6
19.03.2018 13:03:17,9,7
19.03.2018 13:03:32,10,8
19.03.2018 13:03:47,11,9
19.03.2018 13:04:02,12,10
19.03.2018 13:04:17,13,11
19.03.2018 13:04:32,14,12
19.03.2018 13:04:47,15,13
19.03.2018 13:05:02,16,14
19.03.2018 13:05:17,17,15
19.03.2018 13:05:32,18,16
19.03.2018 13:05:47,19,17
18 changes: 18 additions & 0 deletions tests/data/test_four.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Time,ValueFour,ValueFour2
19.03.2018 13:01:47,3,1
19.03.2018 13:02:02,4,2
19.03.2018 13:02:17,5,3
19.03.2018 13:02:32,6,4
19.03.2018 13:02:47,7,5
19.03.2018 13:03:02,8,6
19.03.2018 13:03:17,9,7
19.03.2018 13:03:32,10,8
19.03.2018 13:03:47,11,9
19.03.2018 13:04:02,12,10
19.03.2018 13:04:17,13,11
19.03.2018 13:04:32,14,12
19.03.2018 13:04:47,15,13
19.03.2018 13:05:02,16,14
19.03.2018 13:05:17,17,15
19.03.2018 13:05:32,18,16
19.03.2018 13:05:47,19,17
2 changes: 1 addition & 1 deletion tests/data/test.csv → tests/data/test_one.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Time,Value,Value2
Time,ValueOne,ValueOne2
19.03.2018 13:01:47,3,1
19.03.2018 13:02:02,4,2
19.03.2018 13:02:17,5,3
Expand Down
18 changes: 18 additions & 0 deletions tests/data/test_three.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Time,ValueThree,ValueThree2
19.03.2018 13:01:47,3,1
19.03.2018 13:02:02,4,2
19.03.2018 13:02:17,5,3
19.03.2018 13:02:32,6,4
19.03.2018 13:02:47,7,5
19.03.2018 13:03:02,8,6
19.03.2018 13:03:17,9,7
19.03.2018 13:03:32,10,8
19.03.2018 13:03:47,11,9
19.03.2018 13:04:02,12,10
19.03.2018 13:04:17,13,11
19.03.2018 13:04:32,14,12
19.03.2018 13:04:47,15,13
19.03.2018 13:05:02,16,14
19.03.2018 13:05:17,17,15
19.03.2018 13:05:32,18,16
19.03.2018 13:05:47,19,17
18 changes: 18 additions & 0 deletions tests/data/test_two.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Time,ValueTwo,ValueTwo2
19.03.2018 13:01:47,3,1
19.03.2018 13:02:02,4,2
19.03.2018 13:02:17,5,3
19.03.2018 13:02:32,6,4
19.03.2018 13:02:47,7,5
19.03.2018 13:03:02,8,6
19.03.2018 13:03:17,9,7
19.03.2018 13:03:32,10,8
19.03.2018 13:03:47,11,9
19.03.2018 13:04:02,12,10
19.03.2018 13:04:17,13,11
19.03.2018 13:04:32,14,12
19.03.2018 13:04:47,15,13
19.03.2018 13:05:02,16,14
19.03.2018 13:05:17,17,15
19.03.2018 13:05:32,18,16
19.03.2018 13:05:47,19,17

0 comments on commit ebb2254

Please sign in to comment.