-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpy2chomp.py
119 lines (103 loc) · 3.43 KB
/
npy2chomp.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Module to convert RBC frames (npy files) to input required by CHoMP
Module for running CHomP
-
created by kel 6/23/2012
New Cells: 110125, 130125, 140125, 40125, 50125
Old Cells: 100125, 120125, 50125, 90125
"""
import numpy
import matplotlib.pylab as plt
import re
import subprocess, os
import pickle as pkl
def npy3d2chomp ( arr, output, threshold ):
"""
Convert numpy array to CHomP tuple format
"""
aMean = arr[arr>0].mean()
tHeight = threshold*aMean
tupleList = []
for i in arr.shape[0]:
for j in arr.shape[1]:
for k in arr.shape[2]:
if arr[i][j][k] <= tHeight:
tupleList.append((i,j,k))
tupleArr = numpy.array(tupleList)
array2chomp(tupleArr,output)
def npy2chomp ( arr, k, output, threshold ):
"""
Convert numpy array to CHomP tuple format
"""
aMean = arr[arr>0].mean()
tHeight = threshold*aMean
tupleList = []
for i in xrange(arr.shape[0]):
for j in xrange(arr.shape[1]):
if arr[i][j] <= tHeight and not int(arr[i][j])==0:
tupleList.append((k,i,j))
tupleArr = numpy.array(tupleList)
array2chomp(tupleArr,output)
def array2chomp( arr, savename ):
"""
Convert an array to chomp format, ( , , ). Write the resulting
column of numbers to disk.
"""
rows = map( lambda x: str(x)+'\n', map( tuple, iter( arr ) ) )
with open( savename, 'a' ) as fh:
fh.writelines( rows )
fh.close()
def run_chomp( fname, savename ):
"""
Call chomp to compute the betti numbers of the image in file fname.
See http://chomp.rutgers.edu
"""
cmd = ['chomp', fname]
try:
with open( savename, 'w' ) as fh:
p = subprocess.call( cmd, stdout=fh )
except:
print "subprocess returned with code", p
def chomp_block (folder,output,m,n, threshold):
#grab files, skip directories
if not folder.endswith('/'):
folder+='/'
if os.path.isdir (folder):
dlist = os.listdir(folder)
frames = []
for f in dlist:
if f.endswith('npy') and not os.path.isdir(folder+f):
frames.append(f)
else:
print 'Error - First argument not a directory!!'
return
frames.sort(key=natural_key)
for ind, frame in zip(xrange(len(frames)),frames):
if ind >= m and ind <= n:
data = numpy.load(folder+frame)
npy2chomp(data, ind, output, threshold)
def run_block(cell,m,n,thresholds):
"""
Run chomp on 3d block
cell is of form 'n11', 'o9', etc...
m, n starting, ending indices
thresholds list of thresholds
"""
fnames = pkl.load(open('fileNames.pkl','r'))
folder = fnames[cell]
path = folder.split('Cells_Jesse')[-1]
title = path.split('/')[-2] #ex 'new_110125/'
subtitle = '_'+str(m)+'_'+str(n)+'_'
cubStub = '/data/ChompData/Blocks'+path+title+subtitle
outStub = '/data/ChompData/ChompOutput'+path+title+subtitle
for t in thresholds:
str_t = ''.join(str(t).split('.'))
cubFile = cubStub+str_t+'.cub'
chomp_block(fnames[cell],cubFile,m,n,t)
savename = outStub+str_t+'.txt'
run_chomp(cubFile,savename)
def natural_key(string_):
"""
Use with frames.sort(key=natural_key)
"""
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_)]