Skip to content

custom model tutorial can't complete "posterior_alr_to_clr" function #101

@mestaki

Description

@mestaki

Hey @gibsramen,

Been a while man, hope you're doing well mate!

A couple of issues I ran into trying to run the example from the Custom Model tutorial

  1. To link to download the toy biom table is broken, qiita perhaps changed the names if it re-processed data. I downloaded a different biom from the same study on qiita and continued.
  2. Everything seems to go fine until the last step trying to convert the alr to clr:
from birdman.transform import posterior_alr_to_clr

inference = nb_lme.to_inference()
inference.posterior = posterior_alr_to_clr(
    inference.posterior,
    alr_params=["subj_int", "beta_var"],
    dim_replacement={"feature_alr": "feature"},
    new_labels=filt_tbl.ids("observation")
)

Which gives the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[27], line 4
      1 from birdman.transform import posterior_alr_to_clr
      3 inference = nb_lme.to_inference()
----> 4 inference.posterior = posterior_alr_to_clr(
      5     inference.posterior,
      6     alr_params=["subj_int", "beta_var"],
      7     dim_replacement={"feature_alr": "feature"},
      8     new_labels=filt_tbl.ids("observation")
      9 )

File ~/miniforge3/envs/birdman/lib/python3.10/site-packages/birdman/transform.py:35, in posterior_alr_to_clr(posterior, alr_params, dim_replacement, new_labels)
     32 all_chain_clr_coords = []
     34 for i, chain_alr_coords in all_chain_alr_coords.groupby("chain"):
---> 35     chain_clr_coords = _beta_alr_to_clr(chain_alr_coords)
     36     all_chain_clr_coords.append(chain_clr_coords)
     38 all_chain_clr_coords = np.array(all_chain_clr_coords)

File ~/miniforge3/envs/birdman/lib/python3.10/site-packages/birdman/transform.py:106, in _beta_alr_to_clr(beta)
     95 def _beta_alr_to_clr(beta: np.ndarray) -> np.ndarray:
     96     """Convert feature-covariate coefficients from ALR to CLR.
     97 
     98     :param beta: Matrix of beta ALR coordinates (n draws x p covariates x
   (...)
    104     :rtype: np.ndarray
    105     """
--> 106     num_draws, num_covariates, num_features = beta.shape
    107     beta_clr = np.zeros((num_draws, num_covariates, num_features+1))
    108     for i in range(num_covariates):  # TODO: vectorize

ValueError: too many values to unpack (expected 3)

From a quick look, it seems like beta_clr is expecting 3 variables but beta_var in the arviz object has 4 dimensions.

inference.posterior.beta_var
xarray.DataArray
'beta_var'

    chain: 1draw: 500covariate: 2feature_alr: 288

    array([[[[-6.55434  , -6.27254  , -5.65512  , ..., -7.84382  ,
              -6.47899  , -7.76177  ],
             [-0.0595921, -0.174455 , -1.51572  , ...,  0.142977 ,
               0.694416 ,  2.38791  ]],

            [[-6.79598  , -6.16958  , -5.85937  , ..., -8.02273  ,
              -6.68281  , -8.1183   ],
             [-0.292202 ,  0.258621 , -1.01791  , ..., -0.0856463,
               0.353943 ,  1.41398  ]],

            [[-6.66547  , -6.00688  , -5.62712  , ..., -7.97881  ,
              -6.91975  , -7.59707  ],
             [-0.648292 , -0.0806183, -0.69713  , ..., -0.214338 ,
               0.619013 ,  3.15721  ]],

            ...,

            [[-7.17717  , -6.16336  , -6.61791  , ..., -7.75436  ,
              -6.76365  , -7.69841  ],
             [-0.556393 , -0.262732 ,  0.0275394, ..., -0.117538 ,
               0.356467 ,  2.71722  ]],

            [[-6.89142  , -6.13325  , -6.08336  , ..., -7.57816  ,
              -6.45819  , -7.49152  ],
             [ 0.232536 , -0.19252  , -0.727028 , ...,  0.161462 ,
               0.220744 ,  3.17469  ]],

            [[-6.77205  , -5.85398  , -6.18575  , ..., -8.15794  ,
              -6.47406  , -7.10965  ],
             [-0.0734689, -0.473724 , -0.206687 , ...,  0.313864 ,
               0.599177 ,  2.49647  ]]]], shape=(1, 500, 2, 288))

    Coordinates:
        covariate
        (covariate)
        object
        'Intercept' "C(pre_post, Treatme...
        feature_alr
        (feature_alr)
        <U32
        'd56f3440f5516315b7c17f94bfba345...
        draw
        (draw)
        int64
        0 1 2 3 4 5 ... 495 496 497 498 499
        chain
        (chain)
        int64
        0
    Indexes: (4)
    Attributes: (0)



    chain: 1draw: 500covariate: 2feature_alr: 288

So chain is also included here which is the 4th dimension. I tried slicing chain out from beta_var but then it complains of an earlier call from this line where it is expecting chain to do a groupby.

Any suggestions here?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions