Exemple #1
0
def submit_a_verification_job(request, scenario_id):
    adapter = EMOD_Adapter(request.user.username)

    emod_scenario = EMODBaseline.from_dw(id=scenario_id)
    config_json = ast.literal_eval(emod_scenario.get_file_by_type('config').content)

    config_json['parameters']['Simulation_Duration'] = 20 * 365  # 20 years
    emod_scenario.add_file_from_string('config', 'config.json', json.dumps(config_json), description='SOMETHING')

    my_simulation_duration = config_json['parameters']['Simulation_Duration']

    ## Set the run start_date based on location and config's start_time
    my_start_date = \
        datetime.datetime.strptime(emod_scenario.template.climate_start_date, '%Y-%m-%d').date() \
            + datetime.timedelta(days=config_json['parameters']['Start_Time'])

    # Initialize the run
    run = adapter.save_run(scenario_id=scenario_id,
                              template_id=int(emod_scenario.template.id),
                              start_date=my_start_date,
                              duration=my_simulation_duration,
                              name='calibration-verification',
                              description=str(datetime.datetime.now()),
                              location_ndx=emod_scenario.template.location_key.id,
                              timestep_interval=1,
                              run_id=-1,
                              as_object=True)

    run.baseline_key_id = scenario_id
    run.save()

    # add in JCD for config file
    changes = []
    newdict = copy.deepcopy(config_json)

    newdict['config.json/parameters'] = newdict.pop('parameters')
    changes.append(Change.node(name='config.json', node=[newdict], mode='-'))

    run.jcd = JCD.from_changes(changes)
    run.save()

    ### Launch the run

    # 5853 - hard-code to 1 for now
    #reps_per_exec = int(self.get_cleaned_data_for_step('start')['reps_per_exec'])
    reps_per_exec = 1

    # submit returns tuple (success, message)
    try:
        status = submit(run, user=request.user, manifest=True, reps_per_exec=reps_per_exec)

        set_notification('alert-success', '<strong>Success!</strong> Run launched.', request.session)
    except (RuntimeError, TypeError, AssertionError), e:
        set_notification('alert-error', '<strong>Error!</strong> ' + str(e.message), request.session)
    def handle(self, *args, **options):
        if len(args)==1 and "-h"==args[0]:
            print "Usage: test_locations"
            sys.exit(0)



        tstart = time.time()
        run = DimRun.objects.get(id = 1509)
        number = len(run.dimexecution_set.all())
        print "%s executions to go" % number
        adapter = EMOD_Adapter("admin")
        i = 0
        for execution in run.dimexecution_set.all():
            adapter.fetch_data(execution.id, channel_name="Daily EIR")
            i += 1
            print "%s / %s (%s)" % (i, number, time.time() - tstart)

        tend = time.time()
        print "timing: fetch_data from run 1509 took %f sec" % (tend-tstart)
Exemple #3
0
    def get_context_data(self, **kwargs):
        self.return_list.clear()
        # context = super(GeoJsonView, self).get_context_data(**kwargs)

        if 'run' in self.request.GET:
            run = DimRun.objects.get(id=int(self.request.GET['run']))
            if not run:
                raise Http404('Run with id %s does not exist' % self.request.GET['run'])
            location = run.location_key
        elif 'location' in self.request.GET:
            location = DimLocation.objects.filter(pk=int(self.request.GET['location']))
            if not location.exists():
                raise Http404('Location with id %s does not exist' % self.request.GET['location'])
            location = location[0]
        else:
            raise Http404('Keywords location or run must be specified')

        adapter = EMOD_Adapter()
        self.return_list = json.loads(adapter.fetch_geojson(location=location.id))

        return
def viewtastic_fetch_keys(request, run_id):
    """
    For a given run, the fetch_keys function fetches all of the keys and values that
    were swept on for that specific run.

    :param request: needed for providing a validated username
    :type request: ajax request object
    :param run_id: the run to fetch the keys for
    :type run_id: int
    :return: a dictionary of key names and values for the given run
    """

    adapter = EMOD_Adapter(user_name=request.user.username)
    try:
        run_id = int(run_id)
    except ValueError:
        # This is a simulation ID, return empty list
        return HttpResponse(content=json.dumps([]), content_type="application/json")
    keys = adapter.fetch_keys(run_id=run_id)
    return_keys = []
    order_counter = 1

    for element in keys:
        for key, value in sorted(element.iteritems()):
            the_key = {'name': key}

            # Workaround for Bug #6199
            # There is inconsistency between representing numbers in javascript and python.
            # In python, there are two separate types, float and int. 1 (integer) is not the same as 1.0 (float)
            # In contrast, javascript has only one type - number. 1.0 (float) will be represented as 1.
            # As a workaround, we will represent sweep value as a string in visualizer.
            str_value = []
            for val in value:
                str_value.append(str(val))
            the_key['options'] = str_value
            the_key['order'] = order_counter
            order_counter += 1
            return_keys.append(the_key)

    return HttpResponse(content=json.dumps(return_keys), content_type="application/json")
def viewtastic_fetch_channels(request, run_id):
    """
    For a given run, the fetch_channels function fetches all of the channels available for viewing.

    :param request: needed for providing a validated username
    :type request: ajax request object
    :param run_id: the run to fetch the keys for.
    :type run_id: int
    :return: a dictionary of channel ids and channel info (contains title and type for each channel)
    """

    adapter = EMOD_Adapter(user_name=request.user.username)
    try:
        run_id = int(run_id)
    except ValueError:
        # This is a simulation ID, return list of channels in InsectChart
        sim = Simulation.objects.get(id=int(run_id[1:]))
        config = json.loads(sim.simulationoutputfile_set.get(name="InsetChart.json").get_contents())
        return_channels = []
        for channel in config["Channels"]:
            return_channels.append({"id": channel,
                                    "info": {
                                        "title": channel,
                                        "units": "no"
                                        # "type": "minor vector"
                                    }
                                    }
                                   )
    else:
        channels = adapter.fetch_channels(run_id=run_id, as_object=False)
        return_channels = []

        for key, value in channels.iteritems():
            the_channel = {'id': key, 'info': value}
            return_channels.append(the_channel)

    return HttpResponse(content=json.dumps(sorted(return_channels, key=lambda channel: channel['info'])),
                        content_type="application/json")
def viewtastic_fetch_runs(request, scenario_id):
    """
    For a given scenario, the fetch_runs function fetches all of the runs and returns their names and ids in a
    dictionary.

    :param request: needed for providing a validated username
    :type request: ajax request object
    :param scenario_id: the scenario to fetch the runs for
    :type scenario_id: int
    :return: a dictionary of run names and ids for the given scenario
    """

    adapter = EMOD_Adapter(user_name=request.user.username)
    return_runs = []

    baseline = DimBaseline.objects.get(id=scenario_id)

    if baseline.simulation_group is None:
        runs = DimRun.objects.filter(baseline_key=scenario_id).order_by('-id')

        for run in runs:
            the_run = {'name': run.name, 'id': run.pk}
            the_run_executions = adapter.fetch_executions(run.pk)
            the_run['executions'] = the_run_executions
            return_runs.append(the_run)
    else:
        # Result Simulation ID instead
        sim = baseline.simulation_group.simulations.all()[0]
        return_runs = [{"name": baseline.name,
                        "id": "s%s" % sim.id,
                        "executions": ["s%s" % sim.id]}]

    if request.is_ajax():
        return HttpResponse(content=json.dumps(return_runs), content_type="application/json")

    return return_runs
def viewtastic_fetch_data(request):
    """
    For a given execution dictionary, the fetch_data function returns a chart json for insertion into
    a highcharts object.
    This function also returns the channel id for asynchronous data flow and identification reasons.

    :param request: needed for providing a validated username and providing the execution dictionary via the
    packaged GET data.
    :type request: ajax request object
    :return: a dictionary containing the channel id and the associated chart for the selected channel
    """
    temp_chart_json = {}
    temp_chart_json.clear()
    adapter = EMOD_Adapter(user_name=request.user.username)  # get_appropriate_model_adapter(request)

    dict_list = []
    for key in request.POST:
        dict_list.append(json.loads(key))

    # if there weren't any sweeps done, then the execution id needs to be passed to the function.
    the_params = None
    try:
        the_params = dict_list[0]['parameters']
        # Turn the dict of lists into a list of dicts, ordered by the mod order from when the fetch_keys
        # was called
        the_param_list = []
        for sweep_object in the_params:
            the_params[sweep_object]['name'] = sweep_object
            the_param_list.append(the_params[sweep_object])

        final_param_list = []
        for param in sorted(the_param_list, key=lambda sweep: sweep['mod_order']):
            final_param_list.append({param['name']: param['values']})

    except:
        the_execid = dict_list[0]['execution']

    try:
        chart_type = dict_list[0]['chart_type']
    except:
        chart_type = 'time_series'

    try:
        the_aggregation = dict_list[0]['aggregation']
    except:
        the_aggregation = 'daily'

    try:
        the_run = int(dict_list[0]['run_id'])
        the_channel = int(dict_list[0]['channel_id'])
    except:
        # Requesting Simulation data
        sim_id = int(dict_list[0]['run_id'][1:])  # Remove first 's' character
        the_channel = dict_list[0]['channel_id']
        start_date = datetime.datetime(1995, 1, 1)
        insetchartfile = SimulationOutputFile.objects.get(simulation=sim_id, name="InsetChart.json")
        data = json.loads(insetchartfile.get_contents())["Channels"][the_channel]["Data"]
        data = as_chart(data, start_date)

    else:
        # if the parameters aren't available then try getting the data via the execution id
        if the_params is not None:
            if the_aggregation == 'yearly_sum':
                data = adapter.fetch_data(group_by=True,
                                          exec_dict=final_param_list,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)
            else:
                data = adapter.fetch_data(exec_dict=final_param_list,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)
        else:
            if the_aggregation == 'yearly_sum':
                data = adapter.fetch_data(group_by=True,
                                          execution_id=the_execid,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)
            else:
                data = adapter.fetch_data(execution_id=the_execid,
                                          run_id=the_run,
                                          channel_id=the_channel,
                                          as_chart=True, as_highstock=True)

    temp_chart_json = json.loads(data)

    temp_chart_json['chart']['height'] = 335 + (15 * len(temp_chart_json['series']))
    temp_chart_json['chart']['width'] = 500

    temp_chart_json['rangeSelector'] = {'enabled': False}
    real_return_json = dict(channel_id=the_channel,
                            chart_JSON=temp_chart_json,
                            chart_type=chart_type,
                            aggregation=the_aggregation)

    return HttpResponse(content=json.dumps(real_return_json), content_type="application/json")