Skip to content

Commit

Permalink
Merge pull request #80 from artemis-beta/teleplot
Browse files Browse the repository at this point in the history
Teleplot v0.2.0
  • Loading branch information
LanceMaverick committed Jan 26, 2017
2 parents 2fab8ec + da4892b commit d2cf444
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
53 changes: 50 additions & 3 deletions beards/teleplot/TelePlot.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
import matplotlib.pyplot as plt
from uuid import uuid4
import mpmath as mt
import re
import numpy as np

trig_dict = {'sin' : mt.sin, 'cos' : mt.cos, 'tan' : mt.tan,
'asin' : mt.asin, 'acos' : mt.acos, 'atan' : mt.tan,
'cosec' : mt.csc, 'sec' : mt.sec, 'cot' : mt.cot,
'cospi' : mt.cospi, 'sinpi' : mt.sinpi, 'sinc' : mt.sinc}
hyp_dict = {'sinh' : mt.sinh, 'cosh' : mt.cosh, 'tanh' : mt.tan,
'asinh' : mt.asinh, 'acosh' : mt.acosh, 'atanh' : mt.tanh,
'cosech' : mt.csch, 'sech' : mt.sech, 'coth' : mt.coth}

logInd_dict = {'log' : mt.log, 'exp' : mt.exp, 'log10' : mt.log10}

others_dict = {'sqrt' : mt.sqrt, 'cbrt' : mt.cbrt, 'root' : mt.root,
'power' : mt.power, 'expm1' : mt.expm1,
'fac' : mt.factorial, 'fac2' : mt.fac2, 'gamma' : mt.gamma,
'rgamma' : mt.gamma, 'loggamma' : mt.loggamma,
'superfac' : mt.superfac, 'hyperfac' : mt.hyperfac,
'barnesg' : mt.barnesg, 'psi' : mt.psi,
'harmonic' : mt.harmonic}

eqn_parser = {}
eqn_parser.update(trig_dict)
eqn_parser.update(hyp_dict)
eqn_parser.update(logInd_dict)
eqn_parser.update(others_dict)

def checkandParse(string):
opts2perform = {}
for key in eqn_parser:
regex_str = '({})\(([xy\+\-\/\*\.\d]+)\)'.format(key)
res = re.findall(regex_str, string)
if len(res) > 0:
opts2perform[key] = res[0]
return opts2perform

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

def parseOpts(self, in_array):
range_ = [-10, 10]
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]
if 'range' in opts_tuple[0]:
numbers = opts_tuple[1].split(',')
range_ = numbers
n = 100
if len(range_) > 2:
n = (float(range_[1])-float(range_[0]))/int(range_[2])
self.x = np.linspace(float(range_[0]), float(range_[1]), n)

def savePlot(self):
plt.xlabel(self.xlabel)
plt.ylabel(self.ylabel)
print(self.y)
plt.plot(self.x,self.y)
filename = './{}.png'.format(str(uuid4())[:6])
plt.savefig(filename)
plt.clf()
return filename
46 changes: 31 additions & 15 deletions beards/teleplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def format_msg(msg):
return ' '.join(get_args(text)).title()


class NationalRailDepartures(BeardChatHandler):
class TelePlotSB(BeardChatHandler):
__userhelp__ = """
The following commands are available:
"""
Expand All @@ -31,30 +31,46 @@ class NationalRailDepartures(BeardChatHandler):
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)
eq_parser = TelePlot.eqn_parser
options = re.findall(r'\-(\w+)\s\"([\w\d\/\s\.\-\'\)\(\,]+)', in_string)

print("options: ")
print(options)
plotter = TelePlot.TelePlot()
plotter.parseOpts(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)
opts2perform = TelePlot.checkandParse(in_string)
if len(opts2perform) == 0:
eqn = re.findall(r'(\([\w\(\)\d\s\-\+\*\/]+\))', in_string)
eqn = eqn[0]
for i in plotter.x:
equation = eqn.replace('x','{}'.format(i)).replace('(','').replace(')','')
print(equation)
j = sp.simplify(equation)
plotter.y.append(j)
else:
strings_for_y = []
for i in plotter.x:
res = re.findall(r'\(([\w\*\d\+\/\-\.\,]+)\)', in_string)[0]
final_string = res[0]
for key in opts2perform:
inside = opts2perform[key][1].replace('x','{}'.format(i))
inside = sp.simplify(inside)
operation = eq_parser[key](inside)
final_string = final_string.replace('x'.format(opts2perform[key][0], opts2perform[key][1]), '{}'.format(operation))
print(final_string)
strings_for_y.append(final_string)
for y in strings_for_y:
j = sp.simplify(y)
plotter.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)
file_name = plotter.savePlot()
await self.sender.sendPhoto(('temp.png', open('{}'.format(file_name), 'rb')))
os.remove('{}'.format(file_name))
1 change: 1 addition & 0 deletions beards/teleplot/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bs4==0.0.1
sympy==0.7.6.1
numpy==1.11.0
matplotlib==1.5.1
mpmath==0.19

0 comments on commit d2cf444

Please sign in to comment.