-
Notifications
You must be signed in to change notification settings - Fork 1
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
Plotting - major steps towards final product #2
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e49aa7b
Bug fix test_estimate.py
MartinHeroux 20449b2
Refactor codebase and adjust test accordingly
MartinHeroux 762675b
estimate.calc also to return estimates_a and estimates_b
MartinHeroux 29b870b
Basic plotting complete
MartinHeroux 7cae277
Data and basic diff plotting - ax2 not correct
MartinHeroux 344bf8b
Refactor + new strategy started (see additional comment)
MartinHeroux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import NamedTuple, Tuple, Union, Literal | ||
from pathlib import Path | ||
|
||
from pliffy import estimate | ||
|
||
|
||
class ABD(NamedTuple): | ||
"""Helper namedtuple""" | ||
|
||
a: Union[str, int, float, "estimate.Estimates"] = None | ||
b: Union[str, int, float, "estimate.Estimates"] = None | ||
diff: Union[str, int, float, "estimate.Estimates"] = None | ||
|
||
|
||
class PlotInfo(NamedTuple): | ||
"""Information used to generate plot | ||
|
||
Includes sensible defaults to reduce need for user input | ||
""" | ||
|
||
x_tick_labels: ABD = ABD(a="a", b="b", diff="diff") | ||
measure_units: str = "Amplitude (a.u.)" | ||
plot_name: str = "figure" | ||
save: Literal[True, False] = False | ||
save_path: Path = None | ||
|
||
summary_symbol: ABD = ABD(a="o", b="o", diff="^") | ||
symbol_color: ABD = ABD(a="black", b="black", diff="black") | ||
summary_symbol_size: ABD = ABD(a=5, b=5, diff=6) | ||
raw_data_symbol_size: ABD = ABD(a=3, b=3, diff=3) | ||
paired_data_joining_lines: bool = True | ||
paired_data_line_width: int = 1 | ||
paired_data_line_color: str = "gainsboro" | ||
ci_line_width: ABD = ABD(a=1, b=1, diff=1) | ||
horiz_line_to_diffs: bool = False | ||
join_ab_means: bool = True | ||
ax1_y_range: Tuple = None | ||
ax2_y_range: Tuple = None | ||
ax1_y_ticks: Tuple = None | ||
ax2_y_ticks: Tuple = None | ||
ab_sub_label: str = None | ||
bottom_box: bool = False | ||
alpha: float = 0.2 | ||
|
||
|
||
class PliffyData(NamedTuple): | ||
"""Data and details required from user | ||
|
||
See :function:`pliffy.estimates.calc` parameters for details. | ||
""" | ||
|
||
a: list = None | ||
b: list = None | ||
design: Literal["paired", "unpaired"] = "unpaired" | ||
ci_percentage: int = 95 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
from pathlib import Path | ||
|
||
import matplotlib.pyplot as plt | ||
import matplotlib | ||
|
||
from pliffy import estimate, blocks | ||
|
||
matplotlib.rcParams.update({"font.size": 9}) | ||
|
||
|
||
X_VALS = blocks.ABD(a=1, b=2, diff=2.8) | ||
|
||
|
||
class Figure: | ||
def __init__(self, pliffy_data, plot_info, estimates, ax_ab): | ||
self.pliffy_data = pliffy_data | ||
self.plot_info = plot_info | ||
self.estimates = estimates | ||
if ax_ab is None: | ||
ax_ab = self._make_figure_axis() | ||
self.ax_ab = ax_ab | ||
self.jitter = 0.05 / max([len(self.pliffy_data.a), len(self.pliffy_data.b)]) | ||
self._plot_ab() | ||
|
||
def _make_figure_axis(self): | ||
width_height_in_inches = (8.2 / 2.54, 8.2 / 2.54) | ||
_, ax = plt.subplots(figsize=width_height_in_inches, dpi=600) | ||
return ax | ||
|
||
def _create_diff_axis(self): | ||
pass | ||
|
||
def _plot_ab(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like you signaled what is "internal" |
||
self._plot_raw_data() | ||
self._plot_ab_means() | ||
self._plot_ab_cis() | ||
self._tweak_ab_xaxis() | ||
self._tweak_ab_yaxis() | ||
|
||
if self.plot_info.save: | ||
plot_name = self.plot_info.plot_name + ".png" | ||
fig_path = Path(self.plot_info.save_path) / plot_name | ||
plt.savefig(fig_path) | ||
plt.show() | ||
|
||
def _plot_raw_data(self): | ||
if ( | ||
self.pliffy_data.design == "paired" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constant, also would expect |
||
) and self.plot_info.paired_data_joining_lines: | ||
self._plot_paired_lines() | ||
else: | ||
self._plot_ab_raw_data() | ||
|
||
def _plot_ab_means(self): | ||
self.ax_ab.plot( | ||
X_VALS.a, | ||
self.estimates.a.mean, | ||
marker=self.plot_info.summary_symbol.a, | ||
color=self.plot_info.symbol_color.a, | ||
markersize=self.plot_info.summary_symbol_size.a, | ||
) | ||
self.ax_ab.plot( | ||
X_VALS.b, | ||
self.estimates.b.mean, | ||
marker=self.plot_info.summary_symbol.b, | ||
color=self.plot_info.symbol_color.b, | ||
markersize=self.plot_info.summary_symbol_size.b, | ||
) | ||
|
||
def _plot_ab_cis(self): | ||
self.ax_ab.plot( | ||
[X_VALS.a, X_VALS.a], | ||
self.estimates.a.ci, | ||
color=self.plot_info.symbol_color.a, | ||
linewidth=self.plot_info.ci_line_width.a, | ||
) | ||
self.ax_ab.plot( | ||
[X_VALS.b, X_VALS.b], | ||
self.estimates.b.ci, | ||
color=self.plot_info.symbol_color.b, | ||
linewidth=self.plot_info.ci_line_width.b, | ||
) | ||
|
||
def _plot_paired_lines(self): | ||
x_vals = [X_VALS.a, X_VALS.b] | ||
for a, b in zip(self.pliffy_data.a, self.pliffy_data.b): | ||
self.ax_ab.plot( | ||
x_vals, | ||
[a, b], | ||
color=self.plot_info.paired_data_line_color, | ||
linewidth=self.plot_info.paired_data_line_width, | ||
alpha=self.plot_info.alpha, | ||
) | ||
x_vals[0] += self.jitter | ||
x_vals[1] -= self.jitter | ||
|
||
def _plot_ab_raw_data(self): | ||
x_val_a = X_VALS.a + 0.1 | ||
x_val_b = X_VALS.b - 0.1 | ||
for a, b in zip(self.pliffy_data.a, self.pliffy_data.b): | ||
self.ax_ab.plot( | ||
x_val_a, | ||
a, | ||
color=self.plot_info.symbol_color.a, | ||
marker=self.plot_info.summary_symbol.a, | ||
markeredgewidth=0, | ||
markersize=self.plot_info.raw_data_symbol_size.a, | ||
alpha=self.plot_info.alpha, | ||
) | ||
self.ax_ab.plot( | ||
x_val_b, | ||
b, | ||
color=self.plot_info.symbol_color.b, | ||
marker=self.plot_info.summary_symbol.b, | ||
markeredgewidth=0, | ||
markersize=self.plot_info.raw_data_symbol_size.b, | ||
alpha=self.plot_info.alpha, | ||
) | ||
x_val_a += self.jitter | ||
x_val_b -= self.jitter | ||
|
||
def _tweak_ab_xaxis(self): | ||
self.ax_ab.spines["top"].set_visible(False) | ||
self.ax_ab.set_xlim((0.8, 3)) | ||
self.ax_ab.set_xticks([X_VALS.a, X_VALS.b, X_VALS.diff]) | ||
self.ax_ab.set_xticklabels( | ||
[ | ||
self.plot_info.x_tick_labels.a, | ||
self.plot_info.x_tick_labels.b, | ||
self.plot_info.x_tick_labels.diff, | ||
] | ||
) | ||
|
||
def _tweak_ab_yaxis(self): | ||
self.ax_ab.spines["right"].set_visible(False) | ||
self.ax_ab.set_ylabel(self.plot_info.measure_units) | ||
ab_yticks = self.ax_ab.get_yticks() | ||
self.ab_ytick_step = ab_yticks[1] - ab_yticks[0] | ||
min_past_lowest_ytick = ab_yticks[0] > min( | ||
min(self.pliffy_data.a), min(self.pliffy_data.b) | ||
) | ||
max_past_highest_ytick = ab_yticks[-1] < max( | ||
max(self.pliffy_data.a), max(self.pliffy_data.b) | ||
) | ||
if min_past_lowest_ytick and max_past_highest_ytick: | ||
y_ticks_adjusted = ( | ||
[ab_yticks[0] - self.ab_ytick_step] | ||
+ list(ab_yticks) | ||
+ [ab_yticks[-1] + self.ab_ytick_step] | ||
) | ||
if min_past_lowest_ytick and not max_past_highest_ytick: | ||
y_ticks_adjusted = [ab_yticks[0] - self.ab_ytick_step] + list(ab_yticks) | ||
if not min_past_lowest_ytick and max_past_highest_ytick: | ||
y_ticks_adjusted = list(ab_yticks) + [ab_yticks[-1] + self.ab_ytick_step] | ||
if not min_past_lowest_ytick and not max_past_highest_ytick: | ||
y_ticks_adjusted = list(ab_yticks) | ||
self.ax_ab.set_yticks(y_ticks_adjusted) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,15 @@ | ||
from typing import NamedTuple, Tuple, Union, Literal | ||
|
||
from pliffy import estimate | ||
|
||
from pliffy import blocks, estimate, figure | ||
|
||
class _ABD(NamedTuple): | ||
"""Helper namedtuple used by PlotInfo""" | ||
a: Union[str, int] = None | ||
b: Union[str, int] = None | ||
diff: Union[str, int] = None | ||
|
||
|
||
class PlotInfo(NamedTuple): | ||
"""Information used to generate plot | ||
|
||
Includes sensible defaults to reduce need for user input | ||
""" | ||
colors: _ABD = _ABD(a="black", b="black", diff="black") | ||
symbol_size_summary: _ABD = _ABD(a=3, b=3, diff=3) | ||
symbol_size_subject: _ABD = _ABD(a=1, b=1, diff=1) | ||
symbols: _ABD = _ABD(a="o", b="o", diff="^") | ||
x_values: _ABD = _ABD(a=1, b=2, diff=3) | ||
horiz_line_to_diffs: bool = False | ||
join_ab_means: bool = True | ||
ax1_y_range: Tuple = None | ||
ax2_y_range: Tuple = None | ||
ax1_y_ticks: Tuple = None | ||
ax2_y_ticks: Tuple = None | ||
ab_sub_label: str = None | ||
bottom_box: bool = False | ||
alpha: float = 0.7 | ||
|
||
|
||
class PliffyData(NamedTuple): | ||
"""Data and details required from user | ||
|
||
See :function:`pliffy.estimates.calc` parameters for details. | ||
def plot(pliffy_data: blocks.PliffyData, plot_info: blocks.PlotInfo = blocks.PlotInfo(), ax=None): | ||
"""Main user interface to generate plot | ||
""" | ||
a: list = None | ||
b: list = None | ||
design: Literal["paired", "unpaired"] = "unpaired" | ||
measure_units: str = "au" | ||
ci_percentage: int = 95 | ||
estimates_a, estimates_b, estimates_diff = estimate.calc(pliffy_data) | ||
estimates = blocks.ABD(a=estimates_a, b=estimates_b, diff=estimates_diff) | ||
#TODO: print to screen and save to file all estimates | ||
figure.Figure(pliffy_data, plot_info, estimates, ax) | ||
|
||
|
||
def plot(pliffy_data: PliffyData, plot_info: PlotInfo = PlotInfo()): | ||
"""Main user interfact to generate plot | ||
""" | ||
estimates_diff, diff_vals = estimate.calc(pliffy_data) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be
ax_ab=None
?