From 72b0db610f796d65af37b547100b45be695a35d9 Mon Sep 17 00:00:00 2001 From: Anselm Hahn Date: Fri, 15 May 2020 10:19:04 +0200 Subject: [PATCH 1/3] Add bashplot folder --- .gitignore | 1 + bashplot/__init__.py | 1 + bashplot/bashplot.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+) create mode 100644 .gitignore create mode 100644 bashplot/__init__.py create mode 100644 bashplot/bashplot.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa496a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bashplot/test* diff --git a/bashplot/__init__.py b/bashplot/__init__.py new file mode 100644 index 0000000..a4e2017 --- /dev/null +++ b/bashplot/__init__.py @@ -0,0 +1 @@ +__version__ = "0.1" diff --git a/bashplot/bashplot.py b/bashplot/bashplot.py new file mode 100644 index 0000000..a4caa97 --- /dev/null +++ b/bashplot/bashplot.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +import sys +import os +import argparse +from pathlib import Path + +import numpy as np +import plotille as plt + +if sys.version < "3": + print("Unsupported Python Version (version < 3)!") + sys.exit(1) + + From 84dbca766b7d078ff64b2de50d8531cdecb9519d Mon Sep 17 00:00:00 2001 From: Anselm Hahn Date: Fri, 15 May 2020 22:56:15 +0200 Subject: [PATCH 2/3] Finished classical plot wirthout legend --- .gitignore | 1 + Pipfile | 13 +++ Pipfile.lock | 62 ++++++++++++ bashplot/bashplot.py | 200 ++++++++++++++++++++++++++++++++++++++- test/test_sample_data.py | 27 ++++++ 5 files changed, 301 insertions(+), 2 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock create mode 100644 test/test_sample_data.py diff --git a/.gitignore b/.gitignore index aa496a0..619ad11 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bashplot/test* +bashplot/log.out diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..a7fd5ea --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +plotille = "*" +numpy = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..6d12a2c --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,62 @@ +{ + "_meta": { + "hash": { + "sha256": "5c5c3b14822f76bbbda8d1f6c75582d7db484228d4c5c67390c60461fdf2e2ad" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "numpy": { + "hashes": [ + "sha256:00d7b54c025601e28f468953d065b9b121ddca7fff30bed7be082d3656dd798d", + "sha256:02ec9582808c4e48be4e93cd629c855e644882faf704bc2bd6bbf58c08a2a897", + "sha256:0e6f72f7bb08f2f350ed4408bb7acdc0daba637e73bce9f5ea2b207039f3af88", + "sha256:1be2e96314a66f5f1ce7764274327fd4fb9da58584eaff00b5a5221edefee7d6", + "sha256:2466fbcf23711ebc5daa61d28ced319a6159b260a18839993d871096d66b93f7", + "sha256:2b573fcf6f9863ce746e4ad00ac18a948978bb3781cffa4305134d31801f3e26", + "sha256:3f0dae97e1126f529ebb66f3c63514a0f72a177b90d56e4bce8a0b5def34627a", + "sha256:50fb72bcbc2cf11e066579cb53c4ca8ac0227abb512b6cbc1faa02d1595a2a5d", + "sha256:57aea170fb23b1fd54fa537359d90d383d9bf5937ee54ae8045a723caa5e0961", + "sha256:709c2999b6bd36cdaf85cf888d8512da7433529f14a3689d6e37ab5242e7add5", + "sha256:7d59f21e43bbfd9a10953a7e26b35b6849d888fc5a331fa84a2d9c37bd9fe2a2", + "sha256:904b513ab8fbcbdb062bed1ce2f794ab20208a1b01ce9bd90776c6c7e7257032", + "sha256:96dd36f5cdde152fd6977d1bbc0f0561bccffecfde63cd397c8e6033eb66baba", + "sha256:9933b81fecbe935e6a7dc89cbd2b99fea1bf362f2790daf9422a7bb1dc3c3085", + "sha256:bbcc85aaf4cd84ba057decaead058f43191cc0e30d6bc5d44fe336dc3d3f4509", + "sha256:dccd380d8e025c867ddcb2f84b439722cf1f23f3a319381eac45fd077dee7170", + "sha256:e22cd0f72fc931d6abc69dc7764484ee20c6a60b0d0fee9ce0426029b1c1bdae", + "sha256:ed722aefb0ebffd10b32e67f48e8ac4c5c4cf5d3a785024fdf0e9eb17529cd9d", + "sha256:efb7ac5572c9a57159cf92c508aad9f856f1cb8e8302d7fdb99061dbe52d712c", + "sha256:efdba339fffb0e80fcc19524e4fdbda2e2b5772ea46720c44eaac28096d60720", + "sha256:f22273dd6a403ed870207b853a856ff6327d5cbce7a835dfa0645b3fc00273ec" + ], + "index": "pypi", + "version": "==1.18.4" + }, + "plotille": { + "hashes": [ + "sha256:44afcfaf9d276c6eeef9978bf480127f33f046fe4f88d177bd4899b2acd266a2" + ], + "index": "pypi", + "version": "==3.7" + }, + "six": { + "hashes": [ + "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a", + "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c" + ], + "version": "==1.14.0" + } + }, + "develop": {} +} diff --git a/bashplot/bashplot.py b/bashplot/bashplot.py index a4caa97..e4ffe7c 100644 --- a/bashplot/bashplot.py +++ b/bashplot/bashplot.py @@ -1,14 +1,210 @@ #!/usr/bin/env python3 -import sys -import os import argparse +import os from pathlib import Path +import sys import numpy as np import plotille as plt +try: + from . import __version__ +except ImportError: + from __init__ import __version__ + if sys.version < "3": print("Unsupported Python Version (version < 3)!") sys.exit(1) +def log(arg, mode=None): + + if mode: + print(f"[ERROR] {arg}") + else: + print(arg) + + +def load_data(fname, args): + + data = np.genfromtxt( + fname, + dtype=np.float, + comments=args["comments"], + delimiter=args["delimiter"], + skip_header=args["skip_header"], + skip_footer=args["skip_footer"], + usecols=args["usecols"], + ) + return data + + +def _plot(fig, x, Y): + if Y.shape[1] == 1: + fig.plot(x, Y[:, 0]) + return fig + else: + fig.plot(x, Y[:, 0]) + return _plot(fig, x, Y[:, 1:]) + + +def plot(data, args): + + fig = plt.Figure() + fig.widht = args["size"][0] + fig.height = args["size"][1] + + if args["x_limits"]: + fig.set_x_limits(min_=args["x_limits"][0], max_=args["x_limits"][1]) + else: + fig.set_x_limits(min_=np.min(data[:, 0]), max_=np.max(data[:, 0])) + + if args["y_limits"]: + fig.set_y_limits(min_=args["y_limits"][0], max_=args["y_limits"][1]) + else: + fig.set_y_limits(min_=np.min(data[:, 1]), max_=np.max(data[:, 1])) + + if args["color"]: + fig.color_mode = "rgb" + + x = data[:, 0] + y = data[:, 1:] + + fig = _plot(fig, x=x, Y=y) + print(fig.show(legend=False)) + + +def bashplot(fnames, args): + + if len(fnames) == 1: + data = load_data(fname=fnames[0], args=args) + plot(data, args) + else: + data = load_data(fname=fnames[0], args=args) + plot(data, args) + return bashplot(fnames[1:], args) + + +def get_parser(): + parser = argparse.ArgumentParser( + description=("Instant data plotting from the terminal into the terminal") + ) + # Arguments for loading the data + parser.add_argument( + "infile", nargs="*", type=str, help="load data file(s) as ASCII" + ) + parser.add_argument("-p", "--pos", help="plot", default=1, type=int) + + parser.add_argument( + "-cm", + "--comments", + help=( + "define the character to indicate the start of the document; default is None" + ), + default=None, + type=str, + ) + parser.add_argument( + "-d", + "--delimiter", + help=("define the type of deilimiter; default is any consecutive 'whitespace'"), + default=None, + type=str, + ) + parser.add_argument( + "-skh", + "--skip_header", + help=( + "define the number of skipped rows from the beginning of the document; " + "default is 0" + ), + default=0, + type=int, + ) + parser.add_argument( + "-skf", + "--skip_footer", + help=( + "define the number of skipped rows from the end of the document; " + "default is 0" + ), + default=0, + type=int, + ) + parser.add_argument( + "-col", + "--usecols", + help=( + "define the pair of x- and y-columns for the data plot; default is None. " + "This means all available will be loaded; otherwise tip a series of " + "columns started with 0 like 0, 1, 4" + ), + default=None, + nargs=2, + type=int, + ) + # Arguments for plotting the data + parser.add_argument( + "-sz", + "--size", + help=( + "define the width and heights of the plots; default is width = 60 and the " + "heights = 40. The values have to be int-values." + ), + default=[60, 40], + nargs=2, + type=int, + ) + parser.add_argument( + "-x", + "--x_limits", + help=( + "define the mimimum and maximum x-range of the plot; default is the automatic " + "generated minimum and the maximum of the data, but can be replaced by any " + "float-values." + ), + default=None, + nargs=2, + type=float, + ) + parser.add_argument( + "-y", + "--y_limits", + help=( + "define the mimimum and maximum y-range of the plot; default is the automatic " + "generated minimum and the maximum of the data, but can be replaced by any " + "float-values." + ), + default=None, + nargs=2, + type=float, + ) + parser.add_argument( + "-c", "--color", help=("enable RGB colorized plots"), action="store_true" + ) + parser.add_argument( + "-v", + "--version", + help=("displays the current version of bathchplot"), + action="store_true", + ) + args = vars(parser.parse_args()) + return args + + +def command_line_runner(): + args = get_parser() + + if args["version"]: + log(__version__) + + if not args["infile"]: + log("Missing input file(s)!", mode=1) + return + else: + fnames = args["infile"] + bashplot(fnames, args) + + +if __name__ == "__main__": + command_line_runner() diff --git a/test/test_sample_data.py b/test/test_sample_data.py new file mode 100644 index 0000000..b7dcc0a --- /dev/null +++ b/test/test_sample_data.py @@ -0,0 +1,27 @@ +import os +import unittest + +import numpy as np + + +def generate(fname="test_data.txt"): + """Generation of example test-set. + + generate() generates an example test-set for sin from -2 * pi to +2 * pi for sinus. + + + Parameters + ---------- + fname : str, optional + Filename of the to exported ASCII-data. Default is `test_data.txt`. + """ + + x = np.linspace(-2 * np.pi, 2 * np.pi) + y = np.sin(x) + + data = np.array([x, y]).T + np.savetxt(fname, data) + + +if __name__ == "__main__": + generate() From 883784ddf605c92500ed65de31eb1dcc719c402d Mon Sep 17 00:00:00 2001 From: Anselm Hahn Date: Fri, 15 May 2020 23:22:08 +0200 Subject: [PATCH 3/3] Might be a canvas bug for the RGB --- bashplot/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 184 bytes bashplot/bashplot.py | 58 +++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 bashplot/__pycache__/__init__.cpython-37.pyc diff --git a/bashplot/__pycache__/__init__.cpython-37.pyc b/bashplot/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2701b44907179bf7371fde63cad5951ae3791d68 GIT binary patch literal 184 zcmZ?b<>g`kf^&ZR;zWS-V-N=h7=a82ATH(r5-AK(3@MDk44O<;%m#XfewvK8xZ~r? zQj3Z+^Yh~4S27ea0abyCUw- Ca59kq literal 0 HcmV?d00001 diff --git a/bashplot/bashplot.py b/bashplot/bashplot.py index e4ffe7c..ee92ee5 100644 --- a/bashplot/bashplot.py +++ b/bashplot/bashplot.py @@ -39,49 +39,56 @@ def load_data(fname, args): return data -def _plot(fig, x, Y): +def _plot(fig, x, Y, label): if Y.shape[1] == 1: - fig.plot(x, Y[:, 0]) + fig.plot(x, Y[:, 0], label=label) return fig else: - fig.plot(x, Y[:, 0]) - return _plot(fig, x, Y[:, 1:]) + fig.plot(x, Y[:, 0], label=label) + return _plot(fig, x, Y[:, 1:], label=label) -def plot(data, args): +def plot(data, args, label): fig = plt.Figure() fig.widht = args["size"][0] fig.height = args["size"][1] + try: - if args["x_limits"]: - fig.set_x_limits(min_=args["x_limits"][0], max_=args["x_limits"][1]) - else: - fig.set_x_limits(min_=np.min(data[:, 0]), max_=np.max(data[:, 0])) + if args["x_limits"]: + fig.set_x_limits(min_=args["x_limits"][0], max_=args["x_limits"][1]) + else: + fig.set_x_limits(min_=np.min(data[:, 0]), max_=np.max(data[:, 0])) - if args["y_limits"]: - fig.set_y_limits(min_=args["y_limits"][0], max_=args["y_limits"][1]) - else: - fig.set_y_limits(min_=np.min(data[:, 1]), max_=np.max(data[:, 1])) + if args["y_limits"]: + fig.set_y_limits(min_=args["y_limits"][0], max_=args["y_limits"][1]) + else: + fig.set_y_limits(min_=np.min(data[:, 1]), max_=np.max(data[:, 1])) - if args["color"]: - fig.color_mode = "rgb" + if args["color"]: + fig.color_mode = "rgb" - x = data[:, 0] - y = data[:, 1:] + x = data[:, 0] + y = data[:, 1:] - fig = _plot(fig, x=x, Y=y) - print(fig.show(legend=False)) + fig = _plot(fig, x=x, Y=y, label=label) + if args["legend"]: + print(fig.show(legend=True)) + else: + print(fig.show(legend=False)) + except IndexError: + log(f"corrupted data in {label}", mode=1) + sys.exit(1) def bashplot(fnames, args): if len(fnames) == 1: - data = load_data(fname=fnames[0], args=args) - plot(data, args) + data = load_data(fname=Path(fnames[0]), args=args) + plot(data, args, label=fnames[0]) else: - data = load_data(fname=fnames[0], args=args) - plot(data, args) + data = load_data(fname=Path(fnames[0]), args=args) + plot(data, args, label=fnames[0]) return bashplot(fnames[1:], args) @@ -180,7 +187,10 @@ def get_parser(): type=float, ) parser.add_argument( - "-c", "--color", help=("enable RGB colorized plots"), action="store_true" + "-c", "--color", help=("enable RGB colorized the plots"), action="store_true" + ) + parser.add_argument( + "-l", "--legend", help=("enable the legend of the plots"), action="store_false" ) parser.add_argument( "-v",