Bambi error: "cannot pickle 'fortran' object"

a = np.random.normal(6, 2.5, 160)
b = np.random.normal(8, 2, 120)
df = pd.DataFrame({“Group”: [“a”] * 160 + [“b”] * 120, “Val”: np.hstack([a, b])})
df

model_1 = bmb.Model(“Val ~ Group”, df)
results_1 = model_1.fit()

#I am a novice:
problem: even the simplest example from bambi gives the error: “cannot pickle ‘fortran’ object”. I reinstalled anaconda and pip installed bambi but did not correct error. I am new to pymc3 and bambi. Can anyone suggest a managable solution?
thanking you in advance. DD

Hello!

Could you share which versions of PyMC and Bambi you’re using?

You can do

import bambi as bmb
import pymc as pm

print("Bambi:", bmb.__version__)
print("PyMC": pm.__version__)

If import pymc as pm does not work, it may indicate you’re using pymc3. We encourage using the new version of PyMC, which is v4.

[quote=“tcapretto, post:2, topic:10858”]

`Thank you for your help.

I installed pymc with: ‘pip install pymc’`
then I imported:
import arviz as az
import bambi as bmb
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import pymc as pm

print(“Bambi:”, bmb.version)
print(“PyMC:”, pm.version)
gives:
Bambi: 0.9.1
PyMC: 4.3.0

then following the Bambi first example from the Bambi documentation:

Comparison of two means (T-test):

bambinos.github :frowning:Comparison of two means (T-test) — Bambi 0.9.1 documentation)

a = np.random.normal(6, 2.5, 160)
b = np.random.normal(8, 2, 120)
df = pd.DataFrame({“Group”: [“a”] * 160 + [“b”] * 120, “Val”: np.hstack([a, b])})
df.sample(10)

model_1 = bmb.Model(“Val ~ Group”, df)
results_1 = model_1.fit()
this still gives:
‘cannot pickle ‘fortran’ object’ error.

If instead I
import pymc3 as pm

print(“Bambi:”, bmb.version)
print(“PyMC3:”, pm.version)
I get:
Bambi: 0.9.1
PyMC: 3.11.5
and then run the model, I still get the same error.
mayby I am not installing pymc correctly and the two versions are interferring with each other?

I have since gone on to run other models on Bambi successfully, but when I return to this example above from the Bambi website it fails with the aforementioned error.
If I modify the model by setting the intercept to zero it then works!:
model_2 = bmb.Model(“Val ~ 0+Group”, df)
results_2 = model_2.fit()
but why the first model, designated model1(model_1 = bmb.Model(“Val ~ Group”, df)) on the Bambi website fails on my computer I do not know. Comparing two means is a useful model.
Putting this all together I am not sure if its just this example model that is at fault, or that my install is mixed up between pymc3 and pymc4. I was using pymc3 since the learning examples seem to be mostly pymc3, however I would be happy to migrate to pymc4 if the code transition is not too difficult.
Any ideas as to why the Bambi documentation example model 1 is failing? would give confidence to using other Bambi models. Thank you for expertise and guidance on the above. DD

Could you create a new environment and install the development version?

pip install git+https://github.com/bambinos/bambi.git

I tried pip install git+https://github.com/bambinos/bambi.git into c promp and error message returns "Cannot find command ‘git’. Do you have ‘git’ installed and in your path.
I since tried the following (added cores=1 to results_1 = model_1.fit) and it appears to now work and does not give the error message:“cannot pickle fortran object.”
Example:
np.random.seed(1234)

a = np.random.normal(6, 2.5, 160)
b = np.random.normal(8, 2, 120)
df = pd.DataFrame({“Group”: [“a”] * 160 + [“b”] * 120, “Val”: np.hstack([a, b])})
df.sample(10)

model_1 = bmb.Model(“Val ~ Group”, df)

results_1 = model_1.fit(cores=1)
az.summary(results_1)

also tried adding the argument “cores =1” to other simple models and it prevented the error occurring and gives good results.
eg:
#Bayesian Linear Regression with Bambi | by Khuyen Tran | Towards Data Science
import pandas as pd

df = pd.read_csv(
https://media.githubusercontent.com/media/khuyentran1401/Data-science
“/master/statistics/bayes_linear_regression/student-score.csv”)

df.head(10)
plt.scatter(df[‘mat’], df.por, label=“sampled data”)
import bambi as bmb

gauss_model = bmb.Model(‘por ~mat’, data=df)

Fit the model using 1000 on each of 4 chains

gauss_fitted = gauss_model.fit(draws=1000, chains=4, cores=1)
az.summary(gauss_fitted)
Again putting ‘cores=1’ as a model.fit argument allowed the model to work without error. I am using a new HP laptop just purchased running windows11 to learn pymc, Bayesian and Bambi (and python). It could be useful to other novices to know that setting cores=1 will allow the simple starting examples on Bambi to run. Not sure why this fix works?
I am happy now to continue my exploration into Bambi and I am encouraged by the helpful support that is afforded to novices.
Comment:
Bambi is exactly what I think is useful to the novice statistician to explore their data with Bayesian models. A cursory look at the examples (on the Bambi site and elsewhere), I would love to see Bambi models for paired means and paired proportions added to the examples to bridge the gap when moving from classical to Bayesian models. A short introductory book or manual on Bambi for the Novice programmer would be a great starting point for new programmers to cover a broader range of models and would be very welcome.
Meanwhile I will continue to explore the available models.
Thanks again for your time, expertise, interest and help.
DD

So it looks like it’s a problem with multiprocessing.

For there record, I saw this issue in Github, perhaps it’s related NUTS Sampler Stuck as 0.00% [0/8000 00:00<? Sampling 4 chains, 0 divergences] · Issue #6315 · pymc-devs/pymc · GitHub