Commit a4b8cfc4 by Jigyasa Watwani

code to plot movies from the data, find perimeter and long axis

parent 089ebb16
Showing with 104 additions and 0 deletions
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.widgets import Slider
def perimeter(X, Y, n):
# Initialize perimeter
perimeter = 0
# Calculate distance between each pair of consecutive points
for j in range(n):
# Get current point
x1, y1 = X[j], Y[j]
# Get next point (if i is the last index, get the first point)
if j == n-1:
x2, y2 = X[0], Y[0]
else:
x2, y2 = X[j+1], Y[j+1]
# Calculate distance between points and add to perimeter
perimeter += np.sqrt((x2-x1)**2 + (y2-y1)**2)
return perimeter
def long_axis(x, y):
x_diff = x[:, None] - x
y_diff = y[:, None] - y
distances = np.sqrt(x_diff**2 + y_diff**2)
return np.max(distances)
# NOTE: check perimeter and long_axis functions on known shapes
# x = np.array([0, 1, 0, -1])
# y = np.array([2, 0, -2, 0])
# print(x, y)
# print(long_axis(x, y))
# raise SystemExit
file = np.loadtxt('xy_coordinates_movie1.txt')
number_of_pixels = file.shape[0]
time_points = file.shape[1]
time_array = np.arange(0, 5*int(time_points/2), 5)
# x, y coordinates from data
x, y = np.zeros((number_of_pixels, int(time_points/2))), np.zeros((number_of_pixels, int(time_points/2)))
for i in range(0, int(time_points/2)):
x[:, i] = file[:, 2*i]
y[:, i] = file[:, 2*i+1]
# Interpolate a value to replace NaN based on its neighbors
x = pd.DataFrame(x)
x = np.array(x.interpolate(method='linear', axis=0, limit_direction='both'))
y= pd.DataFrame(y)
y = np.array(y.interpolate(method='linear', axis=0, limit_direction='both'))
# x, y = (time x pixels)
x = x.T
y = y.T
# make movie of the contours
fig, ax = plt.subplots(1, 1, sharex=True, figsize=(8,8))
ax.set_xlabel(r'$x$')
ax.set_xlim(np.min(x), np.max(x))
ax.set_ylim(np.min(y), np.max(y))
ax.set_ylabel(r'$y$')
outlineplot, = ax.plot(x[0], y[0], marker = 'o', ms=3, linestyle='None')
def update(value):
ti = np.abs(time_array - value).argmin()
outlineplot.set_ydata(y[ti])
outlineplot.set_xdata(x[ti])
plt.draw()
sax = plt.axes([0.1, 0.92, 0.7, 0.02])
slider = Slider(sax, r'$t/\tau$', min(time_array), max(time_array),
valinit=min(time_array), valfmt='%3.1f',
fc='#999999')
slider.drawon = False
slider.on_changed(update)
plt.show()
# find the long axis
long_axis_array = np.zeros(int(time_points/2))
for k in range(0, int(time_points/2)):
long_axis_array[k] = long_axis(x[k], y[k])
plt.plot(time_array, long_axis_array, marker='o', linestyle='None')
plt.xlabel('Time')
plt.ylabel('Length of the long axis')
plt.show()
# find the perimeter
perimeter_array = np.zeros(int(time_points/2))
for k in range(0, int(time_points/2)):
perimeter_array[k] = perimeter(x[k], y[k], number_of_pixels)
plt.plot(time_array, perimeter_array, marker='o', linestyle='None')
plt.xlabel('Time')
plt.ylabel('Perimeter')
plt.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment