Coding Projects and Examples
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
import pims
images = pims.ImageSequence('../data/*.jpg')
pfiles = glob.glob('../data/github_test_*.txt')
pfiles.sort()
images
<Frames>
Source: D:\Dropbox\morphagenetests\sptv_code\data\*.jpg
Length: 20 frames
Frame Shape: (150, 250, 3)
Pixel Datatype: uint8
data = pd.read_csv(pfiles[0], skiprows=3, usecols=[0, 1, 2, 3],
names=['x', 'y', 'u', 'v'])
plt.imshow(images[0])
plt.quiver(data.x,data.y,data.u,data.v)
<matplotlib.quiver.Quiver at 0x200a3497f40>
data = pd.read_csv(pfiles[-1], skiprows=3, usecols=[0, 1, 2, 3],
names=['x', 'y', 'u', 'v'])
plt.imshow(images[-1])
plt.quiver(data.x,data.y,data.u,data.v)
<matplotlib.quiver.Quiver at 0x200a3597910>
run ../src/main.py
pts = pd.read_csv('../data/pjt_highfric_15deg_glass1cm_071619_crop_artificial_pts_temp.csv')
pts.head()
frame | particle | x | y | |
---|---|---|---|---|
0 | 501.0 | 0.0 | -1.678560 | 0.168361 |
1 | 501.0 | 1.0 | 0.753549 | 4.346883 |
2 | 501.0 | 2.0 | 0.753549 | 8.475915 |
3 | 501.0 | 3.0 | -0.500503 | 12.327540 |
4 | 501.0 | 4.0 | -0.500503 | 16.456572 |
def tp_plot_traj(trajs,images,sample_int=1,particle_int=1,scaled=False,save=False,
cmap=plt.cm.copper):
'''
Convenience function to plot all trajs in a given dataframe
with scaled axes.
Optional settings for plotting every [sample_int] particle location and/or
every [particle_int] particle, useful for extremely large datasets.
'''
f, ax = plt.subplots(figsize=(15,15))
particles = np.sort(trajs['particle'].unique()[::particle_int])
trajs = trajs[trajs.particle.isin(particles)]
# initialize plot
# place ticks outside of plot to avoid covering image
# remove right and upper axes to simplify plot
# only plot ticks on the left and bottom of plot
ax.tick_params(axis='y', direction='out')
ax.tick_params(axis='x', direction='out')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# plot particle trajs leading up to current fram
if scaled:
x = trajs['x']/scale
y = trajs['y']/scale
else:
x = trajs['x']
y = trajs['y']
ax.imshow(np.flipud(images[-1]),alpha=0.5)
ax.scatter(x[::sample_int],y[::sample_int],
c=trajs['frame'][::sample_int],marker='o',
s=2, cmap=cmap,lw=0,
vmin=trajs.frame.min(),vmax=trajs.frame.max())
plt.axis('scaled')
if scaled:
ax.set_xlim([0,im_w/scale])
ax.set_ylim([0,im_h/scale])
ax.set_xlabel('Width [cm]')
ax.set_ylabel('Height [cm]')
else:
ax.set_xlim([0,im_w])
ax.set_ylim([0,im_h])
ax.set_xlabel('Width [px]')
ax.set_ylabel('Height [px]')
if save:
plt.savefig('%s_f%05ito%05i.png'%(prefix,trajs.frame.min(),
trajs.frame.max()),dpi=200,
bbox_inches='tight')
plt.close('all')
tp_plot_traj(pts,images,particle_int=25)