-
Notifications
You must be signed in to change notification settings - Fork 10
/
pyneal.py
302 lines (240 loc) · 10.9 KB
/
pyneal.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"""Pyneal Real-time fMRI Acquisition and Analysis
This is the main Pyneal application, designed to be called directly from the
command line on the computer designated as your real-time analysis machine.
It expects to receive incoming slice data from the Pyneal-Scanner application,
which should be running concurrently elsewhere (e.g. on the scanner console
itself)
Once this application is called, it'll take care of opening the
GUI, loading settings, launching separate threads for monitoring and analyzing
incoming data, and hosting the Analysis output server
"""
# python 2/3 compatibility
from __future__ import print_function
import os
import sys
from os.path import join
import glob
import time
import argparse
import subprocess
import atexit
import webbrowser as web
import yaml
import nibabel as nib
import numpy as np
import zmq
from src.pynealLogger import createLogger
from src.scanReceiver import ScanReceiver
from src.pynealPreprocessing import Preprocessor
from src.pynealAnalysis import Analyzer
from src.resultsServer import ResultsServer
import src.GUIs.pynealSetup.setupGUI as setupGUI
# Set the Pyneal Root dir based on where this file lives
pynealDir = os.path.abspath(os.path.dirname(__file__))
def launchPyneal(headless=False, customSettingsFile=None):
"""Main Pyneal Loop.
This function will launch setup GUI, retrieve settings, initialize all
threads, and start processing incoming scans
"""
### Read Settings ------------------------------------
# Read the settings file, and launch the setup GUI to give the user
# a chance to update the settings. Hitting 'submit' within the GUI
# will update the setupConfig file with the new settings
if customSettingsFile:
print('Loading custom settings file: {}'.format(customSettingsFile))
settingsFile = customSettingsFile
else:
settingsFile = join(pynealDir, 'src/GUIs/pynealSetup/setupConfig.yaml')
if not headless:
# Launch GUI to let user update the settings file
setupGUI.launchPynealSetupGUI(settingsFile)
elif headless:
print('Running headless...')
print('Using settings in {}'.format(settingsFile))
assert os.path.exists(settingsFile), 'Running headless, but settings file does not exist: {}'.format(settingsFile)
# Read the new settings file, store as dict
with open(settingsFile, 'r') as ymlFile:
settings = yaml.safe_load(ymlFile)
### Create the output directory, put in settings dict
outputDir = createOutputDir(settings['outputPath'])
settings['seriesOutputDir'] = outputDir
### Set Up Logging ------------------------------------
# The createLogger function will do a lot of the formatting set up
# behind the scenes. You can write to this log by calling the
# logger var and specifying the level, e.g.: logger.debug('msg')
# Other modules can write to this same log by calling
# the command: logger = logging.getLogger('PynealLog')
logFname = join(outputDir, 'pynealLog.log')
logger = createLogger(logFname)
print('Logs written to: {}'.format(logFname))
# write all settings to log
for k in settings:
logger.info('Setting: {}: {}'.format(k, settings[k]))
print('-'*20)
### Launch Threads -------------------------------------
# Scan Receiver Thread, listens for incoming volume data, builds matrix
scanReceiver = ScanReceiver(settings)
scanReceiver.daemon = True
scanReceiver.start()
logger.debug('Starting Scan Receiver')
# Results Server Thread, listens for requests from end-user (e.g. task
# presentation), and sends back results
resultsServer = ResultsServer(settings)
resultsServer.daemon = True
resultsServer.start()
logger.debug('Starting Results Server')
### Create processing objects --------------------------
# Class to handle all preprocessing
preprocessor = Preprocessor(settings)
# Class to handle all analysis
analyzer = Analyzer(settings)
### Launch Real-time Scan Monitor GUI
if settings['launchDashboard']:
### launch the dashboard app as its own separate process. Once called,
# it will set up a zmq socket to listen for inter-process messages on
# the 'dashboardPort', and will host the dashboard website on the
# 'dashboardClientPort'
pythonExec = sys.executable # path to the local python executable
p = subprocess.Popen([
pythonExec,
join(pynealDir,
'src/GUIs/pynealDashboard/pynealDashboard.py'),
str(settings['dashboardPort']),
str(settings['dashboardClientPort'])
])
# Set up the socket to communicate with the dashboard server
dashboardContext = zmq.Context.instance()
dashboardSocket = dashboardContext.socket(zmq.REQ)
dashboardSocket.connect('tcp://127.0.0.1:{}'.format(settings['dashboardPort']))
# make sure subprocess and dashboard ports get killed at close
atexit.register(cleanup, p, dashboardContext)
# Open dashboard in browser
# s = '127.0.0.1:{}'.format(settings['dashboardClientPort'])
# print(s)
# web.open('127.0.0.1:{}'.format(settings['dashboardClientPort']))
# send configuration settings to dashboard
configDict = {'mask': os.path.split(settings['maskFile'])[1],
'analysisChoice': (settings['analysisChoice'] if settings['analysisChoice'] in ['Average', 'Median'] else 'Custom'),
'volDims': str(nib.load(settings['maskFile']).shape),
'numTimepts': settings['numTimepts'],
'outputPath': outputDir}
sendToDashboard(dashboardSocket,
topic='configSettings',
content=configDict)
### Wait For Scan To Start -----------------------------
while not scanReceiver.scanStarted:
time.sleep(.5)
logger.debug('Scan started')
### Set up remaining configuration settings after first volume arrives
while not scanReceiver.completedVols[0]:
time.sleep(.1)
preprocessor.set_affine(scanReceiver.get_affine())
### Process scan -------------------------------------
# Loop over all expected volumes
for volIdx in range(settings['numTimepts']):
### make sure this volume has arrived before continuing
while not scanReceiver.completedVols[volIdx]:
time.sleep(.1)
### start timer
startTime = time.time()
### Retrieve the raw volume
rawVol = scanReceiver.get_vol(volIdx)
### Preprocess the raw volume
preprocVol = preprocessor.runPreprocessing(rawVol, volIdx)
### Analyze this volume
result = analyzer.runAnalysis(preprocVol, volIdx)
# send result to the resultsServer
resultsServer.updateResults(volIdx, result)
### Calculate processing time for this volume
elapsedTime = time.time() - startTime
# update dashboard (if dashboard is launched)
if settings['launchDashboard']:
# completed volIdx
sendToDashboard(dashboardSocket, topic='volIdx', content=volIdx)
# timePerVol
timingParams = {'volIdx': volIdx,
'processingTime': np.round(elapsedTime, decimals=3)}
sendToDashboard(dashboardSocket, topic='timePerVol',
content=timingParams)
### Save output files
resultsServer.saveResults()
scanReceiver.saveResults()
### Figure out how to clean everything up nicely at the end
resultsServer.killServer()
scanReceiver.killServer()
def sendToDashboard(dashboardSocket, topic=None, content=None):
""" Send a message to the dashboard
Construct a JSON message using the supplied `topic` and `content`, and send
it out over the `dashboardSocket` object
Parameters:
-----------
dashboardSocket : zmq socket object
Instance of dashboard socket class
topic : string
Topic type of message to send to Pyneal dashboard. Must be one of the
expected topic types in order for the dashboard to make sense of it.
(See: src/GUIs/pynealDashboard/pynealDashboard.py)
content :
The actual content you want sent to the dashboard. The `content`
dtype will vary depending on topic
"""
if topic is None:
raise Exception('Dashboard message has topic set to None')
if content is None:
raise Exception('Dashboard message has content set to None')
# format the message to send to dashboard
msg = {'topic': topic, 'content': content}
# send
dashboardSocket.send_json(msg)
#logger.debug('sent to dashboard: {}'.format(msg))
# recv the response (should just be 'success')
response = dashboardSocket.recv_string()
if response != 'success':
print(response)
raise Exception('Could not send this dashboard: {}'.format(msg))
#logger.debug('response from dashboard: {}'.format(response))
def createOutputDir(parentDir):
"""Create a new output directory
A new output subdirectory will be created in the supplied parent dir.
Output directories are named sequentially, starting with pyneal_001. This
function will find all existing pyneal_### directories in the `parentDir`
and name the new output directory accordingly.
Parameters:
-----------
parentDir : string
full path to the parent directory where you'd like the new output
subdirectory to appear
Returns:
--------
string
full path to the new output subdirectory
"""
# find any existing pyneal_### directories, create the next one in series
existingDirs = glob.glob(join(parentDir, 'pyneal_*'))
if len(existingDirs) == 0:
outputDir = join(parentDir, 'pyneal_001')
else:
# add 1 to highest numbered existing dir
nextDirNum = int(sorted(existingDirs)[-1][-3:]) + 1
outputDir = join(parentDir, 'pyneal_' + str(nextDirNum).zfill(3))
# create the output dir and return full path to it
os.makedirs(outputDir)
return outputDir
def cleanup(pid, context):
# kill dashboard server subprocess
print('stopping dashboard subprocess')
pid.terminate()
# kill dashboard client server
context.destroy()
### ----------------------------------------------
if __name__ == '__main__':
# Start Pyneal by calling pyneal.py from the command line
parser = argparse.ArgumentParser()
parser.add_argument('--noGUI',
action='store_true',
help="run in headless mode, no setup GUI. (requires a valid settings file, either supplied here with -s option, or in {pyneal root}/src/GUIs/pynealSetup/setupConfig.yaml)")
parser.add_argument('-s', '--settingsFile',
default=None,
help="specify the path to a custom settings file")
args = parser.parse_args()
launchPyneal(headless=args.noGUI, customSettingsFile=args.settingsFile)