File size: 3,200 Bytes
0dfb40d 3adf8d4 0e60a05 0dfb40d 4561153 64eeeed a35a874 89e4f8f 0dfb40d 64eeeed 73396ce 54dcd4b 64eeeed 0dfb40d 81a0426 0dfb40d 81a0426 0dfb40d 81a0426 0dfb40d 81a0426 0dfb40d 0e60a05 0dfb40d 63e2400 0dfb40d cf306d8 0e60a05 ec01724 671dbb5 01a3c1e 6ce18df 54dcd4b 0dfb40d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import matplotlib.pyplot as plt
import random
import gradio as gr
import numpy as np
import pandas as pd
def generate_random_walk(iters, step_size = 1, random_seed = None):
# random.seed(random_seed)
iters = int(iters)
directions = ['east', 'north', 'west', 'south']
start_point = [0, 0]
if random_seed is None:
random_seed = random.randint(1, 100000)
else:
random_seed = random_seed
def distance_from_start(final_coord, start_coord, round_to=2):
return round(np.sqrt((final_coord[0] - start_coord[0])**2 + (final_coord[1] - start_coord[1])**2), round_to)
def step_addition(old_coord, step):
return [sum(x) for x in zip(old_coord, step)]
def step_determination():
direction = random.choice(directions)
if direction == 'east':
return [1*step_size, 0]
elif direction == 'west':
return [-1*step_size, 0]
elif direction == 'north':
return [0, 1*step_size]
elif direction == 'south':
return [0, -1*step_size]
coordinate_list = [start_point]
for i in range(iters):
new_step = step_determination()
new_coordinate = step_addition(coordinate_list[-1], new_step)
coordinate_list.append(new_coordinate)
x = [i[0] for i in coordinate_list]
y = [i[1] for i in coordinate_list]
df = pd.DataFrame({'x':x,'y':y})
csv_file = "2d_random_walk_coordinates.csv"
df.to_csv(csv_file, index=False)
fig, ax = plt.subplots(1)
base_marker_size = 10
markersize = base_marker_size / np.sqrt(iters)
ax.plot(x, y, marker='o', markersize=markersize, linestyle='None')
ax.plot(x[0], y[0], marker='o', markersize=5, color="red")
ax.plot(x[-1], y[-1], marker='o', markersize=5, color="orange")
ax.text(start_point[0], start_point[1], 'Start', color='red')
ax.text(x[-1], y[-1], 'End', color='orange')
x_max_index = x.index(max(x))
x_min_index = x.index(min(x))
y_max_index = y.index(max(y))
y_min_index = y.index(min(y))
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))
ax.legend([info_text], loc='best', handlelength=0, handletextpad=0, fancybox=True, fontsize=8)
plt.title( '2D Random Walk\nsteps=' + str(iters)+', step size='+ str(step_size)+ ', seed = '+str((random_seed)) )
plt.grid()
fig.canvas.draw()
image_array = np.array(fig.canvas.renderer.buffer_rgba())
return image_array, csv_file
iters = gr.Number(value=1e5,label="How many random steps?")
step_size = gr.Number(value=1,label="Step size")
random_seed = gr.Number(value=42,label="Random seed (Delete it to go full random mode)")
iface = gr.Interface(fn=generate_random_walk, inputs=[iters, step_size, random_seed], outputs=["image","file"], title="2-D Random Walk", description="Uniform steps along NEWS directions only")
iface.launch()
|