Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
jpn-- committed Feb 10, 2022
1 parent b67fa58 commit 0705dbd
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ book/_build
book/api/generated
book/example/_sharrow_cache_
latent-class-example-report.html
doc/build
sandbox.py
9 changes: 7 additions & 2 deletions .idea/Larch.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions .idea/runConfigurations/PyTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion book/user-guide/choice-models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"cats = df_ca['altid'].astype(pd.CategoricalDtype(['Car', 'Bus', 'Walk'])).cat\n",
"df_ca['altnum'] = cats.codes + 1\n",
"df_ca = df_ca.set_index(['caseid', 'altnum'])\n",
"data = lx.Dataset.from_idca(df_ca.sort_index(), fill_unavail=0)\n",
"data = lx.Dataset.from_idca(df_ca.sort_index(), fill_missing=0)\n",
"data = data.drop_vars(\"_avail_\")\n",
"data['ChosenCode'] = (data['Chosen'] * data['Chosen'].altnum).sum('altnum')\n",
"data.coords['alt_names'] = lx.DataArray(cats.categories, dims=('altnum'), coords={'altnum': data.altnum})\n",
Expand Down
27 changes: 19 additions & 8 deletions larch/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,11 @@ def set_altnames(self, altnames, inplace=False):
return obj

@classmethod
def from_idca(cls, df, crack=True, altnames=None, avail='_avail_', fill_unavail=None):
def from_idca(cls, df, crack=True, altnames=None, avail='_avail_', fill_missing=None):
"""
Construct a Dataset from an idco-format DataFrame.
Construct a Dataset from an idca-format DataFrame.
This method loads the data as dense arrays.
Parameters
----------
Expand All @@ -814,14 +816,19 @@ def from_idca(cls, df, crack=True, altnames=None, avail='_avail_', fill_unavail=
avail : str, default '_avail_'
When the imported data is in idce format (i.e. sparse) then
an availability indicator is computed and given this name.
fill_unavail : scalar or Mapping, optional
fill_missing : scalar or Mapping, optional
Fill values to use for missing values when imported data is
in idce format (i.e. sparse). Give a single value to use
globally, or a mapping of {variable: value} or {dtype: value}.
Returns
-------
Dataset
See Also
--------
Dataset.from_idce : Construct a Dataset from a sparse idca-format DataFrame.
"""
if df.index.nlevels != 2:
raise ValueError("source idca dataframe must have a two "
Expand All @@ -846,20 +853,20 @@ def from_idca(cls, df, crack=True, altnames=None, avail='_avail_', fill_unavail=
if avail not in ds and len(df) < ds.n_cases * ds.n_alts:
av = DataArray.from_series(pd.Series(1, index=df.index)).fillna(0).astype(np.int8)
ds[avail] = av
if fill_unavail is not None:
if isinstance(fill_unavail, Mapping):
if fill_missing is not None:
if isinstance(fill_missing, Mapping):
for k, i in ds.items():
if ds.ALTID not in i.dims:
continue
if k not in fill_unavail and i.dtype not in fill_unavail:
if k not in fill_missing and i.dtype not in fill_missing:
continue
filler = fill_unavail.get(k, fill_unavail[i.dtype])
filler = fill_missing.get(k, fill_missing[i.dtype])
ds[k] = i.where(ds['_avail_']!=0, filler)
else:
for k, i in ds.items():
if ds.ALTID not in i.dims:
continue
ds[k] = i.where(ds['_avail_']!=0, fill_unavail)
ds[k] = i.where(ds['_avail_']!=0, fill_missing)
return ds

@classmethod
Expand Down Expand Up @@ -897,6 +904,10 @@ def from_idce(cls, df, crack=True, altnames=None, dim_name=None, alt_index='alt_
Returns
-------
Dataset
See Also
--------
Dataset.from_idca : Construct a dense Dataset from a idca-format DataFrame.
"""
if df.index.nlevels != 2:
raise ValueError("source idce dataframe must have a two "
Expand Down

0 comments on commit 0705dbd

Please sign in to comment.