Skip to content

Commit 0201917

Browse files
author
Hamel Husain
authored
Merge pull request #276 from fastai/fix-sparkline
add ceil to make mx and mn work in sparkline
2 parents b641e30 + e389b35 commit 0201917

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

fastcore/xtras.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ def truncstr(s:str, maxlen:int, suf:str='…', space='')->str:
242242
spark_chars = '▁▂▃▅▆▇'
243243

244244
# Cell
245-
def _sparkchar(x, mn, incr, empty_zero):
245+
def _ceil(x, lim=None): return x if (not lim or x <= lim) else lim
246+
247+
def _sparkchar(x, mn, mx, incr, empty_zero):
246248
if x is None or (empty_zero and not x): return ' '
247249
if incr == 0: return spark_chars[0]
248-
res = int((x-mn)/incr-0.5)
250+
res = int((_ceil(x,mx)-mn)/incr-0.5)
249251
return spark_chars[res]
250252

251253
# Cell
@@ -254,7 +256,7 @@ def sparkline(data, mn=None, mx=None, empty_zero=False):
254256
valid = [o for o in data if o is not None]
255257
if not valid: return ' '
256258
mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)
257-
res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]
259+
res = [_sparkchar(x=o, mn=mn, mx=mx, incr=(mx-mn)/n, empty_zero=empty_zero) for o in data]
258260
return ''.join(res)
259261

260262
# Cell

nbs/03_xtras.ipynb

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@
618618
{
619619
"data": {
620620
"text/plain": [
621-
"['g', 'a', 'f', 'c', 'e', 'd', 'h', 'b']"
621+
"['g', 'e', 'b', 'd', 'a', 'f', 'h', 'c']"
622622
]
623623
},
624624
"execution_count": null,
@@ -1249,7 +1249,7 @@
12491249
{
12501250
"data": {
12511251
"text/plain": [
1252-
"Path('.ipynb_checkpoints')"
1252+
"Path('hamel.py')"
12531253
]
12541254
},
12551255
"execution_count": null,
@@ -1283,7 +1283,7 @@
12831283
{
12841284
"data": {
12851285
"text/plain": [
1286-
"(Path('../fastcore/net.py'), Path('03a_parallel.ipynb'))"
1286+
"(Path('../fastcore/xtras.py'), Path('08_script.ipynb'))"
12871287
]
12881288
},
12891289
"execution_count": null,
@@ -1400,10 +1400,12 @@
14001400
"outputs": [],
14011401
"source": [
14021402
"#export\n",
1403-
"def _sparkchar(x, mn, incr, empty_zero):\n",
1403+
"def _ceil(x, lim=None): return x if (not lim or x <= lim) else lim\n",
1404+
"\n",
1405+
"def _sparkchar(x, mn, mx, incr, empty_zero):\n",
14041406
" if x is None or (empty_zero and not x): return ' '\n",
14051407
" if incr == 0: return spark_chars[0]\n",
1406-
" res = int((x-mn)/incr-0.5)\n",
1408+
" res = int((_ceil(x,mx)-mn)/incr-0.5)\n",
14071409
" return spark_chars[res]"
14081410
]
14091411
},
@@ -1419,7 +1421,7 @@
14191421
" valid = [o for o in data if o is not None]\n",
14201422
" if not valid: return ' '\n",
14211423
" mn,mx,n = ifnone(mn,min(valid)),ifnone(mx,max(valid)),len(spark_chars)\n",
1422-
" res = [_sparkchar(o,mn,(mx-mn)/n,empty_zero) for o in data]\n",
1424+
" res = [_sparkchar(x=o, mn=mn, mx=mx, incr=(mx-mn)/n, empty_zero=empty_zero) for o in data]\n",
14231425
" return ''.join(res)"
14241426
]
14251427
},
@@ -1443,6 +1445,33 @@
14431445
"print(f' with \"empty_zero\": {sparkline(data, empty_zero=True )}')"
14441446
]
14451447
},
1448+
{
1449+
"cell_type": "markdown",
1450+
"metadata": {},
1451+
"source": [
1452+
"You can set a maximum and minimum for the y-axis of the sparkline with the arguments `mn` and `mx` respectively:"
1453+
]
1454+
},
1455+
{
1456+
"cell_type": "code",
1457+
"execution_count": null,
1458+
"metadata": {},
1459+
"outputs": [
1460+
{
1461+
"data": {
1462+
"text/plain": [
1463+
"'▂▅▇▇'"
1464+
]
1465+
},
1466+
"execution_count": null,
1467+
"metadata": {},
1468+
"output_type": "execute_result"
1469+
}
1470+
],
1471+
"source": [
1472+
"sparkline([1,2,3,400], mn=0, mx=3)"
1473+
]
1474+
},
14461475
{
14471476
"cell_type": "code",
14481477
"execution_count": null,
@@ -1531,8 +1560,8 @@
15311560
"name": "stdout",
15321561
"output_type": "stream",
15331562
"text": [
1534-
"Num Events: 3, Freq/sec: 272.3\n",
1535-
"Most recent: ▇▁▁▂▁ 460.5 360.2 359.1 391.6 379.3\n"
1563+
"Num Events: 7, Freq/sec: 346.2\n",
1564+
"Most recent: ▃▇▆▁▆ 320.1 427.2 389.6 208.7 401.4\n"
15361565
]
15371566
}
15381567
],

0 commit comments

Comments
 (0)