Skip to content

Commit e60b89d

Browse files
committed
Small changes. Iris dataset. New unitests
1 parent 9dc9fa9 commit e60b89d

7 files changed

Lines changed: 218 additions & 52 deletions

File tree

dabest/plot_tools.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def get_swarm_spans(coll):
4444
Given a matplotlib Collection, will obtain the x and y spans
4545
for the collection. Will return None if this fails.
4646
"""
47+
if coll is None:
48+
raise ValueError("The collection `coll` parameter cannot be None")
49+
4750
x, y = np.array(coll.get_offsets()).T
4851
try:
4952
return x.min(), x.max(), y.min(), y.max()
@@ -233,7 +236,6 @@ def normalize_dict(nested_dict, target):
233236
for key, value in nested_dict.items():
234237
if isinstance(value, dict):
235238
for subkey in value.keys():
236-
# value[subkey] = target[subkey]['right']
237239
if subkey in val.keys():
238240
if val[subkey] != 0:
239241
# Address the problem when one of the label have zero value
@@ -248,6 +250,12 @@ def normalize_dict(nested_dict, target):
248250

249251

250252
def width_determine(labels, data, pos="left"):
253+
if labels is None:
254+
raise ValueError("The `labels` parameter cannot be None")
255+
256+
if data is None:
257+
raise ValueError("The `data` parameter cannot be None")
258+
251259
widths_norm = defaultdict()
252260
for i, label in enumerate(labels):
253261
myD = {}
@@ -513,16 +521,12 @@ def single_sankey(
513521
if sankey and strip_on:
514522
for leftLabel, rightLabel in itertools.product(leftLabels, rightLabels):
515523
labelColor = leftLabel
524+
516525
if rightColor:
517526
labelColor = rightLabel
518-
if (
519-
len(
520-
dataFrame[
521-
(dataFrame.left == leftLabel) & (dataFrame.right == rightLabel)
522-
]
523-
)
524-
> 0
525-
):
527+
528+
if len(dataFrame[(dataFrame.left == leftLabel) &
529+
(dataFrame.right == rightLabel)]) > 0:
526530
# Create array of y values for each strip, half at left value,
527531
# half at right, convolve
528532
ys_d = np.array(
@@ -531,18 +535,11 @@ def single_sankey(
531535
)
532536
ys_d = np.convolve(ys_d, 0.05 * np.ones(20), mode="valid")
533537
ys_d = np.convolve(ys_d, 0.05 * np.ones(20), mode="valid")
534-
ys_u = np.array(
535-
50
536-
* [
537-
leftWidths_norm[leftLabel]["bottom"]
538-
+ ns_l_norm[leftLabel][rightLabel]
539-
]
540-
+ 50
541-
* [
542-
rightWidths_norm[rightLabel]["bottom"]
543-
+ ns_r_norm[leftLabel][rightLabel]
544-
]
545-
)
538+
# to remove the array wrapping behaviour of black
539+
# fmt: off
540+
ys_u = np.array(50 * [leftWidths_norm[leftLabel]['bottom'] + ns_l_norm[leftLabel][rightLabel]] + \
541+
50 * [rightWidths_norm[rightLabel]['bottom'] + ns_r_norm[leftLabel][rightLabel]])
542+
# fmt: on
546543
ys_u = np.convolve(ys_u, 0.05 * np.ones(20), mode="valid")
547544
ys_u = np.convolve(ys_u, 0.05 * np.ones(20), mode="valid")
548545

nbs/API/bootstrap.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
21-
"# | default_exp _bootstrap_tools"
21+
"#| default_exp _bootstrap_tools"
2222
]
2323
},
2424
{
@@ -28,7 +28,7 @@
2828
"metadata": {},
2929
"outputs": [],
3030
"source": [
31-
"# | hide\n",
31+
"#| hide\n",
3232
"from __future__ import division\n",
3333
"from nbdev.showdoc import *\n",
3434
"import nbdev\n",
@@ -43,7 +43,7 @@
4343
"metadata": {},
4444
"outputs": [],
4545
"source": [
46-
"# |export\n",
46+
"#| export\n",
4747
"import numpy as np\n",
4848
"import pandas as pd\n",
4949
"import seaborn as sns\n",
@@ -60,7 +60,7 @@
6060
"metadata": {},
6161
"outputs": [],
6262
"source": [
63-
"# |export\n",
63+
"#| export\n",
6464
"class bootstrap:\n",
6565
" \"\"\"\n",
6666
" Computes the summary statistic and a bootstrapped confidence interval.\n",
@@ -274,7 +274,7 @@
274274
"metadata": {},
275275
"outputs": [],
276276
"source": [
277-
"# |export\n",
277+
"#| export\n",
278278
"def jackknife_indexes(data):\n",
279279
" # Taken without modification from scikits.bootstrap package.\n",
280280
" \"\"\"\n",

nbs/API/confint_1group.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"metadata": {},
5454
"outputs": [],
5555
"source": [
56-
"#|export\n",
56+
"#| export\n",
5757
"import numpy as np\n",
5858
"from numpy.random import PCG64, RandomState\n",
5959
"from scipy.stats import norm\n",

nbs/API/plot_tools.ipynb

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
" Given a matplotlib Collection, will obtain the x and y spans\n",
100100
" for the collection. Will return None if this fails.\n",
101101
" \"\"\"\n",
102+
" if coll is None:\n",
103+
" raise ValueError(\"The collection `coll` parameter cannot be None\")\n",
104+
"\n",
102105
" x, y = np.array(coll.get_offsets()).T\n",
103106
" try:\n",
104107
" return x.min(), x.max(), y.min(), y.max()\n",
@@ -288,7 +291,6 @@
288291
" for key, value in nested_dict.items():\n",
289292
" if isinstance(value, dict):\n",
290293
" for subkey in value.keys():\n",
291-
" # value[subkey] = target[subkey]['right']\n",
292294
" if subkey in val.keys():\n",
293295
" if val[subkey] != 0:\n",
294296
" # Address the problem when one of the label have zero value\n",
@@ -303,6 +305,12 @@
303305
"\n",
304306
"\n",
305307
"def width_determine(labels, data, pos=\"left\"):\n",
308+
" if labels is None:\n",
309+
" raise ValueError(\"The `labels` parameter cannot be None\")\n",
310+
"\n",
311+
" if data is None:\n",
312+
" raise ValueError(\"The `data` parameter cannot be None\")\n",
313+
" \n",
306314
" widths_norm = defaultdict()\n",
307315
" for i, label in enumerate(labels):\n",
308316
" myD = {}\n",
@@ -568,16 +576,12 @@
568576
" if sankey and strip_on:\n",
569577
" for leftLabel, rightLabel in itertools.product(leftLabels, rightLabels):\n",
570578
" labelColor = leftLabel\n",
579+
" \n",
571580
" if rightColor:\n",
572581
" labelColor = rightLabel\n",
573-
" if (\n",
574-
" len(\n",
575-
" dataFrame[\n",
576-
" (dataFrame.left == leftLabel) & (dataFrame.right == rightLabel)\n",
577-
" ]\n",
578-
" )\n",
579-
" > 0\n",
580-
" ):\n",
582+
" \n",
583+
" if len(dataFrame[(dataFrame.left == leftLabel) & \n",
584+
" (dataFrame.right == rightLabel)]) > 0:\n",
581585
" # Create array of y values for each strip, half at left value,\n",
582586
" # half at right, convolve\n",
583587
" ys_d = np.array(\n",
@@ -586,18 +590,11 @@
586590
" )\n",
587591
" ys_d = np.convolve(ys_d, 0.05 * np.ones(20), mode=\"valid\")\n",
588592
" ys_d = np.convolve(ys_d, 0.05 * np.ones(20), mode=\"valid\")\n",
589-
" ys_u = np.array(\n",
590-
" 50\n",
591-
" * [\n",
592-
" leftWidths_norm[leftLabel][\"bottom\"]\n",
593-
" + ns_l_norm[leftLabel][rightLabel]\n",
594-
" ]\n",
595-
" + 50\n",
596-
" * [\n",
597-
" rightWidths_norm[rightLabel][\"bottom\"]\n",
598-
" + ns_r_norm[leftLabel][rightLabel]\n",
599-
" ]\n",
600-
" )\n",
593+
" # to remove the array wrapping behaviour of black\n",
594+
" # fmt: off\n",
595+
" ys_u = np.array(50 * [leftWidths_norm[leftLabel]['bottom'] + ns_l_norm[leftLabel][rightLabel]] + \\\n",
596+
" 50 * [rightWidths_norm[rightLabel]['bottom'] + ns_r_norm[leftLabel][rightLabel]])\n",
597+
" # fmt: on\n",
601598
" ys_u = np.convolve(ys_u, 0.05 * np.ones(20), mode=\"valid\")\n",
602599
" ys_u = np.convolve(ys_u, 0.05 * np.ones(20), mode=\"valid\")\n",
603600
"\n",

nbs/tests/data/iris.csv

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
sepal_length,sepal_width,petal_length,petal_width,species
2+
5.1,3.5,1.4,0.2,setosa
3+
4.9,3.0,1.4,0.2,setosa
4+
4.7,3.2,1.3,0.2,setosa
5+
4.6,3.1,1.5,0.2,setosa
6+
5.0,3.6,1.4,0.2,setosa
7+
5.4,3.9,1.7,0.4,setosa
8+
4.6,3.4,1.4,0.3,setosa
9+
5.0,3.4,1.5,0.2,setosa
10+
4.4,2.9,1.4,0.2,setosa
11+
4.9,3.1,1.5,0.1,setosa
12+
5.4,3.7,1.5,0.2,setosa
13+
4.8,3.4,1.6,0.2,setosa
14+
4.8,3.0,1.4,0.1,setosa
15+
4.3,3.0,1.1,0.1,setosa
16+
5.8,4.0,1.2,0.2,setosa
17+
5.7,4.4,1.5,0.4,setosa
18+
5.4,3.9,1.3,0.4,setosa
19+
5.1,3.5,1.4,0.3,setosa
20+
5.7,3.8,1.7,0.3,setosa
21+
5.1,3.8,1.5,0.3,setosa
22+
5.4,3.4,1.7,0.2,setosa
23+
5.1,3.7,1.5,0.4,setosa
24+
4.6,3.6,1.0,0.2,setosa
25+
5.1,3.3,1.7,0.5,setosa
26+
4.8,3.4,1.9,0.2,setosa
27+
5.0,3.0,1.6,0.2,setosa
28+
5.0,3.4,1.6,0.4,setosa
29+
5.2,3.5,1.5,0.2,setosa
30+
5.2,3.4,1.4,0.2,setosa
31+
4.7,3.2,1.6,0.2,setosa
32+
4.8,3.1,1.6,0.2,setosa
33+
5.4,3.4,1.5,0.4,setosa
34+
5.2,4.1,1.5,0.1,setosa
35+
5.5,4.2,1.4,0.2,setosa
36+
4.9,3.1,1.5,0.2,setosa
37+
5.0,3.2,1.2,0.2,setosa
38+
5.5,3.5,1.3,0.2,setosa
39+
4.9,3.6,1.4,0.1,setosa
40+
4.4,3.0,1.3,0.2,setosa
41+
5.1,3.4,1.5,0.2,setosa
42+
5.0,3.5,1.3,0.3,setosa
43+
4.5,2.3,1.3,0.3,setosa
44+
4.4,3.2,1.3,0.2,setosa
45+
5.0,3.5,1.6,0.6,setosa
46+
5.1,3.8,1.9,0.4,setosa
47+
4.8,3.0,1.4,0.3,setosa
48+
5.1,3.8,1.6,0.2,setosa
49+
4.6,3.2,1.4,0.2,setosa
50+
5.3,3.7,1.5,0.2,setosa
51+
5.0,3.3,1.4,0.2,setosa
52+
7.0,3.2,4.7,1.4,versicolor
53+
6.4,3.2,4.5,1.5,versicolor
54+
6.9,3.1,4.9,1.5,versicolor
55+
5.5,2.3,4.0,1.3,versicolor
56+
6.5,2.8,4.6,1.5,versicolor
57+
5.7,2.8,4.5,1.3,versicolor
58+
6.3,3.3,4.7,1.6,versicolor
59+
4.9,2.4,3.3,1.0,versicolor
60+
6.6,2.9,4.6,1.3,versicolor
61+
5.2,2.7,3.9,1.4,versicolor
62+
5.0,2.0,3.5,1.0,versicolor
63+
5.9,3.0,4.2,1.5,versicolor
64+
6.0,2.2,4.0,1.0,versicolor
65+
6.1,2.9,4.7,1.4,versicolor
66+
5.6,2.9,3.6,1.3,versicolor
67+
6.7,3.1,4.4,1.4,versicolor
68+
5.6,3.0,4.5,1.5,versicolor
69+
5.8,2.7,4.1,1.0,versicolor
70+
6.2,2.2,4.5,1.5,versicolor
71+
5.6,2.5,3.9,1.1,versicolor
72+
5.9,3.2,4.8,1.8,versicolor
73+
6.1,2.8,4.0,1.3,versicolor
74+
6.3,2.5,4.9,1.5,versicolor
75+
6.1,2.8,4.7,1.2,versicolor
76+
6.4,2.9,4.3,1.3,versicolor
77+
6.6,3.0,4.4,1.4,versicolor
78+
6.8,2.8,4.8,1.4,versicolor
79+
6.7,3.0,5.0,1.7,versicolor
80+
6.0,2.9,4.5,1.5,versicolor
81+
5.7,2.6,3.5,1.0,versicolor
82+
5.5,2.4,3.8,1.1,versicolor
83+
5.5,2.4,3.7,1.0,versicolor
84+
5.8,2.7,3.9,1.2,versicolor
85+
6.0,2.7,5.1,1.6,versicolor
86+
5.4,3.0,4.5,1.5,versicolor
87+
6.0,3.4,4.5,1.6,versicolor
88+
6.7,3.1,4.7,1.5,versicolor
89+
6.3,2.3,4.4,1.3,versicolor
90+
5.6,3.0,4.1,1.3,versicolor
91+
5.5,2.5,4.0,1.3,versicolor
92+
5.5,2.6,4.4,1.2,versicolor
93+
6.1,3.0,4.6,1.4,versicolor
94+
5.8,2.6,4.0,1.2,versicolor
95+
5.0,2.3,3.3,1.0,versicolor
96+
5.6,2.7,4.2,1.3,versicolor
97+
5.7,3.0,4.2,1.2,versicolor
98+
5.7,2.9,4.2,1.3,versicolor
99+
6.2,2.9,4.3,1.3,versicolor
100+
5.1,2.5,3.0,1.1,versicolor
101+
5.7,2.8,4.1,1.3,versicolor
102+
6.3,3.3,6.0,2.5,virginica
103+
5.8,2.7,5.1,1.9,virginica
104+
7.1,3.0,5.9,2.1,virginica
105+
6.3,2.9,5.6,1.8,virginica
106+
6.5,3.0,5.8,2.2,virginica
107+
7.6,3.0,6.6,2.1,virginica
108+
4.9,2.5,4.5,1.7,virginica
109+
7.3,2.9,6.3,1.8,virginica
110+
6.7,2.5,5.8,1.8,virginica
111+
7.2,3.6,6.1,2.5,virginica
112+
6.5,3.2,5.1,2.0,virginica
113+
6.4,2.7,5.3,1.9,virginica
114+
6.8,3.0,5.5,2.1,virginica
115+
5.7,2.5,5.0,2.0,virginica
116+
5.8,2.8,5.1,2.4,virginica
117+
6.4,3.2,5.3,2.3,virginica
118+
6.5,3.0,5.5,1.8,virginica
119+
7.7,3.8,6.7,2.2,virginica
120+
7.7,2.6,6.9,2.3,virginica
121+
6.0,2.2,5.0,1.5,virginica
122+
6.9,3.2,5.7,2.3,virginica
123+
5.6,2.8,4.9,2.0,virginica
124+
7.7,2.8,6.7,2.0,virginica
125+
6.3,2.7,4.9,1.8,virginica
126+
6.7,3.3,5.7,2.1,virginica
127+
7.2,3.2,6.0,1.8,virginica
128+
6.2,2.8,4.8,1.8,virginica
129+
6.1,3.0,4.9,1.8,virginica
130+
6.4,2.8,5.6,2.1,virginica
131+
7.2,3.0,5.8,1.6,virginica
132+
7.4,2.8,6.1,1.9,virginica
133+
7.9,3.8,6.4,2.0,virginica
134+
6.4,2.8,5.6,2.2,virginica
135+
6.3,2.8,5.1,1.5,virginica
136+
6.1,2.6,5.6,1.4,virginica
137+
7.7,3.0,6.1,2.3,virginica
138+
6.3,3.4,5.6,2.4,virginica
139+
6.4,3.1,5.5,1.8,virginica
140+
6.0,3.0,4.8,1.8,virginica
141+
6.9,3.1,5.4,2.1,virginica
142+
6.7,3.1,5.6,2.4,virginica
143+
6.9,3.1,5.1,2.3,virginica
144+
5.8,2.7,5.1,1.9,virginica
145+
6.8,3.2,5.9,2.3,virginica
146+
6.7,3.3,5.7,2.5,virginica
147+
6.7,3.0,5.2,2.3,virginica
148+
6.3,2.5,5.0,1.9,virginica
149+
6.5,3.0,5.2,2.0,virginica
150+
6.2,3.4,5.4,2.3,virginica
151+
5.9,3.0,5.1,1.8,virginica

nbs/tests/test_99_confidence_intervals.ipynb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@
213213
"display_name": "python3",
214214
"language": "python",
215215
"name": "python3"
216-
},
217-
"language_info": {
218-
"name": "python",
219-
"version": "3.10.12"
220216
}
221217
},
222218
"nbformat": 4,

nbs/tests/test_plot_tools.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
from dabest.plot_tools import get_swarm_spans, width_determine
3+
import numpy as np
4+
5+
6+
def test_get_swarm_spans_wrong_parameters():
7+
error_msg = "The collection `coll` parameter cannot be None"
8+
with pytest.raises(ValueError) as excinfo:
9+
get_swarm_spans(None)
10+
11+
assert error_msg in str(excinfo.value)
12+
13+
14+
def test_width_determine():
15+
error_msg = "The `labels` parameter cannot be None"
16+
with pytest.raises(ValueError) as excinfo:
17+
width_determine(None, [])
18+
19+
assert error_msg in str(excinfo.value)
20+
21+
error_msg = "The `data` parameter cannot be None"
22+
with pytest.raises(ValueError) as excinfo:
23+
width_determine("some_labels", None)
24+
25+
assert error_msg in str(excinfo.value)

0 commit comments

Comments
 (0)