Skip to content

Use a Firebase cache

gcattan edited this page Oct 27, 2022 · 4 revisions

With pyRiemann-qiskit, there is the possibility to use Firestore as a remote database to cache results.

Setup a NoSql database

Get started with Google Firebase and create your first NoSql Firestore database.

You can start filling your database, by following this schema:

image

However, the only required field to get started is to create a collection with the name 'datasets'.

Get a certificate with credentials

Go to Settings > Service Accounts and click on Generate new private key:

image

Save the JSON file on your local machine and copy its content to pyriemann_qiskit/utils/firebase_cert.py.

Additionally, you can copy the certificate to your GitHub fork of this repository to benefit from the Firebase cache on your pipelines. To do so, go to Settings > Secret and then add a secret call FIREBASE_CERTIFICATE:

image

Before pasting your JSON credential as a value for this secret, it is necessary to inline it and replaces r'\n' with '\n' in the private_key field. More detail here.

Use the cache!

Follow the implementation use for the tests/test_utils_firebase.py to use the cache:

def test_cache(get_dataset):
    def dataset_gen():
        return get_dataset(n_samples=10, n_features=5,
                           n_classes=2, type="rand")

    dataset = MockDataset(dataset_gen, n_subjects=3)
    pipeline = make_pipeline(StandardScaler(), SVC(C=3.0))
    cache = Cache(str(dataset), pipeline, mock_data={})
    scores = {}

    for subject in dataset.subjects_:
        X, y = dataset.get_data(subject)
        pipeline.fit(X, y)
        y_pred = pipeline.predict(X)

        cache.add(subject, y, y_pred)
        score = balanced_accuracy_score(y, y_pred)
        scores[subject] = score

    for subject in dataset.subjects_:
        y, y_pred = cache.get_result(subject)
        score = balanced_accuracy_score(y, y_pred)
        assert score == scores[subject]

Additional notes

The FirebaseConnector and Cache classes inside pyriemann_qiskit.utils admit a mock_data parameter for testing purposes or if you want to use a local dictionary instead of a remote database. In this case, the dictionary provided in the mock_data parameter will be used and no connection is required to Firebase.