Esempio n. 1
0
 def derive_run_variable(self, instrument_var, reduction_run):
     return RunVariable(name=instrument_var.name,
                        value=instrument_var.value,
                        is_advanced=instrument_var.is_advanced,
                        type=instrument_var.type,
                        help_text=instrument_var.help_text,
                        reduction_run=reduction_run)
Esempio n. 2
0
 def derive_run_variable(instrument_var, reduction_run):
     """
     :return: A run variable class populated with input data
     """
     return RunVariable(name=instrument_var.name,
                        value=instrument_var.value,
                        is_advanced=instrument_var.is_advanced,
                        type=instrument_var.type,
                        help_text=instrument_var.help_text,
                        reduction_run=reduction_run)
Esempio n. 3
0
def getReductionRun(with_variables=True):
    instrument = InstrumentUtils().get_instrument("valid")
    experiment = Experiment(reference_number=1)
    experiment.save()
    reduction_run = ReductionRun(
        instrument=instrument,
        run_number=1,
        experiment=experiment,
        run_version=0,
        status=StatusUtils().get_queued(),
        script=getValidScript("reduce.py"),
    )
    reduction_run.save()

    if with_variables:
        variable = RunVariable(
            reduction_run=reduction_run, name="test", value="testvalue1", type="text", is_advanced=False
        )
        variable.save()
        reduction_run.run_variables.add(variable)

        variable = RunVariable(
            reduction_run=reduction_run, name="advanced_test", value="testvalue2", type="text", is_advanced=True
        )
        variable.save()
        reduction_run.run_variables.add(variable)

        reduction_run.save()

    return reduction_run
Esempio n. 4
0
    def test_get_script_and_arguments_successful(self):
        run_variables = []

        reduction_run = getReductionRun()
        reduction_run.script = getValidScript("reduce.py")

        variable = RunVariable(
            reduction_run=reduction_run, name="test", value="testvalue1", type="text", is_advanced=False
        )
        variable.save()
        run_variables.append(variable)
        variable = RunVariable(
            reduction_run=reduction_run, name="advanced_test", value="testvalue2", type="text", is_advanced=True
        )
        variable.save()
        run_variables.append(variable)

        script, arguments = ReductionRunUtils().get_script_and_arguments(reduction_run)

        self.assertNotEqual(script, None, "Expecting to get a script path back.")
        self.assertNotEqual(script, "", "Expecting to get a script path back.")
        self.assertNotEqual(arguments, None, "Expecting to get some arguments path back.")
        self.assertNotEqual(arguments, {}, "Expecting to get some arguments path back.")
        self.assertTrue("standard_vars" in arguments, "Expecting arguments to have a 'standard_vars' key.")
        self.assertTrue("advanced_vars" in arguments, "Expecting arguments to have a 'advanced_vars' key.")
        self.assertEqual(
            arguments["standard_vars"]["test"], "testvalue1", "Expecting to find testvalue1 in standard_vars."
        )
        self.assertEqual(
            arguments["advanced_vars"]["advanced_test"], "testvalue2", "Expecting to find testvalue2 in advanced_vars."
        )
Esempio n. 5
0
def getReductionRun(with_variables=True):
    instrument = InstrumentUtils().get_instrument('valid')
    experiment = Experiment(reference_number=1)
    experiment.save()
    reduction_run = ReductionRun(instrument=instrument, run_number=1, experiment=experiment, run_version=0, status=StatusUtils().get_queued(), script=getValidScript('reduce.py'))
    reduction_run.save()        

    if with_variables:            
        variable = RunVariable(reduction_run=reduction_run,name='test',value='testvalue1',type='text',is_advanced=False)
        variable.save()
        reduction_run.run_variables.add(variable)

        variable = RunVariable(reduction_run=reduction_run,name='advanced_test',value='testvalue2',type='text',is_advanced=True)
        variable.save()
        reduction_run.run_variables.add(variable)

        reduction_run.save()

    return reduction_run
Esempio n. 6
0
    def test_get_script_and_arguments_successful(self):
        run_variables = []

        reduction_run = getReductionRun()
        reduction_run.script = getValidScript("reduce.py")

        variable = RunVariable(reduction_run=reduction_run,
                               name='test',
                               value='testvalue1',
                               type='text',
                               is_advanced=False)
        variable.save()
        run_variables.append(variable)
        variable = RunVariable(reduction_run=reduction_run,
                               name='advanced_test',
                               value='testvalue2',
                               type='text',
                               is_advanced=True)
        variable.save()
        run_variables.append(variable)

        script, arguments = ReductionRunUtils().get_script_and_arguments(
            reduction_run)

        self.assertNotEqual(script, None,
                            "Expecting to get a script path back.")
        self.assertNotEqual(script, "", "Expecting to get a script path back.")
        self.assertNotEqual(arguments, None,
                            "Expecting to get some arguments path back.")
        self.assertNotEqual(arguments, {},
                            "Expecting to get some arguments path back.")
        self.assertTrue('standard_vars' in arguments,
                        "Expecting arguments to have a 'standard_vars' key.")
        self.assertTrue('advanced_vars' in arguments,
                        "Expecting arguments to have a 'advanced_vars' key.")
        self.assertEqual(arguments['standard_vars']['test'], 'testvalue1',
                         "Expecting to find testvalue1 in standard_vars.")
        self.assertEqual(arguments['advanced_vars']['advanced_test'],
                         'testvalue2',
                         "Expecting to find testvalue2 in advanced_vars.")
Esempio n. 7
0
def run_confirmation(request, instrument=None):
    if request.method != 'POST':
        return redirect('instrument_summary', instrument=instrument.name)

    # POST
    instrument = Instrument.objects.get(name=instrument)
    run_numbers = []

    if 'run_number' in request.POST:
        run_numbers.append(int(request.POST.get('run_number')))
    else:
        range_string = request.POST.get('run_range').split(',')
        # Expand list
        for item in range_string:
            if '-' in item:
                split_range = item.split('-')
                run_numbers.extend(
                    range(int(split_range[0]),
                          int(split_range[1]) + 1)
                )  # because this is a range, the end bound is exclusive!
            else:
                run_numbers.append(int(item))
        # Make sure run numbers are distinct
        run_numbers = set(run_numbers)

    queued_status = StatusUtils().get_queued()
    queue_count = ReductionRun.objects.filter(instrument=instrument,
                                              status=queued_status).count()

    context_dictionary = {
        'runs': [],
        'variables': None,
        'queued': queue_count,
    }

    # Check that RB numbers are the same
    rb_number = ReductionRun.objects.filter(
        instrument=instrument,
        run_number__in=run_numbers).values_list('experiment__reference_number',
                                                flat=True).distinct()
    if len(rb_number) > 1:
        context_dictionary[
            'error'] = 'Runs span multiple experiment numbers (' + ','.join(
                str(i)
                for i in rb_number) + ') please select a different range.'

    for run_number in run_numbers:
        old_reduction_run = ReductionRun.objects.filter(
            run_number=run_number).order_by('-run_version').first()

        # Check old run exists
        if old_reduction_run is None:
            context_dictionary['error'] = "Run number " + str(
                run_number) + " doesn't exist."

        use_current_script = request.POST.get('use_current_script',
                                              u"true").lower() == u"true"
        if use_current_script:
            script_text = InstrumentVariablesUtils().get_current_script_text(
                instrument.name)[0]
            default_variables = InstrumentVariablesUtils(
            ).get_default_variables(instrument.name)
        else:
            script_text = old_reduction_run.script
            default_variables = old_reduction_run.run_variables.all()

        new_variables = []

        for key, value in request.POST.iteritems():
            if 'var-' in key:
                name = None
                if 'var-advanced-' in key:
                    name = key.replace('var-advanced-', '').replace('-', ' ')
                    is_advanced = True
                if 'var-standard-' in key:
                    name = key.replace('var-standard-', '').replace('-', ' ')
                    is_advanced = False

                if name is not None:
                    default_var = next(
                        (x for x in default_variables if x.name == name), None)
                    if not default_var:
                        continue
                    if len(value) > InstrumentVariable._meta.get_field(
                            'value').max_length:
                        context_dictionary['error'] = 'Value given in ' + str(
                            name) + ' is too long.'
                    variable = RunVariable(name=default_var.name,
                                           value=value,
                                           is_advanced=is_advanced,
                                           type=default_var.type,
                                           help_text=default_var.help_text)
                    new_variables.append(variable)

        if len(new_variables) == 0:
            context_dictionary[
                'error'] = 'No variables were found to be submitted.'

        # User can choose whether to overwrite with the re-run or create new data
        if request.POST.get('overwrite_checkbox') == 'on':
            overwrite_previous_data = True
        else:
            overwrite_previous_data = False

        if 'error' in context_dictionary:
            return context_dictionary

        run_description = request.POST.get('run_description')

        new_job = ReductionRunUtils().createRetryRun(
            old_reduction_run,
            script=script_text,
            overwrite=overwrite_previous_data,
            variables=new_variables,
            username=request.user.username,
            description=run_description)

        try:
            MessagingUtils().send_pending(new_job)
            context_dictionary['runs'].append(new_job)
            context_dictionary['variables'] = new_variables

        except Exception as e:
            new_job.delete()
            context_dictionary['error'] = 'Failed to send new job. (%s)' % str(
                e)

    return context_dictionary
Esempio n. 8
0
def run_confirmation(request, instrument):
    """
    Handles request for user to confirm re-run
    """
    if request.method != 'POST':
        return redirect('instrument_summary', instrument=instrument.name)

    # POST
    # pylint:disable=no-member
    instrument = Instrument.objects.get(name=instrument)
    range_string = request.POST.get('run_range')

    queued_status = StatusUtils().get_queued()
    # pylint:disable=no-member
    queue_count = ReductionRun.objects.filter(instrument=instrument,
                                              status=queued_status).count()
    context_dictionary = {
        'runs': [],
        'variables': None,
        'queued': queue_count,
    }

    try:
        run_numbers = input_processing.parse_user_run_numbers(range_string)
    except SyntaxError as exception:
        context_dictionary['error'] = exception.msg
        return context_dictionary

    # Determine user level to set a maximum limit to the number of runs that can be re-queued
    if request.user.is_superuser:
        max_runs = 500
    elif request.user.is_staff:
        max_runs = 50
    else:
        max_runs = 20

    if len(run_numbers) > max_runs:
        context_dictionary["error"] = "{0} runs were requested, but only {1} runs can be " \
                                      "queued at a time".format(len(run_numbers), max_runs)
        return context_dictionary

    # Check that RB numbers are the same for the range entered
    # pylint:disable=no-member
    rb_number = ReductionRun.objects.filter(instrument=instrument, run_number__in=run_numbers) \
        .values_list('experiment__reference_number', flat=True).distinct()
    if len(rb_number) > 1:
        context_dictionary['error'] = 'Runs span multiple experiment numbers ' \
                                      '(' + ','.join(str(i) for i in rb_number) + ')' \
                                      ' please select a different range.'
        return context_dictionary

    for run_number in run_numbers:
        # pylint:disable=no-member
        matching_previous_runs_queryset = ReductionRun.objects.\
            filter(instrument=instrument,
                   run_number=run_number).order_by('-run_version')
        most_recent_previous_run = matching_previous_runs_queryset.first()

        # Check old run exists
        if most_recent_previous_run is None:
            context_dictionary['error'] = "Run number %s hasn't been" \
                                          "ran by autoreduction yet." % str(run_number)

        # Check it is not currently queued
        queued_runs = matching_previous_runs_queryset.filter(
            status=queued_status).first()
        if queued_runs is not None:
            context_dictionary['error'] = "Run number {0} is already queued to run".\
                format(queued_runs.run_number)
            return context_dictionary

        use_current_script = request.POST.get('use_current_script',
                                              u"true").lower() == u"true"
        if use_current_script:
            script_text = InstrumentVariablesUtils().get_current_script_text(
                instrument.name)[0]
            default_variables = InstrumentVariablesUtils(
            ).get_default_variables(instrument.name)
        else:
            script_text = most_recent_previous_run.script
            default_variables = most_recent_previous_run.run_variables.all()

        new_variables = []

        for key, value in list(request.POST.items()):
            if 'var-' in key:
                name = None
                if 'var-advanced-' in key:
                    name = key.replace('var-advanced-', '').replace('-', ' ')
                    is_advanced = True
                if 'var-standard-' in key:
                    name = key.replace('var-standard-', '').replace('-', ' ')
                    is_advanced = False

                if name is not None:
                    default_var = next(
                        (x for x in default_variables if x.name == name), None)
                    if not default_var:
                        continue
                    # pylint:disable=protected-access,no-member
                    if len(value) > InstrumentVariable._meta.get_field(
                            'value').max_length:
                        context_dictionary['error'] = 'Value given in {} is too long.'\
                            .format(str(name))
                    variable = RunVariable(name=default_var.name,
                                           value=value,
                                           is_advanced=is_advanced,
                                           type=default_var.type,
                                           help_text=default_var.help_text)
                    new_variables.append(variable)

        if not new_variables:
            context_dictionary[
                'error'] = 'No variables were found to be submitted.'

        # User can choose whether to overwrite with the re-run or create new data
        overwrite_previous_data = bool(
            request.POST.get('overwrite_checkbox') == 'on')

        if 'error' in context_dictionary:
            return context_dictionary

        run_description = request.POST.get('run_description')
        max_desc_len = 200
        if len(run_description) > max_desc_len:
            context_dictionary["error"] = "The description contains {0} characters, " \
                                          "a maximum of {1} are allowed".\
                format(len(run_description), max_desc_len)
            return context_dictionary

        new_job = ReductionRunUtils().createRetryRun(
            user_id=request.user.id,
            reduction_run=most_recent_previous_run,
            script=script_text,
            overwrite=overwrite_previous_data,
            variables=new_variables,
            description=run_description)

        try:
            MessagingUtils().send_pending(new_job)
            context_dictionary['runs'].append(new_job)
            context_dictionary['variables'] = new_variables

        # pylint:disable=broad-except
        except Exception as exception:
            new_job.delete()
            context_dictionary['error'] = 'Failed to send new job. (%s)' % str(
                exception)

    return context_dictionary