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)

from tqdm import tqdm_notebook
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
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 import tqdm, tqdm_notebook
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

rc('animation', html='html5')
fontsize = 40
PWD = os.getcwd()


/home/zhangji/stokes_flow_master/codeStore/support_fun_table.py:12: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  matplotlib.use('agg')

In [13]:
importlib.reload(spf_tb)

job_dir = 'ecoC01B05_passive_psi-0d'
table_name = 'ecoC01B05_tau1c_passive'
theta, phi = 0.137, 5.882  # red case
theta, phi = 1.503, 4.679  # green case
theta, phi = 1.639, 3.743  # green case
# theta, phi = 0.137, 3.609  # cyan case

t_headle = 'th%5.3f_ph%5.3f_(.*?).pickle' % (theta, phi)
t_path = os.listdir(os.path.join(PWD, job_dir))
filename = [filename for filename in os.listdir(os.path.join(PWD, job_dir)) 
            if re.match(t_headle, filename) is not None][0]
with open(os.path.join(PWD, job_dir, filename), 'rb') as handle:
    tpick = pickle.load(handle)
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])
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)


/home/zhangji/stokes_flow_master/codeStore/support_fun_table.py:12: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  matplotlib.use('agg')
-ini_theta 1.639092 -ini_phi 3.743174 -ini_psi 0.000000
Out[13]:
True

In [2]:
# show phase map of theta-phi, part 1
importlib.reload(spf_tb)
job_dir = 'ecoC01B05_passive_psi-0d'

t_headle = '(.*?).pickle'
t_path = os.listdir(os.path.join(PWD, job_dir))
filename_list = [filename for filename in os.listdir(os.path.join(PWD, job_dir)) 
                 if re.match(t_headle, filename) is not None]
ini_theta_list = []
ini_phi_list = []
lst_eta_list = []
theta_max_fre_list = []
phi_max_fre_list = []
psi_max_fre_list = []
eta_max_fre_list = []
pickle_path_list = []
idx_list = []
for i0, tname in enumerate(tqdm_notebook(filename_list[:])):
    tpath = os.path.join(PWD, job_dir, tname)
    with open(tpath, 'rb') as handle:
        tpick = pickle.load(handle)
    ini_theta_list.append(tpick['ini_theta'])
    ini_phi_list.append(tpick['ini_phi'])
    lst_eta_list.append(tpick['Table_eta'][-1])
    pickle_path_list.append(tpath)
    idx_list.append(i0)
    
    # fft rule
    tx = tpick['Table_t']
    tmin = np.max((0, tx.max() - 1000))
    idx = tx > tmin
#     pick_fre = np.min((spf_tb.get_major_fre(tpick['Table_t'], tpick['Table_theta']), 
#                        spf_tb.get_major_fre(tpick['Table_t'], tpick['Table_phi']), 
#                        spf_tb.get_major_fre(tpick['Table_t'], tpick['Table_psi']), 
#                        spf_tb.get_major_fre(tpick['Table_t'], tpick['Table_eta']), ))
#     tmin = tx.max() - 1 / pick_fre * 10
#     idx = np.logical_and(np.hstack((True, np.diff(tx)>0)), tx > tmin)
    theta_max_fre_list.append(spf_tb.get_major_fre(tx[idx], tpick['Table_theta'][idx]))
    phi_max_fre_list.append(spf_tb.get_major_fre(tx[idx], tpick['Table_phi'][idx]))
    psi_max_fre_list.append(spf_tb.get_major_fre(tx[idx], tpick['Table_psi'][idx]))
    eta_max_fre_list.append(spf_tb.get_major_fre(tx[idx], tpick['Table_eta'][idx]))

data0 = pd.DataFrame({'ini_theta': ini_theta_list, 
                 'ini_phi': ini_phi_list, 
                 'lst_eta': lst_eta_list, 
                 'theta_max_fre': theta_max_fre_list, 
                 'phi_max_fre': phi_max_fre_list, 
                 'psi_max_fre': psi_max_fre_list, 
                 'eta_max_fre': eta_max_fre_list, 
                 'data_idx': idx_list })
data = data0.pivot_table(index=['ini_theta'], columns=['ini_phi'])
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)


/home/zhangji/stokes_flow_master/codeStore/support_fun_table.py:12: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  matplotlib.use('agg')


In [ ]:
# sort all frequrents
with np.printoptions(precision=10, suppress=True, threshold=1e10):
    print(np.flipud(np.sort(data.theta_max_fre.values.flatten())))

In [7]:
# show phase map of theta-phi, part 2
def show_phase_map(tuse):
    fig = plt.figure(figsize=(20, 12))
    fig.patch.set_facecolor('white')
    ax0 = fig.add_subplot(111, polar=True)
    n_xticks = 32
    xticks = np.arange(n_xticks)
    ax0.set_xticks(xticks / n_xticks * 2 * np.pi)
    ax0.set_xticklabels(['$\dfrac{%d}{%d}2\pi$' % (i0, n_xticks) for i0 in xticks])
    ax0.set_yticklabels([])
    ax0.set_ylim(0, np.pi)
    im = ax0.pcolor(tuse.columns.values, tuse.index.values, tuse.values, 
                    cmap=plt.get_cmap('Set2'))
    fig.colorbar(im, ax=ax0, orientation='vertical').ax.tick_params(labelsize=fontsize)

show_phase_map(theta_max_fre)
show_phase_map(phi_max_fre)
show_phase_map(psi_max_fre)
show_phase_map(eta_max_fre)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-0b0bec5ade99> in <module>()
     14     fig.colorbar(im, ax=ax0, orientation='vertical').ax.tick_params(labelsize=fontsize)
     15 
---> 16 show_phase_map(theta_max_fre)
     17 show_phase_map(phi_max_fre)
     18 show_phase_map(psi_max_fre)

NameError: name 'theta_max_fre' is not defined

In [28]:
# put images with same frequence into a subdirect
importlib.reload(spf_tb)
job_dir = 'ecoC01B05_passive_psi-0c'
tfre = data.theta_max_fre
check_fre = 0.021
atol_fre  = 0.0009
print('frequence in the range (%f, %f)' % (check_fre - atol_fre, check_fre + atol_fre))

full_dir = os.path.join(PWD, job_dir)
spf_tb.save_separate_angle_fft(full_dir, tfre, check_fre, atol_fre)


frequence in the range (0.020100, 0.021900)
make folder /home/zhangji/stokes_flow_master/head_Force/do_calculate_table/ecoC01B05_passive_psi-0c/fre_separate/fre_0.021000
frequence in the range (0.020100, 0.021900)
/home/zhangji/stokes_flow_master/codeStore/support_fun_table.py:12: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  matplotlib.use('agg')
th0.000_ph0.000_ps0.000_D20190703_T050316.pickle, Fth=0.020349
th0.000_ph0.262_ps0.000_D20190703_T050317.pickle, Fth=0.020350
th0.714_ph0.262_ps0.000_D20190703_T050315.pickle, Fth=0.020700
th1.000_ph0.262_ps0.000_D20190703_T050317.pickle, Fth=0.020351
th0.000_ph0.524_ps0.000_D20190703_T050316.pickle, Fth=0.020349
th0.714_ph0.524_ps0.000_D20190703_T050316.pickle, Fth=0.020901
th0.000_ph0.785_ps0.000_D20190703_T050316.pickle, Fth=0.020349
th0.143_ph0.785_ps0.000_D20190703_T050316.pickle, Fth=0.020351
th1.285_ph0.785_ps0.000_D20190703_T050316.pickle, Fth=0.020899
th0.000_ph1.047_ps0.000_D20190703_T050317.pickle, Fth=0.020349
th0.428_ph1.047_ps0.000_D20190703_T050317.pickle, Fth=0.020898
th1.428_ph1.047_ps0.000_D20190703_T050317.pickle, Fth=0.020499
th0.000_ph1.309_ps0.000_D20190703_T050318.pickle, Fth=0.020350
th1.571_ph1.309_ps0.000_D20190703_T050318.pickle, Fth=0.020499
th0.000_ph1.571_ps0.000_D20190703_T050318.pickle, Fth=0.020349
th0.857_ph1.571_ps0.000_D20190703_T050319.pickle, Fth=0.020899
th0.286_ph1.833_ps0.000_D20190703_T050317.pickle, Fth=0.020899
th1.571_ph1.833_ps0.000_D20190703_T050317.pickle, Fth=0.020499
th1.285_ph2.356_ps0.000_D20190703_T050319.pickle, Fth=0.020900
th0.143_ph2.618_ps0.000_D20190703_T050318.pickle, Fth=0.020898
th2.142_ph3.927_ps0.000_D20190703_T050319.pickle, Fth=0.021450
th1.571_ph4.451_ps0.000_D20190703_T050320.pickle, Fth=0.020499
th2.142_ph4.451_ps0.000_D20190703_T050320.pickle, Fth=0.021449
th0.000_ph4.712_ps0.000_D20190703_T050320.pickle, Fth=0.020349
th1.714_ph4.712_ps0.000_D20190703_T050320.pickle, Fth=0.021000
th0.000_ph4.974_ps0.000_D20190703_T050319.pickle, Fth=0.020349
th0.286_ph4.974_ps0.000_D20190703_T050320.pickle, Fth=0.020899
th0.571_ph4.974_ps0.000_D20190703_T050319.pickle, Fth=0.020901
th1.571_ph4.974_ps0.000_D20190703_T050319.pickle, Fth=0.020499
th0.000_ph5.236_ps0.000_D20190703_T052633.pickle, Fth=0.020349
th1.000_ph5.236_ps0.000_D20190703_T052633.pickle, Fth=0.020899
th1.428_ph5.236_ps0.000_D20190703_T052633.pickle, Fth=0.020501
th0.000_ph5.498_ps0.000_D20190703_T053155.pickle, Fth=0.020349
th0.000_ph5.760_ps0.000_D20190703_T053344.pickle, Fth=0.020349
th1.142_ph5.760_ps0.000_D20190703_T053345.pickle, Fth=0.020899
th2.713_ph5.760_ps0.000_D20190703_T053345.pickle, Fth=0.020999
th0.000_ph6.021_ps0.000_D20190703_T053350.pickle, Fth=0.020350
th1.999_ph6.021_ps0.000_D20190703_T053349.pickle, Fth=0.020348
th2.285_ph6.021_ps0.000_D20190703_T053349.pickle, Fth=0.020700

Out[28]:
True

In [ ]:
# create phase map
importlib.reload(spf_tb)
def tget_ax0():
    n_xticks = 32
    xticks = np.arange(n_xticks)
    fig = plt.figure(figsize=(20, 20))
    fig.patch.set_facecolor('white')
    axs = []
    axs.append(fig.add_subplot(221, polar=True))
    axs.append(fig.add_subplot(222, polar=True))
    axs.append(fig.add_subplot(223, polar=True))
    axs.append(fig.add_subplot(224, polar=True))
    for ax0 in axs:
        ax0.set_xticks(xticks / n_xticks * 2 * np.pi)
        ax0.set_xticklabels(['$\dfrac{%d}{%d}2\pi$' % (i0, n_xticks) for i0 in xticks])
        ax0.set_yticklabels([])
        ax0.set_ylim(0, np.pi)
    plt.tight_layout()
    return fig, axs

check_fre_list = [0.064, 0.020,  0.019,  0.016 ]
atol_list =      [0.004, 0.0005, 0.0005, 0.0015]
color_list =     ['b',   'g',    'r',    'c', 'm', 'y', 'k']
psi_lim_fct = 20
resampling_fct = 10

data0['use_max_fre'] = data0.theta_max_fre
case_path_list = spf_tb.separate_fre_path(check_fre_list, atol_list, data0, pickle_path_list)
for idx, psi_lim1 in enumerate(np.linspace(0, 2 * np.pi, psi_lim_fct * 16, 
                                           endpoint=False)[::psi_lim_fct]):
    fig, (ax0, ax1, ax2, ax3) = tget_ax0()
    ax_list = [ax0, ax0, ax1, ax2, ax3]
    psi_lim = (psi_lim1, psi_lim1 + 2 * np.pi / (psi_lim_fct * 16))
    desc = '$\psi\in[%.3f\pi, %.3f\pi)$' % ((psi_lim[0] / np.pi), (psi_lim[1] / np.pi))
    fig.suptitle(desc, fontsize=fontsize*0.8)
    for check_fre, case_path, color, axi in zip(check_fre_list, case_path_list, color_list, ax_list):
        thandle = '%f' % check_fre
        spf_tb.draw_phase_map_theta(case_path, color, psi_lim, axs=(axi, ax_list[-1]), 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)


/home/zhangji/stokes_flow_master/codeStore/support_fun_table.py:12: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  matplotlib.use('agg')
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-106-511764db0087> in <module>()
     36         thandle = '%f' % check_fre
     37         spf_tb.draw_phase_map_theta(case_path, color, psi_lim, axs=(axi, ax_list[-1]), thandle=thandle, 
---> 38                                     resampling=True, resampling_fct=resampling_fct)
     39     tdir = os.path.join(PWD, job_dir, 'phase_mape_fre')
     40     if not os.path.exists(tdir):

~/stokes_flow_master/codeStore/support_fun_table.py in draw_phase_map_theta(case_path, color, psi_lim, axs, resampling, resampling_fct, thandle)
   1233             Table_theta, Table_phi, Table_psi, Table_eta = \
   1234                 resampling_data(Table_t, Table_dt, Table_X, Table_P, Table_P2,
-> 1235                                 Table_theta, Table_phi, Table_psi, Table_eta, resampling_fct)
   1236         tidx = np.logical_and(Table_psi >= psi_lim[0], Table_psi < psi_lim[1])
   1237         for ax0 in tube_flatten((axs,)):

~/stokes_flow_master/codeStore/support_fun_table.py in resampling_data(Table_t, Table_dt, Table_X, Table_P, Table_P2, Table_theta, Table_phi, Table_psi, Table_eta, resampling_fct)
    795     Table_dt = intp_fun(Table_dt)
    796     Table_theta = get_continue_angle(Table_t, Table_theta, t_use)
--> 797     Table_phi = get_continue_angle(Table_t, Table_phi, t_use)
    798     Table_psi = get_continue_angle(Table_t, Table_psi, t_use)
    799     Table_eta = np.arccos(np.sin(Table_theta) * np.sin(Table_phi))

~/stokes_flow_master/codeStore/support_fun_table.py in get_continue_angle(tx, ty1, t_use)
    765             ty[i0 + 1:] = ty[i0 + 1:] + 2 * np.pi
    766     intp_fun1d = interpolate.interp1d(tx, ty, kind='quadratic', copy=False, axis=0,
--> 767                                       bounds_error=True)
    768     return intp_fun1d(t_use) % (2 * np.pi)
    769 

~/anaconda3/lib/python3.5/site-packages/scipy/interpolate/interpolate.py in __init__(***failed resolving arguments***)
    533 
    534             self._spline = make_interp_spline(xx, yy, k=order,
--> 535                                               check_finite=False)
    536             if rewrite_nan:
    537                 self._call = self.__class__._call_nan_spline

~/anaconda3/lib/python3.5/site-packages/scipy/interpolate/_bsplines.py in make_interp_spline(x, y, k, t, bc_type, axis, check_finite)
    851     gbsv, = get_lapack_funcs(('gbsv',), (ab, rhs))
    852     lu, piv, c, info = gbsv(kl, ku, ab, rhs,
--> 853             overwrite_ab=True, overwrite_b=True)
    854 
    855     if info > 0:

KeyboardInterrupt: 

In [ ]: