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 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 to_gms_file(mapshed_data): """ Given a dictionary of MapShed data, uses GWLF-E to convert it to a GMS file """ mapshed_areas = [round(a, 1) for a in mapshed_data['Area']] mapshed_data['Area'] = mapshed_areas pre_z = Parser.DataModel(mapshed_data) output = StringIO() writer = Parser.GmsWriter(output) writer.write(pre_z) output.seek(0) return output
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 run_gwlfe(model_input, inputmod_hash, watershed_id=None): """ 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 result['watershed_id'] = watershed_id return result
def setUp(self): self.basepath = os.path.abspath(os.path.join(__file__, '../')) testgms = os.path.join(self.basepath, 'input_4.gms') input_file = open(testgms, 'r') self.z = Parser.GmsReader(input_file).read()
files += 1 start_time = default_timer() filepath = os.path.abspath(os.path.join(__file__, '../../tests/', filename)) with open(filepath, 'r') as input_json: mapshed_data = json.load(input_json) timing['read_input_file'] = default_timer() # Round Areas mapshed_areas = [round(a, 1) for a in mapshed_data['Area']] mapshed_data['Area'] = mapshed_areas # 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()