From 15c89fb9b544f474449c5dc69dfeb18f2e84626d Mon Sep 17 00:00:00 2001 From: flatberg Date: Fri, 9 Feb 2007 14:02:39 +0000 Subject: [PATCH] Factored out packer A packer.py --- fluents/lib/packer.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 fluents/lib/packer.py 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