Skip to content

Commit 5daf44e

Browse files
authored
Merge pull request #947 from ipelupessy/fix_856
make sure vectorattributes definitions are stored and copied
2 parents 0eac17b + 15469d8 commit 5daf44e

3 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/amuse/datamodel/particles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ def copy(self, memento = None, keep_structure = False, filter_attributes = lambd
386386
converted.append(x)
387387
result.add_particles_to_store(keys, attributes, converted)
388388

389+
object.__setattr__(result, "_derived_attributes", CompositeDictionary(self._derived_attributes))
389390
result._private.collection_attributes = self._private.collection_attributes._copy_for_collection(result)
390391

391392
return result
@@ -425,6 +426,7 @@ def copy_to_new_particles(self, keys = None, keys_generator = None, memento = No
425426

426427
result.add_particles_to_store(particle_keys, attributes, converted)
427428

429+
object.__setattr__(result, "_derived_attributes", CompositeDictionary(self._derived_attributes))
428430
result._private.collection_attributes = self._private.collection_attributes._copy_for_collection(result)
429431

430432
return result

src/amuse/io/store_v2.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from amuse.datamodel import Grid
2424
from amuse.datamodel import GridPoint
2525
from amuse.datamodel import AbstractSet
26+
from amuse.datamodel.base import VectorAttribute
27+
2628

2729
from amuse.io import store_v1
2830

@@ -847,6 +849,7 @@ def store_particles(self, particles, extra_attributes = {}, parent=None, mapping
847849
self.hdf5file.flush()
848850
self.store_collection_attributes(particles, group, extra_attributes, links)
849851
self.store_values(particles, group, links)
852+
self.store_selected_derived_attributes(particles,group)
850853

851854
mapping_from_setid_to_group[id(particles)] = group
852855

@@ -868,6 +871,7 @@ def store_grid(self, grid, extra_attributes = {}, parent=None, mapping_from_seti
868871
compression_opts=self.compression_opts,
869872
)
870873

874+
self.store_selected_derived_attributes(grid, group)
871875
self.store_collection_attributes(grid, group, extra_attributes, links)
872876
self.store_values(grid, group, links)
873877

@@ -924,8 +928,15 @@ def store_values(self, container, group, links = []):
924928
)
925929
dataset.attrs["units"] = "none".encode('ascii')
926930

931+
def store_selected_derived_attributes(self, container, group):
932+
saving=dict()
933+
for key in container._derived_attributes.keys():
934+
attr=container._derived_attributes[key]
935+
if key not in container.GLOBAL_DERIVED_ATTRIBUTES and isinstance(attr, VectorAttribute):
936+
saving[key]=attr
937+
if len(saving):
938+
group.attrs["extra_vector_attributes"]=pickle_to_string(saving)
927939

928-
929940
def store_linked_array(self, attribute, attributes_group, quantity, group, links):
930941
subgroup = attributes_group.create_group(attribute)
931942
shape = quantity.shape
@@ -1127,8 +1138,9 @@ def load_particles_from_group(self, group):
11271138

11281139
self.mapping_from_groupid_to_set[group.id] = particles
11291140
self.load_collection_attributes(particles, group)
1130-
1131-
1141+
if "extra_vector_attributes" in group.attrs.keys():
1142+
self.load_extra_derived_attributes(particles, group)
1143+
11321144
return particles
11331145

11341146
def load_grid_from_group(self, group):
@@ -1143,9 +1155,16 @@ def load_grid_from_group(self, group):
11431155
container._private.attribute_storage = HDF5GridAttributeStorage(shape, group, self)
11441156
self.mapping_from_groupid_to_set[group.id] = container
11451157
self.load_collection_attributes(container, group)
1158+
if "extra_vector_attributes" in group.attrs.keys():
1159+
self.load_extra_derived_attributes(container, group)
11461160

11471161
return container
11481162

1163+
def load_extra_derived_attributes(self, container, group):
1164+
attrs=unpickle_from_string(group.attrs["extra_vector_attributes"])
1165+
for key, attr in attrs.items():
1166+
container._derived_attributes[key]=attr
1167+
11491168
def load_from_group(self, group):
11501169
container_type = group.attrs['type'] if isinstance(group.attrs['type'], str) else group.attrs['type'].decode('ascii')
11511170

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
from amuse.test import amusetest
3+
4+
from amuse.datamodel import new_cartesian_grid, Particles
5+
6+
from amuse.io import read_set_from_file, write_set_to_file
7+
8+
class test_github856(amusetest.TestCase):
9+
def test1(self):
10+
filename=os.path.join(self.get_path_to_results(),"github856.amuse")
11+
12+
g1=new_cartesian_grid((5,5), 1)
13+
write_set_to_file(g1,filename,"amuse")
14+
del g1
15+
g2=read_set_from_file(filename,"amuse")
16+
self.assertEquals(g2.get_axes_names(),"xy")
17+
18+
def test2(self):
19+
g1=Particles(lon=[1,2], lat=[3,4])
20+
g1.add_vector_attribute("lonlat",["lon","lat"])
21+
g2=g1.copy()
22+
self.assertEquals(g2.lonlat,[[1,3],[2,4]])
23+
24+
def test3(self):
25+
filename=os.path.join(self.get_path_to_results(),"github856_2.amuse")
26+
27+
g1=Particles(lon=[1,2], lat=[3,4])
28+
g1.add_vector_attribute("lonlat",["lon","lat"])
29+
write_set_to_file(g1,filename,"amuse")
30+
del g1
31+
g2=read_set_from_file(filename,"amuse")
32+
self.assertEquals(g2.lonlat,[[1,3],[2,4]])

0 commit comments

Comments
 (0)