def validateParameters(self): 'Warn if parameters are missing or unknown' # Initialize scenarioInput = self.input pushWarning = lambda x: store.pushWarning(self.id, x) configurationPacks = [ ('Metric', metric.getModel(scenarioInput['metric model name']), scenarioInput['metric configuration']), ('Network', network.getModel(scenarioInput['network model name']), scenarioInput['network configuration']), ] # For each configurationPack, for configurationName, configurationModel, configuration in configurationPacks: # Load default valueByOptionBySection = configurationModel.VariableStore().getValueByOptionBySection() # Make sure that the configuration has the same key hierarchy as the default for section, valueByOption in valueByOptionBySection.iteritems(): if section not in configuration: pushWarning('%s section missing: %s' % (configurationName, section)) continue for option in valueByOption: if option not in configuration[section]: pushWarning('%s option missing: %s > %s' % (configurationName, section, option)) # Make sure that the input does not have unnecessary parameters for section, valueByOption in configuration.iteritems(): if section not in valueByOptionBySection: pushWarning('%s section unknown: %s' % (configurationName, section)) continue for option in valueByOption: if option not in valueByOptionBySection[section]: pushWarning('%s option unknown: %s > %s' % (configurationName, section, option))
def run(self): # Prepare scenarioInput = self.input scenarioFolder = self.getFolder() expandPath = lambda x: os.path.join(scenarioFolder, x) # Register demographics print 'Registering demographics' nodesPath = expandPath('nodes') targetPath = self.getDatasetPath() sourcePath = expandPath(scenarioInput['demographic file name']) datasetStore = dataset_store.create(targetPath, sourcePath) datasetStore.saveNodesSHP(nodesPath) datasetStore.saveNodesCSV(nodesPath) # Apply metric print 'Applying metric' metricModel = metric.getModel(scenarioInput['metric model name']) metricConfiguration = scenarioInput['metric configuration'] metricValueByOptionBySection = datasetStore.applyMetric(metricModel, metricConfiguration) # Build network print 'Building network' networkModel = network.getModel(scenarioInput['network model name']) networkConfiguration = scenarioInput['network configuration'] networkValueByOptionBySection = datasetStore.buildNetwork(networkModel, networkConfiguration) # Update metric print 'Updating metric' metricValueByOptionBySection = datasetStore.updateMetric(metricModel, metricValueByOptionBySection) # Save output print 'Saving output' metric.saveMetricsCSV(expandPath('metrics-global'), metricModel, metricValueByOptionBySection) datasetStore.saveMetricsCSV(expandPath('metrics-local'), metricModel) datasetStore.saveSegmentsSHP(expandPath('networks-existing'), is_existing=True) datasetStore.saveSegmentsSHP(expandPath('networks-proposed'), is_existing=False) # Bundle store.zipFolder(scenarioFolder + '.zip', scenarioFolder) # Validate self.validateParameters() # Save output self.output = { 'variables': { 'node': dict((str(x.id), dict(input=x.input, output=x.output)) for x in datasetStore.cycleNodes()), 'metric': metricValueByOptionBySection, 'network': networkValueByOptionBySection, }, 'statistics': { 'node': datasetStore.getNodeStatistics(), 'metric': datasetStore.getMetricStatistics(), 'network': datasetStore.getNetworkStatistics(), }, 'warnings': store.popWarnings(self.id), } # Commit Session.commit()
def test_scenarioRun(self): 'for now, just make sure it runs' sourcePath = os.path.join(inputDataPath, "sample_demand_nodes.csv") # make output dir if not exists if not os.path.exists(outputDataPath): os.makedirs(outputDataPath) targetPath = os.path.join(outputDataPath, "dataset.db") datasetStore = dataset_store.create(targetPath, sourcePath) """ // Sample Model Parameter JSON metricValueByOptionBySection = { 'demand (household)': {'household unit demand per household per year': 50} } """ metricConfigPath = os.path.join(baseDirPath, "sample_metric_params.json") metricConfiguration = json.load(open(metricConfigPath, 'r')) """ // Sample Model Parameter JSON networkValueByOptionBySection = { 'algorithm': {'minimum node count per subnetwork': 2} } """ networkConfigPath = os.path.join(baseDirPath, "network_params.json") networkConfiguration = json.load(open(networkConfigPath, 'r')) # Run metric model metricModel = metric.getModel("mvMax5") metricValueByOptionBySection = datasetStore.applyMetric(metricModel, metricConfiguration) # Now that metrics (mvMax in particular) have been calculated # we can build the network networkModel = network.getModel("modKruskal") networkValueByOptionBySection = datasetStore.buildNetwork(networkModel, networkConfiguration) # Now that the network's been built (and the electrification option # is chosen) run the aggregate calculations metricValueByOptionBySection = datasetStore.updateMetric(metricModel, metricValueByOptionBySection) metric.saveMetricsConfigurationCSV(os.path.join(outputDataPath, 'metrics-job-input'), metricConfiguration) metric.saveMetricsCSV(os.path.join(outputDataPath, 'metrics-global'), metricModel, metricValueByOptionBySection) datasetStore.saveMetricsCSV(os.path.join(outputDataPath, 'metrics-local'), metricModel) datasetStore.saveSegmentsSHP(os.path.join(outputDataPath, 'networks-proposed'), is_existing=False)
def clone(self, scenarioID): 'Show form to create a new item based on datasets and parameters from existing scenario' # Make sure the user is logged in personID = h.getPersonID() if not personID: return redirect(url('person_login', targetURL=h.encodeURL(request.path))) # Make sure the user has access to the scenario scenario = Session.query(model.Scenario).filter(model.getScopeFilter(personID)).filter(model.Scenario.id==scenarioID).first() if not scenario: return redirect(url('new_scenario')) # Load scenarioInput = scenario.input # Prepare c.scenario = scenario c.metricModel = metric.getModel(request.GET.get('metricModel', scenarioInput['metric model name'])) c.metricConfiguration = scenarioInput['metric configuration'] c.networkModel = network.getModel(request.GET.get('networkModel', scenarioInput['network model name'])) c.networkConfiguration = scenarioInput['network configuration'] # Return return render('/scenarios/new.mako')
def new(self, format='html'): 'GET /scenarios/new: Show form to create a new item' # If the user is not logged in, if not h.isPerson(): # Redirect to login return redirect(url('person_login', targetURL=h.encodeURL(h.url('new_scenario')))) # Make sure that the requested metric model exists metricModelNames = metric.getModelNames() metricModelName = request.GET.get('metricModel') if metricModelName not in metricModelNames: metricModelName = metricModelNames[0] c.metricModel = metric.getModel(metricModelName) c.metricConfiguration = {} # Make sure that the requested network model exists networkModelNames = network.getModelNames() networkModelName = request.GET.get('networkModel') if networkModelName not in networkModelNames: networkModelName = networkModelNames[0] c.networkModel = network.getModel(networkModelName) c.networkConfiguration = {} # Render form c.scenario = None return render('/scenarios/new.mako')
def run(self): # Prepare scenarioInput = self.input scenarioFolder = self.getFolder() expandPath = lambda x: os.path.join(scenarioFolder, x) # Setup status reporting from time import localtime, strftime time_format = "%Y-%m-%d %H:%M:%S" # Register demographics Job.log("Registering demographics") print "%s Registering demographics" % strftime(time_format, localtime()) nodesPath = expandPath('nodes') targetPath = self.getDatasetPath() sourcePath = expandPath(scenarioInput['demographic file name']) datasetStore = dataset_store.create(targetPath, sourcePath) datasetStore.saveNodesSHP(nodesPath) datasetStore.saveNodesCSV(nodesPath) # Apply metric Job.log("Applying metric") print "%s Applying metric" % strftime(time_format, localtime()) metricModel = metric.getModel(scenarioInput['metric model name']) metricConfiguration = scenarioInput['metric configuration'] metricValueByOptionBySection = datasetStore.applyMetric(metricModel, metricConfiguration) # Build network Job.log("Building network") print "%s Building network" % strftime(time_format, localtime()) networkModel = network.getModel(scenarioInput['network model name']) networkConfiguration = scenarioInput['network configuration'] networkValueByOptionBySection = datasetStore.buildNetwork(networkModel, networkConfiguration, jobLogger=Job) # Update metric Job.log("Updating metric") print "%s Updating metric" % strftime(time_format, localtime()) metricValueByOptionBySection = datasetStore.updateMetric(metricModel, metricValueByOptionBySection) # Save output Job.log("Saving output") print "%s Saving output" % strftime(time_format, localtime()) metric.saveMetricsConfigurationCSV(expandPath('metrics-job-input'), metricConfiguration) metric.saveMetricsCSV(expandPath('metrics-global'), metricModel, metricValueByOptionBySection) datasetStore.saveMetricsCSV(expandPath('metrics-local'), metricModel) datasetStore.saveSegmentsSHP(expandPath('networks-existing'), is_existing=True) datasetStore.saveSegmentsSHP(expandPath('networks-proposed'), is_existing=False) # Bundle store.zipFolder(scenarioFolder + '.zip', scenarioFolder) # Validate self.validateParameters() # Save output self.output = { 'variables': { 'node': dict((str(x.id), dict(input=x.input, output=x.output)) for x in datasetStore.cycleNodes()), 'metric': metricValueByOptionBySection, 'network': networkValueByOptionBySection, }, 'statistics': { 'node': datasetStore.getNodeStatistics(), 'metric': datasetStore.getMetricStatistics(), 'network': datasetStore.getNetworkStatistics(), }, 'warnings': store.popWarnings(self.id), } # Commit Session.commit()
# If we are running the script from the command-line, if __name__ == '__main__': # For each metric model, for metricModelName in metric.getModelNames(): # Load metric model metricModel = metric.getModel(metricModelName) metricRoots, metricLines, metricRows = generateDocumentation(metricModel) # Save and close referenceFile = open(os.path.join(script_process.basePath, 'docs/metric-%s.rst' % metricModelName), 'wt') referenceFile.write(formatHeader('Metric Model %s' % metricModelName, '=')) for root in sorted(metricRoots, key=lambda x: metricModel.roots.index(x)): referenceFile.write('- :ref:`%s`\n' % variable_store.formatLabel(root)) referenceFile.write('\n\n' + 'You can override the value of any variable in the model on a node-by-node basis. To perform a node-level override, use the aliases in the following table as additional columns in your spreadsheet or fields in your shapefile. Both long and short aliases are recognized.') referenceFile.write('\n\n' + formatTable(['Long alias', 'Short alias', 'Units'], metricRows)) referenceFile.write('\n\n' + '\n'.join(metricLines)) referenceFile.close() # For each network model, for networkModelName in network.getModelNames(): # Load network model networkModel = network.getModel(networkModelName) networkRoots, networkLines, networkRows = generateDocumentation(networkModel) # Save and close referenceFile = open(os.path.join(script_process.basePath, 'docs/network-%s.rst' % networkModelName), 'wt') referenceFile.write(formatHeader('Network Model %s' % networkModelName, '=')) for root in sorted(networkRoots, key=lambda x: networkModel.roots.index(x)): referenceFile.write('- :ref:`%s`\n' % variable_store.formatLabel(root)) referenceFile.write('\n\n' + '\n'.join(networkLines)) referenceFile.close()
args = parser.parse_args() # make output dir if not exists outputDataPath = args.output_path if not os.path.exists(outputDataPath): os.makedirs(outputDataPath) targetPath = os.path.join(outputDataPath, "dataset.db") datasetStore = dataset_store.create(targetPath, args.input_nodes_file) # setup models metricModel = metric.getModel(args.metric_model_name) metricConfiguration = json.load(args.metric_model_params) networkModel = network.getModel(args.network_model_name) networkConfiguration = json.load(args.network_model_params) # Run metric model metricValueByOptionBySection = datasetStore.applyMetric(metricModel, metricConfiguration) # Now that metrics (mvMax in particular) have been calculated # we can build the network networkValueByOptionBySection = datasetStore.buildNetwork(networkModel, networkConfiguration) # Now that the network's been built (and the electrification option # is chosen) run the aggregate calculations metricValueByOptionBySection = datasetStore.updateMetric(metricModel, metricValueByOptionBySection) metric.saveMetricsConfigurationCSV(os.path.join(outputDataPath, 'metrics-job-input'), metricConfiguration) metric.saveMetricsCSV(os.path.join(outputDataPath, 'metrics-global'), metricModel, metricValueByOptionBySection)