Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache results for subject/pipeline #82

Merged
merged 34 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3878d9c
add dependency to firebase admin
gcattan Oct 18, 2022
6d22707
add firebase connector
gcattan Oct 18, 2022
4d28911
mock connector
gcattan Oct 18, 2022
c39aae4
add empty certificate for github
gcattan Oct 18, 2022
0a07e71
- retrieve certificate from environment
gcattan Oct 18, 2022
e59ba6a
add entry into api.rst for FirebaseConnector
gcattan Oct 18, 2022
61907ab
add test for firebase connector
gcattan Oct 18, 2022
a7dc646
Test real connection
gcattan Oct 18, 2022
4c5a845
flake8
gcattan Oct 18, 2022
d73b5de
flake8 tests
gcattan Oct 18, 2022
1a39b01
flake8
gcattan Oct 18, 2022
9ede709
fix relative import
gcattan Oct 18, 2022
d4c18a0
fix environ not callable
gcattan Oct 18, 2022
408c8da
update workflow with gitsecret
gcattan Oct 18, 2022
c7d8bb0
use ast to convert string certificate to dictionary
gcattan Oct 18, 2022
d4d6d39
replace ast by eval
gcattan Oct 18, 2022
06a2355
add double eval to escape string
gcattan Oct 18, 2022
d9f6a1b
revert previous commit
gcattan Oct 18, 2022
1d25d57
Change Exception -> ValueError
gcattan Oct 19, 2022
07fb99f
remove duplicated line
gcattan Oct 19, 2022
580a1cc
add documentation
gcattan Oct 19, 2022
1fbfb92
flake8
gcattan Oct 19, 2022
ad87241
add Cache abstraction
gcattan Oct 22, 2022
62462ab
flake8
gcattan Oct 22, 2022
8463791
optional import for mne and firebase_admin
gcattan Oct 22, 2022
a29a97d
clean test
gcattan Oct 22, 2022
af23de6
Move MockDataset to datasets/utils.
gcattan Oct 22, 2022
f40bb8b
Finish documentation
gcattan Oct 22, 2022
3e8ae43
small typos
gcattan Oct 22, 2022
fdaba60
Update pyriemann_qiskit/datasets/utils.py
gcattan Oct 25, 2022
14536ed
Update pyriemann_qiskit/utils/firebase_connector.py
gcattan Oct 25, 2022
f1ea9d8
Update pyriemann_qiskit/utils/firebase_connector.py
gcattan Oct 25, 2022
aa9dd85
Update test_utils_firebase.py
gcattan Oct 25, 2022
970525f
Update test_utils_firebase.py
gcattan Oct 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:
run: |
flake8 examples tests pyriemann_qiskit
- name: Test with pytest
env:
FIREBASE_CERTIFICATE: ${{ secrets.FIREBASE_CERTIFICATE }}
run: |
pytest

12 changes: 12 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ Math

cov_to_corr_matrix

Firebase
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _firebase_api:
.. currentmodule:: pyriemann_qiskit.utils.firebase_connector

.. autosummary::
:toctree: generated/

FirebaseConnector
Cache

Datasets
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. _datasets_api:
Expand All @@ -95,4 +106,5 @@ Datasets
get_linearly_separable_dataset
get_qiskit_dataset
get_feature_dimension
MockDataset

1 change: 1 addition & 0 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ scipy==1.7.3
moabb>=0.4.6
git+https://github.com/pyRiemann/pyRiemann#egg=pyriemann
docplex>=2.21.207
firebase_admin==6.0.1
3 changes: 2 additions & 1 deletion pyriemann_qiskit/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,9 @@ class QuantumClassifierWithDefaultRiemannianPipeline(BaseEstimator,
If None, respective default values for Pegasos and (Q)SVC
are used. The default value for Pegasos is 1000.
For (Q)SVC it is -1 (that is not limit).
shots : int (default:1024)
shots : int | None (default: 1024)
Number of repetitions of each circuit, for sampling.
If None, classical computation will be performed.
feature_entanglement : str | list[list[list[int]]] | \
Callable[int, list[list[list[int]]]]
Specifies the entanglement structure for the ZZFeatureMap.
Expand Down
6 changes: 4 additions & 2 deletions pyriemann_qiskit/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .utils import (get_mne_sample,
get_linearly_separable_dataset,
get_qiskit_dataset,
get_feature_dimension)
get_feature_dimension,
MockDataset)


__all__ = ["get_mne_sample",
"get_linearly_separable_dataset",
"get_qiskit_dataset",
"get_feature_dimension"]
"get_feature_dimension",
"MockDataset"]
91 changes: 89 additions & 2 deletions pyriemann_qiskit/datasets/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from warnings import warn
import numpy as np
from mne import io, read_events, pick_types, Epochs
from mne.datasets import sample
try:
from mne import io, read_events, pick_types, Epochs
from mne.datasets import sample
except Exception:
warn("mne not available. get_mne_sample will fail.")
from qiskit_machine_learning.datasets import ad_hoc_data
from sklearn.datasets import make_classification

Expand Down Expand Up @@ -34,6 +38,10 @@ def get_mne_sample(n_trials=10):
y : ndarray, shape (n_trials,)
Predicted target vector relative to X.

Notes
-----
.. versionadded:: 0.0.1

References
----------
.. [1] Available from: \
Expand Down Expand Up @@ -81,6 +89,10 @@ def get_mne_sample(n_trials=10):
def get_qiskit_dataset():
"""Return qiskit dataset.

Notes
-----
.. versionadded:: 0.0.1

Returns
-------
X : ndarray, shape (n_samples, n_features)
Expand Down Expand Up @@ -108,6 +120,10 @@ def get_qiskit_dataset():
def get_linearly_separable_dataset():
"""Return a linearly separable dataset.

Notes
-----
.. versionadded:: 0.0.1

Returns
-------
X : ndarray, shape (n_samples, n_features)
Expand Down Expand Up @@ -151,6 +167,10 @@ def get_feature_dimension(dataset):
n_features : int
The feature dimension, -1 denotes no data in the dataset.

Notes
-----
.. versionadded:: 0.0.2

Raises
-------
TypeError
Expand All @@ -166,3 +186,70 @@ def get_feature_dimension(dataset):
return v.shape[1]

return -1


class MockDataset():
"""A dataset with mock data.

Parameters
----------
dataset_gen : Callable[[], (List, List)]
A function to generate datasets.
The function accepts no parameters and returns a pair of lists.
The first list contains the samples, the second one the labels.
n_subjects : int
The number of subjects in the dataset.
A dataset will be generated for all subjects using the handler
`dataset_gen`.

Attributes
----------
code_ : str
The code of the dataset.
The code of the dataset is also the string representation
of the dataset.
data_ : Dict
A dictionnary representing the dataset. Ex:
```
{
subject1: (samples1, labels1),
subject2: (samples2, labels2),
...
}
```
subjects_ : List[int]
The subjects of the dataset.

Notes
-----
.. versionadded:: 0.0.3

"""
def __init__(self, dataset_gen, n_subjects: int):
self.code_ = "MockDataset"
self.subjects_ = range(n_subjects)
self.data_ = {}
for subject in self.subjects_:
self.data_[subject] = dataset_gen()

def get_data(self, subject):
"""
Returns the data of a subject.

Parameters
----------
subject : int
A subject in the list of subjects `subjects_`.

Returns
-------
data : the subject's data.

Notes
-----
.. versionadded:: 0.0.3
"""
return self.data_[subject]

def __str__(self) -> str:
return self.code_
5 changes: 4 additions & 1 deletion pyriemann_qiskit/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
square_bin_mat_var,
ClassicalOptimizer,
NaiveQAOAOptimizer)
from .firebase_connector import FirebaseConnector, Cache

__all__ = [
'hyper_params_factory',
Expand All @@ -14,5 +15,7 @@
'square_int_mat_var',
'square_bin_mat_var',
'ClassicalOptimizer',
'NaiveQAOAOptimizer'
'NaiveQAOAOptimizer',
'FirebaseConnector',
'Cache'
]
1 change: 1 addition & 0 deletions pyriemann_qiskit/utils/firebase_cert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
certificate = {}
Loading