Skip to content

Commit

Permalink
reindexing bug fix
Browse files Browse the repository at this point in the history
- when data and variogram have unequal indices, nan are inserted for the
missing steps. These nans are floats rather than strings, this caused
errors when floats were used where strings were expected.

- That has been fixed by reindexing the vg_ser and cast as str.

- If index is obj then indices of vg_ser and data_df are cast to string.
This allows for a consistent datatype and behavior.
  • Loading branch information
faizan90 committed Nov 27, 2019
1 parent a58a432 commit c6113e9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
28 changes: 15 additions & 13 deletions interp/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def set_vgs_ser(self, vgs_ser, index_type='date'):
assert isinstance(vgs_ser.index, object), (
'Data type of index of vg_ser does not match index_type!')

vgs_ser.index = vgs_ser.index.astype(str)

else:
raise AssertionError(
'index_type can only be \'obj\' or \'date\'!')
Expand Down Expand Up @@ -516,7 +518,7 @@ def set_misc_settings(
n_cpus=1,
plot_figs_flag=False,
cell_size=None,
min_value_to_krige_thresh=None,
min_value_to_interp_thresh=None,
min_cutoff_value=None,
max_cutoff_value=None):

Expand All @@ -535,7 +537,7 @@ def set_misc_settings(
Cell size in the units that are consistent with the station
coordinates system. It is used only if no drift or alignment
rasters are specified.
min_value_to_krige_thresh : int or float
min_value_to_interp_thresh : int or float
If all the stations have a value less than or equal to this
for a given time step then all the cells are assigned the mean
value of all the station values. This is good in cases like
Expand All @@ -544,11 +546,11 @@ def set_misc_settings(
min_cutoff_value : None or int or float
If not None, interpolated values below this are set
equal to it. If not None it should be less than
min_value_to_krige_thresh.
min_value_to_interp_thresh.
max_cutoff_value : None or int or float
If not None, interpolated values above this are set
equal to it. If not None it should be greater than
min_value_to_krige_thresh
min_value_to_interp_thresh
'''

assert isinstance(n_cpus, int), 'n_cpus not an integer!'
Expand All @@ -573,16 +575,16 @@ def set_misc_settings(

self._cell_size = float(cell_size)

if min_value_to_krige_thresh is not None:
assert isinstance(min_value_to_krige_thresh, (int, float)), (
'min_value_to_krige_thresh can be a float or an int if '
if min_value_to_interp_thresh is not None:
assert isinstance(min_value_to_interp_thresh, (int, float)), (
'min_value_to_interp_thresh can be a float or an int if '
'not None!')

assert -np.inf <= min_value_to_krige_thresh < np.inf, (
'min_value_to_krige_thresh has to be in between -infinity '
assert -np.inf <= min_value_to_interp_thresh < np.inf, (
'min_value_to_interp_thresh has to be in between -infinity '
'and +infinity!')

self._min_var_thr = float(min_value_to_krige_thresh)
self._min_var_thr = float(min_value_to_interp_thresh)

if min_cutoff_value is not None:
assert isinstance(min_cutoff_value, (int, float)), (
Expand All @@ -607,7 +609,7 @@ def set_misc_settings(
if (self._min_var_thr is not None) and (self._min_var_cut is not None):

assert self._min_var_thr >= self._min_var_cut, (
'min_value_to_krige_thresh has to be greater than or equal to '
'min_value_to_interp_thresh has to be greater than or equal to '
'min_cutoff_value!')

if (self._min_var_cut is not None) and (self._max_var_cut is not None):
Expand All @@ -618,7 +620,7 @@ def set_misc_settings(
if (self._min_var_thr is not None) and (self._max_var_cut is not None):

assert self._min_var_thr < self._max_var_cut, (
'min_value_to_krige_thresh has to be less than '
'min_value_to_interp_thresh has to be less than '
'max_cutoff_value!')

if self._vb:
Expand All @@ -627,7 +629,7 @@ def set_misc_settings(
print('n_cpus:', self._n_cpus)
print('plot_figs_flag:', self._plot_figs_flag)
print('cell_size:', self._cell_size)
print('min_value_to_krige_thresh:', self._min_var_thr)
print('min_value_to_interp_thresh:', self._min_var_thr)
print('min_cutoff_value:', self._min_var_cut)
print('max_cutoff_value:', self._max_var_cut)
print_el()
Expand Down
4 changes: 2 additions & 2 deletions interp/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ def _prepare(self):

self._data_df = self._data_df.reindex(self._time_rng)

if not self._vg_ser_set_flag:
self._vgs_ser = self._vgs_ser.reindex(self._time_rng)
if self._vg_ser_set_flag:
self._vgs_ser = self._vgs_ser.reindex(self._time_rng).astype(str)

self._prpd_flag = True
return
Expand Down
4 changes: 2 additions & 2 deletions interp/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def interpolate_subset(self, all_args):

for time_stn_grp, cmn_time_stn_grp_idxs in grps_in_time:
assert time_stn_grp.size == np.unique(time_stn_grp).size, (
'Non-unique stations in time group!')
'Non-unique stations in step group!')

time_stn_grp_ref_xs = self._crds_df.loc[time_stn_grp, 'X'].values
time_stn_grp_ref_ys = self._crds_df.loc[time_stn_grp, 'Y'].values
Expand All @@ -137,7 +137,7 @@ def interpolate_subset(self, all_args):
nuggetness_flags[i] = check_full_nuggetness(vg_model)

if nuggetness_flags[i] and self._vb:
print('Full nugget at time step:', sub_time_steps[i])
print('Full nugget at step:', sub_time_steps[i])

else:
vg_models = None
Expand Down
4 changes: 2 additions & 2 deletions test/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def main():
nc_time_units = 'days since 1900-01-01 00:00:00.0'
nc_calendar = 'gregorian'

min_ppt_thresh = 1 # -float('inf') # 1
min_var_val_thresh = 1 # -float('inf') # 1

min_var_val = 0.0 # None
max_var_val = None
Expand Down Expand Up @@ -157,7 +157,7 @@ def main():
n_cpus,
plot_figs_flag,
None,
min_ppt_thresh,
min_var_val_thresh,
min_var_val,
max_var_val)

Expand Down
2 changes: 2 additions & 0 deletions variograms/vgsinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def set_data(
'Data type of index of stns_time_ser_df does not match '
'index_type!')

stns_time_ser_df.index = stns_time_ser_df.index.astype(str)

else:
raise AssertionError(
'index_type can only be \'obj\' or \'date\'!')
Expand Down

0 comments on commit c6113e9

Please sign in to comment.