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

Figure extraction from experiment data by name without extension #1287

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
6 changes: 6 additions & 0 deletions qiskit_experiments/framework/experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,12 @@ def figure(
raise ExperimentEntryNotFound(f"Figure {figure_key} not found.")
figure_key = self._figures.keys()[figure_key]

# All figures must have '.svg' in their names when added, as the extension is added to the key
# name in the `add_figures()` method of this class.
if isinstance(figure_key, str):
if not figure_key.endswith(".svg"):
figure_key += ".svg"

figure_data = self._figures.get(figure_key, None)
if figure_data is None and self.service:
figure = self.service.figure(experiment_id=self.experiment_id, figure_name=figure_key)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
upgrade:
- |
Figures in `ExperimentData` objects can now be access without '.svg' extension.
ItamarGoldman marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions test/database_service/test_db_experiment_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ def test_get_figure(self):
exp_data = ExperimentData(experiment_type="qiskit_test")
figure_template = "hello world {}"
name_template = "figure_{}.svg"
name_template_wo_ext = "figure_{}"

for idx in range(3):
exp_data.add_figures(
str.encode(figure_template.format(idx)), figure_names=name_template.format(idx)
Expand All @@ -418,6 +420,11 @@ def test_get_figure(self):
self.assertEqual(expected_figure, exp_data.figure(name_template.format(idx)).figure)
self.assertEqual(expected_figure, exp_data.figure(idx).figure)

# Check that figure will be returned without file extension in name
expected_figure = str.encode(figure_template.format(idx))
self.assertEqual(expected_figure, exp_data.figure(name_template_wo_ext.format(idx)).figure)
self.assertEqual(expected_figure, exp_data.figure(idx).figure)

file_name = uuid.uuid4().hex
self.addCleanup(os.remove, file_name)
exp_data.figure(idx, file_name)
Expand Down