Skip to content

Commit

Permalink
Merge pull request #584 from LinkedEarth/depth_nans
Browse files Browse the repository at this point in the history
Depth nans
  • Loading branch information
khider authored Jun 19, 2024
2 parents 3080ce3 + e6f8ca8 commit 311db8f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pyleoclim/core/geoseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,17 @@ def __init__(self, time, value, lat, lon, elevation = None, time_unit=None, time
time_name='Time'
else:
pass


if dropna:
if depth is not None:
matrix = np.array([time,value,depth])
matrix = tsbase.dropna_matrix(matrix)
time = matrix[0,:]
value = matrix[1,:]
depth = matrix[2,:]
else:
pass

# assign latitude
if lat is not None:
lat = float(lat)
Expand Down
41 changes: 41 additions & 0 deletions pyleoclim/tests/test_core_GeoSeries.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,47 @@ def multiple_pinkgeoseries(nrecs = 20, seed = 108, geobox=[-85.0,85.0,-180,180])

return pyleo.MultipleGeoSeries(ts_list, label='Multiple Pink GeoSeries')

class TestUIGeoSeriesInit:
''' Test for GeoSeries instantiation '''

def test_init_no_dropna_depth(self, evenly_spaced_series):
ts = evenly_spaced_series
t = ts.time
v = ts.value
d = np.arange(len(t))
v[0] = np.nan
ts2 = pyleo.GeoSeries(time=t,value=v,depth=d,dropna=False, verbose=False,lat=0,lon=0)
assert np.isnan(ts2.value[0])
assert ts2.depth[0] == d[0]

def test_init_dropna_depth(self, evenly_spaced_series):
ts = evenly_spaced_series
t = ts.time
v = ts.value
d = np.arange(len(t))
v[0] = np.nan
ts2 = pyleo.GeoSeries(time=t,value=v,depth=d,dropna=True, verbose=False,lat=0,lon=0)
print(ts2.value)
assert ~np.isnan(ts2.value[0])
assert ts2.depth[0] == d[1]

def test_init_no_dropna(self, evenly_spaced_series):
ts = evenly_spaced_series
t = ts.time
v = ts.value
v[0] = np.nan
ts2 = pyleo.GeoSeries(time=t,value=v,dropna=False, verbose=False,lat=0,lon=0)
assert np.isnan(ts2.value[0])

def test_init_dropna(self, evenly_spaced_series):
ts = evenly_spaced_series
t = ts.time
v = ts.value
v[0] = np.nan
ts2 = pyleo.GeoSeries(time=t,value=v,dropna=True, verbose=False,lat=0,lon=0)
print(ts2.value)
assert ~np.isnan(ts2.value[0])

@pytest.mark.xfail # will fail until pandas is fixed
class TestUIGeoSeriesResample():
''' test GeoSeries.Resample()
Expand Down
30 changes: 30 additions & 0 deletions pyleoclim/utils/tsbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,36 @@ def dropna(ys, ts, verbose=False):

return ys, ts

def dropna_matrix(matrix, verbose=False):
'''Drop NaN values from matrix
Remove columns from matrix that contain nans
Parameters
----------
matrix : array
A matrix, NaNs allowed
verbose : bool
If True, will print a warning message
Returns
-------
matrix : array
The matrix without nans
See Also
--------
https://pandas.pydata.org/docs/reference/api/pandas.Series.dropna.html
'''
matrix = matrix[:, ~np.isnan(matrix).any(axis=0)]

if verbose and any(np.isnan(matrix)):
print('NaNs have been detected and dropped.')

return matrix

def sort_ts(ys, ts, ascending = True, verbose=False):
''' Sort timeseries
Expand Down

0 comments on commit 311db8f

Please sign in to comment.