# Pytesting "pymc3 functions"

**URL:** <https://discourse.pymc.io/t/pytesting-pymc3-functions/7311>\
**Category:** Questions\
**Created:** [April 22, 2021, 2:28pm UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311 "2021-04-22T14:28:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [April 22, 2021, 2:28pm UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/1 "2021-04-22T14:28:48Z")

</div>

I have written a number of functions that return pymc3 variables, for example a prior.  
I am finding it a bit difficult to test these as the result is typically a theano tensor of some sort. Do you have any recommendations on how to test such functions?

I end up writing a lot of tests for types and len(model.free\_RVs) but it seems to be a tedious job.

thank you

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 22, 2021, 5:02pm UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/2 "2021-04-22T17:02:46Z")

</div>

Can you provide more details about the type of functions you are trying to test?

You can always test the output of a "PyMC"function by compiling your expressions within a `theano.function([inputs], output)` but maybe you can do something simpler by checking the nodes returned by your function…

---

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [April 22, 2021, 5:59pm UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/3 "2021-04-22T17:59:14Z")

</div>

Hi, thank you for getting back to me @ricardoV94.  
My apologies, I should have given an example.  
My functions are to generate a simple inference case based on a dictionary input.  
So lets say we have a dict with something like

`{"distribution":"normal","mean":5,"sd":2}`

as an input to my _generate\_normal\_prior(specs: dict)_-function.  
this function returns a prior based on the input. The return value is then of type theano.var.TensorVariable. If the funtion returned i.e. a float,array,dict i may have done an assert statement verifying i got my wanted results. How to do this is less trivial to me for a function that returns a theano variable.

Hope this was clearifying

---

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [April 23, 2021, 7:07am UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/4 "2021-04-23T07:07:12Z")

</div>

did that make sense @ricardoV94 ?

thanks

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 23, 2021, 7:25am UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/5 "2021-04-23T07:25:12Z")

</div>

I am still not sure what your function is returning. I imagined it was something like:

```python
def generate_normal(params):
  return pm.Normal(**params)

with pm.Model() as m:
  x = generate_normal({'mu': 0, 'sigma': 2})

```

In which case `x` is not a theano TensorVariable but a `pymc3.model.FreeRV` no?

---

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [April 23, 2021, 7:42am UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/6 "2021-04-23T07:42:17Z")

</div>

you are exactly right. That is basically my function. However it seems to be a theano type?

![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/ec3d6ec56bd78ed232de4b7af45a25d3aa65d4ba.png)

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [April 23, 2021, 7:50am UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/7 "2021-04-23T07:50:23Z")

</div>

Yeah, you are right, it was just Ipython pretty printing that confused me.

So you can evaluate the logp of your distributions via

```python
with pm.Model() as m:
  x = generate_normal({'name': 'x', 'mu': 0, 'sigma': 2})

x.logp({'x': 1}) # array(-1.73708571)
scipy.stats.norm(0, 2).logpdf(1) # -1.737085713764618

```

---

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [April 23, 2021, 7:54am UTC](https://discourse.pymc.io/t/pytesting-pymc3-functions/7311/8 "2021-04-23T07:54:02Z")

</div>

ah thats a neat trick to check that you created the right kind of variable. awesome, thank you so much!
