Coverage for tests/ana/visualize/test_plot_hloop.py : 97%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# SPDX-FileCopyrightText: 2020 Jonathan Pieper <jpieper@stud.uni-frankfurt.de>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5import unittest
7import logging # System Modules
8import os
10# Basic Plotting libraries
11import matplotlib.pyplot as plt
12import matplotlib
13import seaborn as sns
14from mpl_toolkits.axes_grid1.inset_locator import inset_axes
16# Math / Science Libraries
17import pandas as pd
18import numpy as np
19import scipy
20import ana
22logging.basicConfig(level=logging.WARNING)
25class PlotHloopsCase(unittest.TestCase):
26 def setUp(self):
27 self.meas = {}
29 filename = 'output/'
30 if not os.path.exists(filename):
31 os.makedirs(filename)
33 # figures?
34 self.save_figures = True
35 self.ext = 'pdf' # or png, pgf
36 self.figsize = 16, 12
37 self.style = dict(
38 default=True,
39 figsize=self.figsize,
40 )
42 self.eva = ana.HandleM(directory='**/data')
43 self.m = ana.Hloop(57)
44 self.m.style.set_style(**self.style)
46 self.plot = ana.plot.Plot()
48 if os.path.exists('data/angles_info.csv'):
49 self.a = pd.read_csv('data/angles_info.csv')
50 else:
51 self.a = pd.DataFrame()
53 def test_plot(self):
54 self.plot.plot_hloops([54,55], 'output/hloop-compare-1')
56 def test_plot_m429(self):
57 self.plot.plot_single_hloop(nr=429, xlim=(-300, 300))
59 def test_m57_zoomed(self):
60 # Global information
61 x1, x2 = -750, 750
62 y1, y2 = -1.5, .5
63 main_color = '#CCCCFF'
65 # Inset position and limits
66 ## Position = x_pos, y_pos, width, height
67 ## unit of positions are in % of frame
68 ## x_pos, ypos points to lower left corner of inset
69 i1pos = .69, .3, .3, .3
70 i1x1, i1x2 = -100, 100
71 i2pos = .65, .8, .34, .2
72 i2x1, i2x2 = 530, 620
73 i2y1, i2y2 = -.175, 0.05
74 i3pos = .055, .65, .25, .3
75 i3x1, i3x2 = -650, -250
76 i3y1, i3y2 = -.05, .125
77 i4pos = .06, .03, .3, .25
78 i4x1, i4x2 = -100, 0
79 i4y1, i4y2 = -1.35, -.75
80 i5pos = .07, .36, .4, .28
81 i5x1, i5x2 = -300, 50
82 i5y1, i5y2 = -.7, .45
84 # Highlighting limits
85 h0range, h0color, h0alpha = 25, 'blue', .1
86 h1x1, h1x2, h1color, h1alpha = -611, -443, 'orange', .1
87 h2x1, h2x2, h2color, h2alpha = -291, -443, 'red', .1
88 h3x1, h3x2, h3color, h3alpha = -291.13, 36.56, 'green', .1
90 # Create Plot
91 fig, ax = plt.subplots(figsize=self.figsize)
93 # Plot hysetersis
94 self.m.plot_strayfield(ax, 'm57: Strayfield ($90^\\circ$)')
96 # Draw Inset 1
97 inset = inset_axes(ax, width='100%', height='90%',
98 bbox_to_anchor=i1pos,
99 bbox_transform=ax.transAxes)
100 # Highlight 0 / 3
101 inset.fill([-h0range, -h0range, h0range, h0range], [y2, y1, y1, y2],
102 h0color, alpha=h0alpha)
103 inset.fill([h3x1, h3x1, h3x2, h3x2], [y2, y1, y1, y2], h3color,
104 alpha=h3alpha)
106 # Highlight tertiary range
107 i1tert = h0range / 3 # Tertiary range
108 inset.plot([i1tert, i1tert], [y1, y2], 'b--', alpha=.5)
109 inset.plot([-i1tert, -i1tert], [y1, y2], 'b--', alpha=.5)
111 self.m.plot_strayfield(inset, '$B \\in (-100, 100)$ mT', nolegend=True)
112 inset.set_xlim(i1x1, i1x2)
113 inset.set_ylim(y1, y2)
115 # Draw Inset 2
116 inset2 = inset_axes(ax, width='100%', height='90%',
117 bbox_to_anchor=i2pos,
118 bbox_transform=ax.transAxes)
119 self.m.plot_strayfield(inset2, '$B \\in (500, 600)$ mT', nolegend=True)
120 inset2.set_xlim(i2x1, i2x2)
121 inset2.set_ylim(i2y1, i2y2)
123 # Draw Inset 3
124 inset3 = inset_axes(ax, width='100%', height='90%',
125 bbox_to_anchor=i3pos,
126 bbox_transform=ax.transAxes)
127 self.m.plot_strayfield(inset3, '$B \\in (%s, %s)$ mT' % (i3x1, i3x2),
128 nolegend=True)
129 inset3.set_xlim(i3x1, i3x2)
130 inset3.set_ylim(i3y1, i3y2)
131 # Highlight 1 / 2
132 inset3.fill([h1x1, h1x1, h1x2, h1x2], [i3y1, i3y2, i3y2, i3y1],
133 h1color, alpha=h1alpha)
134 inset3.fill([h2x1, h2x1, h2x2, h2x2], [i3y1, i3y2, i3y2, i3y1],
135 h2color, alpha=h2alpha)
137 # Draw Inset 4
138 inset4 = inset_axes(ax, width='100%', height='90%',
139 bbox_to_anchor=i4pos,
140 bbox_transform=ax.transAxes)
141 self.m.plot_strayfield(inset4, '$B \\in (%s, %s)$ mT' % (i4x1, i4x2),
142 nolegend=True)
143 inset4.set_xlim(i4x1, i4x2)
144 inset4.set_ylim(i4y1, i4y2)
146 # Draw Inset 5
147 inset5 = inset_axes(ax, width='100%', height='90%',
148 bbox_to_anchor=i5pos,
149 bbox_transform=ax.transAxes)
150 self.m.plot_strayfield(inset5, '$B \\in (%s, %s)$ mT' % (i5x1, i5x2),
151 nolegend=True)
152 inset5.set_xlim(i5x1, i5x2)
153 inset5.set_ylim(i5y1, i5y2)
154 inset5.fill([h3x1, h3x1, h3x2, h3x2], [i5y2, i5y1, i5y1, i5y2],
155 h3color, alpha=h3alpha)
157 # Main Plot limits
158 ax.set_xlim(x1, x2)
159 ax.set_ylim(y1, y2)
161 # Highlight in main plot
162 ax.fill([-h0range, -h0range, h0range, h0range], [y1, y2, y2, y1],
163 h0color, alpha=h0alpha)
164 ax.fill([h1x1, h1x1, h1x2, h1x2], [y1, y2, y2, y1], h1color,
165 alpha=h1alpha)
166 ax.fill([h2x1, h2x1, h2x2, h2x2], [y1, y2, y2, y1], h2color,
167 alpha=h2alpha)
168 ax.fill([h3x1, h3x1, h3x2, h3x2], [y1, y2, y2, y1], h3color,
169 alpha=h3alpha)
171 # Remove x and y labels
172 for i, inset_ax in enumerate([inset, inset2, inset3, inset4, inset5]):
173 inset_ax.set_xlabel('')
174 inset_ax.set_ylabel('')
175 inset_ax.set_title('')
176 ann_x, ann_xx = inset_ax.get_xlim()
177 ann_x += (ann_xx - ann_x) * .05
178 ann_yy, ann_y = inset_ax.get_ylim()
179 ann_y -= (ann_y - ann_yy) * .20
180 inset_ax.text(x=ann_x, y=ann_y, s=chr(i + 97) + ')',
181 fontdict=dict(fontweight='bold', fontsize=24),
182 bbox=dict(boxstyle="round,pad=0.1", fc=main_color,
183 ec="b", lw=1))
185 # Save as image (if needed)
186 if self.save_figures:
187 self.m.style.save_plot('output/m57_zoomed.' + self.ext)
189 return True
192if __name__ == '__main__':
193 unittest.main()