def main(): log = logging.getLogger('gwlfe') log.setLevel(logging.DEBUG) ch = logging.StreamHandler() log.addHandler(ch) parser = argparse.ArgumentParser( description='Run the GWLF-E model for a specified gms file.') parser.add_argument('input', type=str, nargs="+", help='Input GMS file') parser.add_argument( '--output', type=str, nargs="?", help='Optional output file to write result of model run') args = parser.parse_args() gms_filenames = args.input for gms_filename in gms_filenames: fp = open(gms_filename, 'r') z = Parser.GmsReader(fp).read() result, z = gwlfe.run(z) if (args.output != None): # gms out filename is sepcified so write gmsout_filename = args.output log.debug("Writing GMS output file (%s)" % (gmsout_filename)) with open(gmsout_filename, "w") as file: test = Parser.GmsWriter(file) test.write(z)
def test_gms_output(self): """ Test that GmsWriter is able to replicate the sample GMS created from MapShed. """ z = Parser.GmsReader(self.input_file).read() output = StringIO() _, output_z = gwlfe.run(z) output_writer = Parser.GmsWriter(output) output_writer.writeOutput(output_z) ground_truth = csv.reader(self.output_file, delimiter=",") output.seek(0) output_parsed = csv.reader(output, delimiter=",") error = False for i, row in enumerate(zip(ground_truth, output_parsed)): for j, column in enumerate(zip(row[0], row[1])): ground_truth_val = column[0] output_val = column[1] try: try: np.testing.assert_allclose(float(ground_truth_val), float(output_val), rtol=1e-07) except ValueError: # catch all non float values self.assertEqual(ground_truth_val, output_val) except AssertionError as e: print("Error on line %i, column %i" % (i + 1, j)) print(e) error = True if (error == True): raise AssertionError("Not all variables within margin of error")
def test_gms_output(self): """ Test that GmsWriter is able to replicate the sample GMS created from MapShed. """ z = Parser.GmsReader(self.input_file).read() output = StringIO() _, output_z = gwlfe.run(z) output_writer = Parser.GmsWriter(output) output_writer.writeOutput(output_z) ground_truth = csv.reader(self.output_file, delimiter=",") output.seek(0) output_parsed = csv.reader(output, delimiter=",") error = False for i, row in enumerate(izip(ground_truth, output_parsed)): for j, column in enumerate(izip(row[0], row[1])): ground_truth_val = column[0] output_val = column[1] try: try: np.testing.assert_allclose(float(ground_truth_val), float(output_val), rtol=1e-07) except ValueError: # catch all non float values self.assertEqual(ground_truth_val, output_val) except AssertionError as e: print("Error on line %i, column %i" % (i + 1, j)) print(e) error = True if (error == True): raise AssertionError("Not all variables within margin of error")
def main(): log = logging.getLogger('gwlfe') log.setLevel(logging.DEBUG) ch = logging.StreamHandler() log.addHandler(ch) parser = argparse.ArgumentParser(description='Run the GWLF-E model for a specified gms file.') parser.add_argument('input', type=str, nargs="+", help='Input GMS file') parser.add_argument('--output', type=str, nargs="?", help='Optional output file to write result of model run') args = parser.parse_args() gms_filenames = args.input for gms_filename in gms_filenames: fp = open(gms_filename, 'r') z = Parser.GmsReader(fp).read() result, z = gwlfe.run(z) if (args.output != None): # gms out filename is sepcified so write gmsout_filename = args.output log.debug("Writing GMS output file (%s)" % (gmsout_filename)) with open(gmsout_filename, "w") as file: test = Parser.GmsWriter(file) test.write(z)
def setUpClass(self, input_file_name, output_file_name): self.basepath = os.path.abspath(os.path.join(__file__, '../')) inputfile = os.path.join(self.basepath,input_file_name) outputfile = os.path.join(self.basepath,output_file_name) input_file = open(inputfile, 'r') self.z = Parser.GmsReader(input_file).read() self.generated_output, _ = gwlfe.run(self.z) self.static_output = json.load(open(outputfile, 'r'))
def test_generated_output_matches_static_output(self): """ Test generated output using the sample GMS, input_4.gms versus static output generated using the same file. """ input_file = open('test/input_4.gms', 'r') z = parser.GmsReader(input_file).read() generated_output = gwlfe.run(z) static_output = json.load(open('test/input_4.output', 'r')) self.assertEqual(generated_output, static_output)
def main(): log = logging.getLogger('gwlfe') log.setLevel(logging.DEBUG) ch = logging.StreamHandler() log.addHandler(ch) gms_filename = sys.argv[1] fp = open(gms_filename, 'r') z = parser.GmsReader(fp).read() result = gwlfe.run(z) print(json.dumps(result, indent=4))
def run_gwlfe(model_input, inputmod_hash): """ Given a model_input resulting from a MapShed run, converts that dictionary to an intermediate GMS file representation, which is then parsed by GWLF-E to create the final data model z. We run GWLF-E on this final data model and return the results. This intermediate GMS file representation needs to be created because most of GWLF-E logic is written to handle GMS files, and to support dictionaries directly we would have to replicate all that logic. Thus, it is easier to simply create a GMS file and have it read that. """ output = to_gms_file(model_input) reader = parser.GmsReader(output) z = reader.read() result = gwlfe.run(z) result['inputmod_hash'] = inputmod_hash return result
# Prepare input GMS pre_z = Parser.DataModel(mapshed_data) output = StringIO() writer = Parser.GmsWriter(output) writer.write(pre_z) output.seek(0) timing['prepare_input_gms'] = default_timer() # Read input GMS reader = Parser.GmsReader(output) z = reader.read() timing['read_input_gms'] = default_timer() # Run the model result, _ = gwlfe.run(z) timing['run_gwlfe'] = default_timer() # Write to file outpath = os.path.abspath( os.path.join(__file__, '../output/', filename)) with open(outpath, 'w') as outfile: json.dump(result, outfile) timing['write_output_file'] = default_timer() # Save timings timing['total'] = timing['write_output_file'] - start_time for s in range(len(stages) - 2, 1, -1): timing[stages[s]] -= timing[stages[s - 1]] timing['read_input_file'] -= start_time
# Prepare input GMS pre_z = parser.DataModel(mapshed_data) output = StringIO() writer = parser.GmsWriter(output) writer.write(pre_z) output.seek(0) timing['prepare_input_gms'] = default_timer() # Read input GMS reader = parser.GmsReader(output) z = reader.read() timing['read_input_gms'] = default_timer() # Run the model result = gwlfe.run(z) timing['run_gwlfe'] = default_timer() # Write to file outpath = os.path.abspath( os.path.join(__file__, '../output/', filename)) with open(outpath, 'w') as outfile: json.dump(result, outfile) timing['write_output_file'] = default_timer() # Save timings timing['total'] = timing['write_output_file'] - start_time for s in range(len(stages) - 2, 1, -1): timing[stages[s]] -= timing[stages[s - 1]] timing['read_input_file'] -= start_time