DebasishDhal99
commited on
Commit
·
0dfb40d
1
Parent(s):
7bf9357
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import matplotlib.pyplot as plt
|
2 |
+
import random
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
|
6 |
+
def generate_random_walk(iters):
|
7 |
+
directions = ['east', 'north', 'north', 'south']
|
8 |
+
start_point = [0, 0]
|
9 |
+
|
10 |
+
def distance_from_start(final_coord, start_coord, round_to=2):
|
11 |
+
return round(np.sqrt((final_coord[0] - start_coord[0])**2 + (final_coord[1] - start_coord[1])**2), round_to)
|
12 |
+
|
13 |
+
def step_addition(old_coord, step):
|
14 |
+
return [sum(x) for x in zip(old_coord, step)]
|
15 |
+
|
16 |
+
def step_determination():
|
17 |
+
direction = random.choice(directions)
|
18 |
+
if direction == 'east':
|
19 |
+
return [1, 0]
|
20 |
+
elif direction == 'west':
|
21 |
+
return [-1, 0]
|
22 |
+
elif direction == 'north':
|
23 |
+
return [0, 1]
|
24 |
+
elif direction == 'south':
|
25 |
+
return [0, -1]
|
26 |
+
|
27 |
+
coordinate_list = [start_point]
|
28 |
+
|
29 |
+
for i in range(iters):
|
30 |
+
new_step = step_determination()
|
31 |
+
new_coordinate = step_addition(coordinate_list[-1], new_step)
|
32 |
+
coordinate_list.append(new_coordinate)
|
33 |
+
|
34 |
+
x = [i[0] for i in coordinate_list]
|
35 |
+
y = [i[1] for i in coordinate_list]
|
36 |
+
|
37 |
+
fig, ax = plt.subplots(1)
|
38 |
+
|
39 |
+
base_marker_size = 10
|
40 |
+
markersize = base_marker_size / np.sqrt(iters)
|
41 |
+
|
42 |
+
ax.plot(x, y, marker='o', markersize=markersize, linestyle='None')
|
43 |
+
|
44 |
+
ax.plot(x[0], y[0], marker='o', markersize=5, color="red")
|
45 |
+
ax.plot(x[-1], y[-1], marker='o', markersize=5, color="orange")
|
46 |
+
|
47 |
+
ax.text(start_point[0], start_point[1], 'Start', color='red')
|
48 |
+
ax.text(x[-1], y[-1], 'End', color='orange')
|
49 |
+
|
50 |
+
x_max_index = x.index(max(x))
|
51 |
+
x_min_index = x.index(min(x))
|
52 |
+
y_max_index = y.index(max(y))
|
53 |
+
y_min_index = y.index(min(y))
|
54 |
+
|
55 |
+
info_text = 'Start point=' + str(start_point) + '\n' +'End point=' + str([x[-1],y[-1]]) + '\n' +'Displacement =' + str(distance_from_start([x[-1], y[-1]], start_point)) + '\n' +'Max x = ' + str(max(x)) + '\n' + 'Min x = ' + str(min(x)) + '\n' + 'Max y = ' + str(max(y)) + '\n' + 'Min y = ' + str(min(y))
|
56 |
+
ax.legend([info_text], loc='best', handlelength=0, handletextpad=0, fancybox=True, fontsize=8)
|
57 |
+
|
58 |
+
plt.title('2D Random Walk, iterations = ' + str(iters))
|
59 |
+
plt.grid()
|
60 |
+
|
61 |
+
# Save the plot to an in-memory buffer and return it
|
62 |
+
buf = io.BytesIO()
|
63 |
+
plt.savefig(buf, format='png')
|
64 |
+
buf.seek(0)
|
65 |
+
|
66 |
+
return buf.read()
|
67 |
+
|
68 |
+
# Create a Gradio interface
|
69 |
+
iface = gr.Interface(fn=generate_random_walk, inputs="text", outputs="image", title="Random Walk Plot")
|
70 |
+
iface.launch()
|