Skip to content

Commit

Permalink
test(test_flows): refactor test_add_to_perioddata into independent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleaf committed Oct 18, 2024
1 parent 002ee06 commit 59220ab
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 62 deletions.
29 changes: 20 additions & 9 deletions sfrmaker/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def shellmound_model(get_shellmound_model):
return copy.deepcopy(get_shellmound_model)


@pytest.fixture(scope='function')
def shellmound_grid(shellmound_model):
m = shellmound_model
@pytest.fixture(scope='module')
def get_shellmound_grid(get_shellmound_model):
m = copy.deepcopy(get_shellmound_model)
mg = flopy.discretization.StructuredGrid(delr=m.dis.delr.array, # cell spacing along a row
delc=m.dis.delc.array, # cell spacing along a column
xoff=500955.0, yoff=1176285.0, # lower left corner of model grid
Expand All @@ -154,6 +154,9 @@ def shellmound_grid(shellmound_model):
m._modelgrid = mg
return mg

@pytest.fixture(scope='function')
def shellmound_grid(get_shellmound_grid):
return copy.deepcopy(get_shellmound_grid)

@pytest.fixture(scope='function')
def shellmound_sfrmaker_grid(shellmound_grid):
Expand Down Expand Up @@ -239,7 +242,7 @@ def tylerforks_sfrdata(tylerforks_model, tylerforks_lines_from_NHDPlus,


@pytest.fixture(scope='module')
def lines_from_shapefile(test_data_path):
def get_lines_from_shapefile(test_data_path):
flowlines_file = '{}/shellmound/flowlines.shp'.format(test_data_path)
lns = sfrmaker.Lines.from_shapefile(flowlines_file,
id_column='COMID',
Expand All @@ -258,16 +261,25 @@ def lines_from_shapefile(test_data_path):


@pytest.fixture(scope='function')
def shellmound_sfrdata(shellmound_model, lines_from_shapefile,
shellmound_grid):
m = shellmound_model
lines = copy.deepcopy(lines_from_shapefile)
def lines_from_shapefile(get_lines_from_shapefile):
return copy.deepcopy(get_lines_from_shapefile)


@pytest.fixture(scope='module')
def get_shellmound_sfrdata(get_shellmound_model, get_lines_from_shapefile,
get_shellmound_grid):
m = copy.deepcopy(get_shellmound_model)
lines = copy.deepcopy(get_lines_from_shapefile)
shellmound_grid = copy.deepcopy(get_shellmound_grid)
# from the lines and StructuredGrid instances, make a sfrmaker.sfrdata instance
# (lines are intersected with the model grid and converted to reaches, etc.)
sfrdata = lines.to_sfr(grid=shellmound_grid,
model=m)
return sfrdata

@pytest.fixture(scope='function')
def shellmound_sfrdata(get_shellmound_sfrdata):
return copy.deepcopy(get_shellmound_sfrdata)

@pytest.fixture(params=['shellmound_sfrdata',
'tylerforks_sfrdata'])
Expand All @@ -277,7 +289,6 @@ def sfrdata(request,
return {'shellmound_sfrdata': shellmound_sfrdata,
'tylerforks_sfrdata': tylerforks_sfrdata}[request.param]


@pytest.fixture(scope='function')
def neversink_lines_from_nhdplus_hr(datapath):
NHDPlusHR_paths = [f'{datapath}/neversink_rondout/NHDPLUS_HR_1.gdb',
Expand Down
123 changes: 70 additions & 53 deletions sfrmaker/test/test_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,104 +61,121 @@ def test_get_inflows_from_parent_model(shellmound_sfrdata):
#inset_grid, active_area=None
)


def test_add_to_perioddata(shellmound_sfrdata):
sfrd = shellmound_sfrdata # copy.deepcopy(shellmound_sfrdata)
rd = shellmound_sfrdata.reach_data.copy()
line_id = dict(zip(rd.iseg, rd.line_id))
@pytest.fixture(scope='function')
def data(shellmound_sfrdata):
sfr_routing = shellmound_sfrdata.segment_routing.copy()
data = {
'sfrd': shellmound_sfrdata, # copy.deepcopy(shellmound_sfrdata)
'rd': shellmound_sfrdata.reach_data.copy()
}
line_id = dict(zip(data['rd'].iseg, data['rd'].line_id))

# routing for source hydrography
flowline_routing = {line_id.get(k, 0): line_id.get(v, 0)
for k, v in sfr_routing.items()}
# routing for source hydrography
data['flowline_routing'] = {line_id.get(k, 0): line_id.get(v, 0)
for k, v in sfr_routing.items()}
nlines = 4
seq = add_line_sequence(flowline_routing, nlines=nlines)

data['seq'] = add_line_sequence(data['flowline_routing'], nlines=nlines)
return data

def test_add_to_perioddata1(data):
# two inflows applied upstream of model on same path;
# results should be summed an applied to first line_id in model
sfrd = data['sfrd']
flowline_routing = data['flowline_routing']
rd = data['rd']
flows = pd.DataFrame({'Q_avg': [100., 10., 200., 20.],
'per': [0, 1, 0, 1],
'line_id': [2, 2, 4, 4]})
'per': [0, 1, 0, 1],
'line_id': [2, 2, 4, 4]})
add_to_perioddata(sfrd, flows,
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=False)
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=False)
flows = flows.groupby('per').sum()
assert np.allclose(sfrd.period_data['inflow'].values, flows['Q_avg'].values)
assert np.allclose(sfrd.period_data.index.get_level_values(0),
flows.index.values)
assert rd.loc[rd.line_id == seq[-1], 'rno'].values[0] == sfrd.period_data.index.levels[1].values[0]

flows.index.values)
assert rd.loc[rd.line_id == data['seq'][-1], 'rno'].values[0] ==\
sfrd.period_data.index.levels[1].values[0]

def test_add_to_perioddata2(data):
# two inflows applied upstream of model on different paths;
# inflows should be applied to the first lines in the model
# along their respective paths
sfrd = data['sfrd']
flowline_routing = data['flowline_routing']
rd = data['rd']
flowline_routing['6'] = '1000005'
flows = pd.DataFrame({'Q_avg': [100., 10., 200., 20.],
'per': [0, 1, 0, 1],
'line_id': [6, 6, 4, 4]})
'per': [0, 1, 0, 1],
'line_id': [6, 6, 4, 4]})
add_to_perioddata(sfrd, flows,
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=False)
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=False)
assert np.allclose(sfrd.period_data['inflow'].sort_values(), flows['Q_avg'].sort_values())
assert np.allclose(sfrd.period_data.index.get_level_values(0),
sorted(flows['per']))
lines = {flowline_routing['6'], seq[-1]}
sorted(flows['per']))
lines = {flowline_routing['6'], data['seq'][-1]}
rnos = rd.loc[rd.line_id.isin(lines), 'rno']
assert not set(sfrd.period_data.index.levels[1]).difference(rnos)
assert len(set(sfrd.period_data.index.levels[1])) == 2

def test_add_to_perioddata3(data):
# two inflows applied along same path in model;
# one_inflow_per_path=True
# inflows should be summed and applied to most downstream reach
# test updating of period_data
# sfrd.period_data wasn't reset, so should contain
# updated values of 300 and 30 at reach 354
# and existing values of 100 and 10 at 395
sfrd = data['sfrd']
flowline_routing = data['flowline_routing']
flows = pd.DataFrame({'Q_avg': [100., 10., 200., 20.],
'per': [0, 1, 0, 1],
'line_id': [2, 2, 4, 4]})
'per': [0, 1, 0, 1],
'line_id': [2, 2, 4, 4]})
add_to_perioddata(sfrd, flows,
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=True)
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=True)
assert np.allclose(sfrd.period_data['inflow'].values, [300., 100., 30., 10.])
# these reach numbers are liable to change
rnos = [
sfrd.reach_data.loc[sfrd.reach_data['line_id'] == seq[-1], 'rno'].min(),
sfrd.reach_data.loc[sfrd.reach_data['line_id'] == data['seq'][-1], 'rno'].min(),
sfrd.reach_data.loc[sfrd.reach_data['line_id'] == '1000005', 'rno'].min()
]
assert np.allclose(sfrd.period_data.index.levels[1], rnos)

def test_add_to_perioddata3(data):
# two inflows applied along same path in model;
# one_inflow_per_path=False
# inflows should be applied to their respective reaches

sfrd = data['sfrd']
flowline_routing = data['flowline_routing']
flows = pd.DataFrame({'Q_avg': [100., 10., 200., 20.],
'per': [0, 1, 0, 1],
'line_id': ['17955337', '17955337',
flowline_routing['17955337'],
flowline_routing['17955337']
]})
'per': [0, 1, 0, 1],
'line_id': ['17955337', '17955337',
flowline_routing['17955337'],
flowline_routing['17955337']
]})
sfrd._period_data = None
add_to_perioddata(sfrd, flows,
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=False)
flowline_routing=flowline_routing,
variable='inflow',
line_id_column='line_id',
period_column='per',
data_column='Q_avg',
one_inflow_per_path=False)
assert np.allclose(sfrd.period_data['inflow'].sort_index(level='rno').values,
[100., 10., 200., 20.])
[100., 10., 200., 20.])
assert np.allclose(sfrd.period_data.index.levels[1], [354, 401])


Expand Down

0 comments on commit 59220ab

Please sign in to comment.