Skip to content

Commit

Permalink
Merge pull request #76 from artemis-beta/teleplot
Browse files Browse the repository at this point in the history
Teleplot v0.1.0
  • Loading branch information
LanceMaverick committed Jan 23, 2017
2 parents a203905 + 5e64a08 commit 5619c06
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
26 changes: 26 additions & 0 deletions beards/teleplot/TelePlot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import matplotlib.pyplot as plt
from uuid import uuid4


class TelePlot:
def __init__(self, x, y, plot_type='scatter', style='default'):
self.plotType = plot_type
self.plotStyle = style
self.xlabel = 'x'
self.ylabel = 'y'
self.x = x
self.y = y

def parseOpts(self, in_array):
for opts_tuple in in_array:
if 'xaxis' in opts_tuple[0]:
print("Got Axis title for x of " + opts_tuple[1])
self.xlabel = opts_tuple[1]
if 'yaxis' in opts_tuple[0]:
self.ylabel = opts_tuple[1]
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
plt.plot(self.x,self.y)
filename = './{}.png'.format(str(uuid4())[:6])
plt.savefig(filename)
return filename
60 changes: 60 additions & 0 deletions beards/teleplot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#show spacecats test plugin
# Adapted from work by LanceMaverick
import telepot
import telepot.aio
from skybeard.beards import BeardChatHandler
from skybeard.predicates import regex_predicate
from skybeard.decorators import onerror
from skybeard.utils import get_args
from . import TelePlot
import re
import os
import sympy as sp
from numpy import linspace
from math import *

def format_msg(msg):
text = msg['text']
return ' '.join(get_args(text)).title()


class NationalRailDepartures(BeardChatHandler):
__userhelp__ = """
The following commands are available:
"""

__commands__ = [
("teleplot", "makePlot", "Make plot from two arrays.\n`\\teleplot [x1,x2,..,xN] [y1,y2,..,yN]`\n`\\teleplot (x**2+2*x+3)`\nAdditional options: `-xaxis \"label\"`, `-yaxis \"label\"`\n Currently only index, add, subtract, divide, multiply available for equation notation."),
]

@onerror
async def makePlot(self, msg):
in_string = msg['text'].replace('/teleplot ', '')
arrays = re.findall(r'(\[[\-\d\,\.\s]+\])+', in_string)

options = re.findall(r'\-(\w+)\s\"([\w\d\s\.\-\'\)\(\,]+)', in_string)

print("options: ")
print(options)
if len(arrays) < 1:
eqn = re.findall(r'(\([\w\(\)\d\s\-\+\*\/]+\))', in_string)
eqn = eqn[0]
X = linspace(-10, 10, 100)
Y = []
for i in X:
equation = eqn.replace('x','{}'.format(i)).replace('(','').replace(')','')
j = sp.simplify(equation)
Y.append(j)

else:
print("Arrays: ")
assert len(arrays) == 2, "Error: Insufficient Number of Arrays Given."
X = re.findall(r'([\d\.\-]+)', arrays[0])
Y = re.findall(r'([\d\.\-]+)', arrays[1])
print(X,Y)
content_type, chat_type, chat_id = telepot.glance(msg)

plotter = TelePlot.TelePlot(X, Y)
file_name = plotter.parseOpts(options)
await self.sender.sendPhoto(('temp.png', open('{}'.format(file_name), 'rb')))
os.remove('{}'.format(file_name))
4 changes: 4 additions & 0 deletions beards/teleplot/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bs4==0.0.1
sympy==0.7.6.1
numpy==1.11.0
matplotlib==1.5.1
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ urllib3==1.19.1
yarl==0.8.1
pyconfig==3.1.1
dataset==0.7.0

0 comments on commit 5619c06

Please sign in to comment.