-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.py
61 lines (47 loc) · 1.74 KB
/
Utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from os import path
from numpy import shape, linspace, sum, isfinite, copy
def get_bam_length(samfile):
start = samfile.tell()
maxval = path.getsize(samfile.filename) * 2**16
# I don't know why that 2**16 factor is there!
return maxval + 2**16, start
def strip_to_number(dataval, chars='\'" \t #'):
return to_number(dataval.strip(chars))
def to_number(dataval):
""" A forgiving number converter.
Will convert to int if possible, float otherwise, and if neither, will
return the input.
"""
try:
datavalf = float(dataval)
# If we could convert it to a float, it might have been an
# int
try:
return int(dataval)
except ValueError:
# not an int, but since we got to the inner try, it is a
# float
return datavalf
except ValueError:
return dataval
def contains(string_or_iterable):
if isinstance(string_or_iterable, str):
return lambda x: string_or_iterable in x
else:
return lambda x: any(i in x for i in string_or_iterable)
def startswith(string_or_iterable):
if not isinstance(string_or_iterable, str):
string_or_iterable = tuple(string_or_iterable)
return lambda x: x.startswith(string_or_iterable)
def sel_contains(string_or_iterable):
return dict(crit=contains(string_or_iterable), axis=1)
def sel_startswith(string_or_iterable):
return dict(crit=startswith(string_or_iterable), axis=1)
def center_of_mass(data):
dims = shape(data)
cols = dims[-1]
xs = linspace(0, 1, cols, endpoint=True)
data_clean = data.copy()
data_clean[~isfinite(data_clean)] = 0
data_clean += 0.01
return sum(data_clean * xs, axis=len(dims)-1)/sum(data_clean, axis=len(dims)-1)