Ejemplo n.º 1
0
 def load(self, directory, filename):
     if self._content is not None:  # avoid loading multiple times
         if self._content.size > 0:
             it = AffineExpansionStorageContent_Iterator(
                 self._content,
                 flags=["multi_index", "refs_ok"],
                 op_flags=["readonly"])
             while not it.finished:
                 if self._content[
                         it.
                         multi_index] is not None:  # ... but only if there is at least one element different from None
                     if isinstance(self._content[it.multi_index],
                                   AbstractFunctionsList):
                         if len(
                                 self._content[it.multi_index]
                         ) > 0:  # ... unless it is an empty FunctionsList
                             return False
                     elif isinstance(self._content[it.multi_index],
                                     AbstractBasisFunctionsMatrix):
                         if sum(
                                 self._content[it.multi_index].
                                 _component_name_to_basis_component_length
                                 .values()
                         ) > 0:  # ... unless it is an empty BasisFunctionsMatrix
                             return False
                     else:
                         return False
                 it.iternext()
     # Get full directory name
     full_directory = Folders.Folder(
         os.path.join(str(directory), filename))
     # Exit in the trivial case of empty affine expansion
     if self._content.size is 0:
         return True
     # Load content item type and shape
     reference_item = self._load_content_item_type_shape(full_directory)
     # Initialize iterator
     it = AffineExpansionStorageContent_Iterator(
         self._content, flags=["c_index", "multi_index", "refs_ok"])
     # Load content
     self._load_content(reference_item, it, full_directory)
     # Load dicts
     self._load_dicts(full_directory)
     # Reset precomputed slices
     self._precomputed_slices.clear()
     self._prepare_trivial_precomputed_slice(reference_item)
     # Return
     return True
Ejemplo n.º 2
0
        def __getitem__(self, key):
            """
            return the subtensors of size "key" for every element in content. (e.g. submatrices [1:5,1:5] of the affine expansion of A)
            """
            it = AffineExpansionStorageContent_Iterator(
                self._content,
                flags=["multi_index", "refs_ok"],
                op_flags=["readonly"])
            slices = slice_to_array(
                self._content[it.multi_index], key,
                self._component_name_to_basis_component_length,
                self._component_name_to_basis_component_index)

            if slices in self._precomputed_slices:
                return self._precomputed_slices[slices]
            else:
                output = _AffineExpansionStorage.__new__(
                    type(self), *self._content.shape)
                output.__init__(*self._content.shape)
                while not it.finished:
                    # Slice content and assign
                    output[it.multi_index] = self._do_slicing(
                        self._content[it.multi_index], key)
                    # Increment
                    it.iternext()
                self._precomputed_slices[slices] = output
                return output
Ejemplo n.º 3
0
 def _load_dicts(self, full_directory):
     assert DictIO.exists_file(
         full_directory, "component_name_to_basis_component_index")
     self._component_name_to_basis_component_index = DictIO.load_file(
         full_directory,
         "component_name_to_basis_component_index",
         globals={
             "ComponentNameToBasisComponentIndexDict":
             ComponentNameToBasisComponentIndexDict
         })
     assert DictIO.exists_file(
         full_directory, "component_name_to_basis_component_length")
     self._component_name_to_basis_component_length = DictIO.load_file(
         full_directory,
         "component_name_to_basis_component_length",
         globals={"OnlineSizeDict": OnlineSizeDict})
     it = AffineExpansionStorageContent_Iterator(
         self._content,
         flags=["multi_index", "refs_ok"],
         op_flags=["readonly"])
     while not it.finished:
         if self._component_name_to_basis_component_index is not None:
             self._content[
                 it.
                 multi_index]._component_name_to_basis_component_index = self._component_name_to_basis_component_index
         if self._component_name_to_basis_component_length is not None:
             self._content[
                 it.
                 multi_index]._component_name_to_basis_component_length = self._component_name_to_basis_component_length
         it.iternext()
Ejemplo n.º 4
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(
         os.path.join(str(directory), filename))
     full_directory.create()
     # Exit in the trivial case of empty affine expansion
     if self._content.size is 0:
         return
     # Initialize iterator
     it = AffineExpansionStorageContent_Iterator(
         self._content,
         flags=["c_index", "multi_index", "refs_ok"],
         op_flags=["readonly"])
     # Save content item type and shape
     self._save_content_item_type_shape(self._content[it.multi_index],
                                        it, full_directory)
     # Save content
     self._save_content(self._content[it.multi_index], it,
                        full_directory)
     # Save dicts
     self._save_dicts(full_directory)
Ejemplo n.º 5
0
 def __iter__(self):
     return AffineExpansionStorageContent_Iterator(
         self._content, flags=["refs_ok"], op_flags=["readonly"])