Skip to content

Commit

Permalink
Merge pull request #121 from amoodie/strat_display
Browse files Browse the repository at this point in the history
Windows builds are broken for something netcdf/hdf5 library related, but not related to this PR.
  • Loading branch information
Andrew Moodie authored Oct 19, 2022
2 parents 552ff6f + 8fa35d1 commit d6e93b8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
2 changes: 1 addition & 1 deletion deltametrics/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ def __version__():
"""
Private version declaration, gets assigned to dm.__version__ during import
"""
return '0.3.3'
return '0.4.0'
57 changes: 48 additions & 9 deletions deltametrics/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,18 +722,50 @@ def get_display_arrays(VarInst, data=None):
# # DataSection # #
data = data or 'spacetime'
if data in VarInst.strat._spacetime_names:
_S, _Z = np.meshgrid(VarInst['s'], VarInst[VarInst.dims[0]])
_z = VarInst[VarInst.dims[0]]
_z = np.append(_z, _z[-1])
_s = VarInst['s']
_s = np.append(_s, _s[-1])
_S, _Z = np.meshgrid(_s, _z)
return VarInst.values, _S, _Z
elif data in VarInst.strat._preserved_names:
VarInst.strat._check_knows_spacetime()
_S, _Z = np.meshgrid(VarInst['s'], VarInst[VarInst.dims[0]])
_z = VarInst[VarInst.dims[0]]
_z = np.append(_z, _z[-1])
_s = VarInst['s']
_s = np.append(_s, _s[-1])
_S, _Z = np.meshgrid(_s, _z)
return VarInst.strat.as_preserved(), _S, _Z
elif data in VarInst.strat._stratigraphy_names:
# for data sections and mesh quick strat display
# there are a fair number of stylistic quirks here,
# do not use this representation of the data for any
# science calculations!

# make the sparse array dense
_sp = VarInst.strat.as_stratigraphy()
_den = _sp.toarray() # .view(section.DataSectionVariable)
_den = _sp.toarray()

# grab the y values for the array
_arr_Y = VarInst.strat.strat_attr['psvd_flld'][:_sp.shape[0], ...]

# fix the bottom rows
_den[0, ...] = _den[1, ...]
_arr_Y[0, ...] = _arr_Y[1, ...]

# fix the dense values at tops
# not figured this out yet...

# pad the arrays to be data+1 in both dims
_arr_X = np.tile(VarInst['s'], (_sp.shape[0], 1))
return _den[1:, 1:], _arr_X, _arr_Y
_arr_Y = np.pad(_arr_Y, ((0, 0), (0, 1)), mode='edge')
_z = VarInst[VarInst.dims[0]]

# prepend the data with the lowest depth recorded to fill out image
_p = np.min(_arr_Y) * np.ones((_arr_Y.shape[1])) # prepend base
_arr_Y = np.vstack((_p, _arr_Y))
_arr_X = np.pad(_arr_X, ((0, 1), (0, 1)), mode='edge')
return _den, _arr_X, _arr_Y
else:
raise ValueError('Bad data argument: %s' % str(data))

Expand All @@ -745,7 +777,11 @@ def get_display_arrays(VarInst, data=None):
elif data in VarInst.strat._preserved_names:
VarInst.strat._check_knows_spacetime() # always False
elif data in VarInst.strat._stratigraphy_names:
_S, _Z = np.meshgrid(VarInst['s'], VarInst[VarInst.dims[0]])
_z = VarInst[VarInst.dims[0]]
_z = np.append(_z, _z[-1])
_s = VarInst['s']
_s = np.append(_s, _s[-1])
_S, _Z = np.meshgrid(_s, _z)
return VarInst, _S, _Z
else:
raise ValueError('Bad data argument: %s' % str(data))
Expand Down Expand Up @@ -1026,12 +1062,14 @@ def show_one_dimensional_trajectory_to_strata(e, sigma_dist=None,
e = strat._adjust_elevation_by_subsidence(e_in, sigma_dist)
s, p = strat._compute_elevation_to_preservation(e) # strat, preservation
z = strat._determine_strat_coordinates(e, dz=dz, z=z, nz=nz) # vert coordinates
z_disp = np.append(z, z[-1])
sc, dc = strat._compute_preservation_to_cube(s, z)
lst = np.argmin(s < s[-1]) # last elevation

c = np.full_like(z, np.nan)
c[sc[:, 0]] = t[dc[:, 0]]
cp = np.tile(c, (2, 1)).T
cp = np.atleast_2d(c).T

# make the plots
if not ax:
Expand Down Expand Up @@ -1060,17 +1098,18 @@ def show_one_dimensional_trajectory_to_strata(e, sigma_dist=None,
ax_s = divider.append_axes("right", 0.5, pad=0.1, sharey=ax)
ax_s.yaxis.tick_right()
ax_s.xaxis.set_visible(False)
__x, __y = np.meshgrid(np.array([0, 1]), z)
__x, __y = np.meshgrid(np.array([0, 1]), z_disp)
_colmap = plt.cm.get_cmap('viridis', e.shape[0])
ax_s.pcolormesh(__x, __y, cp,
cmap=_colmap, vmin=0, vmax=e.shape[0],
shading='auto')
rasterized=True, shading='flat')
ax_s.hlines(e[p], 0, 1, linestyles='dashed', colors='gray')
_cstr = [str(int(cc)) if np.isfinite(cc) else 'nan' for cc in c.flatten()]
ax_s.set_xlim(0, 1)
if label_strata:
for i, __cstr in enumerate(_cstr):
ax_s.text(0.3, z[i], str(__cstr), fontsize=8)
for i, __cstr in enumerate(_cstr[:-1]):
ax_s.text(0.5, z[i]+((z[i+1]-z[i])/2), str(__cstr),
fontsize=8, ha='center', va='center')

# adjust and add legend
if np.any(e < 0):
Expand Down
2 changes: 1 addition & 1 deletion deltametrics/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def show(self, SectionAttribute, style='shaded', data=None,
ci = ax.pcolormesh(_X, _Y, _data, cmap=_varinfo.cmap,
norm=_varinfo.norm,
vmin=_varinfo.vmin, vmax=_varinfo.vmax,
rasterized=True, shading='auto')
shading='flat', rasterized=True)
elif style in ['line', 'lines']:
_data, _segments = plot.get_display_lines(SectionVariableInstance,
data=data)
Expand Down
4 changes: 2 additions & 2 deletions deltametrics/strat.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ def _compute_preservation_to_cube(strata, z):
with rows in `strat_coords`.
"""
# preallocate boxy arrays and helpers
plate = np.atleast_1d(np.zeros(strata.shape[1:], dtype=np.int8))
plate = np.atleast_1d(np.zeros(strata.shape[1:], dtype=int))
strat_coords, data_coords = [], [] # preallocate sparse idx lists
_zero = np.array([0])

Expand All @@ -600,7 +600,7 @@ def _compute_preservation_to_cube(strata, z):
for k in np.arange(len(z) - 1, -1, -1): # for every z, from the top
e = z[k] # which elevation for this iteration
whr = e < seek_elev # where elev is below strat surface
t = np.maximum(_zero, (np.argmin(strata[:, ...] <= e, axis=0) - 1))
t = np.maximum(_zero, (np.argmin(strata[:, ...] <= e, axis=0)))
plate[whr] = int(1) # track locations in the plate

xy = plate.nonzero()
Expand Down
20 changes: 16 additions & 4 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,10 @@ class TestGetDisplayArrays:
def test_dsv_nostrat_get_display_arrays_spacetime(self):
_data, _X, _Y = plot.get_display_arrays(self.dsv_nostrat,
data='spacetime')
assert (_data.shape == _X.shape) and (_data.shape == _Y.shape)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert np.all(_data == self.dsv_nostrat)

def test_dsv_nostrat_get_display_arrays_preserved(self):
Expand All @@ -424,13 +427,19 @@ def test_dsv_nostrat_get_display_arrays_stratigraphy(self):
def test_dsv_get_display_arrays_spacetime(self):
_data, _X, _Y = plot.get_display_arrays(self.dsv,
data='spacetime')
assert (_data.shape == _X.shape) and (_data.shape == _Y.shape)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert np.all(_data == self.dsv)

def test_dsv_get_display_arrays_preserved(self):
_data, _X, _Y = plot.get_display_arrays(self.dsv,
data='preserved')
assert (_data.shape == _X.shape) and (_data.shape == _Y.shape)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert np.any(np.isnan(_data)) # check that some are False

def test_dsv_get_display_arrays_stratigraphy(self):
Expand Down Expand Up @@ -461,7 +470,10 @@ def test_ssv_get_display_arrays_preserved(self):
def test_ssv_get_display_arrays_stratigraphy(self):
_data, _X, _Y = plot.get_display_arrays(
self.ssv, data='stratigraphy')
assert (_data.shape == _X.shape) and (_data.shape == _Y.shape)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)
assert (_data.shape[0] == _X.shape[0] - 1)
assert (_data.shape[1] == _Y.shape[1] - 1)

def test_ssv_get_display_arrays_badstring(self):
with pytest.raises(ValueError, match=r'Bad data *.'):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_strat.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_onedim_traj_drop_at_end(self):
assert s[-1] == e[-1]
assert s[-1] == e[-1]
assert np.all(np.isnan(c[2:])) # from z>=1 to z==3
assert np.all(c[:2] == 0)
assert np.all(c[:2] == 1)
assert lst == 1

def test_onedim_traj_drop_at_end_to_zero(self):
Expand All @@ -335,7 +335,7 @@ def test_onedim_traj_upsanddowns(self):
assert z[-1] == 7
assert s[-1] == 6
assert np.all(p.nonzero()[0] == (2, 3, 7, 10))
assert c[0] == 1
assert c[0] == 2

def test_onedim_traj_upsanddowns_negatives(self):
# e = np.array([0, 0, -1, -4, -2, 3, 3.5, 3, 3, 4, 4])
Expand Down

0 comments on commit d6e93b8

Please sign in to comment.