def get_dummy_data(request, remote_job_id):
    """
        Create a dummy job and plot data
        @param request: request object
        @param job_id: RemoteJob pk
    """
    try:
        remote_job = RemoteJob.objects.get(pk=remote_job_id)
    except:
        eqsans = Instrument.objects.get(name='eqsans')
        reduction = ReductionProcess(instrument=eqsans,
                                     name='Dummy job',
                                     owner=request.user,
                                     data_file='/tmp/dummy.nxs')
        reduction.save()
        try:
            transaction = Transaction.objects.get(trans_id=-1)
        except:
            transaction = Transaction(trans_id=-1,
                                      owner=request.user,
                                      directory='/tmp')
            transaction.save()
        remote_job = RemoteJob(reduction=reduction,
                              remote_id='-1',
                              transaction=transaction)
        remote_job.save()
        
    breadcrumbs = "<a href='%s'>home</a>" % reverse(settings.LANDING_VIEW)
    breadcrumbs += " &rsaquo; <a href='%s'>eqsans reduction</a>" % reverse('reduction.views.reduction_home',args=['eqsans'])
    breadcrumbs += " &rsaquo; <a href='%s'>jobs</a>" % reverse('eqsans.views.reduction_jobs')
    breadcrumbs += " &rsaquo; dummy job"

    template_values = {'remote_job': remote_job,
                       'parameters': remote_job.reduction.get_data_dict(),
                       'reduction_id': remote_job.reduction.id,
                       'breadcrumbs': breadcrumbs,
                       'back_url': request.path}
    template_values = remote_view_util.fill_job_values(request, remote_job.remote_id, **template_values)
    template_values = users_view_util.fill_template_values(request, **template_values)
    template_values = remote_view_util.fill_template_values(request, **template_values)
    
    # Go through the files and find data to plot
    f = os.path.join(os.path.split(__file__)[0],'..','plotting','data','4065_Iq.txt')
                
    # Do we read this data already?
    plot_object = remote_job.get_first_plot(filename='4065_Iq.txt', owner=request.user)
    if plot_object is not None and plot_object.first_data_layout() is not None:
        data_str = plot_object.first_data_layout().dataset.data
    else:
        # If we don't have data stored, read it from file
        file_content = open(f,'r').read()
        data_str = view_util.process_Iq_data(file_content)
        plot_object = Plot1D.objects.create_plot(request.user,
                                                 data=data_str,
                                                 filename='4065_Iq.txt')
        remote_job.plots.add(plot_object)
    
    template_values['plot_1d'] = data_str
    template_values['plot_object'] = plot_object
    template_values['plot_1d_id'] = plot_object.id if plot_object is not None else None
    
    # Now the 2D data
    f = os.path.join(os.path.split(__file__)[0],'..','plotting','data','4065_Iqxy.nxs')
    plot_object2d = remote_job.get_plot_2d(filename='4065_Iqxy.nxs', owner=request.user)
    if plot_object2d is None:
        numpy.set_printoptions(threshold='nan', nanstr='0', infstr='0')
        fd = h5py.File(f, 'r')
        g = fd['mantid_workspace_1']
        y = g['workspace']['axis1']
        x = g['workspace']['axis2']
        values = g['workspace']['values']
        z_max = numpy.amax(values)
        numpy.set_string_function( lambda x: '['+','.join(map(lambda y:'['+','.join(map(lambda z: "%.4g" % z,y))+']',x))+']' )
        data_str_2d = values[:].__repr__()
        numpy.set_string_function( lambda x: '['+','.join(map(lambda z: "%.4g" % z,x))+']' )

        y_str = y[:].__repr__()
        x_str = x[:].__repr__()
        plot_object2d = Plot2D.objects.create_plot(user=request.user, data=data_str_2d,
                                                   x_axis=x_str, y_axis=y_str,
                                                   z_min=0.0, z_max=z_max, filename='4065_Iqxy.nxs')
        remote_job.plots2d.add(plot_object2d)

    template_values['plot_2d'] = plot_object2d

    return template_values
Exemple #2
0
 def to_db(self, user, reduction_id=None, config_id=None):
     """
         Save reduction properties to DB.
         If we supply a config_id, the properties from that
         configuration will take precedence.
         If no config_id is supplied and the reduction_id 
         provided is found to be associated to a configuration,
         make a new copy of the reduction object so that we
         don't corrupt the configured reduction.
         @param user: User object
         @param reduction_id: pk of the ReductionProcess entry
         @param config_id: pk of the ReductionConfiguration entry
     """
     if not self.is_valid():
         raise RuntimeError, "Reduction options form invalid"
     
     if reduction_id is None:
         reduction_id = self.cleaned_data['reduction_id']
         
     # Find or create a reduction process entry and update it
     if reduction_id is not None:
         reduction_proc = get_object_or_404(ReductionProcess, pk=reduction_id, owner=user)
         # If the user changed the data to be reduced, create a new reduction process entry
         new_reduction = not reduction_proc.data_file==self.cleaned_data['data_file']
         # If the reduction process is configured and the config isn't the provided one
         config_obj = reduction_proc.get_config()
         new_reduction = new_reduction or (config_obj is not None and not config_obj.id == config_id)
     else:
         new_reduction = True
         
     if new_reduction:
         eqsans = Instrument.objects.get(name=INSTRUMENT_NAME)
         reduction_proc = ReductionProcess(owner=user,
                                           instrument=eqsans)
     reduction_proc.name = self.cleaned_data['reduction_name']
     reduction_proc.data_file = self.cleaned_data['data_file']
     reduction_proc.save()
     
     # Set the parameters associated with the reduction process entry
     config_property_dict = {}
     property_dict = copy.deepcopy(self.cleaned_data)
     property_dict['reduction_id'] = reduction_proc.id
     if config_id is not None:
         reduction_config = get_object_or_404(ReductionConfiguration, pk=config_id, owner=user)
         if reduction_proc not in reduction_config.reductions.all():
             reduction_config.reductions.add(reduction_proc)
         config_property_dict = json.loads(reduction_config.properties)
         property_dict.update(config_property_dict)
         reduction_proc.name = reduction_config.name
         reduction_proc.save()
         for item in reduction_config.experiments.all():
             if item not in reduction_proc.experiments.all():
                 reduction_proc.experiments.add(item)
     try:
         properties = json.dumps(property_dict)
         reduction_proc.properties = properties
         reduction_proc.save()
     except:
         logger.error("Could not process reduction properties: %s" % sys.exc_value)
     
     # Find experiment
     process_experiment(reduction_proc, self.cleaned_data['experiment'], instrument_name=INSTRUMENT_NAME)
             
     return reduction_proc.pk