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

Curve analysis drawer refactoring #738

Conversation

nkanazawa1989
Copy link
Collaborator

Summary

This is part of #265

Details and comments

Curve drawer functions are replaced with a mixin class CurveDrawerMixin. With this change, we can draw data at arbitrary timing, thus this is a first step to drop __processed_data from curve analysis (with RB refactoring we don't need to keep raw data). In addition, analysis options for fitting and drawing are organized as separate options.

@nkanazawa1989 nkanazawa1989 changed the title Curve analysis drawer refactoring [WIP] Curve analysis drawer refactoring Mar 17, 2022
Copy link
Contributor

@eggerdj eggerdj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is some preliminary feedback. I like the idea of separating the draw options form other analysis options. It gives some of the options more context.

qiskit_experiments/curve_analysis/drawer_mixin.py Outdated Show resolved Hide resolved
Comment on lines 65 to 67
# Set figure size if axis is not provided.
# Otherwise user may set the figure size by oneself.
# Then avoid applying draw defaults to avoid override user's preference.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit confusing. Do we need this comment? The code looks clean enough.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the comment to option description. Indeed this should not be a comment for developers.
434b894

# Add inset axis. User may provide a single axis object via the analysis option,
# while this analysis tries to draw its result in multiple canvases,
# especially when the analysis consists of multiple curves.
# Inset axis is experimental implementation of matplotlib 3.0 so maybe unstable API.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good idea if the API is unstable?

Copy link
Collaborator Author

@nkanazawa1989 nkanazawa1989 Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already exists in the current codebase and this is unavoidable. I investigated the Matplotlib but there is no approach other than this to add inset axes. Since this is not user-facing API in our codebase, we can update the code without any deprecation when something is deprecated in Matplotlib.



class CurveDrawerMixin:
"""Mixin to provide drawing functionalities for curve fit result."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a few more details regarding this mix-in? I.e. what drawing functionalities does it provide? When is it called in a curve analysis etc...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also mention what sub-classes need to implement. As far as I can tell it looks like they need to implement:

def _default_draw_options(cls):

correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll update the docs after we finalize the design. I'm still wondering mixin vs instance (see my long reply above). You are correct, what the subclass may want to implement is _default_draw_options. But nothing stops you to override other methods (if you need fancy visualization).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change 09a16bf _default_draw_options has been removed. Now developer needs to directly update drawer instance option via analysis options.

@nkanazawa1989 nkanazawa1989 force-pushed the curve_analysis_refactor/drawer branch 2 times, most recently from 1f0f976 to 09a16bf Compare March 28, 2022 19:12
@nkanazawa1989 nkanazawa1989 force-pushed the curve_analysis_refactor/drawer branch from f834cd4 to 680a0d2 Compare March 29, 2022 18:58
@nkanazawa1989 nkanazawa1989 changed the title [WIP] Curve analysis drawer refactoring Curve analysis drawer refactoring Mar 29, 2022
Copy link
Contributor

@eggerdj eggerdj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks mostly good to me a few things need to be clarified here and there. Can you also add a few output examples to the PR to show that it all works fine?

qiskit_experiments/curve_analysis/curve_analysis.py Outdated Show resolved Hide resolved

This method should implement a protocol to initialize a drawing canvas
with user input ``axis`` object. Note that curve analysis drawer
supports visualization in the 2D inset axes. This method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 2D inset axis? Can you define it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in bcd8a6e


A subclass must implement following abstract methods.

initialize_canvas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text below seems very precise. Why can this not be done in the base class? Because you want subclasses to be able to init axis with different sizes and shapes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, subclass is not necessary based on the Matplotlib. For example, plotly will give you cool interactive visualization of experimental results. Someone may want to implement.

Comment on lines 238 to 239
Note that figure and axis might be different plot when a user provide
an axis object which is a part of other multiple axis figure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hard to follow. Can you reformulate?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in bcd8a6e. This is too specific comment for Matplotlib so I moved it to the mpl drawer rather than in the base drawer.

Comment on lines 265 to 270
draw_ops = {
"markersize": 9,
"zorder": 5,
"linestyle": "-",
"linewidth": 2,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to configure these values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. In follow-up you can add arbitrary drawing options to override them. This is indeed one of feedback from the beta-test.

@nkanazawa1989
Copy link
Collaborator Author

These are the figures. Basically the same as before.

T1
image

CR Hamiltonian (multi-canvas)
image

Copy link
Contributor

@eggerdj eggerdj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please consider the additional suggestions before merging.

qiskit_experiments/curve_analysis/curve_analysis.py Outdated Show resolved Hide resolved
@nkanazawa1989 nkanazawa1989 merged commit 9618ad9 into qiskit-community:main Apr 4, 2022
@nkanazawa1989 nkanazawa1989 deleted the curve_analysis_refactor/drawer branch April 4, 2022 14:21
@nkanazawa1989 nkanazawa1989 mentioned this pull request Apr 6, 2022
7 tasks
paco-ri pushed a commit to paco-ri/qiskit-experiments that referenced this pull request Jul 11, 2022
This PR replaces existing curve drawer with JSON serializable new drawer instance. 

- New drawer has dedicated to options to simplify the curve analysis options.
- New drawer is stateful (axis object is internally kept after initialized) so that we can draw different data immediately.
- Drawer code can be easily tracked by IDE.

Co-authored-by: Daniel J. Egger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants