Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 26 additions & 10 deletions src/arviz_plots/plots/lm_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,32 @@ def plot_lm(
y_obs = y
else:
y_obs = obs_vars[: len(y)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can move this inside the else to reduce the complexity of the if clauses. In pseudocode:

if DataArray:
    ...
if Dataset:
    ...
else:
    if None:
        ...
    if str:
        ...
    extract()

elif isinstance(y_obs, str):
y_obs = [y_obs]
observed_y = extract(
dt,
group="observed_data",
var_names=y_obs,
combined=False,
keep_dataset=True,
sample_dims=[],
)
observed_y = extract(
dt,
group="observed_data",
var_names=y_obs,
combined=False,
keep_dataset=True,
sample_dims=[],
)
elif isinstance(y_obs, xr.DataArray):
name = y_obs.name if y_obs.name is not None else "y_obs"
observed_y = xr.Dataset({name: y_obs})
y_obs = [name]
elif isinstance(y_obs, xr.Dataset):
observed_y = y_obs
y_obs = list(observed_y.data_vars)
else:
if isinstance(y_obs, str):
y_obs = [y_obs]
observed_y = extract(
dt,
group="observed_data",
var_names=y_obs,
combined=False,
keep_dataset=True,
sample_dims=[],
)
obs_to_x_map = dict(zip(y_obs, x))
if all(var_name in observed_y.data_vars for var_name in obs_to_x_map):
observed_y = observed_y.rename_vars(obs_to_x_map)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ def test_plot_lm_group_posterior(self, datatree_regression, backend):
assert "pe_line" in pc.viz.children
assert "x" in pc.viz["pe_line"]

def test_plot_lm_y_obs_dataarray(self, datatree_regression, backend):
y_obs_da = datatree_regression["observed_data"]["y"]
pc = plot_lm(datatree_regression, y="y", y_obs=y_obs_da, backend=backend)
assert "figure" in pc.viz.data_vars
assert "observed_scatter" in pc.viz.children

@pytest.mark.parametrize("coverage", (True, False))
def test_plot_loo_pit(self, datatree, coverage, backend):
pc = plot_loo_pit(datatree, coverage=coverage, backend=backend)
Expand Down