Example #1
0
    def test_mass_registration(self):
        # Dimensions
        D = {
            'ntime': create_dimension('ntime', 72),
            'na': create_dimension('na', 7),
            'nbl': create_dimension('nbl', 21),
        }

        # Arrays
        A = {
            'uvw': {
                'name': 'uvw',
                'shape': ('ntime', 'na', 3),
                'dtype': np.float64
            },
            'model_vis': {
                'name': 'model_vis',
                'shape': ('ntime', 'nbl', 'nchan', 4),
                'dtype': np.complex128
            }
        }

        # Properties
        P = {
            'alpha': {
                'name': 'alpha',
                'default': 1.5,
                'dtype': np.float64
            },
            'beta': {
                'name': 'model_vis',
                'default': 1 + 2j,
                'dtype': np.complex128
            }
        }

        # Test registration by dictionary
        dcube = hc.HyperCube()
        dcube.register_dimensions(D)
        dcube.register_arrays(A)
        dcube.register_properties(P)

        # Test registraion by list
        lcube = hc.HyperCube()
        lcube.register_dimensions(iter(D.values()))
        lcube.register_arrays(iter(A.values()))
        lcube.register_properties(iter(P.values()))

        # The two should agree
        self.assertTrue(dcube.dimensions() == lcube.dimensions())
        self.assertTrue(dcube.arrays() == lcube.arrays())
        self.assertTrue(dcube.properties() == lcube.properties())
Example #2
0
    def test_construct_and_copy(self):
        # Dimensions
        D = {
            'ntime': create_dimension('ntime', 72),
            'na': create_dimension('na', 7),
            'nbl': create_dimension('nbl', 21),
        }

        # Arrays
        A = {
            'uvw': {
                'name': 'uvw',
                'shape': ('ntime', 'na', 3),
                'dtype': np.float64
            },
            'model_vis': {
                'name': 'model_vis',
                'shape': ('ntime', 'nbl', 'nchan', 4),
                'dtype': np.complex128
            }
        }

        # Properties
        P = {
            'alpha': {
                'name': 'alpha',
                'default': 1.5,
                'dtype': np.float64
            },
            'beta': {
                'name': 'model_vis',
                'default': 1 + 2j,
                'dtype': np.complex128
            }
        }

        # Explicit registration
        ecube = hc.HyperCube()
        ecube.register_dimensions(D)
        ecube.register_arrays(A)
        ecube.register_properties(P)

        copy = ecube.copy()
        self.assertTrue(copy.dimensions() == ecube.dimensions())
        self.assertTrue(copy.arrays() == ecube.arrays())
        self.assertTrue(copy.properties() == ecube.properties())

        # Register during construction
        ccube = hc.HyperCube(dimensions=D, arrays=A, properties=P)
        self.assertTrue(ccube.dimensions() == ecube.dimensions())
        self.assertTrue(ccube.arrays() == ecube.arrays())
        self.assertTrue(ccube.properties() == ecube.properties())
Example #3
0
    def test_construct_and_copy(self):
        # Dimensions
        D = {
            'ntime': create_dimension('ntime', 72),
            'na' : create_dimension('na', 7),
            'nbl' : create_dimension('nbl', 21),
        }

        # Arrays
        A = {
            'uvw': {
                'name': 'uvw',
                'shape': ('ntime','na',3),
                'dtype': np.float64 },
            'model_vis' : {
                'name': 'model_vis',
                'shape': ('ntime', 'nbl', 'nchan', 4),
                'dtype' : np.complex128  }
        }

        # Properties
        P = {
            'alpha': {
                'name': 'alpha',
                'default' : 1.5,
                'dtype': np.float64 },
            'beta' : {
                'name': 'model_vis',
                'default' : 1 + 2j,
                'dtype' : np.complex128  }
        }

        # Explicit registration
        ecube = hc.HyperCube()
        ecube.register_dimensions(D)
        ecube.register_arrays(A)
        ecube.register_properties(P)

        copy = ecube.copy()
        self.assertTrue(copy.dimensions() == ecube.dimensions())
        self.assertTrue(copy.arrays() == ecube.arrays())
        self.assertTrue(copy.properties() == ecube.properties())

        # Register during construction
        ccube = hc.HyperCube(dimensions=D, arrays=A, properties=P)
        self.assertTrue(ccube.dimensions() == ecube.dimensions())
        self.assertTrue(ccube.arrays() == ecube.arrays())
        self.assertTrue(ccube.properties() == ecube.properties())
Example #4
0
    def test_mass_registration(self):
        # Dimensions
        D = {
            'ntime': create_dimension('ntime', 72),
            'na' : create_dimension('na', 7),
            'nbl' : create_dimension('nbl', 21),
        }

        # Arrays
        A = {
            'uvw': {
                'name': 'uvw',
                'shape': ('ntime','na',3),
                'dtype': np.float64 },
            'model_vis' : {
                'name': 'model_vis',
                'shape': ('ntime', 'nbl', 'nchan', 4),
                'dtype' : np.complex128  }
        }

        # Properties
        P = {
            'alpha': {
                'name': 'alpha',
                'default' : 1.5,
                'dtype': np.float64 },
            'beta' : {
                'name': 'model_vis',
                'default' : 1 + 2j,
                'dtype' : np.complex128  }
        }

        # Test registration by dictionary
        dcube = hc.HyperCube()
        dcube.register_dimensions(D)
        dcube.register_arrays(A)
        dcube.register_properties(P)

        # Test registraion by list
        lcube = hc.HyperCube()
        lcube.register_dimensions(D.itervalues())
        lcube.register_arrays(A.itervalues())
        lcube.register_properties(P.itervalues())

        # The two should agree
        self.assertTrue(dcube.dimensions() == lcube.dimensions())
        self.assertTrue(dcube.arrays() == lcube.arrays())
        self.assertTrue(dcube.properties() == lcube.properties())
Example #5
0
    def register_dimension(self, name, dim_data, **kwargs):
        """
        Registers a dimension on this cube.

        .. code-block:: python

            cube.register_dimension('ntime', 10000,
                        decription="Number of Timesteps",
                        lower_extent=100, upper_extent=200)

        Parameters
        ----------
        dim_data : int or :class:`~hypercube.dims.Dimension`
            if an integer, this will be used to
            define the global_size of the dimension
            and possibly other attributes if they are
            not present in kwargs.
            If a Dimension, it will be updated with
            any appropriate keyword arguments
        description : str
            The description for this dimension.
            e.g. 'Number of timesteps'.
        lower_extent : int
            The lower extent of this dimension
            within the global space
        upper_extent : int
            The upper extent of this dimension
            within the global space
        name : Dimension name


        Returns
        -------
        :class:`~hypercube.dims.Dimension`
            A hypercube Dimension

        """

        if name in self._dims:
            raise AttributeError((
                "Attempted to register dimension '{n}'' "
                "as an attribute of the cube, but "
                "it already exists. Please choose "
                "a different name!").format(n=name))

        # Create the dimension dictionary
        D = self._dims[name] = create_dimension(name,
            dim_data, **kwargs)

        return D
Example #6
0
    def register_dimension(self, name, dim_data, **kwargs):
        """
        Registers a dimension with this Solver object

        Arguments
        ---------
            dim_data : integer or Dimension
                if an integer, this will be used to
                define the global_size of the dimension
                and possibly other attributes if they are
                not present in kwargs.
                If a Dimension, it will be updated with
                any appropriate keyword arguments

        Keyword Arguments
        -----------------
            description : string
                The description for this dimension.
                e.g. 'Number of timesteps'.
            local_size : integer or None
                The local size of this dimension
                on this solver. If None, set to
                the global_size.
            lower_extent : integer or None
                The lower extent of this dimension
                within the global space
            upper_extent : integer or None
                The upper extent of this dimension
                within the global space
            zero_valid : boolean
                If True, this dimension may be zero-sized.

        Returns
        -------
        A Dimension object
        """

        if name in self._dims:
            raise AttributeError((
                "Attempted to register dimension '{n}'' "
                "as an attribute of the solver, but "
                "it already exists. Please choose "
                "a different name!").format(n=name))

        # Create the dimension dictionary
        D = self._dims[name] = create_dimension(name,
            dim_data, **kwargs)

        return D
Example #7
0
    def dimension(self, name, copy=True):
        """
        Returns the requested :class:`~hypercube.dims.Dimension` object

        Parameters
        ----------
        name : str
            Name of the :class:`~hypercube.dims.Dimension` object
        copy : boolean
            Returns a copy of the :class:`~hypercube.dims.Dimension` object if True (Default value = True)

        Returns
        -------
        :class:`~hypercube.dims.Dimension`
            A :class:`~hypercube.dims.Dimension` object.
        """

        try:
            return create_dimension(name, self._dims[name]) if copy else self._dims[name]
        except KeyError:
            raise KeyError("Dimension '{n}' is not registered "
                "on this cube".format(n=name)), None, sys.exc_info()[2]