1 '''
2 common operations on numpy arrays
3 @author: odenas
4 '''
5
6 import numpy as np
7
8 -def fit(x, axis=None, nans=0.0):
9 """scale values of x in the interval [-1, 1]
10
11 @param x: ndarray
12 @param axis: passed to numpy.max and numpy.min
13 @param nans: if nan values are produced replace them with this
14 @return: scaled values of x"""
15
16
17 M = x.max(axis = axis)
18 m = x.min(axis = axis)
19 mid_p = (M + m) / 2
20 rng = (M - m)
21
22 fitx = 2 * (x - mid_p) / rng
23 fitx[ np.isnan(fitx) ] = nans
24
25
26
27
28
29 return fitx
30
32 """transform each component of flattened X examples to 0 mean and 1 std
33 So the values of track t at position i are 0 mean and 1 std
34
35 x: a pandas data panel of the form <anchors> X <tracks> X <genome position>
36 return: (the shifted input,
37 the mean for each input component, the sd of each input component)
38 the latter 2 are arrays of shape(<tracks>, <genome position>)
39 """
40
41
42 m = x.mean( axis = axis )
43 v = x.std( axis = axis )
44
45 normX = ( (x - m) / v )
46 normX[ np.isnan(normX) ] = nans
47
48 return normX, m, v
49