Black Lives Matter. Please consider donating to Black Girls Code today.

Share data between callbacks but not in JSON format

Hi

I am using modAl for active learning. The active learner object needs to be shared between callbacks but can not be convert to JSON. How can I share this object between the callbacks?

Thanks in advance,
Sarah

Can’t you convert it to and from JSON?
Can you share an example?

Here I try to store the learner in the dcc.Store object.

Try #1

learner = ActiveLearner(
        estimator=RandomForestClassifier(),
        query_strategy=uncertainty_sampling,
        X_training=X_initial, y_training=y_initial
    )

    accuracy_scores = [learner.score(X_test, y_test)]
    query_idx, query_inst = learner.query(X_pool)

    al_data = {}

    al_data['learner'] = json.dumps(learner)

Output:

is not JSON serializable

Try #2:

al_data['learner'] = json.dumps(learner._dict_)

Output:

TypeError: <function uncertainty_sampling at 0x00000192271F0EA0> is not JSON serializable

I am not really familiar with the modAl library so I don’t want to change much of the Active Learner object.

A little background on this project: I have one callback (start button) to initialize the learner and than the other callback every time a person labels the data point (so the learner needs to be used here every time)

Could it be an option to cache the object on the server? Either in a memory cache or on disk?

Thanks for the idea!

Indeed, it did work by saving it on the server! I used this library. I had a too large tunnel vision.

Great! Yes, it was exactly something along the lines of pickle or joblib I had in mind :blush: