Skip to content

Commit ada3c9c

Browse files
committed
[OSAB] Add basic scatterplot test
1 parent 24ad106 commit ada3c9c

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

bashplotlib/scatterplot.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import csv
1010
import sys
1111
import optparse
12+
import os
1213
from .utils.helpers import *
1314
from .utils.commandhelp import scatter
1415

@@ -28,27 +29,27 @@ def get_scale(series, is_y=False, steps=20):
2829
return scaled_series
2930

3031

31-
def _plot_scatter(xs, ys, size, pch, colour, title, cs):
32+
def build_scatter(xs, ys, size, pch, colour, title):
3233
plotted = set()
3334

3435
plot_str = ""
3536

3637
if title:
3738
plot_str += box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))
39+
plot_str += os.linesep
3840

3941
plot_str += "-" * (2 * (len(get_scale(xs, False, size)) + 2))
42+
plot_str += os.linesep
4043
for y in get_scale(ys, True, size):
41-
plot_str += "|"
44+
plot_str += "| "
4245
for x in get_scale(xs, False, size):
4346
point = " "
4447
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
4548
if xp <= x and yp >= y and (xp, yp) not in plotted:
4649
point = pch
4750
plotted.add((xp, yp))
48-
if cs:
49-
colour = cs[i]
50-
printcolour(point + " ", True, colour)
51-
plot_str += " |"
51+
plot_str += buildcolour(point + " ", colour)
52+
plot_str += " |" + os.linesep
5253

5354
plot_str += "-" * (2 * (len(get_scale(xs, False, size)) + 2))
5455
return plot_str
@@ -66,7 +67,6 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
6667
colour -- colour of the points
6768
title -- title of the plot
6869
"""
69-
cs = None
7070
if f:
7171
if isinstance(f, str):
7272
with open(f) as fh:
@@ -75,8 +75,6 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
7575
data = [tuple(line.strip().split(',')) for line in f]
7676
xs = [float(i[0]) for i in data]
7777
ys = [float(i[1]) for i in data]
78-
if len(data[0]) > 2:
79-
cs = [i[2].strip() for i in data]
8078
elif isinstance(xs, list) and isinstance(ys, list):
8179
pass
8280
else:
@@ -85,8 +83,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
8583
with open(ys) as fh:
8684
ys = [float(str(row).strip()) for row in fh]
8785

88-
print _plot_scatter(xs, ys, size, pch, colour, title, cs)
89-
86+
print(build_scatter(xs, ys, size, pch, colour, title))
9087

9188

9289
def main():

bashplotlib/utils/helpers.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,30 @@ def get_colour(colour):
3030
"""
3131
Get the escape code sequence for a colour
3232
"""
33-
return bcolours.get(colour, bcolours['ENDC'])
33+
return bcolours.get(colour, bcolours['default'])
3434

3535

36-
def printcolour(text, sameline=False, colour=get_colour("ENDC")):
36+
def printcolour(text, sameline=False, colour=get_colour("default")):
3737
"""
38-
Print color text using escape codes
38+
Print color text
3939
"""
4040
if sameline:
4141
sep = ''
4242
else:
4343
sep = '\n'
44-
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
44+
sys.stdout.write(buildcolour(text, colour) + sep)
45+
46+
47+
def buildcolour(text, colour="default"):
48+
"""
49+
Build color text using escape codes
50+
"""
51+
# Unwise hack to avoid having to deal with colors in the
52+
# tests.
53+
if colour == "default":
54+
return text
55+
else:
56+
return get_colour(colour) + text + bcolours["ENDC"]
4557

4658

4759
def drange(start, stop, step=1.0, include_stop=False):

test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from bashplotlib.scatterplot import build_scatter
2+
3+
if __name__ == "__main__":
4+
# Build a scatter plot
5+
scatter = build_scatter(
6+
[-10,20,30],
7+
[-10,20,30],
8+
10,
9+
'x',
10+
'default',
11+
'test hello')
12+
13+
# This is what we know our scatter plot should
14+
# look like. We have to be careful that indentations
15+
# match.
16+
expected_scatter = """----------------------------
17+
| test hello |
18+
----------------------------
19+
----------------------------
20+
| x |
21+
| |
22+
| |
23+
| x |
24+
| |
25+
| |
26+
| |
27+
| |
28+
| |
29+
| |
30+
| |
31+
| x |
32+
----------------------------"""
33+
34+
# Compare what we got to what we expected, and
35+
# fail with a helpful error message if anything is
36+
# different.
37+
if expected_scatter == scatter:
38+
print("SUCCESS!")
39+
else:
40+
print("FAILURE!")
41+
print("Expected:")
42+
print(expected_scatter)
43+
print("Got:")
44+
print(scatter)

0 commit comments

Comments
 (0)