def saveMetricsCSV(self, targetPath, metricModel, headerType=VS.HEADER_TYPE_SECTION_OPTION): 'Save node-level metrics in CSV format' # Make sure that nodes exist if not self.countNodes(): return # Prepare column headers in order # Use the 1st node's input to get the "pass-through" fields node = self.cycleNodes().next() nodeInput = node.input headerPacks = [('', key) for key in sorted(nodeInput)] # get the section/option values in order from the model # and append to the headerPacks # Note: metricModel.VariableStore.variableClasses should have all the # the variableClasses associated with the model as long as # metricModel.VariableStore() has been called. baseVars = metricModel.VariableStore.variableClasses baseVarHeaders = [(var.section, var.option) for var in sorted(baseVars, key=lambda v: (v.section, v.option))] headerPacks.extend(baseVarHeaders) headerPacksToNames = VS.getFieldNamesForHeaderPacks(metricModel, headerPacks, headerType) csvWriter = csv.writer(open(store.replaceFileExtension(targetPath, 'csv'), 'wb')) csvWriter.writerow(['PROJ.4 ' + self.getProj4()]) csvWriter.writerow([headerPacksToNames[(section, option)] for section, option in headerPacks]) # csvWriter.writerow(['%s > %s' % (section.capitalize(), option.capitalize()) if section else option.capitalize() for section, option in headerPacks]) # For each node, for node in self.cycleNodes(): # Write row csvWriter.writerow([node.output.get(section, {}).get(option, '') if section else node.input.get(option, '') for section, option in headerPacks])
def saveMetricsCSV(self, targetPath, metricModel, headerType=VS.HEADER_TYPE_SECTION_OPTION): 'Save node-level metrics in CSV format' # Make sure that nodes exist if not self.countNodes(): return # Prepare column headers in order # Use the 1st node's input to get the "pass-through" fields node = self.cycleNodes().next() nodeInput = node.input headerPacks = [('', key) for key in sorted(nodeInput)] # get the section/option values in order from the model # and append to the headerPacks # Note: metricModel.VariableStore.variableClasses should have all the # the variableClasses associated with the model as long as # metricModel.VariableStore() has been called. baseVars = metricModel.VariableStore.variableClasses baseVarHeaders = [ (var.section, var.option) for var in sorted(baseVars, key=lambda v: (v.section, v.option)) ] headerPacks.extend(baseVarHeaders) headerPacksToNames = VS.getFieldNamesForHeaderPacks( metricModel, headerPacks, headerType) csvWriter = csv.writer( open(store.replaceFileExtension(targetPath, 'csv'), 'wb')) csvWriter.writerow(['PROJ.4 ' + self.getProj4()]) csvWriter.writerow([ headerPacksToNames[(section, option)] for section, option in headerPacks ]) # csvWriter.writerow(['%s > %s' % (section.capitalize(), option.capitalize()) if section else option.capitalize() for section, option in headerPacks]) # For each node, for node in self.cycleNodes(): # Write row csvWriter.writerow([ node.output.get(section, {}).get(option, '') if section else node.input.get(option, '') for section, option in headerPacks ])
# prep the csv output stream csvWriter = csv.writer(args.outfile) # node input is for the "pass-through" fields nodeInput = node.input headerPacks = [('', key) for key in sorted(nodeInput)] # handle the aliases? # get the section/option values in order from the model # and append to the headerPacks # Note: metricModel.VariableStore.variableClasses should have all the # the variableClasses associated with the model as long as # metricModel.VariableStore() has been called. baseVars = metricModel.VariableStore.variableClasses baseVarHeaders = [(var.section, var.option) for var in sorted(baseVars, key=lambda v: (v.section, v.option))] headerPacks.extend(baseVarHeaders) headerPacksToNames = VS.getFieldNamesForHeaderPacks(metricModel, headerPacks, args.header_type) csvWriter.writerow([headerPacksToNames[(section, option)] for section, option in headerPacks]) node_gen = (node for node in nodes if not node.is_fake) for node in node_gen: # Write row for all node's values (by section, option) csvWriter.writerow([node.output.get(section, {}).get(option, '') if section else node.input.get(option, '') for section, option in headerPacks]) # TODO: do anything with jobVS? Output summary stats?