Exemplo n.º 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
    )
Exemplo n.º 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
  )
Exemplo n.º 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)
    )
Exemplo n.º 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))
Exemplo n.º 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))
Exemplo n.º 6
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))
Exemplo n.º 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) 
  )