Example #1
0
def add_simulation_run(project, cache_directory, scenario_name, run_name,
                       start_year, end_year, run_id):
    '''
    Creates a simulation run node and adds it to the project
    @param project (OpusProject): currently loaded project
    @param cache_directory (String): absolute path to the cache directory
    @param scenario_name (String): name of the scenario that was run
    @param run_name (String): name of the run
    @param start_year (int): start year of run
    @param end_year (int): end year of run
    @param run_id (int): ID number for the run
    '''
    ## XML-ify the name
    # run_name = run_name.replace(' ', '_')

    # Assemble the new run node
    str_atr = {'type': 'string'}
    int_atr = {'type': 'integer'}

    run_node = Element('run', {'type': 'source_data',
                               'name': run_name,
                               'hidden': 'Children',
                               'run_id': str(run_id)})
    # SubElement(run_node, 'run_id', int_atr).text = str(run_id)
    SubElement(run_node, 'scenario_name', str_atr).text = scenario_name
    SubElement(run_node, 'cache_directory', str_atr).text = cache_directory
    SubElement(run_node, 'start_year', int_atr).text = str(start_year)
    SubElement(run_node, 'end_year', int_atr).text = str(end_year)

    # Grab the results manager instance for the GUI and insert the new node
    get_manager_instance('results_manager').add_run(run_node)
    project.dirty = True
    update_mainwindow_savestate()
Example #2
0
    def __init__(self, run_node, parent_widget=None):
        QDialog.__init__(self, parent_widget)
        self.setupUi(self)
        self.run_node = run_node
        self.changed_cache_dir = None

        run_name = run_node.get('name')

        # We don't have a results manager in the unit tests
        results_manager = get_manager_instance('results_manager')
        if results_manager is not None:
            #this is to work around that results_manager_functions.add_simulation_run function
            #isn't able to update existing runs, for example, in case a run being restarted
            project = results_manager.project
            start_year, end_year = get_years_range_for_simulation_run(
                project, run_node=self.run_node)
        else:
            # fallback, fill in existing values...
            start_year = run_node.find('start_year').text
            end_year = run_node.find('end_year').text

        scenario_name = run_node.find('scenario_name').text
        cache_directory = run_node.find('cache_directory').text
        self.original_cache_dir = cache_directory.strip()
        run_id = run_node.get('run_id', 'not available')

        self.lblRun_name.setText(run_name)
        self.lblYears_run.setText('%s - %s' % (start_year, end_year))

        if scenario_name is None:
            scenario_name = ''
        self.lblScenario_name.setText(scenario_name)
        self.lblCache_directory.setText(cache_directory)
        self.lblRunId.setText(run_id)
    def __init__(self, run_node, parent_widget=None):
        QDialog.__init__(self, parent_widget)
        self.setupUi(self)
        self.run_node = run_node
        self.changed_cache_dir = None

        run_name = run_node.get("name")

        # We don't have a results manager in the unit tests
        results_manager = get_manager_instance("results_manager")
        if results_manager is not None:
            # this is to work around that results_manager_functions.add_simulation_run function
            # isn't able to update existing runs, for example, in case a run being restarted
            project = results_manager.project
            start_year, end_year = get_years_range_for_simulation_run(project, run_node=self.run_node)
        else:
            # fallback, fill in existing values...
            start_year = run_node.find("start_year").text
            end_year = run_node.find("end_year").text

        scenario_name = run_node.find("scenario_name").text
        cache_directory = run_node.find("cache_directory").text
        self.original_cache_dir = cache_directory.strip()
        run_id = run_node.get("run_id", "not available")

        self.lblRun_name.setText(run_name)
        self.lblYears_run.setText("%s - %s" % (start_year, end_year))

        if scenario_name is None:
            scenario_name = ""
        self.lblScenario_name.setText(scenario_name)
        self.lblCache_directory.setText(cache_directory)
        self.lblRunId.setText(run_id)
Example #4
0
def update_batch_indicator_visualization(updated_viz_node):
    '''
    Updates a visualization node.
    @param viz_node (Element): the node to update.
    '''
    results_manager = get_manager_instance('results_manager')
    if not results_manager:
        return
    results_manager.update_viz_node(updated_viz_node)
Example #5
0
def add_batch_indicator_visualization(batch_node, viz_node):
    '''
    Adds a new batch indicator visualization to the project
    @param viz_node (Element): Node representing the new batch node to insert
    '''
    results_manager = get_manager_instance('results_manager')
    if not results_manager:
        return
    results_manager.add_viz_node(batch_node, viz_node)
Example #6
0
    def __init__(self, project, parent_widget):
        QWidget.__init__(self, parent_widget)
        self.setupUi(self)

        self.project = project

        #TODO handle this as the variables are handled
        try:
            obj = get_manager_instance('results').xml_controller.model
            self.connect(obj, SIGNAL("layoutChanged()"),
                         self._setup_simulation_data)
        except AttributeError:
            pass

        def test():
            print 'Variables updated'

        self.connect(get_mainwindow_instance(), SIGNAL('variables_updated'),
                     test)

        tool_tip = ('If checked, indicator results will automatically be\n'
                    'created for the currently selected simulation run,\n'
                    'indicator, and years. If unchecked, click\n'
                    'Generate results in order to make results available.')
        self.cb_auto_gen.setToolTip(tool_tip)

        self.inGui = False
        self.logFileKey = 0
        self.available_years_for_simulation_runs = {}

        self.current_indicator = ''
        self.current_indicator_dataset = ''
        self.current_run = ''
        self.current_year = ''
        self._update_current_label()

        self.running_key = None
        self.queued_results = None

        self.setup = True

        self._setup_available_indicators()
        self._setup_simulation_data()

        self.setup = False

        self.already_browsed = {}

        self.generating_results = False
        if self.cb_auto_gen.isChecked():
            self.on_pb_generate_results_clicked()
        # self.pb_export_results.setEnabled(False)
        self.tabwidget_visualizations.removeTab(0)
        self.tabIcon = IconLibrary.icon('table')
        self.tabLabel = "Result Browser"
    def __init__(self, project, parent_widget):
        QWidget.__init__(self, parent_widget)
        self.setupUi(self)

        self.project = project

        # TODO handle this as the variables are handled
        try:
            obj = get_manager_instance("results").xml_controller.model
            self.connect(obj, SIGNAL("layoutChanged()"), self._setup_simulation_data)
        except AttributeError:
            pass

        def test():
            print "Variables updated"

        self.connect(get_mainwindow_instance(), SIGNAL("variables_updated"), test)

        tool_tip = (
            "If checked, indicator results will automatically be\n"
            "created for the currently selected simulation run,\n"
            "indicator, and years. If unchecked, click\n"
            "Generate results in order to make results available."
        )
        self.cb_auto_gen.setToolTip(tool_tip)

        self.inGui = False
        self.logFileKey = 0
        self.available_years_for_simulation_runs = {}

        self.current_indicator = ""
        self.current_indicator_dataset = ""
        self.current_run = ""
        self.current_year = ""
        self._update_current_label()

        self.running_key = None
        self.queued_results = None

        self.setup = True

        self._setup_available_indicators()
        self._setup_simulation_data()

        self.setup = False

        self.already_browsed = {}

        self.generating_results = False
        if self.cb_auto_gen.isChecked():
            self.on_pb_generate_results_clicked()
        # self.pb_export_results.setEnabled(False)
        self.tabwidget_visualizations.removeTab(0)
        self.tabIcon = IconLibrary.icon("table")
        self.tabLabel = "Result Browser"
def add_simulation_run(project, cache_directory, scenario_name, run_name,
                       start_year, end_year, run_id, status):
    '''
    Creates a simulation run node and adds it to the project
    @param project (OpusProject): currently loaded project
    @param cache_directory (String): absolute path to the cache directory
    @param scenario_name (String): name of the scenario that was run
    @param run_name (String): name of the run
    @param start_year (int): start year of run
    @param end_year (int): end year of run
    @param run_id (int): ID number for the run
    @param success (String): True, False or running
    
    This function isn't able to update run_nodes existing in <simulation_runs>
    '''
    ## XML-ify the name
    # run_name = run_name.replace(' ', '_')

    # Assemble the new run node
    str_atr = {'type': 'string'}
    int_atr = {'type': 'integer'}
    if not run_name or run_name is None or run_name == "No description":
        ## if run_name is null or un-informative, use run id as name instead
        run_name = "run_%s" % run_id
    run_node = Element(
        'run', {
            'type': 'source_data',
            'name': run_name,
            'hidden': 'Children',
            'run_id': str(run_id)
        })
    # SubElement(run_node, 'run_id', int_atr).text = str(run_id)
    SubElement(run_node, 'scenario_name', str_atr).text = scenario_name
    SubElement(run_node, 'cache_directory', str_atr).text = cache_directory
    SubElement(run_node, 'start_year', int_atr).text = str(start_year)
    SubElement(run_node, 'end_year', int_atr).text = str(end_year)
    SubElement(run_node, 'status', int_atr).text = str(status)

    # Grab the results manager instance for the GUI and insert the new node
    get_manager_instance('results_manager').add_run(run_node)
Example #9
0
def add_simulation_run(project, cache_directory, scenario_name, run_name,
                       start_year, end_year, run_id, status):
    '''
    Creates a simulation run node and adds it to the project
    @param project (OpusProject): currently loaded project
    @param cache_directory (String): absolute path to the cache directory
    @param scenario_name (String): name of the scenario that was run
    @param run_name (String): name of the run
    @param start_year (int): start year of run
    @param end_year (int): end year of run
    @param run_id (int): ID number for the run
    @param success (String): True, False or running
    
    This function isn't able to update run_nodes existing in <simulation_runs>
    '''
    ## XML-ify the name
    # run_name = run_name.replace(' ', '_')

    # Assemble the new run node
    str_atr = {'type': 'string'}
    int_atr = {'type': 'integer'}
    if not run_name or run_name is None or run_name == "No description":
        ## if run_name is null or un-informative, use run id as name instead
        run_name = "run_%s" % run_id
    run_node = Element('run', {'type': 'source_data',
                               'name': run_name,
                               'hidden': 'Children',
                               'run_id': str(run_id)})
    # SubElement(run_node, 'run_id', int_atr).text = str(run_id)
    SubElement(run_node, 'scenario_name', str_atr).text = scenario_name
    SubElement(run_node, 'cache_directory', str_atr).text = cache_directory
    SubElement(run_node, 'start_year', int_atr).text = str(start_year)
    SubElement(run_node, 'end_year', int_atr).text = str(end_year)
    SubElement(run_node, 'status', int_atr).text = str(status)

    # Grab the results manager instance for the GUI and insert the new node
    get_manager_instance('results_manager').add_run(run_node)
    def _restart_selected_run(self):
        assert self.has_selected_item()
        run_node = self.selected_item().node
        # ask for start_year, end_year
        # start_year default to the last year of years run
        # need to avoid insert auto generate run directory again
#        original_start_year = int(run_node.find('start_year').text)
#        original_end_year = int(run_node.find('end_year').text)
#        restart_year = original_end_year + 1
#        end_year = restart_year + 1

        run_name = run_node.get('name')
        scenario_name = run_node.find('scenario_name').text
        run_id = run_node.get('run_id')
        try:
            run_id = int(run_id)
        except ValueError:
            raise ValueError, "run_id for run %s is invalid: %s; the run cannot be restarted." % \
                  (run_name, run_id)
        
        run_manager = get_run_manager()
        config = run_manager.get_resources_for_run_id_from_history(run_id)

        xml_config = self.project.xml_config
        opusgui = get_mainwindow_instance()
        scenario_manager = get_manager_instance('scenario_manager')
        opusmodel = OpusModel(scenario_manager, xml_config, scenario_name)
        tab_widget = SimulationGuiElementRestart(mainwindow=opusgui, 
                                                 runManager=scenario_manager, 
                                                 model=opusmodel, 
                                                 xml_config=xml_config,
                                                 run_id=run_id,
                                                 run_name=run_name)
        tab_widget.config = config
#        tab_widget.start_year = start_year
#        tab_widget.end_year = end_year
        scenario_manager._attach_tab(tab_widget)
        opusgui.update()
    def _restart_selected_run(self):
        assert self.has_selected_item()
        run_node = self.selected_item().node
        # ask for start_year, end_year
        # start_year default to the last year of years run
        # need to avoid insert auto generate run directory again
        #        original_start_year = int(run_node.find('start_year').text)
        #        original_end_year = int(run_node.find('end_year').text)
        #        restart_year = original_end_year + 1
        #        end_year = restart_year + 1

        run_name = run_node.get('name')
        scenario_name = run_node.find('scenario_name').text
        run_id = run_node.get('run_id')
        try:
            run_id = int(run_id)
        except ValueError:
            raise ValueError, "run_id for run %s is invalid: %s; the run cannot be restarted." % \
                  (run_name, run_id)

        run_manager = get_run_manager()
        config = run_manager.get_resources_for_run_id_from_history(run_id)

        xml_config = self.project.xml_config
        opusgui = get_mainwindow_instance()
        scenario_manager = get_manager_instance('scenario_manager')
        opusmodel = OpusModel(scenario_manager, xml_config, scenario_name)
        tab_widget = SimulationGuiElementRestart(mainwindow=opusgui,
                                                 runManager=scenario_manager,
                                                 model=opusmodel,
                                                 xml_config=xml_config,
                                                 run_id=run_id,
                                                 run_name=run_name)
        tab_widget.config = config
        #        tab_widget.start_year = start_year
        #        tab_widget.end_year = end_year
        scenario_manager._attach_tab(tab_widget)
        opusgui.update()
def update_models_to_run_lists():
    sm = get_manager_instance('scenario_manager')
    if sm is not None:
        sm.validate_models_to_run()
Example #13
0
def update_available_runs(project, scenario_name = '?'):
    '''
    Update the list of available runs and return a list of added runs.
    @param project (OpusProject): currently loaded project
    @return: Two lists; one of the runs that have been added, and the other of
    the runs that have been removed (tuple(list(String), list(String)))
    '''
    if not os.path.exists(project.data_path()):
        return ([], []) # Project data path doesn't exist -- so no runs to add

    results_manager = get_manager_instance('results_manager')
    removed_runs = []
    # Remove runs that exist in the project but not on disk
    run_nodes = get_available_run_nodes(project)
    existing_cache_directories = set()
    for run_node in run_nodes:
        cache_directory = os.path.normpath(run_node.find('cache_directory').text)
        if not os.path.exists(cache_directory):
            results_manager.delete_run(run_node, force=True)   #force deleting base_year_data entry when the directory doesn't exist
            removed_runs.append(run_node.get('name'))
            continue
        # Add the cachedir to the list of seen cachedirs
        existing_cache_directories.add(cache_directory)

    run_manager = get_run_manager()

    # Make sure that the base_year_data is in the list of available runs
    baseyear_dir = os.path.join(project.data_path(), 'base_year_data')
    baseyear_dir = os.path.normpath(baseyear_dir)
    years = []
    if not baseyear_dir in existing_cache_directories:
        try:
            run_manager.get_run_id_from_name(run_name = 'base_year_data')
        except:
            for dir_ in os.listdir(baseyear_dir):
                if len(dir_) == 4 and dir_.isdigit():
                    years.append(int(dir_))
            start_year = min(years)
            end_year = max(years)
            base_year = end_year
            run_name = 'base_year_data'
            run_id = run_manager._get_new_run_id()
            resources = {
                 'cache_directory': baseyear_dir,
                 'description': 'base year data',
                 'years': (base_year, end_year)
            }
            run_manager.add_row_to_history(run_id = run_id,
                                           resources = resources,
                                           status = 'done',
                                           run_name = run_name)

    runs = run_manager.get_run_info(resources = True, status = 'done')
    run_manager.close()

    # Make sure all runs with unique directories are list on the project
    added_runs = []
    for run_id, run_name, _, _, run_resources in runs:
        cache_directory = os.path.normpath(run_resources['cache_directory'])
        if run_name == 'base_year_data':
            found_scenario_name = ''
        else:
            found_scenario_name = scenario_name

        # don't add runs that we have already seen or that don't exist on disk
        if cache_directory in existing_cache_directories or not os.path.exists(cache_directory):
            continue
        start_year, end_year = run_resources['years']
        add_simulation_run(project,
                           cache_directory = cache_directory,
                           scenario_name = found_scenario_name,
                           run_name = run_name,
                           start_year = start_year,
                           end_year = end_year,
                           run_id = run_id)

        existing_cache_directories.add(cache_directory)
        added_runs.append(cache_directory)
    return (added_runs, removed_runs)
Example #14
0
def delete_simulation_run(project, run_name):
    run_node = project.find('results_manager/simulation_runs', name=run_name)
    if run_node:  # delete the run_node only when it exists
        get_manager_instance('results_manager').delete_run(run_node)