├── src ├── PFANodeMDPRefImp.py ├── MergeLayer.py ├── cache_node.py ├── PFANodeMDP.py └── PFACoreUtil.py ├── README └── LICENSE /src/PFANodeMDPRefImp.py: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # Copyright of GNUPFA: 4 | # Copyright (c) 2013, 2014 Institut fuer Neuroinformatik, 5 | # Ruhr-Universitaet Bochum, Germany. All rights reserved. 6 | # 7 | # 8 | # This file is part of GNUPFA. 9 | # 10 | # GNUPFA is free software: you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation, either version 3 of the License, or 13 | # (at your option) any later version. 14 | # 15 | # GNUPFA is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | # 20 | # You should have received a copy of the GNU General Public License 21 | # along with GNUPFA. If not, see . 22 | # 23 | # 24 | # Linking this library statically or dynamically with other modules is 25 | # making a combined work based on this library. Thus, the terms and 26 | # conditions of the GNU General Public License cover the whole 27 | # combination. 28 | # 29 | 30 | 31 | ''' 32 | Created on 13.08.2013 33 | 34 | @author: Stefan Richthofer 35 | ''' 36 | import mdp 37 | import numpy as np 38 | import PFACoreUtil as pfa 39 | 40 | class PFANode(mdp.Node): 41 | '''redifined _init_ Method with p, k as arguments ''' 42 | def __init__(self, p = 2, k = 0, affine = True, input_dim = None, output_dim = None, dtype = None): 43 | super(PFANode, self).__init__(input_dim = input_dim, output_dim = output_dim, dtype = dtype) 44 | self.p = p 45 | self.k = k 46 | self.data = None 47 | self.affine = affine 48 | 49 | ''' Node is trainable ''' 50 | def is_trainable(self): 51 | return True 52 | 53 | '''In this reference implementation, it simply collects the data to process it in _stop_training.''' 54 | def _train(self, x): 55 | n = self.get_input_dim() 56 | x2 = x 57 | if not n is None: 58 | x2 = x.T[:n].T 59 | if self.data is None: 60 | self.data = x2 61 | else: 62 | self.data = np.vstack([self.data, x2]) 63 | 64 | def _stop_training(self): 65 | if self.data is None: 66 | raise TrainingException("train was never called") 67 | r = self.get_output_dim() 68 | if r is None: 69 | r = len(self.data.T) 70 | meanRef, SRef, zRef = pfa.calcSpheringParametersAndDataRefImp(self.data)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 71 | self.mean = meanRef 72 | if not self.affine: 73 | WRef = pfa.calcRegressionCoeffRefImp(zRef, self.p) 74 | XRef = pfa.calcErrorCoeffConstLenRefImp(zRef, WRef, self.k) 75 | self.Ar = pfa.calcExtractionForErrorCovRefImp(XRef, r) 76 | reduced = np.dot(self.data, self.Ar.T) 77 | self.W = pfa.calcRegressionCoeffRefImp(reduced, self.p) 78 | else: 79 | WRef = pfa.calcRegressionCoeffAffineRefImp(zRef, self.p) 80 | XRef = pfa.calcErrorCoeffConstLenAffineRefImp(zRef, WRef, self.k) 81 | Ar = pfa.calcExtractionForErrorCovRefImp(XRef, r) 82 | reduced = np.dot(zRef, Ar) 83 | self.W = pfa.calcRegressionCoeffAffineRefImp(reduced, self.p) 84 | self.Ar = np.dot(SRef, Ar) 85 | 86 | def _execute(self, x): 87 | z0 = x-np.outer(np.ones(len(x)), self.mean) 88 | return np.dot(z0, self.Ar.T) 89 | 90 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | GNUPFA is an experimental Python-implementation of the PFA 2 | algorithm as described in http://arxiv.org/abs/1311.2503. 3 | PFA is implemented as an MDP-node (http://mdp-toolkit.sourceforge.net). 4 | When this project reaches sufficient stability, an integration 5 | into MDP is planned. 6 | 7 | 8 | Files 9 | ===== 10 | 11 | PFANodeMDPRefImp.py 12 | ------------------- 13 | 14 | This is a naive one-to-one implementation of the PFA algorithm. 15 | It caches all supplied data into memory and has *no* real support 16 | for chunking (as the intention of chunking is to read only a 17 | small subset of the data to memory, process it, free memory, 18 | process the next subset and so on). 19 | Further it must process the data several times; one time for 20 | each work step. 21 | Its advantage is that it is an easy to read implementation and 22 | is almost certainly bug free. So it can serve as a controlpoint 23 | for more advanced implementations. 24 | 25 | 26 | PFANodeMDP.py 27 | ------------- 28 | 29 | Contains a smarter PFA implementation that supports real chunking. 30 | It only saves the mean, second moment matrix and several auto 31 | correlation matrices. Each chunk is just used to update these, 32 | eleminating any need for keeping data in memory. 33 | In contrast to PFANodeMDPRefImp, this implementation is rather 34 | complicated as the used equations are largely expanded in order 35 | to perform PFA just on top of auto correlation matrices. 36 | However, its results were compared to those of the reference 37 | implementation for various test data and the dicrepancy is on 38 | floating point numerical level. 39 | 40 | The Layer-aware node uses MergeLayer, an experimental notion to 41 | perform clone layer functionality in a more precise way. This is 42 | only relevant if one applies PFA hirarchically (c.f. mdp.hinet). 43 | 44 | 45 | PFACoreUtil.py 46 | -------------- 47 | 48 | Contains various utility functions, on which PFANodeMDPRefImp and 49 | PFANodeMDP are built on. Additionally features some methods to 50 | evaluate the prediction error of extracted components empirically on 51 | given data. Currently it contains a lot of debugging outputs and 52 | requires clean-up. 53 | 54 | 55 | MergeLayer.py 56 | ------------- 57 | 58 | Introduces an MDP-Layer with an additional merging-phase that merges all 59 | nodes in the layer after training. Merging is done by a given merger, 60 | which is itself a node. After merging, the merger will be used for 61 | execution in a CloneLayer-like fashion. 62 | 63 | The idea behind MergeLayer is a hybrid of ordinary layer and CloneLayer. 64 | The goal in this design is to use separate nodes in the train-phase, 65 | while using only a single node for execution. The difference to CloneLayer 66 | is that in MergeLayer, a different algorithm can be used for combining 67 | horizontally parallel data chunks than for combining time-sequent data 68 | chunks. The latter ones are combined by the nodes in the usual train-phase. 69 | In Contrast to CloneLayer, MergeLayer allows to control how horizontal merging 70 | of the data works. While CloneLayer would push this data into the very same 71 | train method like the time-sequent chunks, MergeLayer uses a merger to combine 72 | horizontal data. 73 | 74 | Note that this implementation is highly experimental. 75 | 76 | 77 | cache_node.py 78 | ------------- 79 | 80 | Introduces caching functionality for MDP flows. Each flow step is saved to disc 81 | after it has been trained. This way, early nodes in the flow don't need to process 82 | the data repeatedly. 83 | And additional feature is that data can be re-ordered after caching. Most MDP nodes 84 | are agnostic to the order of their data anyway, but in layer-case, the order can matter, 85 | if image data from parallel areas is provided sequentially to the node (clone layer). 86 | MergeLayer can solve this, but a reordering cache can solve it too with even lower 87 | memory consumption, as it does not need to have parallel working memory for all areas 88 | in the layer. Note that memory consumption is crucial for potential GPU-based 89 | PFA-implementations. 90 | 91 | This implementation is highly experimental. 92 | 93 | 94 | 95 | License 96 | ======= 97 | 98 | Until it gets integrated into MDP, GNUPFA will be released under GPL, v.3. 99 | See the file "LICENSE" for a copy of this GPL version. 100 | 101 | -------------------------------------------------------------------------------- /src/MergeLayer.py: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # Copyright of GNUPFA: 4 | # Copyright (c) 2013, 2014 Institut fuer Neuroinformatik, 5 | # Ruhr-Universitaet Bochum, Germany. All rights reserved. 6 | # 7 | # 8 | # This file is part of GNUPFA. 9 | # 10 | # GNUPFA is free software: you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation, either version 3 of the License, or 13 | # (at your option) any later version. 14 | # 15 | # GNUPFA is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | # 20 | # You should have received a copy of the GNU General Public License 21 | # along with GNUPFA. If not, see . 22 | # 23 | # 24 | # Linking this library statically or dynamically with other modules is 25 | # making a combined work based on this library. Thus, the terms and 26 | # conditions of the GNU General Public License cover the whole 27 | # combination. 28 | # 29 | 30 | 31 | ''' 32 | Created on Oct 16, 2013 33 | 34 | @author: Stefan Richthofer 35 | ''' 36 | 37 | from mdp.hinet.layer import Layer 38 | from mdp import Node 39 | from mdp.hinet import FlowNode 40 | 41 | class Merger(Node): 42 | def is_trainable(self): 43 | """Per default, a merger is not trainable, since it would not be trained except, 44 | it appears multiply in the network. 45 | """ 46 | return False 47 | 48 | # def needs_stop_training_before_merge(self): 49 | # return False 50 | 51 | ### Methods to be implemented by the user 52 | 53 | # this are the methods the user has to overwrite 54 | 55 | def _merge(self, node): 56 | pass 57 | 58 | def _stop_merging(self): 59 | pass 60 | 61 | def freeMem(self): 62 | pass 63 | 64 | ### User interface to the overwritten methods 65 | 66 | def merge(self, node):#, *args, **kwargs): 67 | """Update the internal structures according to the input node `node`. 68 | 69 | `node` is an mdp-node that has been trained as part of a layer. The 70 | merger-subclass should exactly know about the node-type and the node's 71 | internal structure to retrieve the relevant data from it. 72 | 73 | By default, subclasses should overwrite `_merge` to implement their 74 | merging phase. The docstring of the `_train` method overwrites this 75 | docstring. 76 | """ 77 | 78 | self._merge(node) 79 | 80 | def stop_merging(self):#, *args, **kwargs): 81 | """Stop the merging phase. 82 | 83 | By default, subclasses should overwrite `_stop_merging` to implement 84 | this functionality. The docstring of the `_stop_merging` method 85 | overwrites this docstring. 86 | """ 87 | self._stop_merging()#self, *args, **kwargs) 88 | #self._train_phase_started = True 89 | #self._training = False 90 | 91 | class FlowMerger(Merger, FlowNode): 92 | """ 93 | Note that FlowMerger can be trainable though it is a merger. 94 | Edit: It should better not be trainable since this causes problems 95 | with train_phase and is_training. 96 | 97 | This is because the flow might contain trainable nodes as 98 | intermediate steps. 99 | You should use MergableFlowNodes instead of ordinary flows 100 | as merge sources. They ensure that the merger is used to 101 | execute the already trained/merged components. 102 | """ 103 | def __init__(self, merge_indices, flow, input_dim=None, output_dim=None, dtype=None): 104 | super(FlowMerger, self).__init__(flow, input_dim, output_dim, dtype) 105 | self.merge_indices = merge_indices 106 | self.current_merge = 0 107 | #self._train_phase = len(self._train_seq) 108 | 109 | def is_training(self): 110 | return False 111 | 112 | # def needs_stop_training_before_merge(self): 113 | # i = self.merge_indices[self.current_merge] 114 | # if self.get_current_train_phase() == i: 115 | # return self._flow.flow[i].needs_stop_training_before_merge() 116 | # else: 117 | # return True 118 | 119 | def _merge(self, node): 120 | i = self.merge_indices[self.current_merge] 121 | self._flow.flow[i].merge(node._flow.flow[i]) 122 | node._flow.flow[i].freeMem() 123 | 124 | def _stop_merging(self): 125 | i = self.merge_indices[self.current_merge] 126 | self._flow.flow[i]._stop_merging() 127 | self.current_merge += 1 128 | #self.self._train_phase_started = True 129 | #self._training = False 130 | 131 | # def force_stop_training(self, *args, **kwargs): 132 | # """Stop the training phase. 133 | # 134 | # By default, subclasses should overwrite `_stop_training` to implement 135 | # this functionality. The docstring of the `_stop_training` method 136 | # overwrites this docstring. 137 | # """ 138 | # #if self.is_training() and self._train_phase_started == False: 139 | # # raise TrainingException("The node has not been trained.") 140 | # 141 | # #if not self.is_training(): 142 | # # err_str = "The training phase has already finished." 143 | # # raise TrainingFinishedException(err_str) 144 | # 145 | # # close the current phase. 146 | # self._train_seq[self._train_phase][1](*args, **kwargs) 147 | # self._train_phase += 1 148 | # self._train_phase_started = False 149 | # # check if we have some training phase left 150 | # if self.get_remaining_train_phase() == 0: 151 | # self._training = False 152 | 153 | class MergableFlowNode(FlowNode): 154 | 155 | #def __init__(self, flow, input_dim=None, output_dim=None, dtype=None): 156 | def __init__(self, flow, merger, input_dim, output_dim, dtype): 157 | super(MergableFlowNode, self).__init__(flow, input_dim, output_dim, dtype) 158 | self.merger = merger #None 159 | #self._train_seq_cache = None 160 | 161 | #def set_merger(self, merger): 162 | # self.merger = merger 163 | 164 | # def _get_train_seq(self): 165 | # if self._train_seq_cache is None: 166 | # self._train_seq_cache = self._build_train_seq() 167 | # return self._train_seq_cache 168 | # 169 | # def _build_train_seq(self): 170 | def _get_train_seq(self): 171 | """ 172 | Return a training sequence containing all training phases. 173 | In contrast to the original FlowNode, MergableFlowNode uses 174 | a given merger (must be provided by a call to set_merger) 175 | to process the data through the already trained part. 176 | """ 177 | def get_train_function(_i_node, _node): 178 | # This internal function is needed to channel the data through 179 | # the nodes in front of the current nodes. 180 | # using nested scopes here instead of default args, see pep-0227 181 | def _train(x, *args, **kwargs): 182 | if i_node > 0: 183 | #_node.train(self._flow.execute(x, nodenr=_i_node-1), *args, **kwargs) 184 | #print "delegate exec in train to merger " 185 | #print str(self.merger._flow.flow[0].Ar) 186 | _node.train(self.merger._flow.execute(x, nodenr=_i_node-1), *args, **kwargs) 187 | #_node.train(self.merger.execute(x), *args, **kwargs) 188 | else: 189 | _node.train(x, *args, **kwargs) 190 | #self.merger._train_phase_started = True 191 | return _train 192 | 193 | train_seq = [] 194 | for i_node, node in enumerate(self._flow): 195 | if node.is_trainable(): 196 | remaining_len = (len(node._get_train_seq()) 197 | - self._pretrained_phase[i_node]) 198 | train_seq += ([(get_train_function(i_node, node), 199 | node.stop_training)] * remaining_len) 200 | 201 | # try fix the dimension of the internal nodes and the FlowNode 202 | # after the last node has been trained 203 | def _get_stop_training_wrapper(self, node, func): 204 | def _stop_training_wrapper(*args, **kwargs): 205 | func(*args, **kwargs) 206 | self._fix_nodes_dimensions() 207 | return _stop_training_wrapper 208 | 209 | if train_seq: 210 | train_seq[-1] = (train_seq[-1][0], 211 | _get_stop_training_wrapper(self, self._flow[-1], train_seq[-1][1])) 212 | return train_seq 213 | 214 | 215 | def train(self, x, *args, **kwargs): 216 | print "MergableFlowNode train "+str(self._train_phase) 217 | super(MergableFlowNode, self).train(x, *args, **kwargs) 218 | 219 | 220 | def skip_training(self): 221 | """ 222 | Close the current phase without actually performing it. 223 | This is useful if we know that it was already performed for the 224 | current inner node from a different reference. 225 | Only thing left to do is inform the surrounding flow. 226 | This method does it. 227 | """ 228 | #node._train_seq[self._train_phase][1](*args, **kwargs) 229 | self._train_phase += 1 230 | self._train_phase_started = False 231 | # check if we have some training phase left 232 | if self.get_remaining_train_phase() == 0: 233 | self._training = False 234 | 235 | class MergeLayer(Layer): 236 | """Layer with an additional merging-phase that merges all nodes in the layer 237 | after training. Merging is done by a given merger, which is itself a node. 238 | After merging, the merger will be used for execution in a CloneLayer-like 239 | fashion. 240 | 241 | The idea behind MergeLayer is a hybrid of ordinary layer and CloneLayer. 242 | The goal in this design is to use separate nodes in the train-phase, 243 | while using only a single node for execution. The difference to CloneLayer 244 | is that in MergeLayer, a different algorithm can be used for combining 245 | horizontally parallel data chunks than for combining time-sequent data 246 | chunks. The latter ones are combined by the nodes in the usual train-phase. 247 | In Contrast to CloneLayer, MergeLayer allows to control how horizontal merging 248 | of the data works. While CloneLayer would push this data into the very same 249 | train method like the time-sequent chunks, MergeLayer uses a merger to combine 250 | horizontal data. 251 | """ 252 | 253 | def __init__(self, merger, nodes, call_stop_training = False, dtype=None): 254 | """Setup the layer with the given list of nodes. 255 | 256 | Keyword arguments: 257 | merger -- Merger to be used. 258 | nodes -- List of the nodes to be used. 259 | """ 260 | super(MergeLayer, self).__init__(nodes, dtype=dtype) 261 | self.merger = merger 262 | self.call_stop_training = call_stop_training 263 | 264 | def _stop_training(self, *args, **kwargs): 265 | """Stop training of the internal node.""" 266 | if self.call_stop_training: 267 | super(MergeLayer, self)._stop_training() 268 | for node in self.nodes: 269 | self.merger.merge(node) 270 | self.merger.stop_merging() 271 | self.trained_nodes = self.nodes 272 | self.nodes = (self.merger,) * len(self.trained_nodes) 273 | if self.output_dim is None: 274 | self.output_dim = self._get_output_dim_from_nodes() 275 | 276 | class FlowMergeLayer(Layer): 277 | """Like MergeLayer, but more aware of multiple training phases. 278 | The inner nodes must be MergableFlows. 279 | Given that these need multiple training phases, AdvancedMergeLayer 280 | can treat some phases like MergeLayer, others like CloneLayer or ordinary Layer. 281 | If the flows contain non-merge steps with a reused 282 | Node-reference, this reference should not have stop_training called 283 | several times. 284 | 285 | Warning: Nesting of FlowMergers is not supported! 286 | 287 | If the index of a training phase appears in merge_indices, it calls: 288 | 289 | super(FlowMergeLayer).stop_training 290 | for node in self.nodes: 291 | self.merger.merge(node) 292 | self.merger.stop_merging() 293 | 294 | If the index appears in no list, it calls: 295 | 296 | super(FlowMergeLayer).stop_training() 297 | 298 | If the index appears in clone_indices, it calls: 299 | 300 | merger.stop_training() 301 | for node in self.nodes: 302 | #close training phase without calling stop_training: 303 | node.skip_training() 304 | 305 | It expects merger to contain the same node reference as the usual inner 306 | nodes. (CloneLayer-like fashion but taking merger as the reference handle). 307 | Expecting the inner nodes of the layer to be different flows containing the 308 | same node reference, the flow's training phase is closed without actually 309 | performing it (it was already performed by merger.stop_training). 310 | 311 | The lists are expected to contain the indices in strictly ascending order. 312 | 313 | Note that unlike merge layer, the call of layer-wide stop_training can't be 314 | avoided, since the training phase index must increase. So AdvancedMergeLayer 315 | has no parameter call_stop_training. 316 | 317 | todo: Maybe find better solution for merge/train sequence 318 | Use mode list instead of index list: [merge, clone, train, train, clone, merge,...] 319 | """ 320 | 321 | def __init__(self, merger, nodes, merge_indices, clone_indices, dtype=None): 322 | """Setup the layer with the given list of nodes. 323 | 324 | Keyword arguments: 325 | merger -- Merger to be used. Must be a FlowMerger. 326 | nodes -- List of the nodes to be used. These must be MergableFlows. 327 | """ 328 | #for node in nodes: 329 | #node.set_merger(merger) 330 | super(FlowMergeLayer, self).__init__(nodes, dtype) 331 | self.merger = merger 332 | self.merge_indices = merge_indices 333 | self.clone_indices = clone_indices 334 | #offsets: 335 | self.current_merge = 0 336 | self.current_clone = 0 337 | self.current_train = 0 338 | 339 | def _stop_training(self, *args, **kwargs): 340 | """Stop training of the internal node.""" 341 | #print "FlowMerger stop training" 342 | if self.current_merge < len(self.merge_indices) and self.current_train == self.merge_indices[self.current_merge]: 343 | #print "FlowMerger.super stop training..." 344 | #super(FlowMergeLayer, self).stop_training() 345 | for node in self.nodes: 346 | node.stop_training() 347 | #print "FlowMerger.super stop training done" 348 | #self._train_phase 349 | for node in self.nodes: 350 | self.merger.merge(node) 351 | self.merger.stop_merging() 352 | #print "stop merging done...." 353 | self.current_merge += 1 354 | elif self.current_clone < len(self.clone_indices) and self.current_train == self.clone_indices[self.current_clone]: 355 | #self.merger.force_stop_training() 356 | #self.merger.skip_training() 357 | self.nodes[0].stop_training() 358 | for i in range(1, len(self.nodes)): 359 | self.nodes[i].skip_training() 360 | self.current_clone += 1 361 | else: 362 | #I currently see no usecase for this. Maybe remove it... 363 | super(FlowMergeLayer, self).stop_training() 364 | self.current_train += 1 365 | #print "Close Layer training? "+str(self.get_remaining_train_phase()) 366 | if self.get_remaining_train_phase() == 1: #This already indicates the last phase, because stop_training decrements it after the call to _stop_training 367 | self.trained_nodes = self.nodes 368 | self.nodes = (self.merger,) * len(self.trained_nodes) 369 | #print "Inserted merger as nodes" 370 | if self.output_dim is None: 371 | self.output_dim = self._get_output_dim_from_nodes() 372 | -------------------------------------------------------------------------------- /src/cache_node.py: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # Copyright of GNUPFA: 4 | # Copyright (c) 2013, 2014 Institut fuer Neuroinformatik, 5 | # Ruhr-Universitaet Bochum, Germany. All rights reserved. 6 | # 7 | # 8 | # This file is part of GNUPFA. 9 | # 10 | # GNUPFA is free software: you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation, either version 3 of the License, or 13 | # (at your option) any later version. 14 | # 15 | # GNUPFA is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | # 20 | # You should have received a copy of the GNU General Public License 21 | # along with GNUPFA. If not, see . 22 | # 23 | # 24 | # Linking this library statically or dynamically with other modules is 25 | # making a combined work based on this library. Thus, the terms and 26 | # conditions of the GNU General Public License cover the whole 27 | # combination. 28 | # 29 | 30 | 31 | ''' 32 | Created on Dec 21, 2013 33 | 34 | @author: Stefan Richthofer 35 | ''' 36 | from mdp import Node 37 | from mdp import TrainingException 38 | from mdp.hinet import FlowNode 39 | from mdp.hinet.layer import CloneLayer 40 | import numpy as np 41 | 42 | class CacheNode(Node): 43 | 44 | def __init__(self, filename, cacheSize = -1, input_dim=None, output_dim=None, dtype=None): 45 | super(CacheNode, self).__init__(input_dim, output_dim, dtype) 46 | self.cacheName = filename 47 | self.cacheSize = cacheSize 48 | self.cache = None #np.memmap(filename, dtype='float32', mode='w+', shape=(3,4)) 49 | self.cachePos = 0 50 | self.defaultOutputLength = 0 51 | self.cacheLength = 0 52 | 53 | def reshape(self, shape): 54 | if self.cache is None: 55 | return 56 | s = self.cache.dtype.itemsize 57 | for n in shape: 58 | s *= n 59 | self.cache._mmap.resize(s) 60 | del self.cache 61 | self.cache = np.memmap(self.cacheName, dtype=self.dtype, mode='readwrite', shape=shape) 62 | 63 | #Used to fill the cache 64 | def _train(self, x): 65 | # print self.dtype 66 | if len(x) > self.defaultOutputLength: 67 | self.defaultOutputLength = len(x) 68 | self.cacheLength += len(x) 69 | if self.cache is None: 70 | if self.cacheSize == -1: 71 | #self.cache = np.memmap(self.cacheName, dtype='float32', mode='w+', shape = x.shape) 72 | self.cache = np.memmap(self.cacheName, dtype=self.dtype, mode='w+', shape = x.shape) 73 | else: 74 | #self.cache = np.memmap(self.cacheName, dtype='float32', mode='w+', shape = (self.cacheSize, len(x[0]))) 75 | self.cache = np.memmap(self.cacheName, dtype=self.dtype, mode='w+', shape = (self.cacheSize, len(x[0]))) 76 | elif self.cacheSize == -1: 77 | self.reshape((self.cache.shape[0]+len(x), len(x[0]))) 78 | # print x[0][0].dtype.itemsize 79 | # print self.cache._mmap.size() 80 | # #self.cache._mmap.resize( (self.cache.shape[0]+len(x), len(x[0])) ) 81 | # print self.cache.shape 82 | # newShape = (self.cache.shape[0]+len(x), len(x[0])) 83 | # memmap_resize( newShape, self.cache ) 84 | # del self.cache 85 | # self.cache = np.memmap(self.cacheName, dtype=self.dtype, mode='w+', shape = newShape) 86 | # print "new size: "+str(self.cache._mmap.size()) 87 | # print self.cache.reshape(newShape) 88 | self.cache[self.cachePos:self.cachePos+len(x)] = x 89 | # print self.cache._mmap.size() 90 | # print self.cache[0][0] 91 | # print self.cache[0][0].dtype.itemsize 92 | # print "---" 93 | self.cachePos += len(x) 94 | 95 | def _stop_training(self): 96 | self.cachePos = 0 97 | 98 | def read(self, off, read_len = -1): 99 | if self.cache is None: 100 | raise TrainingException("CacheNode was not filled (i.e. trained).") 101 | if off >= self.cacheLength: 102 | return None 103 | l = read_len 104 | if l == -1: 105 | l = self.defaultOutputLength 106 | if off+l > self.cacheLength: 107 | l = self.cacheLength-off 108 | return self.cache[off:off+l] 109 | 110 | def _execute(self, x): 111 | er = self.read(self.cachePos, len(x)) 112 | self.cachePos += len(x) 113 | return er 114 | 115 | class ReorderingCacheNode(CacheNode): 116 | 117 | def __init__(self, fieldsize, filename, cacheSize = -1, input_dim=None, output_dim=None, dtype=None): 118 | super(ReorderingCacheNode, self).__init__(filename, cacheSize, input_dim, fieldsize, dtype) 119 | self.fieldsize = fieldsize 120 | 121 | def _train(self, x): 122 | self.cacheLength += len(x)*len(x[0])/self.fieldsize 123 | if len(x)*len(x[0])/self.fieldsize > self.defaultOutputLength: 124 | self.defaultOutputLength = len(x)*len(x[0])/self.fieldsize 125 | if self.cache is None: 126 | if self.cacheSize == -1: 127 | self.cache = np.memmap(self.cacheName, dtype=self.dtype, mode='w+', shape = x.shape) 128 | else: 129 | self.cache = np.memmap(self.cacheName, dtype=self.dtype, mode='w+', shape = (self.cacheSize, len(x[0]))) 130 | elif self.cacheSize == -1: 131 | self.reshape( (self.cache.shape[0]+len(x), len(x[0])) ) 132 | self.cache[self.cachePos:self.cachePos+len(x)] = x 133 | self.cachePos += len(x) 134 | 135 | def read(self, off, read_len = -1): 136 | if self.cache is None: 137 | raise TrainingException("CacheNode was not filled (i.e. trained).") 138 | if off >= self.cacheLength: 139 | return None 140 | l = read_len 141 | if l == -1: 142 | l = self.defaultOutputLength 143 | if off+l > self.cacheLength: 144 | l = self.cacheLength-off 145 | off1 = off % len(self.cache) 146 | if off1+l > len(self.cache): 147 | l = len(self.cache)-off1 148 | off2 = (off / len(self.cache)) * self.fieldsize 149 | return self.cache[off1:off1+l, off2:off2+self.fieldsize] 150 | 151 | def read_in_order(self, off, read_len = -1): 152 | return super(ReorderingCacheNode, self).read(off, read_len) 153 | 154 | def _execute(self, x): 155 | er = self.read(self.cachePos, len(x)*len(x[0])/self.fieldsize) 156 | self.cachePos += len(x) 157 | return er 158 | 159 | def contains(array, value): 160 | for i in range(len(array)): 161 | if array[i] == value: 162 | return True 163 | return False 164 | 165 | def buildCaches(cacheIndices, length, basename, cacheSize = -1): 166 | caches = [] 167 | for i in range(length): 168 | if (cacheIndices is None) or contains(cacheIndices, i): 169 | caches.append(CacheNode(basename+"_"+str(i), cacheSize)) 170 | else: 171 | caches.append(None) 172 | return caches 173 | 174 | class CachingFlowNode(FlowNode): 175 | 176 | def __init__(self, flow, caches = [], cacheSize = -1, input_dim=None, output_dim=None, dtype=None): 177 | super(CachingFlowNode, self).__init__(flow, input_dim, output_dim, dtype) 178 | self._train_seq_cache = None 179 | self.caches = caches 180 | 181 | def _check_nodes_consistency(self, flow = None): 182 | """Check the dimension consistency of a list of nodes.""" 183 | if flow is None: 184 | flow = self._flow.flow 185 | for i in range(1, len(flow)): 186 | out = flow[i-1].output_dim 187 | if not self.caches[i] is None: 188 | out = self.caches[i].output_dim 189 | inp = flow[i].input_dim 190 | self._flow._check_dimension_consistency(out, inp) 191 | 192 | def _fix_nodes_dimensions(self): 193 | """Try to fix the dimensions of the internal nodes.""" 194 | if len(self._flow) > 1: 195 | prev_node = self._flow[0] 196 | for node in self._flow[1:]: 197 | if node.input_dim is None: 198 | node.input_dim = prev_node.output_dim 199 | prev_node = node 200 | self._check_nodes_consistency() 201 | if self._flow[-1].output_dim is not None: 202 | # additional checks are performed here 203 | self.output_dim = self._flow[-1].output_dim 204 | 205 | def find_first_cache(self):#, _i_node): 206 | #print "n: "+str(_i_node) 207 | for i in range(len(self.caches)):#_i_node+1): 208 | if not (self.caches[i] is None): 209 | return i 210 | return -1 211 | 212 | def _get_train_seq(self): 213 | """Return a training sequence containing all training phases.""" 214 | def get_train_function(_i_node, _node): 215 | # This internal function is needed to channel the data through 216 | # the nodes in front of the current nodes. 217 | # using nested scopes here instead of default args, see pep-0227 218 | #cachePos = self.find_nearest_cache(_i_node) 219 | def _train(x, *args, **kwargs): 220 | if i_node > 0: 221 | _node.train(self._flow.execute(x, nodenr=_i_node-1), *args, **kwargs) 222 | # if cachePos == _i_node: 223 | # _node.train(self.caches[cachePos].execute(x), *args, **kwargs) 224 | # elif cachePos == -1: 225 | # _node.train(self._flow.execute(x, nodenr=_i_node-1), *args, **kwargs) 226 | # else: 227 | # y = self.caches[cachePos].execute(x) 228 | # for i in range(cachePos, _i_node-1): 229 | # y = self._flow.flow[i].execute(y) 230 | # _node.train(y, *args, **kwargs) 231 | else: 232 | _node.train(x, *args, **kwargs) 233 | return _train 234 | 235 | train_seq = [] 236 | startCache = self.find_first_cache() 237 | if startCache == -1: 238 | startCache = len(self._flow.flow) 239 | 240 | #for i_node, node in enumerate(self._flow): 241 | for i_node in range(startCache): 242 | node = self._flow[i_node] 243 | if node.is_trainable(): 244 | remaining_len = (len(node._get_train_seq()) - self._pretrained_phase[i_node]) 245 | train_seq += ([(get_train_function(i_node, node), node.stop_training)] * remaining_len) 246 | 247 | if not self.caches[startCache] is None: 248 | def _train_tail(x, *args, **kwargs): 249 | if startCache > 0: 250 | self.caches[startCache].train(self._flow.execute(x, nodenr=startCache-1), *args, **kwargs) 251 | else: 252 | self.caches[startCache].train(x) 253 | 254 | def _stop_train_tail(*args, **kwargs): 255 | startCache = self.find_first_cache() 256 | if startCache == -1: 257 | startCache = len(self._flow.flow) 258 | currentTrainNode = startCache 259 | while currentTrainNode < len(self._flow.flow): 260 | #trainNode = self._flow.flow[currentTrainNode] 261 | if self._flow.flow[currentTrainNode].is_trainable(): 262 | remaining_len = (len(self._flow.flow[currentTrainNode]._get_train_seq()) - self._pretrained_phase[i_node]) 263 | for i in range(remaining_len): 264 | readOff = 0 265 | trainData = self.caches[startCache].read(readOff) 266 | while not trainData is None: 267 | for j in range(startCache, currentTrainNode): 268 | trainData = self._flow.flow[j].execute(trainData) 269 | self._flow.flow[currentTrainNode].train(trainData, *args, **kwargs) 270 | readOff += len(trainData) 271 | trainData = self.caches[startCache].read(readOff) 272 | self._flow.flow[currentTrainNode].stop_training(*args, **kwargs) 273 | currentTrainNode += 1 274 | if len(self.caches) < currentTrainNode and not self.caches[currentTrainNode] is None: 275 | readOff = 0 276 | trainData = self.caches[startCache].read(readOff) 277 | while not trainData is None: 278 | for j in range(startCache, currentTrainNode): 279 | trainData = self._flow.flow[j].execute(trainData) 280 | self.caches[currentTrainNode].train(trainData, *args, **kwargs) 281 | readOff += len(trainData) 282 | trainData = self.caches[startCache].read(readOff) 283 | self.caches[currentTrainNode].stop_training(*args, **kwargs) 284 | startCache = currentTrainNode 285 | 286 | self._fix_nodes_dimensions() 287 | 288 | train_seq.append((_train_tail, _stop_train_tail)) 289 | 290 | else: 291 | 292 | # try fix the dimension of the internal nodes and the FlowNode 293 | # after the last node has been trained 294 | def _get_stop_training_wrapper(self, node, func): 295 | def _stop_training_wrapper(*args, **kwargs): 296 | func(*args, **kwargs) 297 | self._fix_nodes_dimensions() 298 | return _stop_training_wrapper 299 | 300 | if train_seq: 301 | train_seq[-1] = (train_seq[-1][0], _get_stop_training_wrapper(self, self._flow[-1], train_seq[-1][1])) 302 | 303 | return train_seq 304 | 305 | class CacheCloneLayer(CloneLayer):#, CacheNode): 306 | #multi inheritance fails because mro would delgate __init__ call in Layer to CacheNode instead of Node. 307 | 308 | """A CloneLayer variant that caches all training data to disc and 309 | then trains the backing node with reordered data. The data is reordered 310 | such that each field is completely trained (i.e. all chunks, if multiple 311 | chunks are used) before the next field starts. 312 | """ 313 | 314 | def __init__(self, cacheName, node, n_nodes=1, dtype=None): 315 | """Setup the layer with the given list of nodes. 316 | 317 | Keyword arguments: 318 | node -- Node to be cloned. 319 | n_nodes -- Number of repetitions/clones of the given node. 320 | """ 321 | 322 | super(CacheCloneLayer, self).__init__(node=node, n_nodes=n_nodes, dtype=dtype) 323 | self.node = node # attribute for convenience 324 | self.cache = ReorderingCacheNode(node.input_dim, cacheName) 325 | self.cacheLength = 0 326 | 327 | def _get_train_seq(self): 328 | return [(self._train, self._stop_training)] 329 | 330 | def read(self, off, read_len = -1): 331 | return self.cache.read_in_order(off, read_len) 332 | 333 | # def _get_train_seq(self): 334 | # """Return the train sequence. 335 | # 336 | # The length is set by the node with maximum length. 337 | # """ 338 | # max_train_length = 0 339 | # for node in self.nodes: 340 | # node_length = len(node._get_train_seq()) 341 | # if node_length > max_train_length: 342 | # max_train_length = node_length 343 | # return ([[self._train, self._stop_training]] * max_train_length) 344 | 345 | def _train(self, x, *args, **kwargs): 346 | self.cacheLength += len(x) 347 | self.cache.train(x) 348 | # """Perform single training step by training the internal nodes.""" 349 | # start_index = 0 350 | # stop_index = 0 351 | # for node in self.nodes: 352 | # start_index = stop_index 353 | # stop_index += node.input_dim 354 | # if node.is_training(): 355 | # node.train(x[:, start_index : stop_index], *args, **kwargs) 356 | 357 | # def _stop_training(self, *args, **kwargs): 358 | # """Stop training of the internal nodes.""" 359 | # for node in self.nodes: 360 | # if node.is_training(): 361 | # node.stop_training(*args, **kwargs) 362 | # if self.output_dim is None: 363 | # self.output_dim = self._get_output_dim_from_nodes() 364 | 365 | def _stop_training(self, *args, **kwargs): 366 | """Stop training of the internal node.""" 367 | self.cache.stop_training() 368 | phases = self.node.get_remaining_train_phase() 369 | for i in range(phases): 370 | pos = 0 371 | while pos < self.cache.cacheLength: 372 | x = self.cache.read(pos) 373 | pos += len(x) 374 | self.node.train(x) 375 | self.node.stop_training(*args, **kwargs) 376 | #if self.node.is_training(): 377 | # self.node.stop_training(*args, **kwargs) 378 | if self.output_dim is None: 379 | self.output_dim = self._get_output_dim_from_nodes() 380 | -------------------------------------------------------------------------------- /src/PFANodeMDP.py: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # Copyright of GNUPFA: 4 | # Copyright (c) 2013, 2014 Institut fuer Neuroinformatik, 5 | # Ruhr-Universitaet Bochum, Germany. All rights reserved. 6 | # 7 | # 8 | # This file is part of GNUPFA. 9 | # 10 | # GNUPFA is free software: you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation, either version 3 of the License, or 13 | # (at your option) any later version. 14 | # 15 | # GNUPFA is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | # 20 | # You should have received a copy of the GNU General Public License 21 | # along with GNUPFA. If not, see . 22 | # 23 | # 24 | # Linking this library statically or dynamically with other modules is 25 | # making a combined work based on this library. Thus, the terms and 26 | # conditions of the GNU General Public License cover the whole 27 | # combination. 28 | # 29 | 30 | 31 | ''' 32 | Created on Sep 9, 2013 33 | 34 | @author: Stefan Richthofer 35 | ''' 36 | import mdp 37 | import numpy as np 38 | import PFACoreUtil as pfa 39 | import PFANodeMDPRefImp as ref 40 | from MergeLayer import Merger 41 | 42 | class PFANode(mdp.Node): 43 | '''redefined _init_ Method with p, k as arguments''' 44 | def __init__(self, p = 2, k = 0, affine = True, input_dim = None, output_dim = None, dtype = None): 45 | super(PFANode, self).__init__(input_dim = input_dim, output_dim = output_dim, dtype = dtype) 46 | self.p = p 47 | self.k = k 48 | self.l = None 49 | self.affine = affine 50 | self.evThreshold = 0.0000000001 51 | self.sindex = -1 52 | self.maxindex = -1 53 | self.layerIndex = -1 54 | self.layerMax = -1 55 | 56 | '''Node is trainable''' 57 | def is_trainable(self): 58 | return True 59 | 60 | '''Saves relevant information of the data for further processing in stop_training.''' 61 | def _train(self, x): 62 | #print "PFA train "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 63 | n = self.get_input_dim() 64 | x2 = x 65 | if not n is None: 66 | x2 = x.T[:n].T 67 | if self.l is None: 68 | # self.data = x2 69 | self.startData = x2[:self.p+self.k] 70 | self.l = 1.0*len(x2) 71 | self.mean = x2.mean(0) 72 | #self.secondMoment = np.dot(x2.T, x2)/(1.0*len(x2)) 73 | data_pk = x2[self.p+self.k:] 74 | self.corList = [np.dot(data_pk.T, data_pk)] 75 | for i in range(1, self.k+self.p+1): 76 | self.corList.append(np.dot(data_pk.T, x2[self.p+self.k-i:-i])) 77 | else: 78 | #Keeping always the averaged version instead of just accumulating here and finally dividing through 79 | #the whole length in _stop_training may be better for large data, since the magnitude of the matrix 80 | #entries is better kept in a sane range 81 | # self.data = np.vstack([self.data, x2]) 82 | self.mean = (self.l/(self.l+len(x2))) * self.mean + (len(x2)/(self.l+len(x2))) * x2.mean(0) 83 | #self.secondMoment = (self.l/(self.l+len(x2))) * self.secondMoment + (1.0/(self.l+len(x2))) * np.dot(x2.T, x2) 84 | #self.secondMoment = (self.l*self.secondMoment+np.dot(x2.T, x2))/(self.l+len(x2)) 85 | self.corList[0] += np.dot(x2.T, x2) 86 | #self.corList[1] += np.dot(x2[1:].T, x2[:-1])+np.dot(x2[:1].T, self.endData[-1:]) 87 | for i in range(1, self.k+self.p+1): 88 | self.corList[i] += np.dot(x2[i:].T, x2[:-i])+np.dot(x2[:i].T, self.endData[-i:]) 89 | self.l += 1.0*len(x2) 90 | self.endData = x2[-self.p-self.k:] 91 | 92 | def _prepare_start_end_cor(self): 93 | start_cor = [] 94 | end_cor = [] 95 | for i in range(len(self.startData)): 96 | startLine = [] 97 | endLine = [] 98 | for j in range(len(self.startData)): 99 | if j < i: 100 | startLine.append(start_cor[j][i].T) 101 | endLine.append(end_cor[j][i].T) 102 | else: 103 | startLine.append(np.outer(self.startData[i], self.startData[j])) 104 | endLine.append(np.outer(self.endData[i], self.endData[j])) 105 | start_cor.append(startLine) 106 | end_cor.append(endLine) 107 | self.start_cor = start_cor 108 | self.end_cor = end_cor 109 | 110 | #This is seperate from _prepare_start_end_cor because merging would be done between these 111 | #Note that it requires that x_cor[i][j] is a reference to x_cor[j][i].T 112 | #This fact must be preserved during merging! 113 | def _start_end_cor_clear_mean(self): 114 | M = np.outer(self.mean, self.mean) 115 | for i in range(len(self.start_cor)): 116 | for j in range(i, len(self.start_cor[i])): 117 | self.start_cor[i][j] += -np.outer(self.startData[i], self.mean)-np.outer(self.mean, self.startData[j])+M 118 | self.end_cor[i][j] += -np.outer(self.endData[i], self.mean)-np.outer(self.mean, self.endData[j])+M 119 | 120 | def _stop_training(self): 121 | #print "PFA stop_training "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 122 | if self.endData is None: 123 | raise mdp.TrainingException("train was never called") 124 | self._prepare_start_end_cor() 125 | self._start_end_cor_clear_mean() 126 | self.calc_PFA() 127 | 128 | def calc_PFA(self): 129 | #print "PFA calc_PFA "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 130 | r = self.get_output_dim() 131 | if r is None: 132 | r = len(self.startData.T) 133 | 134 | #meanRef, SRef, zRef = pfa.calcSpheringParametersAndDataRefImp(self.data)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 135 | 136 | # print "secondMomentTest:" 137 | # print self.secondMoment*self.l 138 | # print (self.corList[0]+np.dot(self.startData.T, self.startData)) 139 | # print "----------------" 140 | # print "chunkTest:" 141 | # print self.corList 142 | 143 | S = pfa.calcSpheringMatrixFromMeanAndSecondMoment(self.mean, (self.corList[0]+np.dot(self.startData.T, self.startData))/self.l, self.l, threshold = self.evThreshold)#, besselsCorrection = 0) 144 | self.S = S 145 | if S.shape[1] < r: 146 | r = S.shape[1] 147 | #this creates an inconsistent output_dim. 148 | #However, it does not matter, if one sets output_dim to an appropriate value 149 | #from the beginning. 150 | #self.output_dim = r 151 | # print self.corList[0] 152 | # print np.dot(self.startData.T, self.startData) 153 | # corList00 = self.start_cor[0][0] 154 | # for r2 in range(1, len(self.startData)): 155 | # corList00 += self.start_cor[r2][r2] 156 | # print corList00 157 | # print "S" 158 | # print S.shape 159 | 160 | meanList0 = [self.mean*self.l-self.startData.mean(0)*(self.p+self.k)] 161 | for i in range(1, self.k+self.p+1): 162 | meanList0.append(meanList0[-1]-self.endData[-i]+self.startData[-i]) 163 | M = np.outer(self.mean*(self.l-self.p-self.k), self.mean)-np.outer(meanList0[0], self.mean) 164 | mns = np.outer(np.ones(self.p+self.k), self.mean) 165 | startZ = np.dot(self.startData-mns, S) 166 | endZ = np.dot(self.endData-mns, S) 167 | #startZ0 = self.startData-mns 168 | #endZ0 = self.endData-mns 169 | 170 | #print np.outer(startZ0[1], startZ0[2]) 171 | # print np.outer(self.endData[0]-self.mean, self.endData[1]-self.mean) 172 | # print np.outer(self.endData[0], self.endData[1])-np.outer(self.mean, self.endData[1])-np.outer(self.endData[0], self.mean)+np.outer(self.mean, self.mean) 173 | # print np.outer(self.endData[0], self.endData[1]) 174 | #print self.start_cor[1][2] 175 | 176 | # zRef = np.dot(self.data-np.outer(np.ones(len(self.data)), self.mean), S) 177 | # z_pk = zRef[self.p+self.k:] 178 | # z_p = zRef[self.p:] 179 | corList = []#np.dot(z_pk.T, z_pk)] 180 | corList0 = [] 181 | for i in range(0, self.k+self.p+1): 182 | corList.append(np.dot(S.T, np.dot(self.corList[i] - np.outer(self.mean, meanList0[i]) + M, S))) 183 | corList0.append(self.corList[i] - np.outer(self.mean, meanList0[i]) + M) 184 | 185 | zetaList = [] 186 | lastLine = [] 187 | for i in range(self.p): 188 | zetaLine = [] 189 | for j in range(self.p): 190 | if j < i: 191 | zetaLine.append(zetaList[j][i].T) 192 | else: 193 | if i == 0: 194 | #zetaLine.append(corList[j]-np.outer(endZ[-1], endZ[-1-j])+np.outer(startZ[self.p+self.k-1], startZ[self.p+self.k-1-j])) 195 | #zetaLine.append(np.dot(S.T, np.dot(corList0[j]-np.outer(endZ0[-1], endZ0[-1-j])+np.outer(startZ0[self.p+self.k-1], startZ0[self.p+self.k-1-j]), S))) 196 | #zetaLine.append(np.dot(S.T, np.dot(corList0[j]-np.outer(endZ0[-1], endZ0[-1-j])+np.outer(startZ0[-1], startZ0[-1-j]), S))) 197 | zetaLine.append(np.dot(S.T, np.dot(corList0[j]-self.end_cor[-1][-1-j]+self.start_cor[-1][-1-j], S))) 198 | else: 199 | #zetaLine.append(lastLine[j-1]-np.outer(endZ[-1-i], endZ[-1-j])+np.outer(startZ[self.p+self.k-1-i], startZ[self.p+self.k-1-j])) 200 | #zetaLine.append(lastLine[j-1]+np.dot(S.T, np.dot(-np.outer(endZ0[-1-i], endZ0[-1-j])+np.outer(startZ0[self.p+self.k-1-i], startZ0[self.p+self.k-1-j]), S))) 201 | #zetaLine.append(lastLine[j-1]+np.dot(S.T, np.dot(-np.outer(endZ0[-1-i], endZ0[-1-j])+np.outer(startZ0[-1-i], startZ0[-1-j]), S))) 202 | zetaLine.append(lastLine[j-1]+np.dot(S.T, np.dot(-self.end_cor[-1-i][-1-j]+self.start_cor[-1-i][-1-j], S))) 203 | zetaList.append(zetaLine) 204 | lastLine = zetaLine 205 | for i in range(self.p): 206 | zetaList[i] = np.hstack(zetaList[i]) 207 | 208 | corList_p = None 209 | zetaList_p = None 210 | lastLine_p = None 211 | if self.k > 0: 212 | corList_p = [corList[0]+np.dot(startZ[self.p:].T, startZ[self.p:])]#[np.dot(z_p.T, z_p)] 213 | #corList_p0 = [corList0[0]+np.dot(startZ0[self.p:].T, startZ0[self.p:])] 214 | # R = np.array(self.start_cor[self.p][self.p]) 215 | # for r2 in range(self.p+1, len(self.startData)): 216 | # R += self.start_cor[r2][r2] 217 | # corList_p0 = [corList0[0]+R] 218 | corList_p0 = [corList0[0]+self.start_cor[self.p][self.p]] 219 | for r2 in range(self.p+1, len(self.startData)): 220 | corList_p0[0] += self.start_cor[r2][r2] 221 | 222 | # corList_p00 = [np.dot(z_p.T, z_p)] 223 | for i in range(1, self.p+1): 224 | # corList_p00.append(np.dot(z_p.T, zRef[self.p-i:-i])) 225 | corList_p.append(corList[i]+np.dot(startZ[self.p:].T, startZ[self.p-i:-i])) 226 | #corList_p0.append(corList0[i]+np.dot(startZ0[self.p:].T, startZ0[self.p-i:-i])) 227 | # R = np.array(self.start_cor[self.p][self.p-i]) 228 | # for r2 in range(self.p+1, len(self.startData)): 229 | # R += self.start_cor[r2][r2-i] 230 | # corList_p0.append(corList0[i]+R) 231 | corList_p0.append(corList0[i]+self.start_cor[self.p][self.p-i]) 232 | for r2 in range(self.p+1, len(self.startData)): 233 | corList_p0[i] += self.start_cor[r2][r2-i] 234 | 235 | # for r in range(self.p+1, len(startZ0)): 236 | # corList_p0[i] += self.start_cor[r][r-i] 237 | # print "--------------" 238 | # print np.dot(startZ0[self.p:].T, startZ0[self.p-i:-i]) 239 | # R = np.outer(startZ0[self.p], startZ0[self.p-i]) 240 | # for ir in range(self.p+1, len(startZ0)): 241 | # R += np.outer(startZ0[ir], startZ0[ir-i]) 242 | # print R 243 | zetaList_p = [] 244 | lastLine_p = [] 245 | for i in range(self.p): 246 | zetaLine_p = [] 247 | for j in range(self.p): 248 | if j < i: 249 | zetaLine_p.append(zetaList_p[j][i].T) 250 | else: 251 | if i == 0: 252 | #zetaLine_p.append(corList_p[j]-np.outer(endZ[-1], endZ[-1-j])+np.outer(startZ[self.p-1], startZ[self.p-1-j])) 253 | #zetaLine_p.append(np.dot(S.T, np.dot(corList_p0[j]-np.outer(endZ0[-1], endZ0[-1-j])+np.outer(startZ0[self.p-1], startZ0[self.p-1-j]), S))) 254 | zetaLine_p.append(np.dot(S.T, np.dot(corList_p0[j]-self.end_cor[-1][-1-j]+self.start_cor[self.p-1][self.p-1-j], S))) 255 | else: 256 | #zetaLine_p.append(lastLine_p[j-1]-np.outer(endZ[-1-i], endZ[-1-j])+np.outer(startZ[self.p-1-i], startZ[self.p-1-j])) 257 | #zetaLine_p.append(lastLine_p[j-1]+np.dot(S.T, np.dot(-np.outer(endZ0[-1-i], endZ0[-1-j])+np.outer(startZ0[self.p-1-i], startZ0[self.p-1-j]), S))) 258 | zetaLine_p.append(lastLine_p[j-1]+np.dot(S.T, np.dot(-self.end_cor[-1-i][-1-j]+self.start_cor[self.p-1-i][self.p-1-j], S))) 259 | zetaList_p.append(zetaLine_p) 260 | lastLine_p = zetaLine_p 261 | for i in range(self.p): 262 | zetaList_p[i] = np.hstack(zetaList_p[i]) 263 | 264 | if not self.affine: 265 | zZ = np.hstack(corList[1:self.p+1]) 266 | ZZ = np.vstack(zetaList) 267 | W = None 268 | if self.k == 0: 269 | ZZI = pfa.invertByProjectionRefImp(ZZ, self.evThreshold) 270 | W = np.dot(zZ, ZZI) 271 | else: 272 | zZ_p = np.hstack(corList_p[1:self.p+1]) 273 | ZZ_p = np.vstack(zetaList_p) 274 | ZZ_pI = pfa.invertByProjectionRefImp(ZZ_p, self.evThreshold) 275 | W = np.dot(zZ_p, ZZ_pI) 276 | self.W0 = W 277 | # WRef = pfa.calcRegressionCoeffRefImp(zRef, self.p, self.evThreshold) 278 | # XRef = pfa.calcErrorCoeffConstLenRefImp(zRef, WRef, self.k) 279 | #X = pfa.calcErrorCoeffConstLenFromCorrelations2(W, zZ, ZZ, lastLine, corList, startZ, endZ, S, self.start_cor, self.end_cor, self.k) 280 | X = pfa.calcErrorCoeffConstLenFromCorrelations2(W, zZ, ZZ, lastLine, corList, S, self.start_cor, self.end_cor, self.k) 281 | #print "xxxx" 282 | #X = pfa.calcErrorCoeffConstLenFromCorrelations2(W, zZ, ZZ, lastLine, corList, S, self.start_cor, self.end_cor, self.k) 283 | 284 | # print "WCompare:" 285 | # print WRef 286 | # print W 287 | # print "XCompare:" 288 | # print XRef 289 | # print X 290 | # print"------" 291 | self.X = X #needed only for debugging 292 | Ar = pfa.calcExtractionForErrorCovRefImp(X, r) 293 | #reduced = np.dot(self.data, self.Ar.T) 294 | # reduced = np.dot(zRef, Ar) 295 | # WRef = pfa.calcRegressionCoeffRefImp(reduced, self.p) 296 | # print "Reduction compare" 297 | A_ = np.kron(np.identity(self.p), Ar) 298 | if (self.k == 0): 299 | zZ = np.dot(Ar.T, np.dot(zZ, A_)) 300 | ZZ = np.dot(A_.T, np.dot(ZZ, A_)) 301 | ZZI = pfa.invertByProjectionRefImp(ZZ, self.evThreshold) 302 | W = np.dot(zZ, ZZI) 303 | else: 304 | zZ_p = np.dot(Ar.T, np.dot(zZ_p, A_)) 305 | ZZ_p = np.dot(A_.T, np.dot(ZZ_p, A_)) 306 | ZZ_pI = pfa.invertByProjectionRefImp(ZZ_p, self.evThreshold) 307 | W = np.dot(zZ_p, ZZ_pI) 308 | # print WRef 309 | # print W 310 | self.W = W 311 | # print "--------------------------" 312 | self.Ar = np.dot(S, Ar) 313 | else: 314 | meanList = [startZ.mean(0)*(-self.p-self.k)] 315 | for i in range(1, self.k+self.p+1): 316 | meanList.append(meanList[-1]-endZ[-i]+startZ[self.p+self.k-i]) 317 | ml = np.hstack(meanList[1:self.p+1]) 318 | zZ_c = np.vstack([np.hstack(corList[1:self.p+1]).T, meanList[0]]).T 319 | ZZ_c = np.vstack([np.vstack(zetaList), ml]) 320 | ml1 = np.hstack([ml, [self.l-self.p-self.k]]) 321 | ZZ_c = np.vstack([ZZ_c.T, ml1]).T 322 | 323 | W_c = None 324 | if self.k == 0: 325 | ZZ_cI = pfa.invertByProjectionRefImp(ZZ_c, self.evThreshold) 326 | W_c = np.dot(zZ_c, ZZ_cI) 327 | else: 328 | meanList_p = [startZ[:self.p].mean(0)*(-self.p)] 329 | for i in range(1, self.p+1): 330 | meanList_p.append(meanList_p[-1]-endZ[-i]+startZ[self.p-i]) 331 | ml_p = np.hstack(meanList_p[1:self.p+1]) 332 | zZ_pc = np.vstack([np.hstack(corList_p[1:self.p+1]).T, meanList_p[0]]).T 333 | ZZ_pc = np.vstack([np.vstack(zetaList_p), ml_p]) 334 | ml1_p = np.hstack([ml_p, [self.l-self.p]]) 335 | ZZ_pc = np.vstack([ZZ_pc.T, ml1_p]).T 336 | 337 | ZZ_pcI = pfa.invertByProjectionRefImp(ZZ_pc, self.evThreshold) 338 | W_c = np.dot(zZ_pc, ZZ_pcI) 339 | self.W0 = W_c 340 | #print self.start_cor 341 | # WRef = pfa.calcRegressionCoeffAffineRefImp(zRef, self.p, self.evThreshold) 342 | # XRef = pfa.calcErrorCoeffConstLenAffineRefImp(zRef, WRef, self.k) 343 | #X = pfa.calcErrorCoeffConstLenAffineFromCorrelations2(W_c, zZ_c, ZZ_c, lastLine, corList, meanList, self.l, startZ, endZ, self.k) 344 | X = pfa.calcErrorCoeffConstLenAffineFromCorrelations2(W_c, zZ_c, ZZ_c, lastLine, corList, meanList, self.l, S, self.start_cor, self.end_cor, self.k) 345 | # print "WCompare (Affine):" 346 | # print WRef 347 | # print W_c 348 | # print "XCompare (Affine):" 349 | # print XRef 350 | #print X 351 | # print"------" 352 | self.X = X #needed only for debugging 353 | Ar = pfa.calcExtractionForErrorCovRefImp(X, r) 354 | # reduced = np.dot(zRef, Ar) 355 | # WRef = pfa.calcRegressionCoeffAffineRefImp(reduced, self.p) 356 | # print "Reduction compare (Affine)" 357 | A_ = pfa.kronAffine(Ar, self.p) 358 | if self.k == 0: 359 | zZ_c = np.dot(Ar.T, np.dot(zZ_c, A_)) 360 | ZZ_c = np.dot(A_.T, np.dot(ZZ_c, A_)) 361 | ZZ_cI = pfa.invertByProjectionRefImp(ZZ_c, self.evThreshold) 362 | W_c = np.dot(zZ_c, ZZ_cI) 363 | else: 364 | zZ_pc = np.dot(Ar.T, np.dot(zZ_pc, A_)) 365 | ZZ_pc = np.dot(A_.T, np.dot(ZZ_pc, A_)) 366 | ZZ_pcI = pfa.invertByProjectionRefImp(ZZ_pc, self.evThreshold) 367 | W_c = np.dot(zZ_pc, ZZ_pcI) 368 | # print WRef 369 | # print W_c 370 | self.W = W_c 371 | # print "--------------------------" 372 | self.Ar = np.dot(S, Ar) 373 | #print "PFA_calc done "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 374 | 375 | def _execute(self, x): 376 | #print "PFA execute "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 377 | z0 = x-np.outer(np.ones(len(x)), self.mean) 378 | return np.dot(z0, self.Ar) 379 | 380 | class PFANodeLayerAware(PFANode): 381 | '''redefined _init_ Method with p, k as arguments ''' 382 | def __init__(self, train_length, p = 2, k = 0, affine = True, input_dim = None, output_dim = None, dtype = None): 383 | super(PFANodeLayerAware, self).__init__(p, k, affine, input_dim, output_dim, dtype) 384 | self.train_length = train_length 385 | self.field_index = 0 386 | self.current_train_index = 0 387 | self.l_acc = 0 388 | 389 | def _merge_init(self): 390 | self.mean_acc = np.array(self.mean) 391 | self.l_acc += self.l 392 | self.corList_acc = [] 393 | for i in range(len(self.corList)): 394 | self.corList_acc.append(np.array(self.corList[i])) 395 | 396 | self.startData_acc = np.array(self.startData) 397 | self.endData_acc = np.array(self.endData) 398 | 399 | self.start_cor_acc = [] 400 | self.end_cor_acc = [] 401 | for i in range(len(self.startData)): 402 | start_line = [] 403 | end_line = [] 404 | for j in range(len(self.startData)): 405 | if j >= i: 406 | start_line.append(np.array(self.start_cor[i][j])) 407 | end_line.append(np.array(self.end_cor[i][j])) 408 | else: 409 | start_line.append(self.start_cor_acc[j][i].T) 410 | end_line.append(self.end_cor_acc[j][i].T) 411 | self.start_cor_acc.append(start_line) 412 | self.end_cor_acc.append(end_line) 413 | 414 | def _merge(self): 415 | #print "PFA inline-merge "+str(node.sindex)+" of "+str(node.maxindex)+" layer "+str(node.layerIndex)+" of "+str(node.layerMax) 416 | self.mean_acc = (self.l_acc/(self.l_acc+self.l)) * self.mean_acc + (self.l/(self.l_acc+self.l)) * self.mean 417 | for i in range(len(self.corList_acc)): 418 | self.corList_acc[i] += self.corList[i] 419 | 420 | self.startData_acc += self.startData 421 | self.endData_acc += self.endData 422 | 423 | for i in range(len(self.startData_acc)): 424 | for j in range(len(self.startData_acc)): 425 | if j >= i: 426 | self.start_cor_acc[i][j] += self.start_cor[i][j] 427 | self.end_cor_acc[i][j] += self.end_cor[i][j] 428 | 429 | self.l_acc += self.l 430 | 431 | def _merge_scale(self): 432 | sc = 1.0*self.field_index 433 | self.startData_acc /= sc 434 | self.endData_acc /= sc 435 | 436 | self.l_acc /= sc 437 | for i in range(len(self.corList_acc)): 438 | self.corList_acc[i] /= sc 439 | 440 | for i in range(len(self.startData_acc)): 441 | for j in range(len(self.startData_acc)): 442 | if j >= i: 443 | self.start_cor_acc[i][j] /= sc 444 | self.end_cor_acc[i][j] /= sc 445 | 446 | def _merge_cor(self): 447 | if self.field_index == 1: 448 | self._merge_init() 449 | else: 450 | self._merge() 451 | self.l = None #causes _train to re-init everything but start_cor, end_cor 452 | 453 | def _train(self, x): 454 | self.current_train_index += len(x) 455 | super(PFANodeLayerAware, self)._train(x) 456 | if self.current_train_index == self.train_length: 457 | self.current_train_index = 0 458 | self.field_index += 1 459 | self._prepare_start_end_cor() 460 | self._merge_cor() 461 | 462 | def _insert_acc(self): 463 | self.mean = self.mean_acc 464 | self.l = self.l_acc 465 | self.corList = self.corList_acc 466 | self.startData = self.startData_acc 467 | self.endData = self.endData_acc 468 | self.start_cor = self.start_cor_acc 469 | self.end_cor = self.end_cor_acc 470 | 471 | def _stop_training(self): 472 | #print "PFA stop_training "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 473 | if self.endData_acc is None: 474 | raise mdp.TrainingException("train was never called") 475 | 476 | self._merge_scale() 477 | self._insert_acc() 478 | self._start_end_cor_clear_mean() 479 | self.calc_PFA() 480 | 481 | 482 | class PFAMerger(PFANode, Merger): 483 | def __init__(self, p = 2, k = 0, affine = True, input_dim = None, output_dim = None, dtype = None): 484 | #super(PFAMerger, self).__init__(p, k, affine, input_dim, output_dim, dtype) 485 | super(PFAMerger, self).__init__(p, k, affine, input_dim, output_dim, dtype) 486 | self.mergeCount = 0 487 | self.execCount = 0 488 | self.Ar = None 489 | 490 | # def _execute(self, x): 491 | # print "PFA merger execute"# ("+str(self.execCount)+") "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 492 | # print "mean a1 "+str(self.mean) 493 | # self.execCount += 1 494 | # z0 = x-np.outer(np.ones(len(x)), self.mean) 495 | # er = np.dot(z0, self.Ar) 496 | # print "mean a2 "+str(self.mean) 497 | # return er 498 | 499 | def _merge_init(self, node): 500 | if node.start_cor is None: 501 | node._prepare_start_end_cor() 502 | 503 | self.mean = np.array(node.mean) 504 | self.l = node.l 505 | self.corList = [] 506 | for i in range(len(node.corList)): 507 | self.corList.append(np.array(node.corList[i])) 508 | 509 | self.startData = np.array(node.startData) 510 | self.endData = np.array(node.endData) 511 | 512 | self.start_cor = [] 513 | self.end_cor = [] 514 | for i in range(len(node.startData)): 515 | start_line = [] 516 | end_line = [] 517 | for j in range(len(node.startData)): 518 | if j >= i: 519 | start_line.append(np.array(node.start_cor[i][j])) 520 | end_line.append(np.array(node.end_cor[i][j])) 521 | else: 522 | start_line.append(self.start_cor[j][i].T) 523 | end_line.append(self.end_cor[j][i].T) 524 | self.start_cor.append(start_line) 525 | self.end_cor.append(end_line) 526 | self.mergeCount = 1 527 | 528 | # def freeMem(self): 529 | # #This hopefully allows the gc to free a lot of memory: 530 | # self.start_cor = None 531 | # self.end_cor = None 532 | # self.mean = None 533 | # self.corList = None 534 | # self.startData = None 535 | # self.endData = None 536 | # self.l = None 537 | # self.mergeCount += 1 538 | 539 | def _merge(self, node): 540 | #print "PFA merge "+str(node.sindex)+" of "+str(node.maxindex)+" layer "+str(node.layerIndex)+" of "+str(node.layerMax) 541 | if self.mergeCount == 0: 542 | self._merge_init(node) 543 | else: 544 | if self.start_cor is None: 545 | #super(PFAMerger, self).__init__(p, k, affine, input_dim, output_dim, dtype) 546 | self._prepare_start_end_cor() 547 | if node.start_cor is None: 548 | node._prepare_start_end_cor() 549 | self.mean = (self.l/(self.l+node.l)) * self.mean + (node.l/(self.l+node.l)) * node.mean 550 | for i in range(len(self.corList)): 551 | self.corList[i] += node.corList[i] 552 | 553 | self.startData += node.startData 554 | self.endData += node.endData 555 | 556 | for i in range(len(self.startData)): 557 | for j in range(len(self.startData)): 558 | if j >= i: 559 | self.start_cor[i][j] += node.start_cor[i][j] 560 | self.end_cor[i][j] += node.end_cor[i][j] 561 | 562 | self.l += node.l 563 | self.mergeCount += 1 564 | 565 | def _merge_scale(self): 566 | #print "mergeCount: "+str(self.mergeCount) 567 | sc = 1.0*self.mergeCount 568 | self.startData /= sc 569 | self.endData /= sc 570 | 571 | self.l /= sc 572 | for i in range(len(self.corList)): 573 | self.corList[i] /= sc 574 | 575 | for i in range(len(self.startData)): 576 | for j in range(len(self.startData)): 577 | if j >= i: 578 | self.start_cor[i][j] /= sc 579 | self.end_cor[i][j] /= sc 580 | 581 | def _stop_training(self): 582 | #print "PFA merger stop training "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 583 | self._prepare_start_end_cor() 584 | 585 | def _stop_merging(self): 586 | #print "PFA stop merging "+str(self.sindex)+" of "+str(self.maxindex)+" layer "+str(self.layerIndex)+" of "+str(self.layerMax) 587 | self._merge_scale() 588 | self._start_end_cor_clear_mean() 589 | self.calc_PFA() 590 | 591 | def execute(self, x, *args, **kwargs): 592 | """ 593 | We skip _pre_execution_checks here, because train-flags 594 | might be in inconsistent states since the merger doesn't 595 | use train and stop training. 596 | Users of MergeLayer must know what they do. 597 | """ 598 | #self._pre_execution_checks(x) 599 | return self._execute(self._refcast(x), *args, **kwargs) 600 | 601 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/PFACoreUtil.py: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # Copyright of GNUPFA: 4 | # Copyright (c) 2013, 2014 Institut fuer Neuroinformatik, 5 | # Ruhr-Universitaet Bochum, Germany. All rights reserved. 6 | # 7 | # 8 | # This file is part of GNUPFA. 9 | # 10 | # GNUPFA is free software: you can redistribute it and/or modify 11 | # it under the terms of the GNU General Public License as published by 12 | # the Free Software Foundation, either version 3 of the License, or 13 | # (at your option) any later version. 14 | # 15 | # GNUPFA is distributed in the hope that it will be useful, 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | # GNU General Public License for more details. 19 | # 20 | # You should have received a copy of the GNU General Public License 21 | # along with GNUPFA. If not, see . 22 | # 23 | # 24 | # Linking this library statically or dynamically with other modules is 25 | # making a combined work based on this library. Thus, the terms and 26 | # conditions of the GNU General Public License cover the whole 27 | # combination. 28 | # 29 | 30 | 31 | import numpy as np 32 | from numpy import linalg as LA 33 | 34 | def NonSquareIdentity(shape, rowOffset = 0, columnOffset = 0): 35 | er = np.zeros(shape) 36 | l = shape[0] 37 | if shape[1] < l: 38 | l = shape[1] 39 | for i in range(l): 40 | er[rowOffset+i][columnOffset+i] = 1.0 41 | return er 42 | 43 | #W is expected to be a broad matrix, transform would be multiplied from left to it, inverse transform (kron) from right 44 | #Transform is expected to be orthogonal (or orthogonal subbase) 45 | def transformPredictor(W, transform): 46 | X_ = np.kron(np.identity(len(W.T)/len(W)), transform.T) 47 | if len(W.T)%len(W) == 0: #Non-affine 48 | return np.dot(transform, np.dot(W, X_)) 49 | else: #affine 50 | return np.hstack([np.dot(transform, np.dot(W.T[:-1].T, X_)), np.dot(transform, W.T[-1:].T)]) 51 | 52 | def kronAffine(A, p): 53 | X_ = np.kron(np.identity(p), A) 54 | z = np.zeros([len(X_), 1]) 55 | X_ = np.hstack([X_, z]) 56 | z = np.zeros([1, len(X_.T)]) 57 | z[0][-1] = 1.0 58 | return np.vstack([X_, z]) 59 | 60 | #Note that secondMoment is expected without besselsCorrection, i.e. secondMomentSum/length rather than secondMomentSum/(length-1) 61 | def calcSpheringMatrixFromMeanAndSecondMoment(mean, secondMoment, length, threshold = 0.00000000001, besselsCorrection = 0): 62 | """ 63 | Returns sphered version of the given data and the sphering parameters as well. 64 | The data is expected in the format (time, dims). The sphering matrix may reduce 65 | the dimensionality of the data by deleting dimensions that have (near) zero 66 | variance. The returned sphering matrix has the shape (high dim, low dim). 67 | The sphering is done with respect to a covariance-matrix including Bessel's correction, 68 | if the besselsCorrection parameter is True (default is False). 69 | """ 70 | # if length == -1: 71 | # length = len(data) 72 | #cov = (secondMoment/length-np.outer(mean, mean)) 73 | cov = (secondMoment-np.outer(mean, mean)) 74 | if besselsCorrection != 0: 75 | cov *= (length/(length-1.0)) 76 | eg, ev = LA.eigh(cov) 77 | eg2 = [] 78 | ev2 = [] 79 | # print "+++sph eg+++" 80 | # print eg 81 | # print "----" 82 | for i in range(0, len(eg)): 83 | if (eg[i] >= threshold): 84 | eg2.append(1.0/np.sqrt(eg[i])) 85 | ev2.append(ev.T[i]) 86 | return np.dot(np.transpose(ev2), np.diag(eg2)) 87 | 88 | def calcSpheringParametersAndDataRefImp(data, threshold = 0.00001, offset = 0, length = -1, besselsCorrection = 0): 89 | """ 90 | Returns sphered version of the given data and the sphering parameters as well. 91 | The data is expected in the format (time, dims). The sphering matrix may reduce 92 | the dimensionality of the data by deleting dimensions that have (near) zero 93 | variance. The returned sphering matrix has the shape (high dim, low dim). 94 | The sphering is done with respect to a covariance-matrix including Bessel's correction, 95 | if the besselsCorrection parameter is True (default is False). 96 | """ 97 | if besselsCorrection != 0: 98 | besselsCorrection = 1 99 | if length == -1: 100 | length = len(data) 101 | mean = data[offset:].mean(0) 102 | mean2 = data[offset:].sum(0)/len(data[offset:]) 103 | data0 = data-np.outer(np.ones(len(data)), mean) 104 | #With Bessel's correction: 105 | #cov = np.multiply(np.dot(data0[offset:].T, data0[offset:]), 1.0/(len(data)-offset-besselsCorrection)) 106 | cov = np.dot(data0[offset:].T, data0[offset:]) 107 | eg, ev = LA.eigh(cov) 108 | eg2 = [] 109 | ev2 = [] 110 | for i in range(0, len(eg)): 111 | if (eg[i] >= threshold): 112 | #eg2.append(1.0/(np.sqrt(eg[i]*len(data)))) 113 | eg2.append(1.0/(np.sqrt(eg[i]/(len(data)-offset-besselsCorrection)))) 114 | ev2.append(ev.T[i]) 115 | S = np.dot(np.transpose(ev2), np.diag(eg2)) 116 | # for i in range(0, len(eg)): 117 | # eg[i] = eg[i]**(-0.5) 118 | # S = np.dot(ev.T, np.diag(eg)) 119 | return [mean, S, data0.dot(S)] 120 | 121 | #def calcAutoCorrelationList(data, p, besselsCorrection = 0): 122 | # er = [] 123 | # data0 = data[p:].T 124 | # for i in range(1, p+1): 125 | # #er.append(np.multiply(np.dot(data0, data[p-i:len(data)-i]), 1.0/(len(data)-p))) 126 | # er.append(np.dot(data0, data[p-i:len(data)-i])) 127 | # return er 128 | # 129 | ## if besselsCorrection != 0: 130 | ## besselsCorrection = 1 131 | ### data0 = data[p:].T 132 | ## for i in range(1, p+1): 133 | ### er.append(np.dot(data0, data[p-i:len(data)-i])) 134 | ## er.append(np.multiply(np.dot(data[i:].T, data[:-i]), (1.0*(len(data)-besselsCorrection))/(len(data)-i-besselsCorrection))) 135 | ## return er 136 | # 137 | #def zZiFromAutoCorrelationsList(auto, i, p): 138 | # return np.hstack(auto[i:i+p]) 139 | # 140 | #def ZZFromAutoCorrelationsList(auto, p, data, besselsCorrection = 0, dataSphered = True): 141 | # if besselsCorrection != 0: 142 | # besselsCorrection = 1 143 | # cov = np.multiply(np.identity(len(data[0])), len(data)-besselsCorrection) 144 | # if not dataSphered: 145 | # #cov = np.multiply(np.dot(data.T, data), 1.0/(len(data)-besselsCorrection)) 146 | # cov = np.dot(data.T, data) 147 | # lines = [] 148 | # #i iterates through lines, j through columns 149 | # for i in range(p): 150 | # line = [] 151 | # for j in range(p): 152 | # if i == j: 153 | # line.append(cov) 154 | # elif i > j: 155 | # line.append(lines[j][i].T) 156 | # else: 157 | # line.append(auto[i-1]) 158 | # lines.append(line) 159 | # for i in range(p): 160 | # lines[i] = np.hstack(lines[i]) 161 | # return np.vstack(lines) 162 | 163 | def invertByProjectionRefImp(M, evThreshold = 0.000001): 164 | eg, ev = LA.eigh(M) 165 | r = evThreshold**2 166 | for i in range(0, len(eg)): 167 | if (eg[i]**2 > r): 168 | eg[i] = 1.0/eg[i] 169 | else: 170 | eg[i] = 0.0 171 | return np.dot(ev, np.dot(np.diag(eg), ev.T)) 172 | 173 | def linearRegressionCoeff(srcData, destData): 174 | srcCov = np.dot(srcData.T, srcData) 175 | cor = np.dot(srcData.T, destData) 176 | srcCovI = invertByProjectionRefImp(srcCov) 177 | return np.dot(srcCovI, cor) 178 | 179 | # def affineRegressionCoeff(srcData, destData): 180 | # srcCov = np.dot(srcData.T, srcData) 181 | # cor = np.dot(srcData.T, destData) 182 | # srcCovI = invertByProjectionRefImp(srcCov) 183 | # return np.dot(srcCovI, cor) 184 | 185 | #def fullRegressionMatrixFromRegressionCoeff(W): 186 | # tmp = np.zeros([len(W[0])-len(W), len(W[0])]) 187 | # for i in range(0, len(tmp)): 188 | # tmp[i][i] = 1.0 189 | # return np.vstack([W, tmp]) 190 | 191 | def calcShiftedDataListRefImp(data, p, p_offset = 1): 192 | dataZeta = [] 193 | off = p_offset 194 | if off == 0: 195 | off += 1 196 | dataZeta.append(data[p:]) 197 | for i in range(off, p+1): 198 | dataZeta.append(data[p-i: -i]) 199 | return dataZeta 200 | 201 | def calcZetaDataRefImp(data, p, delay = 0): 202 | return np.hstack(calcShiftedDataListRefImp(data, p+delay, 1+delay)) 203 | # dataZeta = calcShiftedDataList(data, p-1) 204 | # return np.hstack(dataZeta)[0:len(dataZeta[0])-1-delay] 205 | 206 | def calcZetacDataRefImp(data, p, delay = 0): 207 | return np.hstack([calcZetaDataRefImp(data, p, delay), np.ones([len(data)-p-delay, 1])]) 208 | 209 | def calcZeta0DataRefImp(data, p): 210 | return np.hstack(calcShiftedDataListRefImp(data[1:], p-1, 0)) 211 | 212 | def calcZeta0cDataRefImp(data, p): 213 | return np.hstack([calcZeta0DataRefImp(data, p), np.ones([len(data)-p, 1])]) 214 | 215 | def empiricalRawErrorRefImp(data, W, srcData = None): 216 | """ 217 | Measures how good following equation is fulfilled in average over time: z = W zeta 218 | Evaluates data from p to len(data), retrieves p via len(W[0])/len(W) 219 | """ 220 | sDat = srcData 221 | if sDat is None: 222 | sDat = data 223 | p = len(W[0])/len(W) 224 | pre = np.dot(calcZetaDataRefImp(sDat, p), W.T) 225 | #real = data[0:len(data)-p] 226 | real = data[p:len(data)] 227 | #err = (pre[0]-real[0])**2 228 | #for i in range(1, len(real)): 229 | # err += (pre[i]-real[i])**2 230 | #return np.multiply(err, 1.0/len(real)) 231 | return (LA.norm(real-pre)**2)/len(real) 232 | 233 | def empiricalRawErrorAffineRefImp(data, W_c, srcData = None): 234 | """ 235 | Measures how good following equation is fulfilled in average over time: z = W zeta + c 236 | Evaluates data from p to len(data), retrieves p via len(W[0])/len(W) 237 | """ 238 | sDat = srcData 239 | if sDat is None: 240 | sDat = data 241 | p = (len(W_c[0])-1)/len(W_c) 242 | pre = np.dot(calcZetacDataRefImp(sDat, p), W_c.T) 243 | real = data[p:len(data)] 244 | return (LA.norm(real-pre)**2)/len(real) 245 | 246 | def empiricalRawErrorComponentsRefImp(data, W, srcData = None): 247 | """ 248 | Measures how good following equation is fulfilled in average over time: z = W zeta 249 | Evaluates data from p to len(data), retrieves p via len(W[0])/len(W) 250 | """ 251 | sDat = srcData 252 | if sDat is None: 253 | sDat = data 254 | p = len(W[0])/len(W) 255 | pre = np.dot(calcZetaDataRefImp(sDat, p), W.T) 256 | real = data[p:len(data)] 257 | res = (pre-real).T 258 | er = [] #np.zeros(len(res)) 259 | for i in range(0, len(res)): 260 | er.append(np.inner(res[i], res[i])) 261 | return np.multiply(er, 1.0/len(real)) 262 | 263 | def empiricalRawErrorComponentsAffineRefImp(data, W_c, srcData = None): 264 | """ 265 | Measures how good following equation is fulfilled in average over time: z = W zeta + c 266 | Evaluates data from p to len(data), retrieves p via len(W[0])/len(W) 267 | """ 268 | sDat = srcData 269 | if sDat is None: 270 | sDat = data 271 | p = (len(W_c[0])-1)/len(W_c) 272 | pre = np.dot(calcZetacDataRefImp(sDat, p), W_c.T) 273 | real = data[p:len(data)] 274 | res = (pre-real).T 275 | er = [] #np.zeros(len(res)) 276 | for i in range(0, len(res)): 277 | er.append(np.inner(res[i], res[i])) 278 | return np.multiply(er, 1.0/len(real)) 279 | 280 | def predictNextFeatures(data, W): 281 | p = len(W[0])/len(W) 282 | return np.dot(np.hstack(data[-p:][::-1]), W.T) 283 | 284 | def predictNextFeaturesAffine(data, W_c): 285 | p = (len(W_c[0])-1)/len(W_c) 286 | return np.dot(np.hstack(data[-p:][::-1]), W_c.T[:-1])+W_c.T[-1] 287 | 288 | def calcRegressionCoeffRefImp(data, p, evThreshold = 0.0001): 289 | zeta = calcZetaDataRefImp(data, p) 290 | z_p = data[p:] 291 | zZT = np.dot(z_p.T, zeta) 292 | ZZT = np.dot(zeta.T, zeta) 293 | ZZTI = invertByProjectionRefImp(ZZT, evThreshold) 294 | return np.dot(zZT, ZZTI) 295 | 296 | def calcRegressionCoeffAffineRefImp(data, p, evThreshold = 0.0001): 297 | zetac = calcZetacDataRefImp(data, p) 298 | z_p = data[p:] 299 | zZT = np.dot(z_p.T, zetac) 300 | ZZT = np.dot(zetac.T, zetac) 301 | ZZTI = invertByProjectionRefImp(ZZT, evThreshold) 302 | return np.dot(zZT, ZZTI) 303 | 304 | def buildV_cRefImp(W_c): 305 | p = (len(W_c[0])-1)/len(W_c) 306 | id = np.identity((p-1)*len(W_c)) 307 | zer = np.zeros([len(id), len(W_c)+1]) 308 | bot = np.zeros([len(W_c[0])]) 309 | bot[len(bot)-1] = 1.0 310 | return np.vstack([W_c, np.hstack([id, zer]), bot]) 311 | 312 | def buildVRefImp(W): 313 | p = (len(W[0]))/len(W) 314 | id = np.identity((p-1)*len(W)) 315 | zer = np.zeros([len(id), len(W)]) 316 | return np.vstack([W, np.hstack([id, zer])]) 317 | 318 | def calcExtractionForErrorCovRefImp(X, r): 319 | eg, ev = LA.eigh(X) 320 | sort = np.argsort(eg) 321 | ev2 = [] 322 | for i in range(0, r): 323 | ev2.append(ev.T[sort[i]]) 324 | return np.transpose(ev2) 325 | 326 | def calcExtractionWithWeightsForErrorCovRefImp(X, r): 327 | eg, ev = LA.eigh(X) 328 | sort = np.argsort(eg) 329 | ev2 = [] 330 | eg2 = [] 331 | for i in range(0, r): 332 | ev2.append(ev.T[sort[i]]) 333 | eg2.append(eg[sort[i]]) 334 | return np.transpose(ev2), np.array(eg), np.array(eg2) 335 | 336 | def calcErrorCoeffRefImp(data, W, k = 0): 337 | p = (len(W[0]))/len(W) 338 | V = buildVRefImp(W) 339 | X = np.zeros([len(W), len(W)]) 340 | #z_pk = data[p+k:] 341 | WV = W 342 | for i in range(0, k+1): 343 | if i > 0: 344 | WV = np.dot(WV, V) 345 | zeta1_i = calcZetaDataRefImp(data, p, i) 346 | z_i_pre = np.dot(zeta1_i, WV.T) 347 | res_i = data[p+i:]-z_i_pre 348 | X += np.dot(res_i.T, res_i) 349 | return X 350 | 351 | def calcErrorCoeffConstLenRefImp(data, W, k = 0): 352 | p = (len(W[0]))/len(W) 353 | V = buildVRefImp(W) 354 | X = np.zeros([len(W), len(W)]) 355 | z_pk = data[p+k:] 356 | WV = W 357 | #print z_pk 358 | for i in range(0, k+1): 359 | if i > 0: 360 | WV = np.dot(WV, V) 361 | zeta1_i = calcZetaDataRefImp(data, p, i)[k-i:] 362 | #print zeta1_i 363 | z_i_pre = np.dot(zeta1_i, WV.T) 364 | res_i = z_pk-z_i_pre 365 | X += np.dot(res_i.T, res_i) 366 | return X 367 | 368 | def calcErrorCoeffAffineRefImp(data, W_c, k = 0): 369 | p = (len(W_c[0])-1)/len(W_c) 370 | V_c = buildV_cRefImp(W_c) 371 | X = np.zeros([len(W_c), len(W_c)]) 372 | #z_pk = data[p+k:] 373 | WV_c = W_c 374 | for i in range(0, k+1): 375 | if i > 0: 376 | WV_c = np.dot(WV_c, V_c) 377 | zeta1_i_c = calcZetacDataRefImp(data, p, i) 378 | z_i_pre = np.dot(zeta1_i_c, WV_c.T) 379 | res_i = data[p+i:]-z_i_pre 380 | X += np.dot(res_i.T, res_i) 381 | # if i == 1: 382 | # #print np.dot(zeta1_i_c.T, zeta1_i_c)#/(len(zeta1_i_c)) 383 | # print np.dot(data[p+i:].T, zeta1_i_c) 384 | # print zeta1_i_c.mean(0)*(len(zeta1_i_c)) 385 | # print data[p-1:-1].mean(0)*(len(data)-p) 386 | return X 387 | 388 | def calcErrorCoeffConstLenAffineRefImp(data, W_c, k = 0): 389 | p = (len(W_c[0])-1)/len(W_c) 390 | V_c = buildV_cRefImp(W_c) 391 | X = np.zeros([len(W_c), len(W_c)]) 392 | z_pk = data[p+k:] 393 | WV_c = W_c 394 | #print z_pk 395 | for i in range(0, k+1): 396 | if i > 0: 397 | WV_c = np.dot(WV_c, V_c) 398 | zeta1_i_c = calcZetacDataRefImp(data, p, i)[k-i:] 399 | #print zeta1_i 400 | z_i_pre = np.dot(zeta1_i_c, WV_c.T) 401 | res_i = z_pk-z_i_pre 402 | X += np.dot(res_i.T, res_i) 403 | # if i == 1: 404 | # print np.dot(zeta1_i_c.T, zeta1_i_c)#/(len(zeta1_i_c)) 405 | #print np.dot(data[p+i:].T, zeta1_i_c) 406 | # print zeta1_i_c.mean(0)*(len(zeta1_i_c)) 407 | # print data[p-1:-1].mean(0)*(len(data)-p) 408 | return X 409 | 410 | def calcErrorCoeff(data, W, k = 0): 411 | p = len(W[0])/len(W) 412 | X = np.zeros([len(W), len(W)]) 413 | corList = [np.dot(data[p:].T, data[p:])] 414 | 415 | for i in range(1, p+1): 416 | corList.append(np.dot(data[p:].T, data[p-i:-i])) 417 | 418 | zetaList = [] 419 | lastLine = [] 420 | zetaBlockList = [] 421 | for i in range(p): 422 | zetaLine = [] 423 | for j in range(p): 424 | if j < i: 425 | zetaLine.append(zetaList[j][i].T) 426 | else: 427 | if i == 0: 428 | zetaLine.append(corList[j]+np.outer(data[p-1], data[p-1-j])-np.outer(data[-1], data[-1-j])) 429 | else: 430 | zetaLine.append(lastLine[j-1]+np.outer(data[p-1-i], data[p-1-j])-np.outer(data[-1-i], data[-1-j])) 431 | 432 | zetaList.append(zetaLine) 433 | lastLine = zetaLine 434 | 435 | cov_p = np.dot(data[p:].T, data[p:]) 436 | WV = W 437 | V = buildVRefImp(W) 438 | for i in range(k+1): 439 | if i > 0: 440 | WV = np.dot(WV, V) 441 | if i > 0: 442 | for j in range(i+1, len(corList)): 443 | corList[j] -= np.outer(data[p+i-1], data[p-j+i-1]) 444 | corList.append(np.dot(data[p+i:].T, data[:-p-i])) 445 | zZi = np.hstack(corList[1+i:]) 446 | if i == 0: 447 | for l in range(p): 448 | zetaBlockList.append(np.hstack(zetaList[l])) 449 | else: 450 | for l in range(p): 451 | for j in range(p): 452 | if j < l: 453 | zetaList[l][j] = zetaList[j][l].T 454 | else: 455 | zetaList[l][j] -= np.outer(data[-(i+1)-l], data[-(i+1)-j]) 456 | for l in range(p): 457 | zetaBlockList[l] = np.hstack(zetaList[l]) 458 | ZZi = np.vstack(zetaBlockList) 459 | K = np.dot(zZi, WV.T) 460 | X += cov_p -K-K.T + np.dot(np.dot(WV, ZZi), WV.T) 461 | if i < k: 462 | cov_p -= np.outer(data[p+i], data[p+i]) 463 | return X 464 | 465 | def calcErrorCoeffConstLen(data, W, k = 0): 466 | p = len(W[0])/len(W) 467 | z_pk = data[p+k:] 468 | corList = [np.dot(z_pk.T, z_pk)] 469 | for i in range(1, k+p+1): 470 | corList.append(np.dot(z_pk.T, data[p+k-i:-i])) 471 | return calcErrorCoeffConstLenFromAutoCorrelations(W, corList, data[:p+k], data[-p-k:], k) 472 | 473 | def calcErrorCoeffConstLenFromAutoCorrelations(W, corList, startData, endData, k = 0): 474 | p = len(corList)-1-k 475 | zetaList = [] 476 | lastLine = [] 477 | for i in range(p): 478 | zetaLine = [] 479 | for j in range(p): 480 | if j < i: 481 | zetaLine.append(zetaList[j][i].T) 482 | else: 483 | if i == 0: 484 | zetaLine.append(corList[j]-np.outer(endData[-1], endData[-1-j])+np.outer(startData[p+k-1], startData[p+k-1-j])) 485 | else: 486 | zetaLine.append(lastLine[j-1]-np.outer(endData[-1-i], endData[-1-j])+np.outer(startData[p+k-1-i], startData[p+k-1-j])) 487 | zetaList.append(zetaLine) 488 | lastLine = zetaLine 489 | for i in range(p): 490 | zetaList[i] = np.hstack(zetaList[i]) 491 | ZZi = np.vstack(zetaList) 492 | return calcErrorCoeffConstLenFromCorrelations(W, np.hstack(corList[1:p+1]), ZZi, lastLine, corList, startData, endData, k) 493 | 494 | def calcErrorCoeffConstLenFromCorrelations(W, zZ, ZZ, lastLine, corList, startData, endData, k = 0): 495 | p = len(corList)-1-k 496 | WV = W 497 | V = buildVRefImp(W) 498 | Y = corList[0]*(k+1) 499 | pnl = len(W)*(p-1) 500 | zZi = zZ 501 | ZZi = ZZ 502 | for i in range(0, k+1): 503 | if i > 0: 504 | WV = np.dot(WV, V) 505 | zZi = np.hstack(corList[1+i:p+i+1]) 506 | K = np.dot(zZi, WV.T) 507 | Y += -K-K.T + np.dot(np.dot(WV, ZZi), WV.T) 508 | if i < k: 509 | for j in range(p): 510 | lastLine[j] += -np.outer(endData[-p-i-1], endData[-j-2-i])+np.outer(startData[k-1-i], startData[p-i+k-j-2]) 511 | line = np.hstack(lastLine) 512 | ZZi = np.vstack([np.hstack([ ZZi[len(W):].T[len(W):], line.T[:pnl]]), line]) 513 | return Y 514 | 515 | #def calcErrorCoeffConstLenFromCorrelations2(W, zZ, ZZ, lastLine, corList, S, start_cor, end_cor, k = 0): 516 | #def calcErrorCoeffConstLenFromCorrelations2(W, zZ, ZZ, lastLine, corList, startData, endData, S, start_cor, end_cor, k = 0): 517 | def calcErrorCoeffConstLenFromCorrelations2(W, zZ, ZZ, lastLine, corList, S, start_cor, end_cor, k = 0): 518 | p = len(corList)-1-k 519 | WV = W 520 | V = buildVRefImp(W) 521 | Y = corList[0]*(k+1) 522 | pnl = len(W)*(p-1) 523 | zZi = zZ 524 | ZZi = ZZ 525 | for i in range(0, k+1): 526 | if i > 0: 527 | WV = np.dot(WV, V) 528 | zZi = np.hstack(corList[1+i:p+i+1]) 529 | K = np.dot(zZi, WV.T) 530 | Y += -K-K.T + np.dot(np.dot(WV, ZZi), WV.T) 531 | if i < k: 532 | for j in range(p): 533 | #lastLine[j] += -np.outer(endData[-p-i-1], endData[-j-2-i])+np.outer(startData[-p-1-i], startData[-i-j-2]) 534 | lastLine[j] += np.dot(S.T, np.dot(-end_cor[-p-i-1][-j-2-i]+start_cor[-p-1-i][-i-j-2], S)) 535 | line = np.hstack(lastLine) 536 | ZZi = np.vstack([np.hstack([ ZZi[len(W):].T[len(W):], line.T[:pnl]]), line]) 537 | return Y 538 | 539 | def calcErrorCoeffConstLenRoll(data, W, k = 0): 540 | p = len(W[0])/len(W) 541 | z_pk = data[p+k:] 542 | WV = W 543 | V = buildVRefImp(W) 544 | Y = np.dot(z_pk.T, z_pk)*(k+1) 545 | corList = [np.dot(z_pk.T, z_pk)] 546 | for i in range(1, k+p+1): 547 | corList.append(np.dot(z_pk.T, data[p+k-i:-i])) 548 | 549 | zetaList = [] 550 | lastLine = [] 551 | for i in range(p): 552 | zetaLine = [] 553 | for j in range(p): 554 | if j < i: 555 | zetaLine.append(zetaList[j][i].T) 556 | else: 557 | if i == 0: 558 | zetaLine.append(corList[j]-np.outer(data[-1], data[-1-j])+np.outer(data[p+k-1], data[p+k-1-j])) 559 | else: 560 | zetaLine.append(lastLine[j-1]-np.outer(data[-1-i], data[-1-j])+np.outer(data[p+k-1-i], data[p+k-1-j])) 561 | zetaList.append(zetaLine) 562 | lastLine = zetaLine 563 | for i in range(p): 564 | zetaList[i] = np.hstack(zetaList[i]) 565 | ZZi = np.vstack(zetaList) 566 | 567 | pnl = len(W)*(p-1) 568 | for i in range(0, k+1): 569 | if i > 0: 570 | WV = np.dot(WV, V) 571 | zZi = np.hstack(corList[1+i:p+i+1]) 572 | K = np.dot(zZi, WV.T) 573 | Y += -K-K.T + np.dot(np.dot(WV, ZZi), WV.T) 574 | if i < k: 575 | ZZi = np.roll(np.roll(ZZi, len(W), 0), len(W), 1) 576 | for j in range(p): 577 | lastLine[j] += -np.outer(data[-p-i-1], data[-j-2-i])+np.outer(data[k-1-i], data[p-i+k-j-2]) 578 | ZZi[pnl:, len(W)*j:len(W)*(j+1)] = lastLine[j] 579 | if j != p: 580 | ZZi[len(W)*j:len(W)*(j+1), pnl:] = lastLine[j].T 581 | # line = np.hstack(lastLine) 582 | # for j in range(p): 583 | # lastLine[j] += -np.outer(data[-p-i-1], data[-j-2-i])+np.outer(data[k-1-i], data[p-i+k-j-2]) 584 | # line = np.hstack(lastLine) 585 | # ZZi = np.vstack([np.hstack([ ZZi[pnl:].T[pnl:], line.T[:pnl]]), line]) 586 | # print "ZZi" 587 | # print ZZi 588 | # print ZZi2 589 | 590 | return Y 591 | 592 | def calcErrorCoeffAffine(data, W_c, k = 0): 593 | p = (len(W_c[0])-1)/len(W_c) 594 | X = np.zeros([len(W_c), len(W_c)]) 595 | corList = [np.dot(data[p:].T, data[p:])] 596 | 597 | m = data[p:].mean(0)*(len(data)-p) 598 | meanList = [m] 599 | 600 | for i in range(1, p+1): 601 | corList.append(np.dot(data[p:].T, data[p-i:-i])) 602 | meanList.append(meanList[i-1] - data[-i]+data[p-i]) 603 | #meanList[i] -= data[i-1] 604 | #print np.hstack(meanList[1:]) 605 | 606 | zetaList = [] 607 | lastLine = [] 608 | zetaBlockList = [] 609 | for i in range(p): 610 | zetaLine = [] 611 | for j in range(p): 612 | if j < i: 613 | zetaLine.append(zetaList[j][i].T) 614 | else: 615 | if i == 0: 616 | zetaLine.append(corList[j]+np.outer(data[p-1], data[p-1-j])-np.outer(data[-1], data[-1-j])) 617 | else: 618 | zetaLine.append(lastLine[j-1]+np.outer(data[p-1-i], data[p-1-j])-np.outer(data[-1-i], data[-1-j])) 619 | 620 | zetaList.append(zetaLine) 621 | lastLine = zetaLine 622 | 623 | cov_p = np.dot(data[p:].T, data[p:]) 624 | WV = W_c 625 | V_c = buildV_cRefImp(W_c) 626 | for i in range(k+1): 627 | if i > 0: 628 | WV = np.dot(WV, V_c) 629 | if i > 0: 630 | meanList[0] -= data[p+i-1] 631 | for j in range(i+1, len(corList)): 632 | corList[j] -= np.outer(data[p+i-1], data[p-j+i-1]) 633 | meanList[j] -= data[p-j+i-1] 634 | corList.append(np.dot(data[p+i:].T, data[:-p-i])) 635 | #corList.append(np.dot(data[p:].T, data[p-i:-i])) 636 | #meanList.append(meanList[-1] - data[-p-i]) 637 | meanList.append(data[:-p-i].mean(0)*len(data[:-p-i])) #todo: calc this from meanList[-1] 638 | 639 | zZi_c = np.vstack([np.hstack(corList[1+i:]).T, meanList[0]]).T 640 | if i == 0: 641 | for l in range(p): 642 | zetaBlockList.append(np.hstack(zetaList[l])) 643 | else: 644 | for l in range(p): 645 | for j in range(p): 646 | if j < l: 647 | zetaList[l][j] = zetaList[j][l].T 648 | else: 649 | zetaList[l][j] -= np.outer(data[-(i+1)-l], data[-(i+1)-j]) 650 | for l in range(p): 651 | zetaBlockList[l] = np.hstack(zetaList[l]) 652 | ZZi = np.vstack(zetaBlockList) 653 | ml = np.hstack(meanList[1+i:]) 654 | ZZi_c = np.vstack([ZZi, ml]).T 655 | ml = np.hstack([ml, [len(data)-p-i]]) 656 | ZZi_c = np.vstack([ZZi_c, ml]) 657 | # if i == 1: 658 | # print zZi_c 659 | K = np.dot(zZi_c, WV.T) 660 | X += cov_p -K-K.T + np.dot(np.dot(WV, ZZi_c), WV.T) 661 | if i < k: 662 | cov_p -= np.outer(data[p+i], data[p+i]) 663 | return X 664 | 665 | def calcErrorCoeffConstLenAffine(data, W_c, k = 0): 666 | p = (len(W_c[0])-1)/len(W_c) 667 | z_pk = data[p+k:] 668 | corList = [np.dot(z_pk.T, z_pk)] 669 | meanList = [z_pk.mean(0)*len(z_pk)] 670 | for i in range(1, k+p+1): 671 | corList.append(np.dot(z_pk.T, data[p+k-i:-i])) 672 | meanList.append(meanList[-1]-data[-i]+data[p+k-i]) 673 | return calcErrorCoeffConstLenAffineFromAutoCorrelations(W_c, corList, meanList, len(data), data[:p+k], data[-p-k:], k) 674 | 675 | def calcErrorCoeffConstLenAffineFromAutoCorrelations(W_c, corList, meanList, dataLen, startData, endData, k = 0): 676 | zetaList = [] 677 | lastLine = [] 678 | p = len(corList)-1-k 679 | for i in range(p): 680 | zetaLine = [] 681 | for j in range(p): 682 | if j < i: 683 | zetaLine.append(zetaList[j][i].T) 684 | else: 685 | if i == 0: 686 | zetaLine.append(corList[j]-np.outer(endData[-1], endData[-1-j])+np.outer(startData[p+k-1], startData[p+k-1-j])) 687 | else: 688 | zetaLine.append(lastLine[j-1]-np.outer(endData[-1-i], endData[-1-j])+np.outer(startData[p+k-1-i], startData[p+k-1-j])) 689 | zetaList.append(zetaLine) 690 | lastLine = zetaLine 691 | for i in range(p): 692 | zetaList[i] = np.hstack(zetaList[i]) 693 | ml = np.hstack(meanList[1:p+1]) 694 | print ml 695 | ZZi_c = np.vstack([np.vstack(zetaList), ml]) 696 | ml1 = np.hstack([ml, [dataLen-p-k]]) 697 | ZZi_c = np.vstack([ZZi_c.T, ml1]).T 698 | return calcErrorCoeffConstLenAffineFromCorrelations(W_c, np.vstack([np.hstack(corList[1:p+1]).T, meanList[0]]).T, ZZi_c, lastLine, corList, meanList, dataLen, startData, endData, k) 699 | 700 | def calcErrorCoeffConstLenAffineFromCorrelations(W_c, zZ_c, ZZ_c, lastLine, corList, meanList, dataLen, startData, endData, k = 0): 701 | #print ZZi_c 702 | p = len(corList)-1-k 703 | WV = W_c 704 | V_c = buildV_cRefImp(W_c) 705 | #Y = np.dot(z_pk.T, z_pk)*(k+1) 706 | Y = corList[0]*(k+1) 707 | pnl = len(W_c)*(p-1) 708 | ZZi_c = ZZ_c 709 | zZi_c = zZ_c 710 | for i in range(0, k+1): 711 | if i > 0: 712 | WV = np.dot(WV, V_c) 713 | zZi_c = np.vstack([np.hstack(corList[1+i:p+i+1]).T, meanList[0]]).T 714 | # if i == 1: 715 | # print ZZi_c 716 | K = np.dot(zZi_c, WV.T) 717 | Y += -K-K.T + np.dot(np.dot(WV, ZZi_c), WV.T) 718 | if i < k: 719 | for j in range(p): 720 | lastLine[j] += -np.outer(endData[-p-i-1], endData[-j-2-i])+np.outer(startData[k-1-i], startData[p-i+k-j-2]) 721 | line = np.hstack(lastLine) 722 | ZZi_c = np.vstack([np.hstack([ ZZi_c[len(W_c):-1].T[len(W_c):-1], line.T[:pnl]]), line]) 723 | ml = np.hstack(meanList[i+2:p+i+2]) 724 | ZZi_c = np.vstack([ZZi_c, ml]) 725 | ml1 = np.hstack([ml, [dataLen-p-k]]) 726 | ZZi_c = np.vstack([ZZi_c.T, ml1]).T 727 | return Y 728 | 729 | #def calcErrorCoeffConstLenAffineFromCorrelations2(W_c, zZ_c, ZZ_c, lastLine, corList, meanList, dataLen, startData, endData, k = 0): 730 | def calcErrorCoeffConstLenAffineFromCorrelations2(W_c, zZ_c, ZZ_c, lastLine, corList, meanList, dataLen, S, start_cor, end_cor, k = 0): 731 | #print ZZi_c 732 | p = len(corList)-1-k 733 | WV = W_c 734 | V_c = buildV_cRefImp(W_c) 735 | #Y = np.dot(z_pk.T, z_pk)*(k+1) 736 | Y = corList[0]*(k+1) 737 | pnl = len(W_c)*(p-1) 738 | ZZi_c = ZZ_c 739 | zZi_c = zZ_c 740 | for i in range(0, k+1): 741 | if i > 0: 742 | WV = np.dot(WV, V_c) 743 | zZi_c = np.vstack([np.hstack(corList[1+i:p+i+1]).T, meanList[0]]).T 744 | # if i == 1: 745 | # print ZZi_c 746 | K = np.dot(zZi_c, WV.T) 747 | Y += -K-K.T + np.dot(np.dot(WV, ZZi_c), WV.T) 748 | if i < k: 749 | for j in range(p): 750 | #lastLine[j] += -np.outer(endData[-p-i-1], endData[-j-2-i])+np.outer(startData[-p-1-i], startData[-i-j-2]) 751 | lastLine[j] += np.dot(S.T, np.dot(-end_cor[-p-i-1][-j-2-i]+start_cor[-p-1-i][-i-j-2], S)) 752 | line = np.hstack(lastLine) 753 | #print pnl 754 | #print len(W_c) 755 | #print ZZi_c[len(W_c):-1].T[len(W_c):-1].shape 756 | #print line.T[:pnl].shape 757 | a = np.hstack([ ZZi_c[len(W_c):-1].T[len(W_c):-1], line.T[:pnl]]) 758 | ZZi_c = np.vstack([a, line]) 759 | ml = np.hstack(meanList[i+2:p+i+2]) 760 | ZZi_c = np.vstack([ZZi_c, ml]) 761 | ml1 = np.hstack([ml, [dataLen-p-k]]) 762 | ZZi_c = np.vstack([ZZi_c.T, ml1]).T 763 | return Y 764 | 765 | def calcErrorCoeffConstLenAffineRoll(data, W_c, k = 0): 766 | p = (len(W_c[0])-1)/len(W_c) 767 | z_pk = data[p+k:] 768 | WV = W_c 769 | V_c = buildV_cRefImp(W_c) 770 | Y = np.dot(z_pk.T, z_pk)*(k+1) 771 | corList = [np.dot(z_pk.T, z_pk)] 772 | meanList = [z_pk.mean(0)*len(z_pk)] 773 | for i in range(1, k+p+1): 774 | corList.append(np.dot(z_pk.T, data[p+k-i:-i])) 775 | meanList.append(meanList[-1]-data[-i]+data[p+k-i]) 776 | 777 | zetaList = [] 778 | lastLine = [] 779 | for i in range(p): 780 | zetaLine = [] 781 | for j in range(p): 782 | if j < i: 783 | zetaLine.append(zetaList[j][i].T) 784 | else: 785 | if i == 0: 786 | zetaLine.append(corList[j]-np.outer(data[-1], data[-1-j])+np.outer(data[p+k-1], data[p+k-1-j])) 787 | else: 788 | zetaLine.append(lastLine[j-1]-np.outer(data[-1-i], data[-1-j])+np.outer(data[p+k-1-i], data[p+k-1-j])) 789 | zetaList.append(zetaLine) 790 | lastLine = zetaLine 791 | for i in range(p): 792 | zetaList[i] = np.hstack(zetaList[i]) 793 | ml = np.hstack(meanList[1:p+1]) 794 | # print ml 795 | ZZi_c = np.vstack([np.vstack(zetaList), ml]) 796 | ml1 = np.hstack([ml, [len(z_pk)]]) 797 | ZZi_c = np.vstack([ZZi_c.T, ml1]).T 798 | 799 | #print ZZi_c 800 | 801 | pnl = len(W_c)*(p-1) 802 | for i in range(0, k+1): 803 | if i > 0: 804 | WV = np.dot(WV, V_c) 805 | zZi_c = np.vstack([np.hstack(corList[1+i:p+i+1]).T, meanList[0]]).T 806 | # if i == 1: 807 | # print ZZi_c 808 | K = np.dot(zZi_c, WV.T) 809 | Y += -K-K.T + np.dot(np.dot(WV, ZZi_c), WV.T) 810 | if i < k: 811 | ZZi_c = np.roll(np.roll(ZZi_c[:-1, :-1], len(W_c), 0), len(W_c), 1) 812 | for j in range(p): 813 | lastLine[j] += -np.outer(data[-p-i-1], data[-j-2-i])+np.outer(data[k-1-i], data[p-i+k-j-2]) 814 | ZZi_c[pnl:, len(W_c)*j:len(W_c)*(j+1)] = lastLine[j] 815 | if j != p: 816 | ZZi_c[len(W_c)*j:len(W_c)*(j+1), pnl:] = lastLine[j].T 817 | # ZZi_c2[-1:, len(W_c)*j:len(W_c)*(j+1)] = meanList[i+2+j] 818 | 819 | # for j in range(p): 820 | # lastLine[j] += -np.outer(data[-p-i-1], data[-j-2-i])+np.outer(data[k-1-i], data[p-i+k-j-2]) 821 | # line = np.hstack(lastLine) 822 | # ZZi_c = np.vstack([np.hstack([ ZZi_c[pnl:-1].T[pnl:-1], line.T[:pnl]]), line]) 823 | ml = np.hstack(meanList[i+2:p+i+2]) 824 | ZZi_c = np.vstack([ZZi_c, ml]) 825 | ml1 = np.hstack([ml, [len(z_pk)]]) 826 | ZZi_c = np.vstack([ZZi_c.T, ml1]).T 827 | 828 | # print "ZZi_c" 829 | # print ZZi_c 830 | # print ZZi_c2 831 | return Y 832 | 833 | def createTestData(): 834 | #data = np.outer(range(0, 9), [1, 1.1, 1.11]) 835 | data = np.outer(range(0, 12), [1, 2.0])#, 3.0]) 836 | #place some ouliers: 837 | #data[3][1] = 1.0 838 | #data[5][2] = 1.0 839 | data[5][1] = 1.0 840 | data[4][1] = -1.0 841 | data[7][0] = -2.0 842 | data[7][0] = -3.0 843 | data[10][0] = -4.5 844 | data[11][1] = -6.0 845 | return data 846 | 847 | def PFAReferenceImplementationTest(): 848 | data = createTestData() 849 | p = 2 850 | print data 851 | print "------------------Sphering------------------" 852 | #Sphering: 853 | mean, S, z = calcSpheringParametersAndDataRefImp(data)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 854 | #Test Sphering: 855 | print z 856 | print np.multiply(np.dot(z.T, z), 1.0/len(data)) 857 | 858 | print "------------------Fitting------------------" 859 | #Fitting: 860 | zeta = calcZetaDataRefImp(z, p) 861 | z_p = z[p:] 862 | W = calcRegressionCoeffRefImp(z, p) 863 | #Test fitting: 864 | #print W 865 | pre = np.dot(zeta, W.T) #np.dot(W, zeta.T).T 866 | res = z_p-pre 867 | print np.trace(np.dot(res.T, res))/len(res) 868 | print empiricalRawErrorRefImp(z, W) 869 | errComp = empiricalRawErrorComponentsRefImp(z, W) 870 | print errComp 871 | print np.sum(errComp) 872 | 873 | print "------------------PCA on Error Covariance------------------" 874 | r = 1 875 | #PCA on error covariance: 876 | X = np.dot(res.T, res) 877 | print X 878 | Ar = calcExtractionForErrorCovRefImp(X, r) 879 | m = np.dot(z, Ar) 880 | Wm = calcRegressionCoeffRefImp(m, p) 881 | print m 882 | errComp_m = empiricalRawErrorComponentsRefImp(m, Wm) 883 | print errComp_m 884 | print np.sum(errComp_m) 885 | print np.dot(m.T, m)/len(m) 886 | 887 | print "--------------------------------------------------" 888 | print "------------------Affine variant------------------" 889 | print "--------------------------------------------------" 890 | print "------------------Sphering------------------" 891 | print "like before" 892 | 893 | print "------------------Fitting------------------" 894 | #Fitting: 895 | zeta_c = calcZetacDataRefImp(z, p) 896 | # print zeta_c 897 | 898 | # z_p = z[p:] 899 | W_c = calcRegressionCoeffAffineRefImp(z, p) 900 | #Test fitting: 901 | #print W 902 | pre_c = np.dot(zeta_c, W_c.T) #np.dot(W, zeta.T).T 903 | res_c = z_p-pre_c 904 | print np.trace(np.dot(res_c.T, res_c))/len(res_c) 905 | print empiricalRawErrorAffineRefImp(z, W_c) 906 | errComp_c = empiricalRawErrorComponentsAffineRefImp(z, W_c) 907 | print errComp_c 908 | print np.sum(errComp_c) 909 | print W_c 910 | 911 | print "------------------Fitting Sinus------------------" 912 | sig_x = lambda t: [np.sin(t)] 913 | ts = range(0, 50) 914 | sin_x = np.array([sig_x(t) for t in ts]) 915 | #print sin_x 916 | sin_mean, sin_S, sin_z = calcSpheringParametersAndDataRefImp(sin_x)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 917 | #Test Sphering: 918 | #print sin_z 919 | print "sin_z cov:" 920 | print np.multiply(np.dot(sin_z.T, sin_z), 1.0/len(sin_z)) 921 | 922 | sin_p = 2 923 | sin_zeta_c = calcZetacDataRefImp(sin_z, sin_p) 924 | # print zeta_c 925 | 926 | sin_z_p = sin_z[sin_p:] 927 | sin_W_c = calcRegressionCoeffAffineRefImp(sin_z, sin_p) 928 | #Test fitting: 929 | #print W 930 | sin_pre_c = np.dot(sin_zeta_c, sin_W_c.T) #np.dot(W, zeta.T).T 931 | sin_res_c = sin_z_p-sin_pre_c 932 | print "sin_z, sin_W_c error:" 933 | print np.trace(np.dot(sin_res_c.T, sin_res_c))/len(sin_res_c) 934 | print empiricalRawErrorAffineRefImp(sin_z, sin_W_c) 935 | sin_errComp_c = empiricalRawErrorComponentsAffineRefImp(sin_z, sin_W_c) 936 | print sin_errComp_c 937 | print np.sum(sin_errComp_c) 938 | print "fitted sin W_c:" 939 | print sin_W_c 940 | print "analytic sin W_c:" 941 | cs = np.cos(1.0) 942 | sin_W_c_2 = np.array([[2*cs, -1.0, (-2.0+2.0*cs)*sin_S[0][0]*sin_mean[0]]]) 943 | print empiricalRawErrorAffineRefImp(sin_z, sin_W_c_2) 944 | print sin_W_c_2 945 | 946 | print "------------------PCA on Error Covariance------------------" 947 | r_c = 1 948 | #PCA on error covariance: 949 | X_c = np.dot(res_c.T, res_c) 950 | print X_c 951 | Ar_c = calcExtractionForErrorCovRefImp(X_c, r_c) 952 | m_c = np.dot(z, Ar_c) 953 | Wm_c = calcRegressionCoeffAffineRefImp(m_c, p) 954 | print m_c 955 | print empiricalRawErrorAffineRefImp(m_c, Wm_c) 956 | print np.dot(m_c.T, m_c)/len(m_c) 957 | 958 | print "------------------PCA on Error Covariance - Noisy sine------------------" 959 | sig_x2 = lambda t: [np.sin(t), np.random.rand(1)[0]] 960 | #ts = range(0, 250) 961 | nsin_x = np.array([sig_x2(t) for t in ts]) 962 | nsin_mean, nsin_S, nsin_z = calcSpheringParametersAndDataRefImp(nsin_x)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 963 | #Test Sphering: 964 | #print sin_z 965 | print "nsin_z cov:" 966 | print np.multiply(np.dot(nsin_z.T, nsin_z), 1.0/len(nsin_z)) 967 | 968 | nsin_p = 2 969 | nsin_zeta_c = calcZetacDataRefImp(nsin_z, nsin_p) 970 | ## print zeta_c 971 | 972 | nsin_z_p = nsin_z[nsin_p:] 973 | nsin_W_c = calcRegressionCoeffAffineRefImp(nsin_z, nsin_p) 974 | #Test fitting: 975 | #print W 976 | nsin_pre_c = np.dot(nsin_zeta_c, nsin_W_c.T) #np.dot(W, zeta.T).T 977 | nsin_res_c = nsin_z_p-nsin_pre_c 978 | print "nsin_z, nsin_W_c error:" 979 | print np.trace(np.dot(nsin_res_c.T, nsin_res_c))/len(nsin_res_c) 980 | print empiricalRawErrorAffineRefImp(nsin_z, nsin_W_c) 981 | nsin_errComp_c = empiricalRawErrorComponentsAffineRefImp(nsin_z, nsin_W_c) 982 | print nsin_errComp_c 983 | print np.sum(nsin_errComp_c) 984 | # print "fitted sin W_c:" 985 | # print nsin_W_c 986 | # print "analytic sin W_c:" 987 | # cs = np.cos(1.0) 988 | # nsin_W_c_2 = np.array([[2*cs, -1.0, (-2.0+2.0*cs)*nsin_S[0][0]*nsin_mean[0]]]) 989 | # print empiricalRawErrorAffine(nsin_z, nsin_W_c_2) 990 | # print nsin_W_c_2 991 | 992 | #print "------------------PCA on Error Covariance------------------" 993 | nsin_r_c = 1 994 | #PCA on error covariance: 995 | nsin_X_c = np.dot(nsin_res_c.T, nsin_res_c) 996 | print nsin_X_c 997 | nsin_Ar_c = calcExtractionForErrorCovRefImp(nsin_X_c, nsin_r_c) 998 | nsin_m_c = np.dot(nsin_z, nsin_Ar_c) 999 | nsin_Wm_c = calcRegressionCoeffAffineRefImp(nsin_m_c, nsin_p) 1000 | #print nsin_m_c 1001 | print empiricalRawErrorAffineRefImp(nsin_m_c, nsin_Wm_c) 1002 | print np.dot(nsin_m_c.T, nsin_m_c)/len(nsin_m_c) 1003 | print "fitted nsin Wm_c:" 1004 | print nsin_Wm_c 1005 | print "analytic nsin Wm_c:" 1006 | SI = LA.inv(nsin_S.T) 1007 | factor = LA.norm(np.dot(np.array([1.0, 0]), SI)) 1008 | Ar2 = np.multiply(np.dot(np.array([1.0, 0]), SI), 1.0/factor) 1009 | nsin_Wm_c_2 = np.array([[2*cs, -1.0, (-2.0+2.0*cs)*(1.0/factor)*nsin_mean[0]]]) 1010 | print empiricalRawErrorAffineRefImp(nsin_m_c, nsin_Wm_c_2) 1011 | print nsin_Wm_c_2 1012 | 1013 | print "" 1014 | print " _____________________________________________" 1015 | print "/ \\" 1016 | print "|---------------iteration stuff---------------|" 1017 | print "\\_____________________________________________/" 1018 | 1019 | print "" 1020 | #print zeta 1021 | zeta0 = calcZeta0DataRefImp(z, p) 1022 | #print zeta0 1023 | ZZ0 = np.dot(zeta0.T, zeta) 1024 | zZ = np.dot(z_p.T, zeta) 1025 | ZZ = np.dot(zeta.T, zeta) 1026 | ZZI = invertByProjectionRefImp(ZZ) 1027 | W0 = np.dot(zZ, ZZI) 1028 | V = np.dot(ZZ0, ZZI) 1029 | print W0 1030 | print W 1031 | print "" 1032 | print V 1033 | print buildVRefImp(W) 1034 | 1035 | print "" 1036 | print np.dot(ZZ, ZZI) 1037 | 1038 | print "calc iterated error coeff:" 1039 | X_k = calcErrorCoeffRefImp(z, W, 1) 1040 | Y_k = calcErrorCoeffConstLenRefImp(z, W, 1) 1041 | print X 1042 | print X_k 1043 | print Y_k 1044 | 1045 | print "-------------affine version---------------" 1046 | print "" 1047 | #print zeta 1048 | zeta0_c = calcZeta0cDataRefImp(z, p) 1049 | # print zeta0_c 1050 | # print zeta_c 1051 | ZZ0_c = np.dot(zeta0_c.T, zeta_c) 1052 | zZ_c = np.dot(z_p.T, zeta_c) 1053 | ZZ_c = np.dot(zeta_c.T, zeta_c) 1054 | ZZI_c = invertByProjectionRefImp(ZZ_c) 1055 | W0_c = np.dot(zZ_c, ZZI_c) 1056 | #The following idea of V_c is cumbersome, because it 1057 | #puts error on the constant-one component of zeta_c, 1058 | #if this reduces the overall error. 1059 | #But the constant component may not be compromized in any case, so 1060 | #this way to compute V_c is misleading: 1061 | # V_c_cumb = np.dot(ZZ0_c, ZZI_c) 1062 | # print V_c_cumb 1063 | 1064 | print W0_c 1065 | print W_c 1066 | print "" 1067 | #Better way: 1068 | V_c = buildV_cRefImp(W_c) 1069 | print V_c 1070 | print "calc iterated error coeff:" 1071 | X_k_c = calcErrorCoeffAffineRefImp(z, W_c, 1) 1072 | Y_k_c = calcErrorCoeffConstLenAffineRefImp(z, W_c, 1) 1073 | print X_k 1074 | print X_k_c 1075 | print Y_k 1076 | print Y_k_c 1077 | 1078 | #todo: build unit-test from this: 1079 | def compareSphering(): 1080 | data = createTestData() 1081 | p = 2 1082 | print data 1083 | print "------------------Sphering------------------" 1084 | #Sphering: 1085 | meanRef, SRef, zRef = calcSpheringParametersAndDataRefImp(data, besselsCorrection = 1)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 1086 | #Test Sphering: 1087 | #print z 1088 | #print np.multiply(np.dot(z.T, z), 1.0/len(data)) 1089 | print SRef 1090 | print meanRef 1091 | 1092 | #print data 1093 | mean = data.mean(0) 1094 | secondMoment = np.dot(data.T, data) 1095 | print (secondMoment/len(data)-np.outer(mean, mean)) 1096 | S = calcSpheringMatrixFromMeanAndSecondMoment(mean, secondMoment, len(data), threshold = 0.0000001, besselsCorrection = 1) 1097 | print S 1098 | data0 = data-np.outer(np.ones(len(data)), mean) 1099 | z = np.dot(data0, S) 1100 | print zRef 1101 | print z 1102 | 1103 | def compareFitting(): 1104 | data = createTestData() 1105 | p = 2 1106 | print data 1107 | print "------------------Sphering------------------" 1108 | #Sphering: 1109 | meanRef, SRef, zRef = calcSpheringParametersAndDataRefImp(data, besselsCorrection = 0)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 1110 | print "------------------Fitting------------------" 1111 | #Fitting: 1112 | zeta = calcZetaDataRefImp(zRef, p) 1113 | z_pRef = zRef[p:] 1114 | WRef = calcRegressionCoeffRefImp(zRef, p) 1115 | #print WRef 1116 | ZZRef = np.dot(zeta.T, zeta) 1117 | print ZZRef 1118 | Z11Ref = np.dot(zRef[p-1:-1].T, zRef[p-1:-1]) 1119 | Z12Ref = np.dot(zRef[p-1:-1].T, zRef[p-2:-2]) 1120 | Z22Ref = np.dot(zRef[p-2:-2].T, zRef[p-2:-2]) 1121 | print Z11Ref 1122 | #print Z12Ref 1123 | #print Z22Ref 1124 | print "" 1125 | mean = data.mean(0) 1126 | secondMoment = np.dot(data.T, data) 1127 | S = calcSpheringMatrixFromMeanAndSecondMoment(mean, secondMoment, len(data), threshold = 0.0000001, besselsCorrection = 0) 1128 | #x1 = data[p-1:-1] 1129 | #x2 = data[p-2:-2] 1130 | #X11 = np.dot(x1.T, x1) 1131 | X00 = secondMoment-np.dot(data[0:p].T, data[0:p]) 1132 | X11 = X00-np.outer(data[-1], data[-1])+np.outer(data[p-1], data[p-1]) 1133 | #X12 = np.dot(x1.T, x2) 1134 | X12 = np.dot(data[p-1:-1].T, data[p-2:-2]) 1135 | #X22 = np.dot(x2.T, x2) 1136 | X22 = X11-np.outer(data[-2], data[-2])+np.outer(data[p-2], data[p-2]) 1137 | 1138 | M = np.outer(mean, mean)*(len(data)-p) 1139 | x0 = (mean*len(data)-data[0:p].mean(0)*p) 1140 | xm1 = (x0 - data[-1]+data[p-1]) 1141 | xm2 = (xm1 - data[-2]+data[p-2]) 1142 | #xm1 = data[p-1:-1].mean(0)*(len(data)-p) 1143 | #xm2 = data[p-2:-2].mean(0)*(len(data)-p) 1144 | 1145 | #z1_ = x1 - np.outer(np.ones(len(x1)), meanRef) 1146 | #print np.dot(z1_, SRef) 1147 | print Z11Ref 1148 | # print np.dot(np.dot(x1 - np.outer(np.ones(len(x1)), meanRef), SRef).T, np.dot(x1 - np.outer(np.ones(len(x1)), meanRef), SRef)) 1149 | # print np.dot(np.dot(SRef.T, (x1 - np.outer(np.ones(len(x1)), meanRef)).T), np.dot(x1 - np.outer(np.ones(len(x1)), meanRef), SRef)) 1150 | 1151 | #K = np.dot((x1 - np.outer(np.ones(len(x1)), meanRef)).T, x1 - np.outer(np.ones(len(x1)), meanRef)) 1152 | K11 = X11 +( - np.outer(xm1, mean) - np.outer(mean, xm1) + M) 1153 | print np.dot(S.T, np.dot(K11, S)) 1154 | print "" 1155 | print Z12Ref 1156 | K12 = X12 +( - np.outer(xm1, mean) - np.outer(mean, xm2) + M) 1157 | print np.dot(S.T, np.dot(K12, S)) 1158 | print "" 1159 | print Z22Ref 1160 | K22 = X22 +( - np.outer(xm2, mean) - np.outer(mean, xm2) + M) 1161 | print np.dot(S.T, np.dot(K22, S)) 1162 | print "" 1163 | 1164 | # print ZZRef 1165 | # print ZZRef[2:4].T[2:4] 1166 | 1167 | # print np.dot(x1.T, np.outer(np.ones(len(x1)), meanRef)) 1168 | # print np.outer(x1.mean(0)*len(x1), meanRef) 1169 | 1170 | # print np.dot(np.outer(np.ones(len(x1)), meanRef).T, x1) 1171 | # print np.outer(meanRef, x1.mean(0)*len(x1)) 1172 | 1173 | # print np.dot(np.outer(np.ones(len(x1)), meanRef).T, np.outer(np.ones(len(x1)), meanRef)) 1174 | # print np.outer(meanRef, meanRef)*len(x1) 1175 | 1176 | # x2 = data[p-2:-2] 1177 | # X11 = np.dot(x1.T, x1) 1178 | # X12 = np.dot(x1.T, x2) 1179 | # X22 = np.dot(x2.T, x2) 1180 | # xm1 = x1.mean(0) 1181 | # xm2 = x2.mean(0) 1182 | # K11 = np.outer(meanRef, meanRef) + (X11-np.outer(xm1, meanRef)-np.outer(meanRef, xm1))/(len(data)-p*1.0) 1183 | # Z11 = np.dot(SRef.T, np.dot(K11, SRef)) 1184 | # print Z11 1185 | 1186 | 1187 | #Test fitting: 1188 | #print W 1189 | # pre = np.dot(zeta, W.T) #np.dot(W, zeta.T).T 1190 | # res = z_p-pre 1191 | # print np.trace(np.dot(res.T, res))/len(res) 1192 | # print empiricalRawErrorRefImp(z, W) 1193 | # errComp = empiricalRawErrorComponentsRefImp(z, W) 1194 | # print errComp 1195 | # print np.sum(errComp) 1196 | 1197 | def compareIteration(): 1198 | data = createTestData() 1199 | p = 2 1200 | print data 1201 | print "------------------Sphering------------------" 1202 | #Sphering: 1203 | meanRef, SRef, zRef = calcSpheringParametersAndDataRefImp(data)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 1204 | 1205 | print "------------------Fitting------------------" 1206 | #Fitting: 1207 | z_pRef = zRef[p:] 1208 | zetaRef = calcZetaDataRefImp(zRef, p) 1209 | WRef = calcRegressionCoeffRefImp(zRef, p) 1210 | 1211 | print "------------------PCA on Error Covariance------------------" 1212 | r = 1 1213 | #PCA on error covariance: 1214 | # X = np.dot(res.T, res) 1215 | # print X 1216 | # Ar = calcExtractionForErrorCovRefImp(X, r) 1217 | # m = np.dot(z, Ar) 1218 | # Wm = calcRegressionCoeffRefImp(m, p) 1219 | # print m 1220 | # errComp_m = empiricalRawErrorComponentsRefImp(m, Wm) 1221 | # print errComp_m 1222 | # print np.sum(errComp_m) 1223 | # print np.dot(m.T, m)/len(m) 1224 | # 1225 | # print "--------------------------------------------------" 1226 | # print "------------------Affine variant------------------" 1227 | # print "--------------------------------------------------" 1228 | # print "------------------Sphering------------------" 1229 | # print "like before" 1230 | # 1231 | # print "------------------Fitting------------------" 1232 | # #Fitting: 1233 | # zeta_c = calcZetacDataRefImp(z, p) 1234 | ## print zeta_c 1235 | # 1236 | ## z_p = z[p:] 1237 | # W_c = calcRegressionCoeffAffineRefImp(z, p) 1238 | # #Test fitting: 1239 | # #print W 1240 | # pre_c = np.dot(zeta_c, W_c.T) #np.dot(W, zeta.T).T 1241 | # res_c = z_p-pre_c 1242 | # print np.trace(np.dot(res_c.T, res_c))/len(res_c) 1243 | # print empiricalRawErrorAffineRefImp(z, W_c) 1244 | # errComp_c = empiricalRawErrorComponentsAffineRefImp(z, W_c) 1245 | # print errComp_c 1246 | # print np.sum(errComp_c) 1247 | # print W_c 1248 | # 1249 | # print "------------------Fitting Sinus------------------" 1250 | # sig_x = lambda t: [np.sin(t)] 1251 | # ts = range(0, 50) 1252 | # sin_x = np.array([sig_x(t) for t in ts]) 1253 | # #print sin_x 1254 | # sin_mean, sin_S, sin_z = calcSpheringParametersAndDataRefImp(sin_x)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 1255 | # #Test Sphering: 1256 | # #print sin_z 1257 | # print "sin_z cov:" 1258 | # print np.multiply(np.dot(sin_z.T, sin_z), 1.0/len(sin_z)) 1259 | # 1260 | # sin_p = 2 1261 | # sin_zeta_c = calcZetacDataRefImp(sin_z, sin_p) 1262 | ## print zeta_c 1263 | # 1264 | # sin_z_p = sin_z[sin_p:] 1265 | # sin_W_c = calcRegressionCoeffAffineRefImp(sin_z, sin_p) 1266 | # #Test fitting: 1267 | # #print W 1268 | # sin_pre_c = np.dot(sin_zeta_c, sin_W_c.T) #np.dot(W, zeta.T).T 1269 | # sin_res_c = sin_z_p-sin_pre_c 1270 | # print "sin_z, sin_W_c error:" 1271 | # print np.trace(np.dot(sin_res_c.T, sin_res_c))/len(sin_res_c) 1272 | # print empiricalRawErrorAffineRefImp(sin_z, sin_W_c) 1273 | # sin_errComp_c = empiricalRawErrorComponentsAffineRefImp(sin_z, sin_W_c) 1274 | # print sin_errComp_c 1275 | # print np.sum(sin_errComp_c) 1276 | # print "fitted sin W_c:" 1277 | # print sin_W_c 1278 | # print "analytic sin W_c:" 1279 | # cs = np.cos(1.0) 1280 | # sin_W_c_2 = np.array([[2*cs, -1.0, (-2.0+2.0*cs)*sin_S[0][0]*sin_mean[0]]]) 1281 | # print empiricalRawErrorAffineRefImp(sin_z, sin_W_c_2) 1282 | # print sin_W_c_2 1283 | # 1284 | # print "------------------PCA on Error Covariance------------------" 1285 | # r_c = 1 1286 | # #PCA on error covariance: 1287 | # X_c = np.dot(res_c.T, res_c) 1288 | # print X_c 1289 | # Ar_c = calcExtractionForErrorCovRefImp(X_c, r_c) 1290 | # m_c = np.dot(z, Ar_c) 1291 | # Wm_c = calcRegressionCoeffAffineRefImp(m_c, p) 1292 | # print m_c 1293 | # print empiricalRawErrorAffineRefImp(m_c, Wm_c) 1294 | # print np.dot(m_c.T, m_c)/len(m_c) 1295 | # 1296 | # print "------------------PCA on Error Covariance - Noisy sine------------------" 1297 | # sig_x2 = lambda t: [np.sin(t), np.random.rand(1)[0]] 1298 | # #ts = range(0, 250) 1299 | # nsin_x = np.array([sig_x2(t) for t in ts]) 1300 | # nsin_mean, nsin_S, nsin_z = calcSpheringParametersAndDataRefImp(nsin_x)#, threshold = 0.0000001, offset = 0, length = -1, besselsCorrection = 0) 1301 | # #Test Sphering: 1302 | # #print sin_z 1303 | # print "nsin_z cov:" 1304 | # print np.multiply(np.dot(nsin_z.T, nsin_z), 1.0/len(nsin_z)) 1305 | # 1306 | # nsin_p = 2 1307 | # nsin_zeta_c = calcZetacDataRefImp(nsin_z, nsin_p) 1308 | ### print zeta_c 1309 | # 1310 | # nsin_z_p = nsin_z[nsin_p:] 1311 | # nsin_W_c = calcRegressionCoeffAffineRefImp(nsin_z, nsin_p) 1312 | # #Test fitting: 1313 | # #print W 1314 | # nsin_pre_c = np.dot(nsin_zeta_c, nsin_W_c.T) #np.dot(W, zeta.T).T 1315 | # nsin_res_c = nsin_z_p-nsin_pre_c 1316 | # print "nsin_z, nsin_W_c error:" 1317 | # print np.trace(np.dot(nsin_res_c.T, nsin_res_c))/len(nsin_res_c) 1318 | # print empiricalRawErrorAffineRefImp(nsin_z, nsin_W_c) 1319 | # nsin_errComp_c = empiricalRawErrorComponentsAffineRefImp(nsin_z, nsin_W_c) 1320 | # print nsin_errComp_c 1321 | # print np.sum(nsin_errComp_c) 1322 | ## print "fitted sin W_c:" 1323 | ## print nsin_W_c 1324 | ## print "analytic sin W_c:" 1325 | ## cs = np.cos(1.0) 1326 | ## nsin_W_c_2 = np.array([[2*cs, -1.0, (-2.0+2.0*cs)*nsin_S[0][0]*nsin_mean[0]]]) 1327 | ## print empiricalRawErrorAffine(nsin_z, nsin_W_c_2) 1328 | ## print nsin_W_c_2 1329 | # 1330 | # #print "------------------PCA on Error Covariance------------------" 1331 | # nsin_r_c = 1 1332 | # #PCA on error covariance: 1333 | # nsin_X_c = np.dot(nsin_res_c.T, nsin_res_c) 1334 | # print nsin_X_c 1335 | # nsin_Ar_c = calcExtractionForErrorCovRefImp(nsin_X_c, nsin_r_c) 1336 | # nsin_m_c = np.dot(nsin_z, nsin_Ar_c) 1337 | # nsin_Wm_c = calcRegressionCoeffAffineRefImp(nsin_m_c, nsin_p) 1338 | # #print nsin_m_c 1339 | # print empiricalRawErrorAffineRefImp(nsin_m_c, nsin_Wm_c) 1340 | # print np.dot(nsin_m_c.T, nsin_m_c)/len(nsin_m_c) 1341 | # print "fitted nsin Wm_c:" 1342 | # print nsin_Wm_c 1343 | # print "analytic nsin Wm_c:" 1344 | # SI = LA.inv(nsin_S.T) 1345 | # factor = LA.norm(np.dot(np.array([1.0, 0]), SI)) 1346 | # Ar2 = np.multiply(np.dot(np.array([1.0, 0]), SI), 1.0/factor) 1347 | # nsin_Wm_c_2 = np.array([[2*cs, -1.0, (-2.0+2.0*cs)*(1.0/factor)*nsin_mean[0]]]) 1348 | # print empiricalRawErrorAffineRefImp(nsin_m_c, nsin_Wm_c_2) 1349 | # print nsin_Wm_c_2 1350 | # 1351 | print "" 1352 | print " _____________________________________________" 1353 | print "/ \\" 1354 | print "|---------------iteration stuff---------------|" 1355 | print "\\_____________________________________________/" 1356 | 1357 | print "" 1358 | #print zeta 1359 | zeta0Ref = calcZeta0DataRefImp(zRef, p) 1360 | #print zeta0 1361 | ZZ0Ref = np.dot(zeta0Ref.T, zetaRef) 1362 | zZRef = np.dot(z_pRef.T, zetaRef) 1363 | ZZRef = np.dot(zetaRef.T, zetaRef) 1364 | ZZIRef = invertByProjectionRefImp(ZZRef) 1365 | W0Ref = np.dot(zZRef, ZZIRef) 1366 | VRef = np.dot(ZZ0Ref, ZZIRef) 1367 | # print W0Ref 1368 | # print WRef 1369 | # print "" 1370 | # print VRef 1371 | # print buildVRefImp(WRef) 1372 | # 1373 | # print "" 1374 | # print np.dot(ZZRef, ZZIRef) 1375 | 1376 | print "calc iterated error coeff:" 1377 | k = 4 1378 | X_kRef = calcErrorCoeffRefImp(zRef, WRef, k) 1379 | Y_kRef = calcErrorCoeffConstLenRefImp(zRef, WRef, k) 1380 | #print XRef 1381 | print X_kRef 1382 | #print Y_kRef 1383 | 1384 | 1385 | X = np.zeros([len(WRef), len(WRef)]) 1386 | corList = [np.dot(zRef[p:].T, zRef[p:])] 1387 | 1388 | #for i in range(1, k+p+1): 1389 | for i in range(1, p+1): #macht scheinbar eins zu viel) 1390 | corList.append(np.dot(zRef[p:].T, zRef[p-i:-i])) 1391 | 1392 | zetaList = [] 1393 | lastLine = []#corList 1394 | zetaBlockList = [] 1395 | for i in range(p): 1396 | zetaLine = [] 1397 | for j in range(p): 1398 | if j < i: 1399 | zetaLine.append(zetaList[j][i].T) 1400 | else: 1401 | if i == 0: 1402 | zetaLine.append(corList[j]+np.outer(zRef[p-1], zRef[p-1-j])-np.outer(zRef[-1], zRef[-1-j])) 1403 | else: 1404 | #zetaLine.append(lastLine[j-1]-np.outer(zRef[-2], zRef[-2])+np.outer(zRef[p-2], zRef[p-2])) 1405 | zetaLine.append(lastLine[j-1]+np.outer(zRef[p-1-i], zRef[p-1-j])-np.outer(zRef[-1-i], zRef[-1-j])) 1406 | 1407 | zetaList.append(zetaLine) 1408 | lastLine = zetaLine 1409 | 1410 | # for i in range(p): 1411 | # zetaBlockList.append(np.hstack(zetaList[i])) 1412 | # ZZ0 = np.vstack(zetaBlockList) 1413 | # 1414 | ## for j in range(p): 1415 | ## lastLine[j] -= np.outer(zRef[-p-1], zRef[-2-j]) 1416 | # for i in range(p): 1417 | # for j in range(p): 1418 | # if j < i: 1419 | # zetaList[i][j] = zetaList[j][i].T 1420 | # else: 1421 | # zetaList[i][j] -= np.outer(zRef[-2-i], zRef[-2-j]) 1422 | # 1423 | # #print np.hstack(lastLine) 1424 | # for i in range(p): 1425 | # zetaBlockList[i] = np.hstack(zetaList[i]) 1426 | # ZZ1 = np.vstack(zetaBlockList) 1427 | #print ZZ1 1428 | 1429 | # for i in range(p): 1430 | # for j in range(p): 1431 | # if j < i: 1432 | # zetaList[i][j] = zetaList[j][i].T 1433 | # else: 1434 | # zetaList[i][j] -= np.outer(zRef[-3-i], zRef[-3-j]) 1435 | # 1436 | # #print np.hstack(lastLine) 1437 | # for i in range(p): 1438 | # zetaBlockList[i] = np.hstack(zetaList[i]) 1439 | # ZZ2 = np.vstack(zetaBlockList) 1440 | # print ZZ2 1441 | 1442 | #z_pk = data[p+k:] 1443 | cov_p = np.dot(zRef[p:].T, zRef[p:]) 1444 | WV = WRef 1445 | for i in range(0, k+1): 1446 | if i > 0: 1447 | WV = np.dot(WV, VRef) 1448 | zeta1_i = calcZetaDataRefImp(zRef, p, i) 1449 | # print zRef 1450 | # print zeta1_i 1451 | #print len(zeta1_i) 1452 | #X += np.dot((zRef[p+i:]-z_i_pre).T, zRef[p+i:]-z_i_pre) 1453 | # X += np.dot(zRef[p+i:].T, zRef[p+i:]) - np.dot(z_i_pre.T, zRef[p+i:]) - np.dot(zRef[p+i:].T, z_i_pre) + np.dot(z_i_pre.T, z_i_pre) 1454 | zZiRef = np.dot(zRef[p+i:].T, zeta1_i) 1455 | if i > 0: 1456 | for j in range(i+1, len(corList)): 1457 | corList[j] -= np.outer(zRef[p+i-1], zRef[p-j+i-1]) 1458 | corList.append(np.dot(zRef[p+i:].T, zRef[:-p-i])) #ist scheinbar falsch 1459 | zZi = np.hstack(corList[1+i:])#p+i+1]) 1460 | # print "+++" 1461 | # print zZiRef 1462 | # print zZi 1463 | # print "===" 1464 | ZZiRef = np.dot(zeta1_i.T, zeta1_i) 1465 | 1466 | #print np.hstack(lastLine) 1467 | if i == 0: 1468 | for l in range(p): 1469 | zetaBlockList.append(np.hstack(zetaList[l])) 1470 | else: 1471 | for l in range(p): 1472 | for j in range(p): 1473 | if j < l: 1474 | zetaList[l][j] = zetaList[j][l].T 1475 | else: 1476 | zetaList[l][j] -= np.outer(zRef[-(i+1)-l], zRef[-(i+1)-j]) 1477 | for l in range(p): 1478 | zetaBlockList[l] = np.hstack(zetaList[l]) 1479 | ZZi = np.vstack(zetaBlockList) 1480 | 1481 | # print "i = "+str(i) 1482 | # print zZiRef 1483 | # print zZi 1484 | # print "=========" 1485 | 1486 | K = np.dot(zZi, WV.T) 1487 | X += cov_p -K-K.T + np.dot(np.dot(WV, ZZi), WV.T) 1488 | cov_p -= np.outer(zRef[p+i], zRef[p+i]) 1489 | 1490 | print X 1491 | print calcErrorCoeff(zRef, WRef, k) 1492 | 1493 | print "----------const len-----------" 1494 | #Y = np.zeros([len(WRef), len(WRef)]) 1495 | z_pk = zRef[p+k:] 1496 | WV = WRef 1497 | Y = np.dot(z_pk.T, z_pk)*(k+1) 1498 | corList = [np.dot(z_pk.T, z_pk)] 1499 | for i in range(1, k+p+1): 1500 | corList.append(np.dot(z_pk.T, zRef[p+k-i:-i])) 1501 | 1502 | zetaList = [] 1503 | lastLine = []#corList 1504 | #zetaBlockList = [] 1505 | for i in range(p): 1506 | zetaLine = [] 1507 | for j in range(p): 1508 | if j < i: 1509 | zetaLine.append(zetaList[j][i].T) 1510 | else: 1511 | if i == 0: 1512 | zetaLine.append(corList[j]-np.outer(zRef[-1], zRef[-1-j])+np.outer(zRef[p+k-1], zRef[p+k-1-j])) 1513 | else: 1514 | #zetaLine.append(lastLine[j-1]-np.outer(zRef[-2], zRef[-2])+np.outer(zRef[p-2], zRef[p-2])) 1515 | zetaLine.append(lastLine[j-1]-np.outer(zRef[-1-i], zRef[-1-j])+np.outer(zRef[p+k-1-i], zRef[p+k-1-j])) 1516 | 1517 | zetaList.append(zetaLine) 1518 | lastLine = zetaLine 1519 | 1520 | for i in range(p): 1521 | zetaList[i] = np.hstack(zetaList[i]) 1522 | ZZi = np.vstack(zetaList) 1523 | pnl = len(WRef)*(p-1) 1524 | for i in range(0, k+1): 1525 | if i > 0: 1526 | WV = np.dot(WV, VRef) 1527 | zeta1_i = calcZetaDataRefImp(zRef, p, i)[k-i:] 1528 | zZiRef = np.dot(z_pk.T, zeta1_i) 1529 | zZi = np.hstack(corList[1+i:p+i+1]) 1530 | ZZiRef = np.dot(zeta1_i.T, zeta1_i) 1531 | #ZZi = ZZ0 1532 | # print "i = "+str(i)+":" 1533 | # print ZZi 1534 | # print ZZiRef 1535 | # print "+++"+str(k) 1536 | K = np.dot(zZi, WV.T) 1537 | Y += -K-K.T + np.dot(np.dot(WV, ZZi), WV.T) 1538 | if i < k: 1539 | for j in range(p): 1540 | lastLine[j] += -np.outer(zRef[-p-i-1], zRef[-j-2-i])+np.outer(zRef[k-1-i], zRef[p-i+k-j-2]) 1541 | line = np.hstack(lastLine) 1542 | ZZi = np.vstack([np.hstack([ ZZi[pnl:].T[pnl:], line.T[:pnl]]), line]) 1543 | # print "====" 1544 | # print np.hstack(lastLine) 1545 | print Y_kRef 1546 | print Y 1547 | print calcErrorCoeffConstLen(zRef, WRef, k) 1548 | print calcErrorCoeffConstLenRoll(zRef, WRef, k) 1549 | 1550 | print "-------------affine version---------------" 1551 | print "" 1552 | W_c = calcRegressionCoeffAffineRefImp(zRef, p) 1553 | #print zeta 1554 | zeta0_c = calcZeta0cDataRefImp(zRef, p) 1555 | # print zeta0_c 1556 | # print zeta_c 1557 | # ZZ0_c = np.dot(zeta0_c.T, zeta_c) 1558 | # zZ_c = np.dot(z_p.T, zeta_c) 1559 | # ZZ_c = np.dot(zeta_c.T, zeta_c) 1560 | # ZZI_c = invertByProjectionRefImp(ZZ_c) 1561 | # W0_c = np.dot(zZ_c, ZZI_c) 1562 | #The following idea of V_c is cumbersome, because it 1563 | #puts error on the constant-one component of zeta_c, 1564 | #if this reduces the overall error. 1565 | #But the constant component may not be compromized in any case, so 1566 | #this way to compute V_c is misleading: 1567 | # V_c_cumb = np.dot(ZZ0_c, ZZI_c) 1568 | # print V_c_cumb 1569 | 1570 | # print W0_c 1571 | print W_c 1572 | print "" 1573 | #Better way: 1574 | V_c = buildV_cRefImp(W_c) 1575 | print V_c 1576 | print "calc iterated error coeff:" 1577 | X_k_c = calcErrorCoeffAffineRefImp(zRef, W_c, 4) 1578 | print calcErrorCoeffAffine(zRef, W_c, 4) 1579 | print X_k_c 1580 | 1581 | print "const len:" 1582 | Y_k_c = calcErrorCoeffConstLenAffineRefImp(zRef, W_c, 3) 1583 | print calcErrorCoeffConstLenAffine(zRef, W_c, 3) 1584 | print Y_k_c 1585 | print calcErrorCoeffConstLenAffineRoll(zRef, W_c, 3) 1586 | 1587 | def insertRollTest(): 1588 | print "insert test" 1589 | A = np.multiply(np.reshape(range(25), (5, 5)), 1.0) 1590 | print A 1591 | # B = np.array([[1.5, 2.5], [3.5, 4.5]]) 1592 | # print B 1593 | print A[-3:-1, 3:5] 1594 | # A[2:4, 3:5] = B 1595 | # print A 1596 | # print np.roll(np.roll(A, 2, 0), 2, 1) 1597 | 1598 | if __name__ == "__main__": 1599 | PFAReferenceImplementationTest() 1600 | #compareIteration() 1601 | #insertRollTest() 1602 | --------------------------------------------------------------------------------