Skip to content

Commit

Permalink
extract: Avoid interpolation when everything is NaN (#53)
Browse files Browse the repository at this point in the history
* Avoid interpolation when everything is NaN

* Add a test.

* black
  • Loading branch information
PeterKraus authored Aug 18, 2022
1 parent 50b29a3 commit d259b0d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/dgpost/utils/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,12 @@ def _get_interp(spec, obj, at, ts):
noms = unp.nominal_values(vals)
sigs = unp.std_devs(vals)
mask = ~np.isnan(noms) & ~np.isnan(sigs)
inoms = np.interp(ts, index[mask], noms[mask])
isigs = np.interp(ts, index[mask], sigs[mask])
if np.any(mask):
inoms = np.interp(ts, index[mask], noms[mask])
isigs = np.interp(ts, index[mask], sigs[mask])
else:
inoms = np.ones(len(ts)) * np.NaN
isigs = np.ones(len(ts)) * np.NaN
colint.append(unp.uarray(inoms, isigs))
return colnames, colint, colunits

Expand Down
20 changes: 20 additions & 0 deletions tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,23 @@ def test_extract_df(infile, spec, outfile, datadir):
print(f"{ref.head()=}")
df.to_pickle(outfile)
compare_dfs(ref, df)


@pytest.mark.parametrize(
"inpath, outpath",
[
( # ts0 - interpolate tables with nans
"nan.yaml",
"nan.df.pkl",
),
],
)
def test_extract_nan(inpath, outpath, datadir):
os.chdir(datadir)
dgpost.run(inpath)
df = pd.read_pickle(outpath)
print(f"{df.head()=}")
ref = pd.read_pickle(f"ref.{outpath}")
print(f"{ref.head()=}")
df.to_pickle(f"ref.{outpath}")
compare_dfs(ref, df)
Binary file added tests/test_extract/nan.t1.pkl
Binary file not shown.
Binary file added tests/test_extract/nan.t2.pkl
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/test_extract/nan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: "1.1"
load:
- as: t1
path: nan.t1.pkl
type: table
- as: t2
path: nan.t2.pkl
type: table
extract:
- into: df
from: t1
columns:
- key: xout
as: xout(t1)
- into: df
from: t2
columns:
- key: xout
as: xout(t2)
save:
- table: df
as: nan.df.pkl

Binary file added tests/test_extract/ref.nan.df.pkl
Binary file not shown.

0 comments on commit d259b0d

Please sign in to comment.