Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Depth nans #584

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading