Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to plot contour of multivariate functions #134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ my/
*~

# Generated app.yaml
app.yaml
app.yaml

.vscode
93 changes: 67 additions & 26 deletions app/logic/resultsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ def extract_plot(arguments, evaluated):
result['variable'] = result['variables'][0]
result['input_evaluated'] = [arguments.args[0]]

if len(result['variables']) != 1:
raise ValueError("Cannot plot function of multiple variables")
# if len(result['variables']) != 1:
# raise ValueError("Cannot plot function of multiple variables")
else:
variables = set()
try:
Expand Down Expand Up @@ -517,9 +517,10 @@ def eval_plot(evaluator, components, parameters=None):
parameters = {}

xmin, xmax = parameters.get('xmin', -10), parameters.get('xmax', 10)
ymin, ymax = [-10, 10]
pmin, pmax = parameters.get('tmin', 0), parameters.get('tmax', 2 * sympy.pi)
tmin, tmax = parameters.get('tmin', 0), parameters.get('tmax', 10)
from sympy.plotting.plot import LineOver1DRangeSeries, Parametric2DLineSeries
from sympy.plotting.plot import LineOver1DRangeSeries, Parametric2DLineSeries, SurfaceOver2DRangeSeries
functions = evaluator.get("input_evaluated")
if isinstance(functions, sympy.Basic):
functions = [(functions, 'xy')]
Expand All @@ -539,7 +540,7 @@ def eval_plot(evaluator, components, parameters=None):
else:
variables = func.free_symbols

if len(variables) > 1:
if len(variables) > 3:
raise ValueError("Cannot plot multivariate function")
elif len(variables) == 0:
variable = sympy.Symbol('x')
Expand All @@ -555,17 +556,29 @@ def eval_plot(evaluator, components, parameters=None):
graph_range = (variable, tmin, tmax)

if graph_type in ('xy', 'polar'):
series = LineOver1DRangeSeries(func, graph_range, nb_of_points=150)
if len(variables) == 1 :
series = LineOver1DRangeSeries(func, graph_range, nb_of_points=150)
series = series.get_segments()
if len(variables) == 2:
series = SurfaceOver2DRangeSeries(func, graph_range, ('x', -10, 10), nb_of_points=150)
series = series.get_meshes()
series = list(series)
# print("from resultset line 584", series[0][0])
if variable == 'y':
print("true")
series = SurfaceOver2DRangeSeries(func, graph_range, ('x', -10, 10), nb_of_points=150)
series = series.get_meshes()
series = list(series)
elif graph_type == 'parametric':
series = Parametric2DLineSeries(x_func, y_func, graph_range, nb_of_points=150)
series = series.get_segments()
# returns a list of [[x,y], [next_x, next_y]] pairs
series = series.get_segments()
except TypeError:
raise ValueError("Cannot plot function")

xvalues = []
yvalues = []

zvalues = []
def limit_y(y):
CEILING = 1e8
if y > CEILING:
Expand All @@ -575,26 +588,54 @@ def limit_y(y):
return y

x_transform, y_transform = GRAPH_TYPES[graph_type]
series.append([series[-1][1], None])
for point in series:
if point[0][1] is None:
continue
x = point[0][0]
y = limit_y(point[0][1])
xvalues.append(x_transform(x, y))
yvalues.append(y_transform(x, y))

graphs.append({
'type': graph_type,
'function': sympy.jscode(sympy.sympify(func)),
'points': {
'x': xvalues,
'y': yvalues
},
'data': None
})
if len(variables) == 1 :
series.append([series[-1][1], None])
if(len(variables) == 2 ):
series = list(series)
if(len(variables) == 1 ):
for point in series:
if point[0][1] is None:
continue
x = point[0][0]
y = limit_y(point[0][1])
xvalues.append(x_transform(x, y))
yvalues.append(y_transform(x, y))

graphs.append({
'type': graph_type,
'function': sympy.jscode(sympy.sympify(func)),
'points': {
'x': xvalues,
'y': yvalues
},
'data': None
})
if(len(variables) == 2) :
for points in series[0][0]:
xvalues.append(points)
# print("from resultsets line 641 xvalues", xvalues)
for points in series[1]:
yvalues.append(points[0])
# print("from resultsets line 644 yvalues", yvalues)
for points in series[2].data:
new = []
for p in points:
new.append(p)
# print(new)
zvalues.append(new)
# print("from resultsets line 654 yvalues", zvalues)
graphs.append({
'type': graph_type,
'function': sympy.jscode(sympy.sympify(func)),
'points': {
'x': xvalues,
'y': yvalues,
'z': zvalues
},
'data': None
})
return {
'variable': repr(variable),
'variable': repr(variables),
'graphs': json.dumps(graphs)
}

Expand Down
16 changes: 15 additions & 1 deletion static/js/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ function setupPlots() {
}
var card = $(this).parents('.result_card').data('card');
var graphs = JSON.parse($(this).find('.graphs').text());

if(variable.length == 8) {
var plot = new Plot2D(card, $(this)[0], D3Backend, graphs);
plot.show();

Expand Down Expand Up @@ -721,5 +721,19 @@ function setupPlots() {
attributes: true
});
}
}

if(variable.length >= 8) {
card.element.addClass('plot-card');
svg = document.createElement("svg")
document.getElementsByClassName('plot')[0].appendChild(svg)
document.getElementsByTagName('svg')[0].setAttribute('id', "plotly")
const returned = graphs[0].points
const type = {type : 'contour'}
data = Object.assign(returned , type)
data =[data]
console.log(data);
Plotly.newPlot("plotly", data);
}
});
}
1 change: 1 addition & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"
type="text/javascript"></script>
<script src="https://d3js.org/d3.v3.min.js" type="text/javascript"></script>
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<script src="{{ MEDIA_URL }}js/base.js" type="text/javascript"></script>
<script src="{{ MEDIA_URL }}js/card.js" type="text/javascript"></script>
<script src="{{ MEDIA_URL }}js/plot.js" type="text/javascript"></script>
Expand Down