예제 #1
0
def graphLoadInv(request):
    ''' Form '''
    if request.method == 'POST':
        form = GraphUploadForm(request.POST,
                               request.FILES)  # instantiating form
        if form.is_valid():

            data = form.files['fileObj']  # get data
            invariants = form.cleaned_data[
                'Select_Invariants_you_want_computed']

            data_dir = os.path.join(
                settings.MEDIA_ROOT, 'public',
                strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime()))
            invariants_path = os.path.join(data_dir, 'graphInvariants')

            makeDirIfNone([data_dir])

            # We got a zip
            if os.path.splitext(data.name)[1] == '.zip':
                writeBodyToDisk(data.read(), data_dir)
                graphs = glob(os.path.join(data_dir, '*'))

            else:  # View only accepts a subset of file formats as regulated by template Validate func
                graphs = [os.path.join(data_dir, data.name)]
                saveFileToDisk(data, graphs[0])

            request.session['graph_format'] = form.cleaned_data['graph_format']
            request.session['email'] = form.cleaned_data['email']

            # Launch thread for graphs & email user
            sendJobBeginEmail(request.session['email'],
                              invariants,
                              genGraph=False)

            for graph_fn in graphs:
                task_invariant_compute.delay(invariants, graph_fn,
                                             invariants_path, data_dir,
                                             form.cleaned_data['graph_format'],
                                             request.session['email'])

            request.session['success_msg'] = \
      """
Your job successfully launched. You should receive an email when your job begins and another one when it completes.<br/>
The process may take several hours (dependent on graph size) if you selected to compute all invariants.<br/>
If you do not see an email in your <i>Inbox</i> check the <i>Spam</i> folder and add <code>[email protected]</code> 
to your safe list.
"""
            return HttpResponseRedirect(get_script_prefix() + 'success')
    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
    )
예제 #2
0
def graphLoadInv(request):
  ''' Form '''
  if request.method == 'POST':
    form = GraphUploadForm(request.POST, request.FILES) # instantiating form
    if form.is_valid():

      data = form.files['fileObj'] # get data
      invariants = form.cleaned_data['Select_Invariants_you_want_computed']

      data_dir = os.path.join(settings.MEDIA_ROOT, 'public', 
              strftime("projectStamp%a%d%b%Y_%H.%M.%S/", localtime()))
      invariants_path = os.path.join(data_dir, 'graphInvariants')

      makeDirIfNone([data_dir])

      # We got a zip
      if os.path.splitext(data.name)[1] == '.zip':
        writeBodyToDisk(data.read(), data_dir)
        graphs = glob(os.path.join(data_dir,'*')) 

      else: # View only accepts a subset of file formats as regulated by template Validate func
        graphs = [os.path.join(data_dir, data.name)]
        saveFileToDisk(data, graphs[0])

      request.session['graph_format'] = form.cleaned_data['graph_format']
      request.session['email'] = form.cleaned_data['email']

      # Launch thread for graphs & email user
      sendJobBeginEmail(request.session['email'], invariants, genGraph=False)

      for graph_fn in graphs:
        task_invariant_compute.delay(invariants, graph_fn, invariants_path, 
                data_dir, form.cleaned_data['graph_format'], request.session['email'])

      request.session['success_msg'] = \
"""
Your job successfully launched. You should receive an email when your job begins and another one when it completes.<br/>
The process may take several hours (dependent on graph size) if you selected to compute all invariants.<br/>
If you do not see an email in your <i>Inbox</i> check the <i>Spam</i> folder and add <code>[email protected]</code> 
to your safe list.
"""
      return HttpResponseRedirect(get_script_prefix()+'success')
  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
  )
예제 #3
0
def raw_upload(request):

  if request.method == "POST":
    form = RawUploadForm(request.POST, request.FILES) # instantiating form
    if form.is_valid():
      # TODO: Alter save path
      data_dir = os.path.join(settings.MEDIA_ROOT, "c4", 
                strftime("%a%d%b%Y_%H.%M.%S/", localtime()), "derivatives")

      dti = form.cleaned_data["dti"]
      mprage = form.cleaned_data["mprage"]
      bvalue = form.cleaned_data["bvalue"]
      bvector = form.cleaned_data["bvector"]

      # Save all derivatives
      for _file in dti, mprage, bvalue, bvector:
        saveFileToDisk(_file, os.path.join(data_dir, _file.name))

      ru_model = RawUploadModel()
      ru_model.dtipath = os.path.join(data_dir, dti.name)
      ru_model.mpragepath = os.path.join(data_dir, mprage.name)
      ru_model.atlas = form.cleaned_data["atlas"]
      ru_model.graphsize = "big" if form.cleaned_data["graphsize"] == True else "small"
      ru_model.email = form.cleaned_data["email"] 
      ru_model.save() # Sync to Db

      task_runc4.delay(ru_model.dtipath, ru_model.mpragepath,
          os.path.join(data_dir, bvalue.name), os.path.join(data_dir, bvector.name), 
          form.cleaned_data["graphsize"], ru_model.atlas, form.cleaned_data["email"])

      sendEmail(form.cleaned_data["email"], "MR Images to graphs job started", 
              "Hello,\n\nYour job launched successfully. You will receive another email upon completion.\n\n")

      request.session["success_msg"] =\
"""
Your job successfully launched. You should receive an email to confirm launch
and another when it upon job completion. <br/> The process can take <i>several hours</i> in some cases.
"""
      return HttpResponseRedirect(get_script_prefix()+"success")

  else:
    form = RawUploadForm() # An empty, unbound form

  return render_to_response(
    "c4.html",
    {"RawUploadForm": form},
    context_instance=RequestContext(request)
    )
예제 #4
0
def convert_graph(request):
    if (request.method == 'POST'):
        form = ConvertForm(request.POST, request.FILES)  # instantiating form
        if form.is_valid():
            upload_fn = form.files['fileObj'].name
            save_dir, convert_file_save_loc = getworkdirs()
            saved_file = os.path.join(save_dir, upload_fn)
            saveFileToDisk(form.files['fileObj'], saved_file)

            # If zip is uploaded
            if os.path.splitext(upload_fn)[1].strip() == '.zip':
                unzip(saved_file, save_dir)
                # Delete zip so its not included in the graphs we uploaded
                os.remove(saved_file)
                uploaded_files = glob(os.path.join(
                    save_dir, "*"))  # get the uploaded file names
            else:
                uploaded_files = [saved_file]

            #Browser """
            task_convert.delay(settings.MEDIA_ROOT, uploaded_files,
                               convert_file_save_loc,
                               form.cleaned_data['input_format'],
                               form.cleaned_data['output_format'],
                               form.cleaned_data["Email"])
            request.session['success_msg'] = \
      """
Your job successfully launched. You should receive an email when your job begins and another one when it completes.<br/>
The process may take several hours (dependent on graph size). If your job fails you will receive an email notification as well.<br/>
If you do not see an email in your <i>Inbox</i> check the <i>Spam</i> folder and add <code>[email protected]</code> to your safe list.
"""
        return HttpResponseRedirect(get_script_prefix() + 'success')
    else:
        form = ConvertForm()  # An empty, unbound form

    # Render the form
    return render_to_response('convertupload.html', {'convertForm': form},
                              context_instance=RequestContext(request))
예제 #5
0
def convert_graph(request):
  if (request.method == 'POST'):
    form = ConvertForm(request.POST, request.FILES) # instantiating form
    if form.is_valid():
      upload_fn = form.files['fileObj'].name
      save_dir, convert_file_save_loc = getworkdirs()
      saved_file = os.path.join(save_dir, upload_fn)
      saveFileToDisk(form.files['fileObj'], saved_file)

      # If zip is uploaded
      if os.path.splitext(upload_fn)[1].strip() == '.zip':
        unzip(saved_file, save_dir)
        # Delete zip so its not included in the graphs we uploaded
        os.remove(saved_file)
        uploaded_files = glob(os.path.join(save_dir, "*")) # get the uploaded file names
      else:
        uploaded_files = [saved_file]

      #Browser """
      task_convert.delay(settings.MEDIA_ROOT, uploaded_files, convert_file_save_loc,
      form.cleaned_data['input_format'], form.cleaned_data['output_format'],
      form.cleaned_data["Email"])
      request.session['success_msg'] = \
"""
Your job successfully launched. You should receive an email when your job begins and another one when it completes.<br/>
The process may take several hours (dependent on graph size). If your job fails you will receive an email notification as well.<br/>
If you do not see an email in your <i>Inbox</i> check the <i>Spam</i> folder and add <code>[email protected]</code> to your safe list.
"""
    return HttpResponseRedirect(get_script_prefix()+'success')
  else:
    form = ConvertForm() # An empty, unbound form

  # Render the form
  return render_to_response(
      'convertupload.html',
      {'convertForm': form},
      context_instance=RequestContext(request))
예제 #6
0
파일: download.py 프로젝트: htygithub/m2g
def download(request):
  MAX_NUM_GRAPH_DLS = 1
  ATLASES = {"desikan": os.path.join(settings.ATLAS_DIR, "desikan_atlas.nii") ,
              "slab": os.path.join(settings.ATLAS_DIR, "slab_atlas.nii")}

  if request.method == "POST":
    if request.POST.keys()[0] == "query_type": # Means we are doing a search
      form = DownloadQueryForm(request.POST)
      if form.is_valid():
        gdmof = GraphDownloadModel.objects.filter # typedef
        st = str(".*"+ ".*".join(form.cleaned_data["query"].strip().split()) +".*") # Search Term

        if form.cleaned_data["query_type"] == "all":
          table = GraphTable(
              gdmof(genus__iregex=st)      | gdmof(filepath__iregex=st) |
              gdmof(region__iregex=st)     | gdmof(numvertex__iregex=st)|
              gdmof(numedge__iregex=st)    | gdmof(graphattr__iregex=st)|
              gdmof(vertexattr__iregex=st) |  gdmof(edgeattr__iregex=st)|
              gdmof(sensor__iregex=st)     | gdmof(source__iregex=st)   |
              gdmof(project__iregex=st)
           )
        elif form.cleaned_data["query_type"] == "attribute":
          table = GraphTable(
              gdmof(graphattr__iregex=st)| gdmof(vertexattr__iregex=st)|
              gdmof(edgeattr__iregex=st)
           )
        elif form.cleaned_data["query_type"] == "name":
          table = GraphTable( gdmof(filepath__iregex=st) )
        elif form.cleaned_data["query_type"] == "genus":
          table = GraphTable( gdmof(genus__iregex=st) )
        elif form.cleaned_data["query_type"] == "region":
          table = GraphTable( gdmof(region__iregex=st) )
        elif form.cleaned_data["query_type"] == "project":
          table = GraphTable( gdmof(project__iregex=st) )

        # NOTE: Or equal to as well
        elif form.cleaned_data["query_type"] == "numvertex_gt":
          table = GraphTable( gdmof(numvertex__gte=int(form.cleaned_data["query"])) )
        elif form.cleaned_data["query_type"] == "numedge_gt":
          table = GraphTable( gdmof(numedge__gte=int(form.cleaned_data["query"])) )

        elif form.cleaned_data["query_type"] == "numvertex_lt":
          table = GraphTable( gdmof(numvertex__lte=int(form.cleaned_data["query"])) )
        elif form.cleaned_data["query_type"] == "numedge_lt":
          table = GraphTable( gdmof(numedge__lte=int(form.cleaned_data["query"])) )

        elif form.cleaned_data["query_type"] == "sensor":
          table = GraphTable( gdmof(sensor__iregex=st) )
        elif form.cleaned_data["query_type"] == "source":
          table = GraphTable( gdmof(source__iregex=st) )

        if (len(table.rows) == 0):
          table = None # Get the no results message to show up
        else:
          table.set_html_name("Search Results")

        return render_to_response("downloadgraph.html", {"genera":[],
          "query_result":table}, context_instance=RequestContext(request))
      else:
        return HttpResponseRedirect(get_script_prefix()+"download")

    else: # We just want to download specific files

      form = DownloadGraphsForm(request.POST)

      if form.is_valid():
        selected_files = request.POST.getlist("selection")
        ds_factor = 0 if not request.POST.get("ds_factor") else request.POST.get("ds_factor")

        if ds_factor not in ATLASES.keys():
          ds_factor = int(ds_factor)

        dl_format = form.cleaned_data["dl_format"]

        if not selected_files:
          return HttpResponseRedirect(get_script_prefix()+"download")

        # Something selected for dl/Convert+dl
        else:
          data_dir = os.path.join(settings.MEDIA_ROOT, "public",
                                 strftime("download_%a%d%b%Y_%H.%M.%S/", localtime()))
          dwnld_loc = get_download_path(data_dir)

          sendEmail(form.cleaned_data["Email"], "Job launch notification",
                  "Your download request was received. You will receive an email when it completes.\n\n")

          # Testing only
          task_scale.delay(selected_files, dl_format, ds_factor, ATLASES, form.cleaned_data["Email"], 
          dwnld_loc, os.path.join(data_dir, "archive.zip")) # Testing only

          request.session['success_msg'] = \
"""
Your job successfully launched. You should receive an email when your job begins and another 
one when it completes.<br/> The process may take several hours (dependent on graph size). 
If your job fails you will receive an email notification as well.<br/>
If you do not see an email in your <i>Inbox</i> check the <i>Spam</i> folder and add 
<code>[email protected]</code> to your safe list.
"""
          return HttpResponseRedirect(get_script_prefix()+'success')
      else:
        return HttpResponseRedirect(get_script_prefix()+"download")

  else:
    tbls = []
    for genus in settings.GENERA:
      table = GraphTable(GraphDownloadModel.objects.filter(genus=genus))
      table.set_html_name(genus.capitalize()) # Set the html __repr__
      # TODO: Alter per_page limit to +25
      RequestConfig(request, paginate={"per_page":25}).configure(table) # Each table re-render given a request
      #table.columns["url"].header = "Download Link"

      dl_form = DownloadGraphsForm()
      dl_form.set_name(genus)

      tbls.append((table, dl_form))

  return render_to_response("downloadgraph.html", {"genera":tbls, "query":DownloadQueryForm()},
                            context_instance=RequestContext(request))
예제 #7
0
def buildGraph(request):

    error_msg = ""

    if request.method == "POST":
        form = BuildGraphForm(request.POST,
                              request.FILES)  # instantiating form
        if form.is_valid():

            # Acquire proj names
            proj_dir = form.cleaned_data["UserDefprojectName"]
            site = form.cleaned_data["site"]
            subject = form.cleaned_data["subject"]
            session = form.cleaned_data["session"]
            scanId = form.cleaned_data["scanId"]
            """
      # Private project error checking
      if (form.cleaned_data["Project_Type"] == "private"):
        if not request.user.is_authenticated():
          error_msg = "You must be logged in to make/alter a private project! \
              Please Login or make/alter a public project."

        # Untested TODO: Add join to ensure it a private project
        elif BuildGraphModel.objects.filter(owner=request.user, \
            project_name=proj_dir, site=site, subject=subject, \
            session=session, scanId=scanId).exists():

           error_msg = "The scanID you requested to create already \
               exists in this project path. Please change any of the form values."
      """
            if error_msg:
                return render_to_response(
                    "buildgraph.html", {
                        "buildGraphform": form,
                        "error_msg": error_msg
                    },
                    context_instance=RequestContext(request))
            """
      # If a user is logged in associate the project with thier directory
      if form.cleaned_data["Project_Type"] == "private":
        proj_dir = os.path.join(request.user.username, proj_dir)
      else:
      """
            proj_dir = os.path.join("public", proj_dir)

            # Adapt project name if necesary on disk
            proj_dir = adaptProjNameIfReq(
                os.path.join(
                    settings.MEDIA_ROOT,
                    proj_dir))  # Fully qualify AND handle identical projects

            usrDefProjDir = os.path.join(proj_dir, site, subject, session,
                                         scanId)
            """ Define data directory paths """
            derivatives, graphs = defDataDirs(usrDefProjDir)

            # Create a model object to save data to DB

            grModObj = BuildGraphModel(
                project_name=form.cleaned_data["UserDefprojectName"])
            grModObj.location = usrDefProjDir  # The particular scan location

            grModObj.site = site
            grModObj.subject = subject
            grModObj.session = session
            grModObj.scanId = scanId

            if request.user.is_authenticated():
                grModObj.owner = request.user  # Who created the project

            invariants = form.cleaned_data["invariants"]
            graph_size = form.cleaned_data["graph_size"]
            email = form.cleaned_data["email"]

            if graph_size == "big" and not email:
                return render_to_response(
                    "buildgraph.html", {
                        "buildGraphform":
                        form,
                        "error_msg":
                        "Email address must be \
              provided when processing big graphs due to http timeout's possibly occurring."
                    },
                    context_instance=RequestContext(request))
            """ Acquire fileNames """
            fiber_fn = form.cleaned_data[
                "fiber_file"].name  # get the name of the file input by user
            if form.cleaned_data["data_atlas_file"]:
                data_atlas_fn = form.cleaned_data["data_atlas_file"].name
                print "Storing data atlas ..."
                saveFileToDisk(form.cleaned_data["data_atlas_file"],
                               os.path.join(derivatives, data_atlas_fn))

            print "Storing fibers ..."
            """ Save files in appropriate location """
            saveFileToDisk(form.cleaned_data["fiber_file"],
                           os.path.join(derivatives, fiber_fn))
            grModObj.save()  # Save project data to DB after file upload

            # add entry to owned project
            if request.user.is_authenticated():
                ownedProjModObj = OwnedProjects(project_name=grModObj.project_name, \
                  owner=grModObj.owner, is_private=form.cleaned_data["Project_Type"] == "private")
                ownedProjModObj.save()

            print "\nSaving all files complete..."

            # Make appropriate dirs if they dont already exist
            create_dir_struct([derivatives, graphs])

            # TEST #
            """
      derivatives = "/home/disa/test/build_test"
      graphs = derivatives
      proj_dir = derivatives
      invariant_loc = derivatives 
      # END TEST #
      """

            sendJobBeginEmail(email, invariants)
            task_build.delay(derivatives, graphs, graph_size, invariants,
                             derivatives, email)
            request.session["success_msg"] =\
      """
Your job successfully launched. You should receive an email to confirm launch
and another when it upon job completion. <br/>
<i>The process may take several hours</i> if you selected to compute all invariants.
"""
            return HttpResponseRedirect(get_script_prefix() + "success")

    else:
        form = BuildGraphForm()  # An empty, unbound form

    # Render the form
    return render_to_response("buildgraph.html", {"buildGraphform": form},
                              context_instance=RequestContext(request))
예제 #8
0
파일: buildgraph.py 프로젝트: htygithub/m2g
def buildGraph(request):

  error_msg = ""

  if request.method == "POST":
    form = BuildGraphForm(request.POST, request.FILES) # instantiating form
    if form.is_valid():

      # Acquire proj names
      proj_dir = form.cleaned_data["UserDefprojectName"]
      site = form.cleaned_data["site"]
      subject = form.cleaned_data["subject"]
      session = form.cleaned_data["session"]
      scanId = form.cleaned_data["scanId"]

      """
      # Private project error checking
      if (form.cleaned_data["Project_Type"] == "private"):
        if not request.user.is_authenticated():
          error_msg = "You must be logged in to make/alter a private project! \
              Please Login or make/alter a public project."

        # Untested TODO: Add join to ensure it a private project
        elif BuildGraphModel.objects.filter(owner=request.user, \
            project_name=proj_dir, site=site, subject=subject, \
            session=session, scanId=scanId).exists():

           error_msg = "The scanID you requested to create already \
               exists in this project path. Please change any of the form values."
      """
      if error_msg:
        return render_to_response(
          "buildgraph.html",
          {"buildGraphform": form, "error_msg": error_msg},
          context_instance=RequestContext(request)
          )

      """
      # If a user is logged in associate the project with thier directory
      if form.cleaned_data["Project_Type"] == "private":
        proj_dir = os.path.join(request.user.username, proj_dir)
      else:
      """
      proj_dir = os.path.join("public", proj_dir)

      # Adapt project name if necesary on disk
      proj_dir = adaptProjNameIfReq(os.path.join(
          settings.MEDIA_ROOT, proj_dir)) # Fully qualify AND handle identical projects

      usrDefProjDir = os.path.join(proj_dir, site, subject, session, scanId)
      """ Define data directory paths """
      derivatives, graphs = defDataDirs(usrDefProjDir)

      # Create a model object to save data to DB

      grModObj = BuildGraphModel(project_name = form.cleaned_data["UserDefprojectName"])
      grModObj.location = usrDefProjDir # The particular scan location

      grModObj.site = site
      grModObj.subject = subject
      grModObj.session = session
      grModObj.scanId = scanId

      if request.user.is_authenticated():
        grModObj.owner = request.user # Who created the project

      invariants = form.cleaned_data["invariants"]
      graph_size = form.cleaned_data["graph_size"]
      email = form.cleaned_data["email"]

      if graph_size == "big" and not email:
        return render_to_response(
          "buildgraph.html",
          {"buildGraphform": form, "error_msg": "Email address must be \
              provided when processing big graphs due to http timeout's possibly occurring."},
          context_instance=RequestContext(request)
          )

      """ Acquire fileNames """
      fiber_fn = form.cleaned_data["fiber_file"].name # get the name of the file input by user
      if form.cleaned_data["data_atlas_file"]:
        data_atlas_fn = form.cleaned_data["data_atlas_file"].name
        print "Storing data atlas ..."
        saveFileToDisk(form.cleaned_data["data_atlas_file"], 
            os.path.join(derivatives, data_atlas_fn))

      print "Storing fibers ..."
      """ Save files in appropriate location """
      saveFileToDisk(form.cleaned_data["fiber_file"], 
          os.path.join(derivatives, fiber_fn))
      grModObj.save() # Save project data to DB after file upload

      # add entry to owned project
      if request.user.is_authenticated():
        ownedProjModObj = OwnedProjects(project_name=grModObj.project_name, \
          owner=grModObj.owner, is_private=form.cleaned_data["Project_Type"] == "private")
        ownedProjModObj.save()

      print "\nSaving all files complete..."

      # Make appropriate dirs if they dont already exist
      create_dir_struct([derivatives, graphs])

      # TEST #
      """
      derivatives = "/home/disa/test/build_test"
      graphs = derivatives
      proj_dir = derivatives
      invariant_loc = derivatives 
      # END TEST #
      """

      sendJobBeginEmail(email, invariants)
      task_build.delay(derivatives, graphs, graph_size, invariants, derivatives, email)
      request.session["success_msg"] =\
"""
Your job successfully launched. You should receive an email to confirm launch
and another when it upon job completion. <br/>
<i>The process may take several hours</i> if you selected to compute all invariants.
"""
      return HttpResponseRedirect(get_script_prefix()+"success")

  else:
    form = BuildGraphForm() # An empty, unbound form

  # Render the form
  return render_to_response(
      "buildgraph.html",
      {"buildGraphform": form},
      context_instance=RequestContext(request) 
  )