Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tornado server #26

Open
wants to merge 88 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
066aaa5
ENH: Added visualization script sliced probability maps
demianw May 31, 2013
ccb1c4a
ENH: Added functionalities to the viz tool
demianw Jun 17, 2013
51d80cd
ENH: Added client side for web app, hardcoded
demianw Jun 20, 2013
920fc9c
ENH: First addition of the web application viz
demianw Jun 22, 2013
6ccd1d9
ENH: Added the capbility of adding or removing a tract from the viz
demianw Jun 22, 2013
585853d
ENH: Online refreshing works
demianw Jul 1, 2013
42b3988
ENH: freesurfer_queries.qry
demianw Jul 2, 2013
264559b
ENH: Added the requirements for the tornado server
demianw Jul 2, 2013
3277672
ENH: Port number configuration for the webserver
demianw Jul 2, 2013
bc01332
ENH: Updated requirements
demianw Jul 5, 2013
461bff6
ENH: Corrected path to the main queries for different installation sc…
demianw Jul 5, 2013
d9d1c53
Merge branch 'master' into tornado_server
demianw Jul 5, 2013
d938631
ENH: Added my fork of nibabel until the pull request is accepted
demianw Jul 5, 2013
874d9e8
RFR: Removing debug information
demianw Jul 5, 2013
9bcd7cf
Merge branch 'master' into tornado_server
demianw Jul 5, 2013
8586918
Merge branch 'master' into tornado_server
demianw Jul 7, 2013
1a303fc
Merge branch 'master' into tornado_server
demianw Jul 7, 2013
46c6d31
DOC: Added the tensor module to the documentation
demianw Jul 7, 2013
6316faa
Merge branch 'master' into tornado_server
demianw Jul 8, 2013
e2fcf55
BUG: Corrected bug in tract_math to handle several tracts on input
demianw Jul 8, 2013
74f5a32
Merge branch 'master' into tornado_server
demianw Jul 9, 2013
6725b96
Merge branch 'tornado_server' of github.com:demianw/tract_querier int…
demianw Jul 9, 2013
a9c10f1
Merge branch 'master' into tornado_server
demianw Jul 11, 2013
7d74b17
Merge branch 'master' into tornado_server
demianw Jul 11, 2013
a3bb703
Merge branch 'master' into tornado_server
demianw Jul 11, 2013
70f14e7
ENH: Added first attempt at prototype finding
demianw Jul 23, 2013
a288059
ENH: Added first attempt to project tracts onto prototype
demianw Jul 23, 2013
f135b6b
ENH: Added the tract_math function to export tracts
demianw Jul 26, 2013
3f8c0f1
Merge branch 'private_developement' into private
demianw Jul 26, 2013
60269b4
Merge branch 'tract_obb' into private
demianw Jul 26, 2013
3b746bc
BUG: Bug due to tract_math refactoring
demianw Jul 26, 2013
8eb14f7
Merge branch 'tract_obb' into private
demianw Jul 26, 2013
4640ecd
COMP: Changed for compatibilty with python 2.6
demianw Jul 26, 2013
5ffb080
Merge branch 'master' into private
demianw Jul 26, 2013
9ce0d4e
ENH: Added tensor measures and other tools to tract_math
demianw Jul 26, 2013
a559652
ENH: Added a tool to import values from a CSV file to a tractography
demianw Jul 26, 2013
9fc2e68
Merge branch 'master' into tornado_server
demianw Jul 29, 2013
817dbb3
Merge branch 'master' into private
demianw Jul 29, 2013
3d90e46
ENH: Refactored tract_math support
demianw Aug 12, 2013
d8d8281
Merge branch 'master' into private
demianw Aug 12, 2013
bdaac87
BUG: Corrected bug in refactoring
demianw Oct 10, 2013
f4ce2a0
BUG: Corrected problem with the tests
demianw Oct 10, 2013
3b8a478
BUG: Corrected bug for the 0-tract tractography case
demianw Oct 10, 2013
a45eca7
Merge branch 'master' into private
demianw Feb 10, 2014
4828d9c
ENH: fixed problem in tract math
demianw Feb 18, 2014
0b7187a
ENH: Added support for varargs in tract_math operations
demianw Feb 18, 2014
5b49740
ENH: Now the tractography from file stores the filename
demianw Feb 25, 2014
f6491b7
Merge branch 'master' into private
demianw Mar 5, 2014
301643a
ENH: Slices can be expressed in mm
demianw Mar 19, 2014
92a478d
ENH: Small modifications to tract_math operations
demianw Mar 19, 2014
0d3b135
Merge branch 'private' into tornado_server
demianw Mar 19, 2014
aa2b04c
ENH: Solving conflict when merging with master
demianw Apr 24, 2014
d5203af
BUG: Corrected merge problem
demianw Apr 25, 2014
fbbda46
Merge branch 'master' into tornado_server
demianw Apr 25, 2014
03ef08a
ENH: Bug derived from the merging
demianw Apr 27, 2014
f060b50
Merge branch 'master' into tornado_server
demianw Apr 30, 2014
fb9b906
Merge branch 'master' into tornado_server
demianw Jun 2, 2014
13e673d
ENH: Shell now can handle I/O redirection
demianw Jul 10, 2014
6272e60
ENH: First version of the in-web terminal works
demianw Jul 10, 2014
3af3dcf
ENH: Further interface improvements to the web app
demianw Jul 10, 2014
f3920b1
ENH: Made the shell more pythonic
demianw Jul 11, 2014
c026f56
ENH: Included the WMQL command line on the webpage
demianw Jul 11, 2014
cf91125
ENH: Making web application more websocks oriented
demianw Jul 11, 2014
e9fa22f
ENH: Corrected location of data folder following MHoude's suggestion
demianw Jul 11, 2014
edbc99f
ENH: Largely improved web application
demianw Jul 11, 2014
d609c24
ENH: Added command to see which tracts have been calculated on the we…
demianw Jul 11, 2014
6d103aa
ENH: Viewer improvements
demianw Jul 12, 2014
6836688
ENH: Enhanced the completion algorithm in the webserver
demianw Jul 12, 2014
785a357
ENH: Minor viz touches
demianw Jul 12, 2014
87c1b81
RFR: Cleaned unused files
demianw Jul 14, 2014
4b114cf
Merge branch 'master' into tornado_server
demianw Jul 14, 2014
b7d4f7b
ENH: Unbind the reset key from the viewer so the terminal works better
demianw Jul 15, 2014
bbc7fc7
ENH: Fixed terminal focus issues
demianw Jul 16, 2014
a0b8e11
ENH: Better error reporting on the WMQL web-based terminal
demianw Jul 18, 2014
9369ad6
Merge branch 'master' into tornado_server
demianw Jan 27, 2015
8fd091c
Merge branch 'master' into tornado_server
demianw Mar 27, 2015
643fef2
ENH: Updated to the latest nibabel version
demianw Mar 27, 2015
d73d53f
BUG: Fixed folder hiding when 'h' was pressed on the terminal
demianw Mar 27, 2015
3bbe118
ENH: Terminal now works better when window is resized
demianw Mar 27, 2015
0017494
ENH: Resizing of terminal and footer looks better and works
demianw Mar 27, 2015
cb7cd9d
Merge branch 'master' into tornado_server
demianw Apr 15, 2015
ae33577
Merge branch 'master' into tornado_server
demianw May 13, 2015
04c02ae
ENH: Better support for trackvis header
demianw May 13, 2015
358569b
Merge branch 'master' into tornado_server
demianw May 28, 2015
d031a0a
Merge branch 'master' into tornado_server
demianw Jun 12, 2015
c07c153
Merge branch 'master' into tornado_server
demianw Jul 27, 2015
d904a67
Merge branch 'master' into tornado_server
demianw Sep 4, 2015
d6da8ee
BUG: Corrected bug when translating from VTK to TRK files
demianw Nov 18, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
numpy>=1.6
nose>=1.2
tornado>=3.1
nibabel>=2.0
numpydoc
142 changes: 142 additions & 0 deletions scripts/sliced_probmap_viz
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env python
from pylab import *
import nibabel
import sys
from itertools import izip

import argparse


def main():
parser = argparse.ArgumentParser(description='Superimposed slices viz')
parser.add_argument('-s', dest='slices', type=float, nargs='+',
help='Slices to show')#, required=True)
parser.add_argument('-i', dest='base_image', type=str,
help='base image to show')#, required=True)
parser.add_argument('-j', dest='tract_image', type=str, nargs='+',
help='tract image to show')#, required=True)
parser.add_argument('-o', dest='output', type=str,
help='output image')#, required=True)
parser.add_argument('--invert', dest='inverse_order',
action='store_true', help='invert the slice order')
parser.add_argument('--step', dest='step',
action='store_true', help='Take the slices as `start end step`')
parser.add_argument('-t', dest='threshold', type=float, default=0,
help='lower threshold of the tract image')
parser.add_argument('--axis', dest='axis', type=int, default=2,
help='axis of the slices')
parser.add_argument('--mip', dest='mip', action='store_true',
help='show maximum intensity projections')

parser.add_argument('--mm', dest='mm', action='store_true',
help='slices are in millimeters')


parser.add_argument(
'--displacement', dest='displacement', type=float, default=.1,
help='displacement between axes between 0. and 1.')
parser.add_argument(
'--displacement_direction', dest='displacement_direction', type=str,
choices=('row', 'col'), default='row',
help='displacement direction: "row" or "col"')
parser.add_argument('--color', dest='color', type=str, nargs='+',
default='YlOrRd_r', help='Coloring for the tract (default %(default)s)')
parser.add_argument('--alpha', dest='alpha', type=float, nargs='+', default=1.,
help='Alpha for the probability maps')

parser.add_argument('--print-colors', dest='print_colors', action='store_true',
help='Print the list of colormaps')

args = parser.parse_args()

if args.print_colors:
from matplotlib import colors
maps = [
cm_ for cm_ in dir(cm)
if isinstance(getattr(cm, cm_), colors.Colormap)
]
max_length = max(len(cm_) for cm_ in maps) + 3
str_ = "{:<" + str(max_length) + "}"
for i in xrange(0, len(maps), 4):
maps_ = maps[i: i + 4]
str_line = str_ * len(maps_)
print str_line.format(*maps_)
parser.exit(0)

fig = figure(frameon=False)
fa_img = nibabel.load(args.base_image)
fa = fa_img.get_data()

if isinstance(args.tract_image, str):
args.tract_image = [args.tract_image]
tracts = [nibabel.load(tract_image).get_data() for tract_image in args.tract_image]

if isinstance(args.alpha, float):
args.alpha = [args.alpha]
if len(args.alpha) == 1:
args.alpha *= len(tracts)

if isinstance(args.color, str):
args.color = [args.color]
if len(args.color) == 1:
args.color *= len(tracts)

t_colormap = []
for colormap in args.color:
t_colormap.append(getattr(cm, colormap))
t_colormap[-1].set_under(alpha=0)

fa_cm = cm.gray
fa_cm.set_under(alpha=0)

if args.step:
args.slices = np.linspace(args.slices[0], args.slices[1], (args.slices[1] - args.slices[0]) / args.slices[2])

if args.mm:
ras_pos = zeros(4)
ras_pos[-1] = 1.
if any(np.diag(np.diag(fa_img.get_affine()[:3, :3])) != fa_img.get_affine()[:3, :3]):
raise ValueError("Affine matrix is not diagonal, we don't handle this case")
for i, s in enumerate(args.slices):
ras_pos[args.axis] = s
ijk_pos = np.linalg.solve(fa_img.get_affine(), ras_pos)
args.slices[i] = int(round(ijk_pos[args.axis]))
else:
args.slices = [int(s) for s in args.slices]

for i, y in enumerate(args.slices):
if args.inverse_order:
pos = len(args.slices) - 1 - i
else:
pos = i

if args.displacement_direction == 'row':
fig.add_axes([pos * args.displacement, 0, .5, .5], frameon=False)
else:
fig.add_axes([0, pos * args.displacement, .5, .5], frameon=False)
ax = gca()
ax.hold(True)

slices = [slice(None)] * 3
slices[args.axis] = y
slices = tuple(slices)

imshow(fa[slices].T, cmap=fa_cm, vmin=min(1e-10, fa[slices].min), origin=True, interpolation='spline36')
for tract, colormap in izip(tracts, t_colormap):
thr = min(tract.max() - 1e-10, args.threshold)
if args.mip:
tract_slice = tract.max(args.axis).T
else:
tract_slice = tract[slices].T
imshow(
tract_slice, cmap=colormap,
vmin=thr, vmax=tract.max(), origin=True, interpolation='gaussian'
)
xticks([])
yticks([])
ax.hold(False)
savefig(args.output, bbox_inches='tight', transparent=True, pad_inches=0)


if __name__ == '__main__':
sys.exit(main())
2 changes: 1 addition & 1 deletion scripts/tract_math
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def tract_math_operation(help_text, needs_one_tract=True):


def main():
from tract_querier.tract_math import operations
from tract_querier.tract_math import tract_math_operations as operations
from tract_querier.tract_math import TractMathWrongArgumentsError

usage = r"""
Expand Down
74 changes: 68 additions & 6 deletions scripts/tract_querier
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ def main():
help="Bounding box to apply to the image affine transform and tracts "
"to put both in AC-PC coordinate space"
)
parser.add_option(
'--interactive-webdisplay', dest='interactive_web', action='store_true',
help='Enables interactive display of the results'
)
parser.add_option(
'--interactive-colortable', dest='interactive_colortable', type=str,
help='Atlas colortable for the webgl viz'
)
parser.add_option(
'--interactive-webdisplay-hostname', dest='interactive_hostname',
help='Server port for the interactive display of the results: default: $(default)s',
default='localhost'
)

parser.add_option(
'--interactive-webdisplay-port', dest='interactive_port',
help='Server port for the interactive display of the results: default $(default)04d',
default=9999
)

(options, args) = parser.parse_args()

Expand All @@ -61,6 +80,9 @@ def main():

import tract_querier

if options.interactive_web:
options.interactive = True

if options.bounding_box_affine_transform:
try:
bounding_box_affine_transform = np.fromstring(
Expand Down Expand Up @@ -139,7 +161,19 @@ def main():
options.tractography_file_name
)

tractography_extension = os.path.splitext(options.tractography_file_name)[-1]
input_split = os.path.splitext(options.tractography_file_name)
output_split = os.path.splitext(options.output_file_name)
if len(output_split) > 1:
options.output_file_name = output_split[0]
tractography_extension = output_split[1]

if options.interactive_web is not None and tractography_extension != '.trk':
print (
"The web application only works with Trackvis output files"
" setting the output to Trackvis mode"
)
tractography_extension = '.trk'

if tractography_extension == '.trk':
tractography_extra_kwargs = {
'affine': tr.affine,
Expand Down Expand Up @@ -203,13 +237,11 @@ def main():
extension=tractography_extension, extra_kwargs=tractography_extra_kwargs
)
else:
query_save = (
lambda query_name, query_result:
save_query(
def query_save(query_name, query_result):
return save_query(
query_name, tr, options, {query_name: query_result},
extension=tractography_extension, extra_kwargs=tractography_extra_kwargs
)
)

interactive_shell = tract_querier.TractQuerierCmd(
tractography_spatial_indexing,
Expand All @@ -218,7 +250,10 @@ def main():
include_folders=qry_search_folders
)

interactive_shell.cmdloop()
if options.interactive_web:
activate_webserver(options, interactive_shell)
else:
interactive_shell.cmdloop()


def save_query(query_name, tractography, options, evaluated_queries, extension='.vtk', extra_kwargs={}):
Expand Down Expand Up @@ -272,6 +307,33 @@ def save_tractography_file(
)


def activate_webserver(options, shell):
import os
from tract_querier import tornado_server
from uuid import uuid4
suffix = str(uuid4()).replace('-', '')
options.interactive = True

options.interactive_port = int(options.interactive_port)

print "Starting web server, please direct your browser to http://%s:%04d" % (
options.interactive_hostname,
options.interactive_port
)

tornado_server.xtk_server(
atlas=os.path.abspath(options.atlas_file_name),
colortable=options.interactive_colortable,
hostname=options.interactive_hostname,
port=options.interactive_port,
files_path=os.getcwd(),
suffix=suffix,
shell=shell
)




def affine_transform_tract(affine_transform, tract):
import numpy as np

Expand Down
19 changes: 14 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ def configuration(parent_package='', top_path=None):


if __name__ == "__main__":
requires_base = open('requirements.txt').readlines()
requires = []
for i, req in enumerate(requires_base):
if req.strip().startswith('#') or req.strip().startswith('-'):
continue
req = req.strip()
if '>' in req:
req = req.replace('>', '(>')
req += ')'
requires.append(req)

setup(
name=DISTNAME,
maintainer=MAINTAINER,
Expand All @@ -34,10 +45,7 @@ def configuration(parent_package='', top_path=None):
version=VERSION,
download_url=DOWNLOAD_URL,
long_description=LONG_DESCRIPTION,
requires=[
'numpy(>=1.6)',
'nibabel(>=1.3)'
],
requires=requires,
classifiers=[
'Intended Audience :: Science/Research',
'Programming Language :: Python',
Expand All @@ -49,7 +57,8 @@ def configuration(parent_package='', top_path=None):
],
scripts=[
'scripts/tract_querier',
'scripts/tract_math'
'scripts/tract_math',
'scripts/sliced_probmap_viz',
],
**(configuration().todict())
)
1 change: 1 addition & 0 deletions tract_querier/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def configuration(parent_package='', top_path=None):
config.add_subpackage('code_util')
config.add_subpackage('tensor')
config.add_subpackage('nipype')
config.add_subpackage('tornado_server')
config.add_data_files(('queries', [
'data/FreeSurfer.qry',
'data/JHU_MNI_SS_WMPM_Type_I.qry',
Expand Down
Loading