In [1]:
# import numpy as np
# # !/usr/bin/env python3
# # -*- coding: utf-8 -*-
# """
# Created on 20181219
# @author: zhangji
# Trajection of a ellipse, Jeffery equation.
# """
# %pylab inline
# pylab.rcParams['figure.figsize'] = (25, 11)
# fontsize = 40
# import numpy as np
# import scipy as sp
# from scipy.optimize import leastsq, curve_fit
# from scipy import interpolate
# from scipy.interpolate import interp1d
# from scipy.io import loadmat, savemat
# # import scipy.misc
# import matplotlib
# from matplotlib import pyplot as plt
# from matplotlib import animation, rc
# import matplotlib.ticker as mtick
# from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
# from mpl_toolkits.mplot3d import Axes3D, axes3d
# from sympy import symbols, simplify, series, exp
# from sympy.matrices import Matrix
# from sympy.solvers import solve
# from IPython.display import display, HTML
# from tqdm import tqdm_notebook as tqdm
# import pandas as pd
# import re
# from scanf import scanf
# import os
# import glob
# from codeStore import support_fun as spf
# from src.support_class import *
# from src import stokes_flow as sf
# rc('animation', html='html5')
# PWD = os.getcwd()
# font = {'size': 20}
# matplotlib.rc('font', **font)
# np.set_printoptions(linewidth=90, precision=5)
%load_ext autoreload
%autoreload 2
import os
import glob
import natsort
import numpy as np
import scipy as sp
from scipy.optimize import leastsq, curve_fit
from scipy import interpolate, integrate
from scipy import spatial, signal
# from scipy.interpolate import interp1d
from scipy.io import loadmat, savemat
# import scipy.misc
# import importlib
from IPython.display import display, HTML
import pandas as pd
import pickle
import re
from scanf import scanf
import matplotlib
# matplotlib.use('agg')
from matplotlib import pyplot as plt
import matplotlib.colors as colors
from matplotlib import animation, rc
import matplotlib.ticker as mtick
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
from mpl_toolkits.mplot3d import Axes3D, axes3d
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
from mpl_toolkits.mplot3d.art3d import Line3DCollection
from matplotlib import cm
from tqdm.notebook import tqdm as tqdm_notebook
from tqdm import tqdm
from time import time
from src.support_class import *
from src import jeffery_model as jm
from codeStore import support_fun as spf
from codeStore import support_fun_table as spf_tb
# %matplotlib notebook
%matplotlib inline
rc('animation', html='html5')
fontsize = 40
PWD = os.getcwd()
In [2]:
fig = plt.figure(figsize=(2, 2))
fig.patch.set_facecolor('white')
ax0 = fig.add_subplot(1, 1, 1)
In [3]:
job_dir = 'ecoC01B05_passive_psi-0a'
table_name = 'ecoC01B05_tao1_wm0'
t_headle = '(.*?).pickle'
In [4]:
t_dir = os.path.join(PWD, job_dir)
data = spf_tb.load_table_data_pickle_dir(t_dir, t_headle)
lst_eta = data.lst_eta
theta_max_fre = data.theta_max_fre
phi_max_fre = data.phi_max_fre
psi_max_fre = data.psi_max_fre
eta_max_fre = data.eta_max_fre
data_idx = data.data_idx.fillna(-1).astype(int)
In [7]:
theta, phi = 0.0, 1.714
tpick, _ = spf_tb.load_table_date_pickle(job_dir, theta, phi)
Table_t = tpick['Table_t']
Table_dt = tpick['Table_dt']
Table_X = tpick['Table_X']
Table_P = tpick['Table_P']
Table_P2 = tpick['Table_P2']
Table_theta = tpick['Table_theta']
Table_phi = tpick['Table_phi']
Table_psi = tpick['Table_psi']
Table_eta = tpick['Table_eta']
print('-ini_theta %f -ini_phi %f -ini_psi %f' %
(tpick['Table_theta'][0], tpick['Table_phi'][0], tpick['Table_psi'][0]))
freq_pk = spf_tb.get_major_fre(Table_t, Table_theta)
idx = Table_t > Table_t.max() - 1 / freq_pk * 10
# spf_tb.show_table_result(Table_t[idx], Table_dt[idx], Table_X[idx], Table_P[idx], Table_P2[idx],
# Table_theta[idx], Table_phi[idx], Table_psi[idx], Table_eta[idx], save_every)
# spf_tb.show_theta_phi(Table_t[idx], Table_dt[idx], Table_X[idx], Table_P[idx], Table_P2[idx],
# Table_theta[idx], Table_phi[idx], Table_psi[idx], Table_eta[idx], show_back_direction=False)
spf_tb.show_theta_phi_psi_eta(Table_t[idx], Table_dt[idx], Table_X[idx], Table_P[idx], Table_P2[idx],
Table_theta[idx], Table_phi[idx], Table_psi[idx], Table_eta[idx])
# spf_tb.show_center_X(Table_t[idx], Table_dt[idx], Table_X[idx], Table_P[idx], Table_P2[idx],
# Table_theta[idx], Table_phi[idx], Table_psi[idx], Table_eta[idx],
# table_name=table_name)
Out[7]:
In [31]:
f
Out[31]:
In [39]:
figsize = np.array((16, 9)) * 1
dpi = 100
resamp_theta = spf_tb.get_continue_angle(Table_t, Table_theta)
fs = resamp_theta.size / Table_t.max() / 2
f, t, Zxx = signal.stft(np.cos(resamp_theta), fs=fs, nperseg=2**13)
fig, axs = plt.subplots(1, 1, figsize=figsize, dpi=dpi)
axi = axs
axi.pcolormesh(t, f, np.abs(Zxx))
axi.set_xlim(5000, 10000)
# axi.set_ylim(0, 0.1)
# axi.set_yscale('log')
Out[39]:
In [102]:
14 / 0.016 * 4
Out[102]:
In [143]:
figsize = np.array((16, 9)) * 0.5
dpi = 100
show_prim_freq = 3
use_t = np.linspace(Table_t.min(), Table_t.max(), 1 * Table_t.size)
# resamp_theta = spf_tb.get_continue_angle(Table_t, Table_theta, use_t)
taaa = np.random.sample(2)
print(taaa)
resamp_theta = np.cos(use_t * taaa[0] * 2 * np.pi) + np.cos(use_t * taaa[1] * 2 * np.pi)
ty = np.cos(resamp_theta)
# ty = resamp_theta
tmin = np.max((0, use_t.max() - 1000))
idx = use_t > tmin
freq_pk = spf_tb.get_major_fre(use_t[idx], ty[idx])
idx = use_t > (Table_t.max() - 1 / freq_pk * 30)
# tfft = np.fft.rfft(ty[idx])
fs = ty[idx].size / (use_t[idx].max() - use_t[idx].min())
nperseg = fs / freq_pk * 8
f2, Pxx_den = signal.welch(ty[idx], fs=fs, nperseg=nperseg)
fig, axs = plt.subplots(1, 2, figsize=figsize, dpi=dpi)
axi = axs[0]
axi.plot(use_t[idx], resamp_theta[idx], '.-')
axi = axs[1]
axi.loglog(f2, Pxx_den, '.')
tpk = signal.find_peaks(Pxx_den)[0]
fft_abs_pk = Pxx_den[tpk]
freq_pk = f2[tpk]
tidx = np.argsort(fft_abs_pk)[-show_prim_freq:]
axi.loglog(freq_pk[tidx], fft_abs_pk[tidx], '*')
t1 = 'starred freq: \n' + '\n'.join(['$%.5f$' % freq_pk[ti] for ti in tidx])
axi.text(axi.get_xlim()[0] * 1.1, axi.get_ylim()[0] * 2, t1)
Out[143]:
In [131]:
0.949 / 0.15
Out[131]:
In [89]:
tpk = signal.find_peaks(ty[idx])[0]
fft_abs_pk = tfft_abs[tpk]
freq_pk = tfreq[tpk]
tidx = np.argsort(fft_abs_pk)[-show_prim_freq:]
ax1.loglog(freq_pk[tidx], fft_abs_pk[tidx], '*')
Out[89]:
In [ ]:
In [5]:
# put images with same frequence into a subdirect
check_fre_list = [0.0160, 0.0190, 0.0203, 0.0632]
atol_fre_list = [0.0001, 0.0001, 0.0008, 0.0008]
Table_t_range = np.array((4500, np.inf))
def sub_seperate_0(i1, type_fre, iidx, Table_t_range):
t1 = np.zeros_like(iidx[0])
return t1
def sub_seperate_1(i1, type_fre, iidx, Table_t_range):
theta = type_fre.index.values[iidx[0]]
phi = type_fre.columns.values[iidx[1]]
theta_phi_list = np.vstack((theta, phi)).T
tuse = []
for theta, phi in tqdm_notebook(theta_phi_list, desc='No. %d ' % i1):
tpick, _ = spf_tb.load_table_date_pickle(job_dir, theta, phi)
Table_t = tpick['Table_t']
Table_theta = tpick['Table_theta']
Table_phi = tpick['Table_phi']
Table_psi = tpick['Table_psi']
idx = np.logical_and(Table_t >= Table_t_range[0], Table_t <= Table_t_range[1])
tuse.append(np.max(Table_theta[idx]))
tuse = np.hstack(tuse)
t1 = np.ones_like(tuse)
t1[tuse < 1.6] = 0
t1[tuse > 1.934] = 2
return t1
def sub_seperate_2(i1, type_fre, iidx, Table_t_range):
theta = type_fre.index.values[iidx[0]]
phi = type_fre.columns.values[iidx[1]]
theta_phi_list = np.vstack((theta, phi)).T
tuse = []
for theta, phi in tqdm_notebook(theta_phi_list, desc='No. %d ' % i1):
tpick, _ = spf_tb.load_table_date_pickle(job_dir, theta, phi)
Table_t = tpick['Table_t']
Table_theta = tpick['Table_theta']
Table_phi = tpick['Table_phi']
Table_psi = tpick['Table_psi']
idx = np.logical_and(Table_t >= Table_t_range[0], Table_t <= Table_t_range[1])
tuse.append(np.max(Table_phi[idx]))
tuse = np.hstack(tuse)
t1 = np.ones_like(tuse)
t1[tuse < 4] = 0
t1[tuse > 5.1] = 2
return t1
sub_seperate = {0: sub_seperate_0,
1: sub_seperate_1,
2: sub_seperate_2,
3: sub_seperate_0, }
tfre = theta_max_fre.copy()
type_fre = tfre.copy()
type_fre.iloc[:, :] = -1
i0 = 0
process_total = 0
for i1, (check_fre, atol_fre) in enumerate(zip(check_fre_list[:], atol_fre_list)):
use_idx = np.isclose(tfre, check_fre, rtol=0, atol=atol_fre)
iidx = np.where(use_idx)
process_total = process_total + np.sum(use_idx)
t1 = sub_seperate[i1](i1, type_fre, iidx, Table_t_range)
type_fre.iloc[use_idx] = i0 + t1
i0 = i0 + 1 + np.max(t1)
# plot one of the remaind cases
if np.any(type_fre.values == -1):
pass
assert np.any(type_fre.values > 0)
type_fre2 = type_fre.copy()
type_fre2.iloc[type_fre.values == 0] = 0
type_fre2.iloc[type_fre.values == 1] = 1
type_fre2.iloc[type_fre.values == 2] = 2
type_fre2.iloc[type_fre.values == 3] = 3
type_fre2.iloc[type_fre.values == 4] = 1
type_fre2.iloc[type_fre.values == 5] = 1
type_fre2.iloc[type_fre.values == 6] = 4
type_fre2.iloc[type_fre.values == 7] = 1
spf_tb.show_traj_phase_map_type(type_fre2)
# spf_tb.save_separate_angleList_fft(job_dir, tfre, check_fre_list, atol_fre_list)
In [7]:
t1 = []
for i0 in np.arange(type_fre2.values.max() + 1):
t1.append(np.isclose(type_fre2.values, i0).sum())
print(np.sum(t1))
print(t1)
print(t1 / np.sum(t1))
In [10]:
nshow = np.inf
# nshow = 2
fast_mode1 = 1
figsize = np.array((16, 9)) * 0.5
dpi = 200
tipical_th_ph_list = []
for i0 in np.arange(type_fre2.values.max() + 1)[:]:
iidx = np.where(np.isclose(type_fre2.values, i0))
spf_tb.phase_map_show_idx(type_fre2, tipical_th_ph_list, iidx, job_dir, table_name, fast_mode=fast_mode1, )
In [18]:
Table_t_range1 = np.array((4500, np.inf))
Table_t_range2 = np.array((4500, np.inf))
nshow = np.inf
# nshow = 2
fast_mode2 = True
figsize = np.array((16, 9)) * 0.5
dpi = 200
for i0 in np.arange(type_fre2.values.max() + 1)[:]:
iidx = np.where(np.isclose(type_fre2.values, i0))
spf_tb.phase_map_show_idx_list(type_fre2, iidx, job_dir, nshow=nshow, figsize=figsize, dpi=dpi,
Table_t_range1=Table_t_range1, Table_t_range2=Table_t_range2, fast_mode=fast_mode2, )
In [147]:
# import importlib
# importlib.reload(spf_tb)
tipical_th_ph_list = [[0. , 1.714],
[0.000, 0. ],
[0.143, 0. ],
[0.143, 0.428],
[0.143, 0.571]]
tipical_th_ph_use_idx = [0, 2, 3, 4, ]
Table_t_range = np.array((4500, np.inf))
figsize = np.array((16, 9)) * 0.7
dpi = 200
tipical_th_ph_list_use = np.array(tipical_th_ph_list)[tipical_th_ph_use_idx]
label_list = np.arange(len(tipical_th_ph_list))[tipical_th_ph_use_idx]
spf_tb.show_table_theta_phi_psi_fft_list(tipical_th_ph_list_use, job_dir, label_list=label_list, figsize=figsize, dpi=dpi,
use_welch=True)
spf_tb.show_table_result_list(tipical_th_ph_list_use, job_dir, label_list=label_list, Table_t_range=Table_t_range,
figsize=figsize, dpi=dpi)
Out[147]:
In [92]:
psi_lim_fct = 20
resampling_fct = 10
figsize = np.array((16, 9)) * 0.7
dpi = 200
# case_path_list = separate_fre_path_v2(type_fre2)
# color_list = [np.array(plt.get_cmap('tab20', len(check_fre_list))(i0)[:3]).reshape((1, 3))
# for i0 in np.arange(type_fre2.values.max() + 1)]
for idx, psi_lim1 in enumerate(np.linspace(0, 2 * np.pi, psi_lim_fct * 16, endpoint=False)[::psi_lim_fct]):
psi_lim = (psi_lim1, psi_lim1 + 2 * np.pi / (psi_lim_fct * 16))
# create fig
desc = '$\\psi \\in [%.3f \\pi, %.3f \\pi)$' % ((psi_lim[0] / np.pi), (psi_lim[1] / np.pi))
fig, ax1 = plt.subplots(1, 1, figsize=figsize, dpi=dpi, subplot_kw=dict(polar=True))
n_xticks = 32
xticks = np.arange(n_xticks)
ax1.set_xticks(xticks / n_xticks * 2 * np.pi)
ax1.set_xticklabels(['$\dfrac{%d}{%d}2\pi$' % (i0, n_xticks) for i0 in xticks])
ax1.set_yticklabels([])
ax1.set_ylim(0, np.pi)
fig.suptitle(desc)
fig.tight_layout(rect=[0, 0, 1, 0.95])
for i0 in np.arange(type_fre2.values.max() + 1):
# get case_path
iidx = np.where(np.isclose(type_fre2.values, i0))
theta = type_fre2.index.values[iidx[0]]
phi = type_fre2.columns.values[iidx[1]]
theta_phi_list = np.vstack((theta, phi)).T
case_path = []
for theta, phi in theta_phi_list[:]:
t_headle = 'th%5.3f_ph%5.3f_(.*?).pickle' % (theta, phi)
filename = [filename for filename in os.listdir(job_dir)
if re.match(t_headle, filename) is not None][0]
case_path.append(os.path.join(PWD, job_dir, filename))
# generate fig
thandle = ''
color = np.array(plt.get_cmap('tab20', int(type_fre2.values.max() + 1))(int(i0))[:3]).reshape((1, 3))
spf_tb.draw_phase_map_theta(case_path, color, psi_lim, axs=[ax1, ], thandle=thandle,
resampling=True, resampling_fct=resampling_fct)
tdir = os.path.join(PWD, job_dir, 'phase_mape_fre')
if not os.path.exists(tdir):
os.makedirs(tdir)
figname = os.path.join(tdir, '%04d.png' % (idx))
fig.savefig(os.path.join(tdir, figname))
print('save to %s' % figname)
plt.close(fig)
In [87]:
def _get_last_data_angle(job_dir, theta, phi, ntimes=10, resampling_fct=1):
base_pick, _ = spf_tb.load_table_date_pickle(job_dir, theta, phi)
base_t = base_pick['Table_t']
base_theta = base_pick['Table_theta']
tmin = np.max((0, base_t.max() - 1000))
idx = base_t > tmin
freq_pk = spf_tb.get_major_fre(base_t[idx], base_theta[idx])
idx = base_t > (base_t.max() - 1 / freq_pk * ntimes)
base_t = base_pick['Table_t'][idx]
base_theta = base_pick['Table_theta'][idx]
base_phi = base_pick['Table_phi'][idx]
base_psi = base_pick['Table_psi'][idx]
t1 = int(np.around(1 / freq_pk * ntimes * 16 * resampling_fct))
t_use = np.linspace(base_t.min(), base_t.max(), t1)
base_theta = spf_tb.get_continue_angle(base_t, base_theta, t_use)
base_phi = spf_tb.get_continue_angle(base_t, base_phi, t_use)
base_psi = spf_tb.get_continue_angle(base_t, base_psi, t_use)
return base_t, base_theta, base_phi, base_psi
i0 = 0
iidx = np.where(np.isclose(type_fre2.values, i0))
ttheta = type_fre2.index.values[iidx[0]][114]
tphi = type_fre2.columns.values[iidx[1]][114]
base_t, base_theta, base_phi, base_psi = _get_last_data_angle(job_dir, ttheta, tphi, ntimes=3)
In [91]:
def _get_last_data_angle(job_dir, theta, phi, ntimes=10, resampling_fct=1):
base_pick, _ = spf_tb.load_table_date_pickle(job_dir, theta, phi)
base_t = base_pick['Table_t']
base_theta = base_pick['Table_theta']
tmin = np.max((0, base_t.max() - 1000))
idx = base_t > tmin
freq_pk = spf_tb.get_major_fre(base_t[idx], base_theta[idx])
idx = base_t > (base_t.max() - 1 / freq_pk * ntimes)
base_t = base_pick['Table_t'][idx]
base_theta = base_pick['Table_theta'][idx]
base_phi = base_pick['Table_phi'][idx]
base_psi = base_pick['Table_psi'][idx]
t1 = int(np.around(1 / freq_pk * ntimes * 16 * resampling_fct))
t_use = np.linspace(base_t.min(), base_t.max(), t1)
base_theta = spf_tb.get_continue_angle(base_t, base_theta, t_use)
base_phi = spf_tb.get_continue_angle(base_t, base_phi, t_use)
base_psi = spf_tb.get_continue_angle(base_t, base_psi, t_use)
return base_t, base_theta, base_phi, base_psi
for i0 in np.arange(type_fre2.values.max() + 1)[:]:
iidx = np.where(np.isclose(type_fre2.values, i0))
ttheta = type_fre2.index.values[iidx[0]][0]
tphi = type_fre2.columns.values[iidx[1]][0]
base_t, base_theta, base_phi, base_psi = _get_last_data_angle(job_dir, ttheta, tphi, ntimes=3)
base_theta2 = np.flipud(base_theta)
for i1 in tqdm_notebook(np.arange(type_fre2.values.max() + 1)[:]):
iidx = np.where(np.isclose(type_fre2.values, i1))
theta = type_fre2.index.values[iidx[0]]
phi = type_fre2.columns.values[iidx[1]]
theta_phi_list = np.vstack((theta, phi)).T
max_conv = []
for theta, phi in theta_phi_list[:]:
Table_t, Table_theta, Table_phi, Table_psi = _get_last_data_angle(job_dir, theta, phi, ntimes=5)
t_conv = np.convolve(base_theta2, Table_theta, mode='valid')
max_conv.append(t_conv.max())
# fig, axs = plt.subplots(1, 2)
# ax1 = axs[0]
# ax1.plot(t_conv)
# ax2 = axs[1]
# ax2.plot(Table_theta)
# ax2.plot(base_theta)
max_conv = np.hstack(max_conv)
print(max_conv.max(), max_conv.min(), max_conv.mean(), max_conv.std())
In [89]:
plt.plot(max_conv, '.')
Out[89]:
In [73]:
np.convolve([1, 2, 3], [0, 1, 0.5])
Out[73]:
In [74]:
np.dot([1, 2, 3], [0, 1, 0.5])
Out[74]:
In [ ]: