-
Notifications
You must be signed in to change notification settings - Fork 4
added plotting feature using matplotlib #24
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
Merged
StephenNneji
merged 26 commits into
RascalSoftware:main
from
RabiyaF:port-sld-helper-20
Apr 26, 2024
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c6db6c8
added the plotly SLD helper
RabiyaF 412cacf
Updated the error bars
RabiyaF 92cc08f
Updated the Matplotlib plots and added logic to handle negative points
RabiyaF 4f47663
added the pyqtgraph plotting class
RabiyaF 45fcc82
added the PyQt ploting classes
RabiyaF 4461628
Added that Matplotlib and pyqtgraph classes for plotting
RabiyaF 9129014
Updated the matplotlib plotting class
RabiyaF 77ea9ee
cleaned up the redundant files
RabiyaF 59b7fc9
added test data
RabiyaF 1f6cfef
added tests
RabiyaF 07b35e7
updated the tests
RabiyaF 569dd85
updated requirements.txt
RabiyaF 1b4f6cd
updated setup.py
RabiyaF c8e15af
Update the set_logo logic
RabiyaF e947ce6
Updated the set_icon condition
RabiyaF f819023
Updated variable name
RabiyaF 9867bfc
renamed allLayers to resampledLayers in plotting class
RabiyaF 982b604
removed tests
RabiyaF a530d5d
updated tests
RabiyaF bfce7fa
pickled the plotting data
RabiyaF 87d6571
Added the figure class
RabiyaF 4592cdd
updated tests
RabiyaF 80f997b
updated test
RabiyaF 1a9f1f5
added check for thr grid spec
RabiyaF c264e95
updated plot_ref_sld and tests workflow
RabiyaF b0ce30b
Updated tests
RabiyaF 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 hidden or 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 hidden or 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,193 @@ | ||
| """ | ||
| Plots using the matplotlib library | ||
| """ | ||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| from RAT.rat_core import PlotEventData, makeSLDProfileXY | ||
|
|
||
|
|
||
| class Figure: | ||
| """ | ||
| Creates a plotting figure. | ||
| """ | ||
|
|
||
| def __init__(self, row: int = 1, col: int = 2): | ||
| """ | ||
| Initializes the figure and the subplots. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| row : int | ||
| The number of rows in subplot | ||
| col : int | ||
| The number of columns in subplot | ||
| """ | ||
| self._fig, self._ax = \ | ||
| plt.subplots(row, col, num="Reflectivity Algorithms Toolbox (RAT)") | ||
| plt.show(block=False) | ||
| self._esc_pressed = False | ||
| self._close_clicked = False | ||
| self._fig.canvas.mpl_connect("key_press_event", | ||
| self._process_button_press) | ||
| self._fig.canvas.mpl_connect('close_event', | ||
| self._close) | ||
|
|
||
| def wait_for_close(self): | ||
| """ | ||
| Waits for the user to close the figure | ||
| using the esc key. | ||
| """ | ||
| while not (self._esc_pressed or self._close_clicked): | ||
| plt.waitforbuttonpress(timeout=0.005) | ||
| plt.close(self._fig) | ||
|
|
||
| def _process_button_press(self, event): | ||
| """ | ||
| Process the key_press_event. | ||
| """ | ||
| if event.key == 'escape': | ||
| self._esc_pressed = True | ||
|
|
||
| def _close(self, _): | ||
| """ | ||
| Process the close_event. | ||
| """ | ||
| self._close_clicked = True | ||
|
|
||
|
|
||
| def plot_errorbars(ax, x, y, err, onesided, color): | ||
| """ | ||
| Plots the error bars. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ax : matplotlib.axes._axes.Axes | ||
| The axis on which to draw errorbars | ||
| x : np.ndarray | ||
| The shifted data x axis data | ||
| y : np.ndarray | ||
| The shifted data y axis data | ||
| err : np.ndarray | ||
| The shifted data e data | ||
| onesided : bool | ||
| A boolean to indicate whether to draw one sided errorbars | ||
| color : str | ||
| The hex representing the color of the errorbars | ||
| """ | ||
| y_error = [[0]*len(err), err] if onesided else err | ||
| ax.errorbar(x=x, | ||
| y=y, | ||
| yerr=y_error, | ||
| fmt='none', | ||
| ecolor=color, | ||
| elinewidth=1, | ||
| capsize=0) | ||
| ax.scatter(x=x, y=y, s=3, marker="o", color=color) | ||
|
|
||
|
|
||
| def plot_ref_sld(data: PlotEventData, fig: Figure = None, delay: bool = True): | ||
| """ | ||
| Clears the previous plots and updates the ref and SLD plots. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| data : PlotEventData | ||
| The plot event data that contains all the information | ||
| to generate the ref and sld plots | ||
| fig : Figure | ||
| The figure class that has two subplots | ||
| delay : bool | ||
| Controls whether to delay 0.005s after plot is created | ||
|
|
||
| Returns | ||
| ------- | ||
| fig : Figure | ||
| The figure class that has two subplots | ||
| """ | ||
| if fig is None: | ||
| fig = Figure() | ||
| elif fig._ax.shape != (2,): | ||
| fig._fig.clf() | ||
| fig._ax = fig._fig.subplots(1, 2) | ||
|
|
||
| ref_plot = fig._ax[0] | ||
| sld_plot = fig._ax[1] | ||
|
|
||
| # Clears the previous plots | ||
| ref_plot.cla() | ||
| sld_plot.cla() | ||
|
|
||
| for i, (r, sd, sld, layer) in enumerate(zip(data.reflectivity, | ||
| data.shiftedData, | ||
| data.sldProfiles, | ||
| data.resampledLayers)): | ||
|
|
||
| r, sd, sld, layer = map(lambda x: x[0], (r, sd, sld, layer)) | ||
|
|
||
| # Calculate the divisor | ||
| div = 1 if i == 0 else 2**(4*(i+1)) | ||
|
|
||
| # Plot the reflectivity on plot (1,1) | ||
| ref_plot.plot(r[:, 0], | ||
| r[:, 1]/div, | ||
| label=f'ref {i+1}', | ||
| linewidth=2) | ||
| color = ref_plot.get_lines()[-1].get_color() | ||
|
|
||
| if data.dataPresent[i]: | ||
| sd_x = sd[:, 0] | ||
| sd_y, sd_e = map(lambda x: x/div, (sd[:, 1], sd[:, 2])) | ||
|
|
||
| # Plot the errorbars | ||
| indices_removed = np.flip(np.nonzero(sd_y - sd_e < 0)[0]) | ||
| sd_x_r, sd_y_r, sd_e_r = map(lambda x: | ||
| np.delete(x, indices_removed), | ||
| (sd_x, sd_y, sd_e)) | ||
| plot_errorbars(ref_plot, sd_x_r, sd_y_r, sd_e_r, False, color) | ||
|
|
||
| # Plot one sided errorbars | ||
| indices_selected = [x for x in indices_removed | ||
| if x not in np.nonzero(sd_y < 0)[0]] | ||
| sd_x_s, sd_y_s, sd_e_s = map(lambda x: | ||
| [x[i] for i in indices_selected], | ||
| (sd_x, sd_y, sd_e)) | ||
| plot_errorbars(ref_plot, sd_x_s, sd_y_s, sd_e_s, True, color) | ||
|
|
||
| # Plot the slds on plot (1,2) | ||
| for j in range(1, sld.shape[1]): | ||
| sld_plot.plot(sld[:, 0], | ||
| sld[:, j], | ||
| label=f'sld {i+1}', | ||
| color=color, | ||
| linewidth=2) | ||
|
|
||
| if data.resample[i] == 1 or data.modelType == 'custom xy': | ||
| new = makeSLDProfileXY(layer[0, 1], | ||
| layer[-1, 1], | ||
| data.subRoughs[i], | ||
| layer, | ||
| len(layer), | ||
| 1.0) | ||
|
|
||
| sld_plot.plot([row[0]-49 for row in new], | ||
| [row[1] for row in new], | ||
| color=color, | ||
| linewidth=1) | ||
|
|
||
| # Format the axis | ||
| ref_plot.set_yscale('log') | ||
| ref_plot.set_xscale('log') | ||
| ref_plot.set_xlabel('Qz') | ||
| ref_plot.set_ylabel('Ref') | ||
| ref_plot.legend() | ||
| ref_plot.grid() | ||
|
|
||
| sld_plot.set_xlabel('Z') | ||
| sld_plot.set_ylabel('SLD') | ||
| sld_plot.legend() | ||
| sld_plot.grid() | ||
|
|
||
| if delay: | ||
| plt.pause(0.005) | ||
|
|
||
| return fig | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Binary file not shown.
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.
Uh oh!
There was an error while loading. Please reload this page.