Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #376 from simphony/cuds_2017
Browse files Browse the repository at this point in the history
Problem: model.py causes confusion
  • Loading branch information
stefanoborini committed Jan 25, 2017
2 parents 3cfe724 + 02bf24c commit 0c8b3db
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 43 deletions.
4 changes: 2 additions & 2 deletions doc/source/api/cuds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ CUDS Computational Model

.. autosummary::

~model.CUDS
~cuds.CUDS
~simulation.Simulation

.. rubric:: Description

.. automodule:: simphony.cuds.model
.. automodule:: simphony.cuds.cuds
:members:
:undoc-members:

Expand Down
1 change: 1 addition & 0 deletions simphony/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
class Default():
pass


__all__ = ['CUBA', 'DataContainer']
2 changes: 1 addition & 1 deletion simphony/cuds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .lattice_items import LatticeNode
from .particles import Particles
from .particles_items import Particle, Bond
from .model import CUDS
from .cuds import CUDS
from .simulation import Simulation
from .meta import api

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion simphony/cuds/tests/test_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import uuid
from functools import partial

from simphony.cuds.model import CUDS
from simphony.cuds import CUDS
from simphony.cuds.meta.api import Material
from simphony.testing.utils import compare_material

Expand Down
41 changes: 23 additions & 18 deletions simphony/io/serialisation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import yaml
import numpy

from simphony.cuds.model import CUDS
from simphony.cuds.meta.cuds_component import CUDSComponent
from simphony.core.keywords import KEYWORDS
# from simphony.cuds.meta import *
from simphony.core.cuba import CUBA
import uuid
from collections import OrderedDict
from importlib import import_module

from simphony.core import CUBA
from simphony.core.keywords import KEYWORDS
from simphony.cuds import CUDS
from simphony.cuds.meta.api import CUDSComponent
from simphony.cuds.meta.validation import to_camel_case

_CUDSREFMAP = OrderedDict()
Expand Down Expand Up @@ -91,7 +91,7 @@ def load_CUDS(handle):
"""

comp_dict = {}
cuds_components = {}
name = None
desc = None
for data in yaml.safe_load_all(handle):
Expand All @@ -104,11 +104,11 @@ def load_CUDS(handle):
desc = dict_cuds['DESCRIPTION']
else:
_dict_to_CUDSComponent(cubatype, dict_cuds[cubatype],
comp_dict)
cuds_components)

model = CUDS(name=name, description=desc)
for compid in comp_dict.keys():
model.add(comp_dict[compid])
for comp in cuds_components.values():
model.add(comp)

return model

Expand Down Expand Up @@ -147,11 +147,8 @@ def _CUDSComponent_to_yaml(comp, stream):
# Go through the keys in the component
cdata = comp._data
for key in cdata.keys():
# Skip the UID key, as it's not accepted by the deserializer
if key == CUBA.UID:
continue
# Check if the data is a list or numpy types or CUDSComponents
elif type(cdata[key]) == list:
if type(cdata[key]) == list:
# List of simple types?
if KEYWORDS[key._name_].dtype in [numpy.float64,
numpy.int32, bool]:
Expand Down Expand Up @@ -210,7 +207,7 @@ def _CUDSComponent_to_yaml(comp, stream):
return stream


def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}):
def _dict_to_CUDSComponent(cubatype, comp, comp_dict=None):
""" Generate a CUDSComponent on the basis of a dictionary
provided by PyYaml library.
Expand All @@ -226,12 +223,16 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}):
id(CUDSComponent): CUDSComponent
"""

if comp_dict is None:
comp_dict = {}
# Check that comp does not refer to one of the components that
# have been already constructed
if id(comp) in comp_dict.keys():
return

# Pop out uid, since we need to set it differently.
uid = comp.pop('UID')

if cubatype in KEYWORDS.keys():
# Find corresponding module and instantiate the class
mod = import_module('simphony.cuds.meta.%s' % cubatype.lower())
Expand All @@ -245,8 +246,7 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}):
system_managed_keys = {}
supp_params = [str(e).replace('CUBA.', '')
for e in comp_class.supported_parameters()]
# Don't accept UUID entry from the yaml file
supp_params.remove('UID')

for key in comp.keys():
# Check if the key is supported
if key in supp_params:
Expand Down Expand Up @@ -290,6 +290,9 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}):
# Add system managed components by updating the DataContainer
data = comp_inst.data
data.update(system_managed_keys)

# Set the initial uid
data[CUBA.UID] = uuid.UUID(uid)
comp_inst.data = data
else:
message = 'Unknown CUDSComponent "{}"'
Expand All @@ -298,3 +301,5 @@ def _dict_to_CUDSComponent(cubatype, comp, comp_dict={}):
# Store component under the key representing it's memory location
# so that correct object references from the yaml file are preserved
comp_dict[id(comp)] = comp_inst

return comp_dict
53 changes: 32 additions & 21 deletions simphony/io/tests/test_serialisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import tempfile
import uuid

from simphony.cuds.meta.cuds_component import CUDSComponent
from simphony.cuds.meta.material import Material
from simphony.cuds.meta.material_relation import MaterialRelation
from simphony.cuds.model import CUDS
from simphony.cuds.meta.api import CUDSComponent
from simphony.cuds.meta.api import Material
from simphony.cuds.meta.api import MaterialRelation
from simphony.cuds import CUDS
from simphony.io.serialisation import save_CUDS, load_CUDS


Expand Down Expand Up @@ -62,8 +62,8 @@ def test_save_CUDS_empty(self):

def test_save_CUDS_complicated_data(self):
filename = os.path.join(self.temp_dir, 'test_full.yml')
C = CUDS(name='full',
description='model with crossreferenced components')
cuds = CUDS(name='full',
description='model with crossreferenced components')

M1 = Material(name='steel',
description='FCC steel sphere structure')
Expand All @@ -74,31 +74,42 @@ def test_save_CUDS_complicated_data(self):
MR2 = MaterialRelation(name='epoxy in sheet metal container',
material=[M2, M3])
# M1 is not added
C.add(M2)
C.add(M3)
C.add(MR1)
C.add(MR2)
cuds.add(M2)
cuds.add(M3)
cuds.add(MR1)
cuds.add(MR2)

with closing(open(filename, 'w')) as handle:
save_CUDS(handle, C)
save_CUDS(handle, cuds)
print("cuds data", cuds.data)

with closing(open(filename, 'r')) as handle:
CC = load_CUDS(handle)
loaded_cuds = load_CUDS(handle)
print("loaded_cuds data", loaded_cuds.data)

self.assertEqual(CC.name, C.name)
self.assertEqual(CC.description, C.description)
self.assertEqual(loaded_cuds.name, cuds.name)
self.assertEqual(loaded_cuds.description, cuds.description)

for cuds_item in cuds.iter(CUDSComponent):
print('item original', cuds_item)

for cuds_item in loaded_cuds.iter(CUDSComponent):
print('item loaded', cuds_item)

# Iterate over components in the original model and check
# that they are present in the loaded model. Loaded model
# has additionally material 'M1' included.
for Citem in C.iter(CUDSComponent):
for cuds_item in cuds.iter(CUDSComponent):
# Check items that have name parameter defined
if Citem.name is not None:
CCitem = CC.get(Citem.name)
for key in Citem.data.keys():
Ci = Citem.data[key]
CCi = CCitem.data[key]
_compare_components(Ci, CCi, testcase=self)
print("cuds_item", cuds_item)
if cuds_item.name is not None:
loaded_item = loaded_cuds.get_by_uid(cuds_item.uid)
print("loaded_item", loaded_item)
for key in cuds_item.data.keys():
print(key)
ci = cuds_item.data[key]
li = loaded_item.data[key]
_compare_components(ci, li, testcase=self)


def _compare_components(comp1, comp2, testcase):
Expand Down

0 comments on commit 0c8b3db

Please sign in to comment.