Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nomad-FAIR
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nomad-lab
nomad-FAIR
Commits
595bc21e
Commit
595bc21e
authored
5 years ago
by
Markus Scheidgen
Browse files
Options
Downloads
Patches
Plain Diff
Added datetime datatype to metainfo.
parent
aae8e41a
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!60
v0.6.0 Release
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
nomad/metainfo/__init__.py
+1
-1
1 addition, 1 deletion
nomad/metainfo/__init__.py
nomad/metainfo/example.py
+8
-2
8 additions, 2 deletions
nomad/metainfo/example.py
nomad/metainfo/metainfo.py
+68
-47
68 additions, 47 deletions
nomad/metainfo/metainfo.py
with
77 additions
and
50 deletions
nomad/metainfo/__init__.py
+
1
−
1
View file @
595bc21e
from
.metainfo
import
MSection
,
MCategory
,
Definition
,
Property
,
Quantity
,
SubSection
,
\
Section
,
Category
,
Package
,
Enum
,
m_package
,
units
Section
,
Category
,
Package
,
Enum
,
Datetime
,
m_package
,
units
This diff is collapsed.
Click to expand it.
nomad/metainfo/example.py
+
8
−
2
View file @
595bc21e
"""
An example metainfo package.
"""
import
numpy
as
np
from
datetime
import
datetime
from
nomad.metainfo
import
MSection
,
MCategory
,
Section
,
Quantity
,
Package
,
SubSection
,
Enum
,
units
from
nomad.metainfo
import
MSection
,
MCategory
,
Section
,
Quantity
,
Package
,
SubSection
,
Enum
,
Datetime
,
units
m_package
=
Package
(
links
=
[
'
http://metainfo.nomad-coe.eu
'
])
...
...
@@ -18,6 +19,7 @@ class Parsing(MSection):
parser_version
=
Quantity
(
type
=
str
)
nomad_version
=
Quantity
(
type
=
str
)
warnings
=
Quantity
(
type
=
str
,
shape
=
[
'
0..*
'
])
parse_time
=
Quantity
(
type
=
Datetime
)
class
System
(
MSection
):
...
...
@@ -93,6 +95,10 @@ if __name__ == '__main__':
run
=
Run
()
run
.
code_name
=
'
VASP
'
run
.
code_version
=
'
1.0.0
'
parsing
=
run
.
m_create
(
Parsing
)
parsing
.
parse_time
=
datetime
.
now
()
run
.
m_as
(
VaspRun
).
x_vasp_raw_format
=
'
outcar
'
# The same as
run
.
x_vasp_raw_format
=
'
outcar
'
# type: ignore
...
...
@@ -117,4 +123,4 @@ if __name__ == '__main__':
run
=
Run
.
m_from_dict
(
serializable
)
print
(
run
.
sccs
[
0
].
system
)
print
(
m_package
.
m_to_
dict
(
))
# type: ignore, pylint: disable=undefined-variable
#
print(m_package.m_to_
json(indent=2
)) # type: ignore, pylint: disable=undefined-variable
This diff is collapsed.
Click to expand it.
nomad/metainfo/metainfo.py
+
68
−
47
View file @
595bc21e
...
...
@@ -134,7 +134,7 @@ See the reference of classes :class:`Section` and :class:`Quantities` for detail
.. autoclass:: Quantity
"""
# TODO validation
# TODO validation
and constraints
from
typing
import
Type
,
TypeVar
,
Union
,
Tuple
,
Iterable
,
List
,
Any
,
Dict
,
Set
,
\
Callable
as
TypingCallable
,
cast
...
...
@@ -148,6 +148,9 @@ import itertools
import
numpy
as
np
from
pint.unit
import
_Unit
from
pint
import
UnitRegistry
import
aniso8601
from
datetime
import
datetime
import
pytz
m_package
:
'
Package
'
=
None
...
...
@@ -384,13 +387,40 @@ class Reference(DataType):
return
MProxy
(
value
)
class
__Datetime
(
DataType
):
def
__parse
(
self
,
datetime_str
:
str
)
->
datetime
:
try
:
try
:
return
aniso8601
.
parse_datetime
(
datetime_str
)
except
ValueError
:
date
=
aniso8601
.
parse_date
(
datetime_str
)
return
datetime
(
date
.
year
,
date
.
month
,
date
.
day
)
except
Exception
:
raise
TypeError
(
'
Invalid date literal
"
{0}
"'
.
format
(
datetime_str
))
def
set_normalize
(
self
,
section
:
'
MSection
'
,
quantity_def
:
'
Quantity
'
,
value
:
Any
)
->
Any
:
if
isinstance
(
value
,
str
):
value
=
self
.
__parse
(
value
)
if
not
isinstance
(
value
,
datetime
):
raise
TypeError
(
'
%s is not a datetime.
'
%
value
)
return
value
def
serialize
(
self
,
section
:
'
MSection
'
,
quantity_def
:
'
Quantity
'
,
value
:
Any
)
->
Any
:
value
.
replace
(
tzinfo
=
pytz
.
utc
)
return
value
.
isoformat
()
def
deserialize
(
self
,
section
:
'
MSection
'
,
quantity_def
:
'
Quantity
'
,
value
:
Any
)
->
Any
:
return
self
.
__parse
(
value
)
Dimension
=
__Dimension
()
Unit
=
__Unit
()
QuantityType
=
__QuantityType
()
Callable
=
__Callable
()
# TODO class Datetime(DataType)
Datetime
=
__Datetime
()
class
MObjectMeta
(
type
):
...
...
@@ -737,10 +767,6 @@ class MSection(metaclass=MObjectMeta):
'
The value %s is not an enum value for quantity %s.
'
%
(
value
,
quantity_def
))
elif
quantity_def
in
[
Quantity
.
type
,
Quantity
.
derived
]:
# TODO check these special cases for Quantity quantities
pass
elif
quantity_def
.
type
==
Any
:
pass
...
...
@@ -999,16 +1025,19 @@ class MSection(metaclass=MObjectMeta):
dct
.
pop
(
'
m_parent_index
'
,
None
)
dct
.
pop
(
'
m_parent_sub_section
'
,
None
)
def
items
():
section
=
cls
()
for
name
,
sub_section_def
in
section_def
.
all_sub_sections
.
items
():
if
name
in
dct
:
sub_section_value
=
dct
.
pop
(
name
)
if
sub_section_def
.
repeats
:
yield
name
,
[
sub_section_def
.
sub_section
.
section_cls
.
m_from_dict
(
sub_section_dct
)
for
sub_section_dct
in
sub_section_value
]
for
sub_section_dct
in
sub_section_value
:
sub_section
=
sub_section_def
.
sub_section
.
section_cls
.
m_from_dict
(
sub_section_dct
)
section
.
m_add_sub_section
(
sub_section_def
,
sub_section
)
else
:
yield
name
,
sub_section_def
.
sub_section
.
section_cls
.
m_from_dict
(
sub_section_value
)
sub_section
=
sub_section_def
.
sub_section
.
section_cls
.
m_from_dict
(
sub_section_value
)
section
.
m_add_sub_section
(
sub_section_def
,
sub_section
)
for
name
,
quantity_def
in
section_def
.
all_quantities
.
items
():
if
name
in
dct
:
...
...
@@ -1019,25 +1048,20 @@ class MSection(metaclass=MObjectMeta):
if
isinstance
(
quantity_def
.
type
,
DataType
):
dimensions
=
len
(
quantity_def
.
shape
)
# TODO hand in the context, which is currently create after!
if
dimensions
==
0
:
quantity_value
=
quantity_def
.
type
.
deserialize
(
N
on
e
,
quantity_def
,
quantity_value
)
secti
on
,
quantity_def
,
quantity_value
)
elif
dimensions
==
1
:
quantity_value
=
list
(
quantity_def
.
type
.
deserialize
(
N
on
e
,
quantity_def
,
item
)
quantity_def
.
type
.
deserialize
(
secti
on
,
quantity_def
,
item
)
for
item
in
quantity_value
)
else
:
raise
MetainfoError
(
'
Only numpy quantities can have more than 1 dimension.
'
)
yield
name
,
quantity_value
section
.
m_data
.
dct
[
name
]
=
quantity_value
# type: ignore
dct
=
{
key
:
value
for
key
,
value
in
items
()}
section_instance
=
cast
(
MSectionBound
,
section_def
.
section_cls
())
# TODO !do not update, but set directly!
section_instance
.
m_update
(
**
dct
)
return
section_instance
return
section
def
m_to_json
(
self
,
**
kwargs
):
"""
Returns the data of this section as a json string.
"""
...
...
@@ -1271,9 +1295,6 @@ class Quantity(Property):
virtual
:
'
Quantity
'
=
None
# TODO derived_from = Quantity(type=Quantity, shape=['0..*'])
# TODO categories = Quantity(type=Category, shape=['0..*'])
# TODO converter = Quantity(type=Converter), a class with set of functions for
# normalizing, (de-)serializing values.
def
__get__
(
self
,
obj
,
cls
):
if
obj
is
None
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment