Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Input(object):
- def __init__(self, X):
- self.step = 1.0
- self.noise = 1.0
- self.triuind = (np.arange(23)[:,np.newaxis] <= np.arange(23)[np.newaxis,:]).flatten()
- self.max = 0
- for _ in range(10): self.max = np.maximum(self.max,self.realize(X).max(axis=0))
- X = self.expand(self.realize(X))
- X.append(X)
- self.nbout = X.shape[1]
- self.mean = X.mean(axis=0)
- self.std = (X - self.mean).std()
- self.X = X
- def realize(self,X):
- def _realize_(x):
- inds = np.argsort(-(x**2).sum(axis=0)**.5+np.random.normal(0,self.noise,x[0].shape))
- x = x[inds,:][:,inds]*1
- x = x.flatten()[self.triuind]
- return x
- return np.array([_realize_(z) for z in X])
- def expand(self,X):
- Xexp = []
- for i in range(X.shape[1]):
- for k in np.arange(0,self.max[i]+self.step,self.step):
- Xexp += [np.tanh((X[:, i]-k)/self.step)]
- return np.array(Xexp).T
- # top level code of module1.py
- inp_x = Input(traindata).X # I'm assuming traindata is also a global
- # later code in the same module
- do_stuff(inp_x)
- # code in other modules can do:
- import module1
- do_other_stuff(module1.inp_x) # a module's global variables are attributes on the module
- # functions that use an X value:
- def func1(arg1, arg2, X):
- do_stuff(X)
- def func2(X):
- do_other_stuff(X)
- # main module (or wherever you call func1 and func2 from)
- from module1 import func1
- from module2 import func2
- def main():
- x = Input(traindata).X
- func1("foo", "bar", x)
- func2(x)
- inp = Input(traindata) # save the Input instance, not only X
- # later code:
- do_stuff(inp.X) # use the X attribute, as above
- # other code
- do_other_stuff(inp) # pass the instance, so you can use other attributes or methods
Add Comment
Please, Sign In to add comment