Commit 9dc01f33 authored by Jait Dixit's avatar Jait Dixit

WIP: Add versioning functionality to spaces

parent 5af4d1da
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from prober import Prober from prober import Prober
from operator_prober import OperatorProber
from diagonal_prober import * from diagonal_prober import *
from trace_prober import * from trace_prober import *
from __future__ import division from __future__ import division
import pickle
import itertools import itertools
import numpy as np import numpy as np
import d2o import d2o
from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES
from keepers import Versionable
from nifty.spaces.space import Space from nifty.spaces.space import Space
from nifty.config import nifty_configuration as gc,\ from nifty.config import nifty_configuration as gc,\
...@@ -16,7 +19,7 @@ gl = gdi.get('libsharp_wrapper_gl') ...@@ -16,7 +19,7 @@ gl = gdi.get('libsharp_wrapper_gl')
GL_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global'] GL_DISTRIBUTION_STRATEGIES = DISTRIBUTION_STRATEGIES['global']
class GLSpace(Space): class GLSpace(Versionable, Space):
""" """
.. __ .. __
.. / / .. / /
...@@ -69,6 +72,8 @@ class GLSpace(Space): ...@@ -69,6 +72,8 @@ class GLSpace(Space):
An array containing the pixel sizes. An array containing the pixel sizes.
""" """
_serializable = ('nlat', 'nlon', 'dtype')
# ---Overwritten properties and methods--- # ---Overwritten properties and methods---
def __init__(self, nlat=2, nlon=None, dtype=np.dtype('float')): def __init__(self, nlat=2, nlon=None, dtype=np.dtype('float')):
...@@ -208,3 +213,17 @@ class GLSpace(Space): ...@@ -208,3 +213,17 @@ class GLSpace(Space):
self.logger.warn("nlon was set to an unrecommended value: " self.logger.warn("nlon was set to an unrecommended value: "
"nlon <> 2*nlat-1.") "nlon <> 2*nlat-1.")
return nlon return nlon
# ---Serialization---
def _to_hdf5(self, hdf5_group):
hdf5_group['serialized'] = [
pickle.dumps(getattr(self, item)) for item in self._serializable
]
return None
@classmethod
def _from_hdf5(cls, hdf5_group, loopback_get):
result = cls(
*[pickle.loads(item) for item in hdf5_group['serialized']])
return result
...@@ -33,9 +33,12 @@ ...@@ -33,9 +33,12 @@
""" """
from __future__ import division from __future__ import division
import pickle
import numpy as np import numpy as np
import d2o import d2o
from keepers import Versionable
from nifty.spaces.space import Space from nifty.spaces.space import Space
from nifty.config import nifty_configuration as gc, \ from nifty.config import nifty_configuration as gc, \
...@@ -44,7 +47,7 @@ from nifty.config import nifty_configuration as gc, \ ...@@ -44,7 +47,7 @@ from nifty.config import nifty_configuration as gc, \
hp = gdi.get('healpy') hp = gdi.get('healpy')
class HPSpace(Space): class HPSpace(Versionable, Space):
""" """
.. __ .. __
.. / / .. / /
...@@ -94,6 +97,8 @@ class HPSpace(Space): ...@@ -94,6 +97,8 @@ class HPSpace(Space):
An array with one element containing the pixel size. An array with one element containing the pixel size.
""" """
_serializable = ('nside', 'dtype')
# ---Overwritten properties and methods--- # ---Overwritten properties and methods---
def __init__(self, nside=2, dtype=np.dtype('float')): def __init__(self, nside=2, dtype=np.dtype('float')):
...@@ -203,3 +208,17 @@ class HPSpace(Space): ...@@ -203,3 +208,17 @@ class HPSpace(Space):
raise ValueError( raise ValueError(
"nside must be positive and a multiple of 2.") "nside must be positive and a multiple of 2.")
return nside return nside
# ---Serialization---
def _to_hdf5(self, hdf5_group):
hdf5_group['serialized'] = [
pickle.dumps(getattr(self, item)) for item in self._serializable
]
return None
@classmethod
def _from_hdf5(cls, hdf5_group, loopback_get):
result = cls(
*[pickle.loads(item) for item in hdf5_group['serialized']])
return result
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pickle
import numpy as np import numpy as np
from keepers import Versionable
import d2o import d2o
from power_index_factory import PowerIndexFactory from power_index_factory import PowerIndexFactory
...@@ -11,13 +14,16 @@ from nifty.spaces.rg_space import RGSpace ...@@ -11,13 +14,16 @@ from nifty.spaces.rg_space import RGSpace
from nifty.nifty_utilities import cast_axis_to_tuple from nifty.nifty_utilities import cast_axis_to_tuple
class PowerSpace(Space): class PowerSpace(Versionable, Space):
_serializable = ('log', 'nbin', 'binbounds', 'kindex', 'rho',
'pundex', 'dtype')
# ---Overwritten properties and methods--- # ---Overwritten properties and methods---
def __init__(self, harmonic_domain=RGSpace((1,)), def __init__(self, harmonic_domain=RGSpace((1,)),
distribution_strategy='not', distribution_strategy='not',
log=False, nbin=None, binbounds=None, log=False, nbin=None, binbounds=None, power_index=None,
dtype=np.dtype('float')): dtype=np.dtype('float')):
super(PowerSpace, self).__init__(dtype) super(PowerSpace, self).__init__(dtype)
...@@ -32,12 +38,13 @@ class PowerSpace(Space): ...@@ -32,12 +38,13 @@ class PowerSpace(Space):
"harmonic_domain must be a harmonic space.") "harmonic_domain must be a harmonic space.")
self._harmonic_domain = harmonic_domain self._harmonic_domain = harmonic_domain
power_index = PowerIndexFactory.get_power_index( if power_index is None:
domain=self.harmonic_domain, power_index = PowerIndexFactory.get_power_index(
distribution_strategy=distribution_strategy, domain=self.harmonic_domain,
log=log, distribution_strategy=distribution_strategy,
nbin=nbin, log=log,
binbounds=binbounds) nbin=nbin,
binbounds=binbounds)
config = power_index['config'] config = power_index['config']
self._log = config['log'] self._log = config['log']
...@@ -154,3 +161,42 @@ class PowerSpace(Space): ...@@ -154,3 +161,42 @@ class PowerSpace(Space):
@property @property
def k_array(self): def k_array(self):
return self._k_array return self._k_array
# ---Serialization---
def _to_hdf5(self, hdf5_group):
hdf5_group['serialized'] = [
pickle.dumps(getattr(self, item)) for item in self._serializable
]
return {
'harmonic_domain': self.harmonic_domain,
'pindex': self.pindex,
'k_array': self.k_array
}
@classmethod
def _from_hdf5(cls, hdf5_group, loopback_get):
deserialized =\
[pickle.loads(item) for item in hdf5_group['serialized']]
dtype = deserialized[6]
harmonic_domain = loopback_get('harmonic_domain')
power_index = {
'config': {
'log': deserialized[0], 'nbin': deserialized[1],
'binbounds': deserialized[2]
},
'pindex': loopback_get('pindex'),
'kindex': deserialized[3],
'rho': deserialized[4],
'pundex': deserialized[5],
'k_array': loopback_get('k_array')
}
result = cls(
harmonic_domain=harmonic_domain,
power_index=power_index,
dtype=dtype
)
return result
...@@ -33,15 +33,19 @@ ...@@ -33,15 +33,19 @@
""" """
from __future__ import division from __future__ import division
import pickle
import numpy as np import numpy as np
from keepers import Versionable
from d2o import distributed_data_object,\ from d2o import distributed_data_object,\
STRATEGIES as DISTRIBUTION_STRATEGIES STRATEGIES as DISTRIBUTION_STRATEGIES
from nifty.spaces.space import Space from nifty.spaces.space import Space
class RGSpace(Space): class RGSpace(Versionable, Space):
""" """
.. _____ _______ .. _____ _______
.. / __/ / _ / .. / __/ / _ /
...@@ -103,6 +107,8 @@ class RGSpace(Space): ...@@ -103,6 +107,8 @@ class RGSpace(Space):
Whether or not the grid represents a Fourier basis. Whether or not the grid represents a Fourier basis.
""" """
_serializable = ('shape', 'zerocenter', 'distances', 'harmonic', 'dtype')
# ---Overwritten properties and methods--- # ---Overwritten properties and methods---
def __init__(self, shape=(1,), zerocenter=False, distances=None, def __init__(self, shape=(1,), zerocenter=False, distances=None,
...@@ -320,3 +326,17 @@ class RGSpace(Space): ...@@ -320,3 +326,17 @@ class RGSpace(Space):
temp = np.empty(len(self.shape), dtype=bool) temp = np.empty(len(self.shape), dtype=bool)
temp[:] = zerocenter temp[:] = zerocenter
return tuple(temp) return tuple(temp)
# ---Serialization---
def _to_hdf5(self, hdf5_group):
hdf5_group['serialized'] = [
pickle.dumps(getattr(self, item)) for item in self._serializable
]
return None
@classmethod
def _from_hdf5(cls, hdf5_group, loopback_get):
result = cls(
*[pickle.loads(item) for item in hdf5_group['serialized']])
return result
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment