-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow.py
190 lines (152 loc) · 5.65 KB
/
workflow.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import logging
from thunder import ThunderContext
from thunder import Colorize
from thunder.clustering.kmeans import KMeans
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s',
filename="logs/fmriFlow.log")
class Workflow(object):
"""
A Workflow describes a set of operators which will be executed
sequentially. Each operator of the workflow takes as input
operand the result of the parent operator and outputs the
result as input to the child operator.
[Input Data] => [Parent Operator] => [Result] => [Child Operator] => ....
"""
def __init__(self, dataSource, sc):
self.context = ThunderContext(sc)
self.root = DataSource(dataSource, self.context)
self.last = self.root
# Adds a node to the workflow
def addNode(self, node):
node.input = self.last.result
node.parent = self.last
self.last.child = node
self.last = node
# Clustering with K-Means
def clustering(self, k):
self.addNode(NeuronClustering(self.last, k))
return self
# Feature Extraction
def extract(self):
self.addNode(FeatureExtractor(self.last))
return self
# Data visualization with matplotlib
def visualize(self, nsamples=20):
self.addNode(Visualizer(self.last,self.root, nsamples=nsamples))
return self
# Data visualization (brain)
def visualizeBrain(self, slice=0):
self.addNode(BrainVisualizer(self.last, self.root, slice))
return self
# Workflow execution
def execute(self):
self.root.doExecute()
# Returns the workflow execution plan as a string
def explain(self):
logging.info("Explain")
cur = self.root
st = ''
while cur:
st += "OPERATOR: %s => INPUT [%s] => RESULT [%s]\n"%(cur.name, type(cur.input), cur.result)
cur = cur.child
return st
class WorkflowNode(object):
def __init__(self, parent, root=None):
self.parent = parent
self.name = None
self.child = None
self.input = parent.result
self.root = root
self.result = None
def doExecute(self):
logging.info("Executing: %s"%(self.name))
self.execute()
if self.child:
self.child.doExecute()
else:
logging.info("Workflow execution completed")
def execute(self):
pass
def hasNext(self):
pass
""" Operators """
class DataSource(WorkflowNode):
def __init__(self, data, context):
super(DataSource, self)
self.name = "Datasource"
self.parent = None
self.dataPath = data
self.input = None
if type(data) == str:
try:
self.result = context.loadImages(data, inputFormat='tif')
except Exception as e:
logging.info(e)
try:
self.result = context.loadImages(data)
except Exception as e:
logging.info(e)
else:
self.result = context.loadImagesFromArray(data)
class FeatureExtractor(WorkflowNode):
def __init__(self, parent):
super(FeatureExtractor, self).__init__(parent)
self.name = "FeatureExtractor"
self.result = 'Features'
def execute(self):
self.result = self.parent.result.toTimeSeries()
class NeuronClustering(WorkflowNode):
def __init__(self, parent, k):
super(NeuronClustering, self).__init__(parent)
self.name = "Clustering"
self.result = 'Centers'
self.k = k
def execute(self):
model = KMeans(self.k)
print "RESULT: ",self.parent.result
self.result = model.fit(self.parent.result)
print type
class Visualizer(WorkflowNode):
def __init__(self, parent, root, nsamples=20):
super(Visualizer, self).__init__(parent, root)
self.name = "Visualizer"
self.result = self.parent.result
self.nsamples=nsamples
def execute(self):
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
cmapCat = ListedColormap(sns.color_palette("hls", 10), name='from_list')
plt.gca().set_color_cycle(cmapCat.colors)
if self.parent.name is "FeatureExtractor":
plt.plot(self.parent.result.subset(nsamples=self.nsamples, thresh=0.9).T)
elif self.parent.name is "Clustering":
"""labels = self.parent.result.predict(self.root.result).pack()
brainmap = Colorize(cmap=cmapCat).transform(labels[:,:,0])
plt.imshow(brainmap)
plt.show()"""
labels = self.parent.result.predict(self.parent.parent.result).pack()
brainmap = Colorize(cmap=cmapCat).transform(labels[:,:,0])
plt.figure(1)
plt.imshow(brainmap)
plt.figure(2)
#plt.show()
plt.plot(self.parent.result.centers.T)
plt.show()
self.result = self.parent.result
class BrainVisualizer(WorkflowNode):
def __init__(self, parent, root, slice):
super(BrainVisualizer, self).__init__(parent)
self.name = "Brain"
self.result = self.parent.result
self.root = root
self.slice = slice
def execute(self):
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
cmapCat = ListedColormap(sns.color_palette("hls", 10), name='from_list')
plt.gca().set_color_cycle(cmapCat.colors)
plt.imshow(self.root.result.first()[1][:, :, self.slice])
plt.show()
self.result = self.parent.result