def graphLoadInv(request, webargs=None): ''' Form ''' from glob import glob # Move # request.session.clear() # NEW if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data[ 'Select_Invariants_you_want_computed'] request.session['graphsize'] = form.cleaned_data[ 'Select_graph_size'] dataDir = os.path.join( settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join( dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) # Get all graphs in the directory graphs = glob(os.path.join(dataDir, '*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir, '*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir, '*_smgr.mat'))) else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': graph_fn = request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn return HttpResponseRedirect( "http://mrbrain.cs.jhu.edu" + dataDir.replace( ' ', '%20')) # All spaces are replaced with %20 for urls elif request.method == 'POST' and webargs: if (re.match(re.compile('(b|big)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'big' elif (re.match(re.compile('(s|small)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'small' else: return django.http.HttpResponseBadRequest( "The graph size is required as a web argument") dataDir = os.path.join( settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip) request.session['invariants'] = webargs.split('/')[1].split(',') graphs = glob(os.path.join(dataDir, '*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir, '*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir, '*_smgr.mat'))) request.session['graphInvariants'] = os.path.join( dataDir, 'graphInvariants') for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn # request.session.clear() dwnldLoc = "http://mrbrain.cs.jhu.edu" + dataDir.replace(' ', '%20') return HttpResponse("View Data at: " + dwnldLoc) else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext( request ) # Some failure to input data & returns a key signaling what is requested )
def graphLoadInv(request, webargs=None): ''' Form ''' from glob import glob # Move # request.session.clear() # NEW if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data['Select_Invariants_you_want_computed'] request.session['graphsize'] = form.cleaned_data['Select_graph_size'] dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) # Get all graphs in the directory graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': graph_fn = request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn return HttpResponseRedirect("http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20')) # All spaces are replaced with %20 for urls elif request.method == 'POST' and webargs: if (re.match(re.compile('(b|big)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'big' elif (re.match(re.compile('(s|small)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'small' else: return django.http.HttpResponseBadRequest("The graph size is required as a web argument") dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip) request.session['invariants'] = webargs.split('/')[1].split(',') graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn # request.session.clear() dwnldLoc = "http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20') return HttpResponse("View Data at: " + dwnldLoc) else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested )
def graphLoadInv(request, webargs=None): ''' Form ''' if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): request.session['graphsize'] = 'small' # This accounts for use LCC or not request.session['email'] = form.cleaned_data['email'] data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data['Select_Invariants_you_want_computed'] dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) graphs = glob(os.path.join(dataDir,'*')) # TODO: better way to make sure we are actually collecting graphs here else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) request.session['uploaded_graphs'] = graphs request.session['graph_format'] = form.cleaned_data['graph_format'] request.session['dataDir'] = dataDir request.session['email'] = form.cleaned_data['email'] # Launch thread for graphs & email user sendJobBeginEmail(request.session['email'], request.session['invariants'], genGraph=False) #asyncInvCompute(request) thr = threading.Thread(target=asyncInvCompute, args=(request,)) thr.start() request.session['success_msg'] = \ """ Your job was successfully launched. You should receive an email when your job begins and another one when it completes. The process may take several hours per graph (dependent on graph size) if you selected to compute all invariants. If you do not see an email in your INBOX check the SPAM folder and add [email protected] to your safe list. """ return HttpResponseRedirect(get_script_prefix()+'success') # Programmatic RESTful API elif request.method == 'POST' and webargs: dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] # Not necessarily a zip try: # Assume its a zip first zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip except: print "Non-zip file uploaded ..." graphs = glob(os.path.join(dataDir,'*')) try: request.session['invariants'] = webargs.split('/')[0].split(',') inGraphFormat = webargs.split('/')[1] except: return HttpResponse("Malformated input invariants list or graph format") request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') for graph_fn in graphs: invariant_fns = runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], inGraphFormat) print 'Computing Invariants for annoymous project %s complete...' % graph_fn err_msg="" if len(webargs.split('/')) > 2: err_msg = "" # __init__ err_msg = convert_graph(invariant_fns["out_graph_fn"], "graphml", request.session['graphInvariants'], *webargs.split('/')[2].split(',')) dwnldLoc = request.META['wsgi.url_scheme'] + '://' + \ request.META['HTTP_HOST'] + dataDir.replace(' ','%20') if err_msg: err_msg = "Completed with errors. View Data at: %s\n. Here are the errors:%s" % (dwnldLoc, err_msg) return HttpResponse(err_msg) return HttpResponse("View Data at: " + dwnldLoc) # Browser else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested )
def graphLoadInv(request, webargs=None): ''' Form ''' from glob import glob # Move if request.method == 'POST' and not webargs: form = GraphUploadForm(request.POST, request.FILES) # instantiating form if form.is_valid(): request.session['graphsize'] = form.cleaned_data['Select_graph_size'] request.session['graphsize'] = 'small' if not request.session['graphsize'] else request.session['graphsize'] data = form.files['fileObj'] # get data request.session['invariants'] = form.cleaned_data['Select_Invariants_you_want_computed'] dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') makeDirIfNone([dataDir]) # We got a zip if os.path.splitext(data.name)[1] == '.zip': writeBodyToDisk(data.read(), dataDir) # Get all graphs in the directory graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) else: # View only accepts .mat & zip as regulated by template graphs = [os.path.join(dataDir, data.name)] saveFileToDisk(data, graphs[0]) request.session['uploaded_graphs'] = graphs request.session['invConvertToFormats'] = form.cleaned_data['Convert_result'] request.session['dataDir'] = dataDir if request.session['graphsize'] == 'big': # Launch thread for big graphs & email user request.session['email'] = form.cleaned_data['Email'] sendJobBeginEmail(request.session['email'], request.session['invariants'], genGraph=False) thr = threading.Thread(target=asyncInvCompute, args=(request,)) thr.start() #asyncInvCompute(request) request.session['success_msg'] = "Your job was successfully launched. You should receive an email when your " request.session['success_msg'] += "job begins and another one when it completes. The process may take ~3hrs if you selected to compute all invariants" return HttpResponseRedirect(get_script_prefix()+'success') else: for graph_fn in graphs: graph_fn = request.session['smGrfn'] = graph_fn lcc_fn = None invariant_fns = runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn invConvertToFormats = form.cleaned_data['Convert_result'] # TODO: Make function for this. Duplicate of buildgraph code for fileFormat in invConvertToFormats: # Conversion of all files for inv in invariant_fns.keys(): if isinstance(invariant_fns[inv], list): # Case of eigs for fn in invariant_fns[inv]: convertTo.convertAndSave(fn, fileFormat, os.path.dirname(fn), inv) else: # case of all other invariants convertTo.convertAndSave(invariant_fns[inv], fileFormat, \ os.path.dirname(invariant_fns[inv]), inv) return HttpResponseRedirect("http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20')) # All spaces are replaced with %20 for urls elif request.method == 'POST' and webargs: if (re.match(re.compile('(b|big)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'big' elif (re.match(re.compile('(s|small)', re.IGNORECASE), webargs.split('/')[0])): request.session['graphsize'] = 'small' else: return django.http.HttpResponseBadRequest("The graph size is required as a web argument") dataDir = os.path.join(settings.MEDIA_ROOT, 'tmp', strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime())) makeDirIfNone([dataDir]) uploadedZip = writeBodyToDisk(request.body, dataDir)[0] zipper.unzip(uploadedZip, dataDir) # Unzip the zip os.remove(uploadedZip) # Delete the zip) request.session['invariants'] = webargs.split('/')[1].split(',') graphs = glob(os.path.join(dataDir,'*_fiber.mat')) graphs.extend(glob(os.path.join(dataDir,'*_bggr.mat'))) graphs.extend(glob(os.path.join(dataDir,'*_smgr.mat'))) request.session['graphInvariants'] = os.path.join(dataDir, 'graphInvariants') for graph_fn in graphs: if request.session['graphsize'] == 'big': request.session['bgGrfn'] = graph_fn lcc_fn = graph_fn.split('_')[0] + '_concomp.mat' elif request.session['graphsize'] == 'small': request.session['smGrfn'] = graph_fn lcc_fn = None runInvariants(request.session['invariants'], graph_fn, request.session['graphInvariants'], lcc_fn, request.session['graphsize']) print 'Invariants for annoymous project %s complete...' % graph_fn # request.session.clear() dwnldLoc = "http://mrbrain.cs.jhu.edu"+ dataDir.replace(' ','%20') return HttpResponse("View Data at: " + dwnldLoc) else: form = GraphUploadForm() # An empty, unbound form # Render the form return render_to_response( 'graphupload.html', {'graphUploadForm': form}, context_instance=RequestContext(request) # Some failure to input data & returns a key signaling what is requested )