# The autoreload extension is meant for developers.
# It ensures that libraries are reloaded when you run a cell
# that contains 'import' statements.
%load_ext autoreload
The web interface uses a python script movi_interface.py that deals with the communication with the HBP Morphology Viewer. It defines a class 'MoviInterface' which takes two important arguments:
import movi_interface as movi
%autoreload 2
# moviInterface will open the HBP Morphology Viewer in a new tab
moviInterface = movi.MoviInterface('moviInterface','https://neuroinformatics.nl/HBP/morphology-viewer-dev')
Make sure that the morphology file of the neuronToDisplay
-variable exists on your system, you can download it here. After running the following cell, you should see the neuron displayed in the Morphology Viewer tab, and get a notification when it is ready in this tab.
neuronToDisplay = '../samples/TCneuron_EP36-S1_9-9-17.DAT'
with open(neuronToDisplay,'rb') as fp:
moviCommand = {
"method":"MoVi.import",
"params": {
"name": neuronToDisplay,
"contents": fp.read()
}
}
moviInterface.send(moviCommand)
Make sure that the morphology file of the neuronToConvert
-variable exists on your system, you can download it here. After running the following cell, you should see the neuron displayed in the Morphology Viewer tab, get a notification in this tab when it is ready, and find the resulting XML file in your temporary files folder.
Method 1: Using moviInterface.awaitResponse
This method sends a command to the Morphology Viewer, then blocks the Python execution flow until a response is received. This method is recommended when the result is needed in the next cell. The downside of it is that notebook execution is stalled if the response fails to arrive.
import tempfile,os.path,uuid
import ipywidgets as widgets
neuronToConvert = '../samples/c10861.CNG.swc'
def saveConvertedNeuron(result):
if 'error' in result:
print('Error in saveConvertedNeuron: {}'.format(result['error']))
return
response = result['response']
convertedNeuronFile = os.path.join(tempfile.gettempdir(),os.path.basename(response['name']))
isText = type(response['contents']) == str
with open(convertedNeuronFile,'wt' if isText else 'wb') as fp:
fp.write(response['contents'])
print('Converted neuron file saved as "{}"'.format(convertedNeuronFile))
return convertedNeuronFile
with open(neuronToConvert,'rb') as fp:
moviCommand = {
"method":"MoVi.convert",
"params": {
"name": neuronToConvert,
"contents": fp.read(),
"toMime": "model/mbf.xml+gzip",
"doRender": True
}
}
print('Method 1 (wait for response)')
result = moviInterface.awaitResponse(moviCommand)
stdout = widgets.Output(layout={'border': '10px solid #FA0'})
with stdout:
convertedNeuronFile = saveConvertedNeuron(result)
display(stdout)
Method 1 (wait for response)
Output(layout=Layout(border='10px solid #FA0'))
Method 2: Using moviInterface.send with a callback
The positive side of this method is that it does not block the Python execution flow. But as a consequence, the result will only be ready after all pending Python commands are executed. That means that you cannot use the result of the callback in any of the next cells.
The callback should be the name of a function in the current scope, and this function should take the rpc-response as a single argument. Also note the use of an 'output widget' in the code below. This ensures that any output generated asynchronously will appear below the cell.
print('Method 2 (asynchronous callback)')
# To make the task more challenging, the conversion is now
# inverted, from gzipped XML to gzipped SWC
with open(convertedNeuronFile,'rb') as fp:
moviCommand = {
"method":"MoVi.convert",
"params": {
"name": convertedNeuronFile,
"contents": fp.read(),
"toMime": "model/swc+gzip",
"doRender": True
}
}
# For this method, the use of an output widget is essential.
# Without it, output and errors of the 'saveConvertedNeuron'
# callback will be invisible.
stdout = widgets.Output(layout={'border': '10px solid #FA0'})
def save(result):
# Callback function with a single parameter: the query response
with stdout:
saveConvertedNeuron(result)
moviInterface.send(moviCommand,'save')
with stdout:
print('Awaiting response...')
display(stdout)
Method 2 (asynchronous callback)
Output(layout=Layout(border='10px solid #FA0'))