diff --git a/fluents/lib/packer.py b/fluents/lib/packer.py new file mode 100644 index 0000000..34983e7 --- /dev/null +++ b/fluents/lib/packer.py @@ -0,0 +1,28 @@ +class Packer: + """A compression object used to speed up model calculations. + + Often used in conjunction with crossvalidation and perturbations + analysis. + """ + def __init__(self,array): + self._shape = array.shape + self._array = array + self._packed_data = None + + def expand(self,a): + if self._inflater!=None: + return dot(self._inflater,a) + + def collapse(self,axis=None,mode='svd'): + if not axis: + axis = argmin(self._array.shape) # default is the smallest dim + + if axis == 1: + self._array = self._array.T + u, s, vt = svd(self._array,full_matrices=0) + self._inflater = vt.T + self._packed_data = u*s + return self._packed_data + + def get_packed_data(self): + return self._packed_data