Commit
·
f7450ea
1
Parent(s):
a0fdaee
Decluttering the main app file
Browse files
app.py
CHANGED
|
@@ -4,78 +4,7 @@ import gradio as gr
|
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
-
|
| 8 |
-
# random.seed(random_seed)
|
| 9 |
-
iters = int(iters)
|
| 10 |
-
directions = ['east', 'north', 'west', 'south']
|
| 11 |
-
start_point = [0, 0]
|
| 12 |
-
|
| 13 |
-
if random_seed is None:
|
| 14 |
-
random_seed = random.randint(1, 100000)
|
| 15 |
-
else:
|
| 16 |
-
random_seed = random_seed
|
| 17 |
-
|
| 18 |
-
random.seed(random_seed)
|
| 19 |
-
|
| 20 |
-
def distance_from_start(final_coord, start_coord, round_to=2):
|
| 21 |
-
return round(np.sqrt((final_coord[0] - start_coord[0])**2 + (final_coord[1] - start_coord[1])**2), round_to)
|
| 22 |
-
|
| 23 |
-
def step_addition(old_coord, step):
|
| 24 |
-
return [sum(x) for x in zip(old_coord, step)]
|
| 25 |
-
|
| 26 |
-
def step_determination():
|
| 27 |
-
direction = random.choice(directions)
|
| 28 |
-
if direction == 'east':
|
| 29 |
-
return [1*step_size, 0]
|
| 30 |
-
elif direction == 'west':
|
| 31 |
-
return [-1*step_size, 0]
|
| 32 |
-
elif direction == 'north':
|
| 33 |
-
return [0, 1*step_size]
|
| 34 |
-
elif direction == 'south':
|
| 35 |
-
return [0, -1*step_size]
|
| 36 |
-
|
| 37 |
-
coordinate_list = [start_point]
|
| 38 |
-
|
| 39 |
-
for i in range(iters):
|
| 40 |
-
new_step = step_determination()
|
| 41 |
-
new_coordinate = step_addition(coordinate_list[-1], new_step)
|
| 42 |
-
coordinate_list.append(new_coordinate)
|
| 43 |
-
|
| 44 |
-
x = [i[0] for i in coordinate_list]
|
| 45 |
-
y = [i[1] for i in coordinate_list]
|
| 46 |
-
df = pd.DataFrame({'x':x,'y':y})
|
| 47 |
-
csv_file = "2d_random_walk_coordinates.csv"
|
| 48 |
-
df.to_csv(csv_file, index=False)
|
| 49 |
-
|
| 50 |
-
fig, ax = plt.subplots(1)
|
| 51 |
-
|
| 52 |
-
base_marker_size = 10
|
| 53 |
-
markersize = base_marker_size / np.sqrt(iters)
|
| 54 |
-
|
| 55 |
-
ax.plot(x, y, marker='o', markersize=markersize, linestyle='None')
|
| 56 |
-
|
| 57 |
-
ax.plot(x[0], y[0], marker='o', markersize=5, color="red")
|
| 58 |
-
ax.plot(x[-1], y[-1], marker='o', markersize=5, color="orange")
|
| 59 |
-
|
| 60 |
-
ax.text(start_point[0], start_point[1], 'Start', color='red')
|
| 61 |
-
ax.text(x[-1], y[-1], 'End', color='orange')
|
| 62 |
-
|
| 63 |
-
x_max_index = x.index(max(x))
|
| 64 |
-
x_min_index = x.index(min(x))
|
| 65 |
-
y_max_index = y.index(max(y))
|
| 66 |
-
y_min_index = y.index(min(y))
|
| 67 |
-
|
| 68 |
-
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))
|
| 69 |
-
ax.legend([info_text], loc='best', handlelength=0, handletextpad=0, fancybox=True, fontsize=8)
|
| 70 |
-
|
| 71 |
-
plt.title( '2D Random Walk\nsteps=' + str(iters)+', step size='+ str(step_size)+ ', seed = '+str((random_seed)) )
|
| 72 |
-
plt.grid()
|
| 73 |
-
|
| 74 |
-
fig.canvas.draw()
|
| 75 |
-
image_array = np.array(fig.canvas.renderer.buffer_rgba())
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
return image_array, csv_file
|
| 79 |
|
| 80 |
iters = gr.Number(value=1e6,label="How many random steps?")
|
| 81 |
step_size = gr.Number(value=1,label="Step size")
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
from single_agent_2D import generate_random_walk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
iters = gr.Number(value=1e6,label="How many random steps?")
|
| 10 |
step_size = gr.Number(value=1,label="Step size")
|