Skip to content

Commit

Permalink
plot script updates to new import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Oct 27, 2020
1 parent 5b3d87b commit 2ea0f09
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 562 deletions.
49 changes: 12 additions & 37 deletions deformationcytometer/evaluation/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,44 +259,16 @@ def velfit(r, p0, p1): # for stress versus strain
print('v_max = %5.2f mm/s profile stretch exponent = %5.2f\n' % (vel_fit[0], vel_fit[1]))


def plotDensityScatter(x, y, cmap='viridis', alpha=1, skip=1):
def plotDensityScatter(x, y, cmap='viridis', alpha=1, skip=1, y_factor=1):
x = np.array(x)[::skip]
y = np.array(y)[::skip]
xy = np.vstack([x, y])
xy = np.vstack([x, y*y_factor])
kd = gaussian_kde(xy)(xy)
idx = kd.argsort()
x, y, z = x[idx], y[idx], kd[idx]
plt.scatter(x, y, c=z, s=5, alpha=alpha, cmap=cmap) # plot in kernel density colors e.g. viridis

#TODO: Richie - this function was defined twice...
'''

def plotStressStrainFit(data, config):
fit = config["fit"]
fitfunc = fit["fitfunc"]
p = fit["p"]
err = fit["err"]
cov_ap = fit["cov_ap"]
cov_ao = fit["cov_ao"]
cov_po = fit["cov_po"]
# ----------plot the fit curve----------
xx = np.arange(np.min(data.stress), np.max(data.stress), 0.1) # generates an extended array
plt.plot(xx, (fitfunc(xx, p[0], p[1])), '-', color='black', linewidth=2, zorder=3)
# ----------plot standard error of the fit function----------
dyda = -1 / (p[0] ** 2) * np.log(xx / p[1] + 1) # strain derivative with respect to alpha
dydp = -1 / p[0] * xx / (xx * p[1] + p[1] ** 2) # strain derivative with respect to prestress
dydo = 1 # strain derivative with respect to offset
if 0: # TODO
vary = (dyda * err[0]) ** 2 + (dydp * err[1]) ** 2 + (
dydo * err[2]) ** 2 + 2 * dyda * dydp * cov_ap + 2 * dyda * dydo * cov_ao + 2 * dydp * dydo * cov_po
y1 = fitfunc(xx, p[0], p[1]) - np.sqrt(vary)
y2 = fitfunc(xx, p[0], p[1]) + np.sqrt(vary)
plt.fill_between(xx, y1, y2, facecolor='gray', edgecolor="none", linewidth=0, alpha=0.5)
'''
def plotStressStrainFit(data, config, color="C1"):

def omega(x, p=52.43707149):
Expand Down Expand Up @@ -332,30 +304,33 @@ def fitfunc(x, p0, p1, p2): # for stress versus strain
plt.plot(x2, y2, "-", color=color, lw=4)


def bootstrap_median_error(data):
def bootstrap_error(data, func=np.median):
data = np.asarray(data)
if len(data) <= 1:
return 0
medians = []
for i in range(1000):
medians.append(np.median(data[np.random.random_integers(len(data) - 1, size=len(data))]))
medians.append(func(data[np.random.random_integers(len(data) - 1, size=len(data))]))
return np.nanstd(medians)


def plotBinnedData(x, y, bins, color="black", mew=1):
def plotBinnedData(x, y, bins, bin_func=np.median, error_func=None, color="black", mew=1):
strain_av = []
stress_av = []
strain_err = []
for i in range(len(bins) - 1):
index = (x > bins[i]) & (x < bins[i + 1])
index = (bins[i] < x) & (x < bins[i + 1])
yy = y[index]
strain_av.append(np.median(yy))
strain_av.append(bin_func(yy))
# yy = yy[yy>0]
# strain_err.append(np.std(np.log(yy)) / np.sqrt(len(yy)))
strain_err.append(bootstrap_median_error(yy)) # np.quantile(yy, [0.25, 0.75]))
if error_func is None:
strain_err.append(bootstrap_error(yy, bin_func)) # np.quantile(yy, [0.25, 0.75]))
elif error_func == "quantiles":
strain_err.append(np.abs(np.quantile(yy, [0.25, 0.75])-bin_func(yy))) # np.quantile(yy, [0.25, 0.75]))

stress_av.append(np.median(x[index]))
plt.errorbar(stress_av, strain_av, yerr=strain_err, marker='s', mfc='white', \
plt.errorbar(stress_av, strain_av, yerr=np.array(strain_err).T, marker='s', mfc='white', \
mec=color, ms=7, mew=mew, lw=0, ecolor='black', elinewidth=1, capsize=3)


Expand Down
198 changes: 2 additions & 196 deletions figures/figure_angle_and_size.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
# -*- coding: utf-8 -*-
"""
Created on Tue May 22 2020
@author: Ben
# This program reads a txt file with the analyzed cell position, shape (semi-major and semi-minor axis etc.),
# computes the cell strain and the fluid shear stress acting on each cell,
# plots the data (strain versus stress) for each cell using a kernel density estimate for the datapoint color,
# and fits a stress stiffening equation to the data
# The results such as maximum flow speed, cell mechanical parameters, etc. are stored in
# the file 'all_data.txt' located at the same directory as this script
"""
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from scripts.helper_functions import getInputFile, getConfig, getData, getInputFolder
from scripts.helper_functions import refetchTimestamps, getVelocity, filterCells, correctCenter, getStressStrain, fitStiffness
from scripts.helper_functions import initPlotSettings, plotVelocityProfile, plotStressStrain, plotMessurementStatus
from scripts.helper_functions import storeEvaluationResults, plotDensityScatter, plotStressStrainFit, plotBinnedData, load_all_data
from deformationcytometer.evaluation.helper_functions import plotDensityScatter, load_all_data
import numpy as np
import pandas as pd
import glob
from pathlib import Path

import pylustrator
pylustrator.start()

for index, pressure in enumerate([1]):
ax = plt.subplot(1, 3, index+1)

#data, config = load_all_data(r"\\131.188.117.96\biophysDS\emirzahossein\microfluidic cell rhemeter data"+
# r"\microscope4\2020_may\2020_05_22_alginateDMEM2%\[0-9]\*_result.txt", pressure=pressure)
data, config = load_all_data([
r"\\131.188.117.96\biophysDS\emirzahossein\microfluidic cell rhemeter data\microscope4\2020_july\2020_07_21_alginate2%_dmem_NIH_time_2\[0-9]\*_result.txt",
r"\\131.188.117.96\biophysDS\emirzahossein\microfluidic cell rhemeter data\microscope4\2020_may\2020_05_22_alginateDMEM2%\[0-9]\*_result.txt",
Expand All @@ -41,180 +21,6 @@
plt.subplot(1, 2, 2)
plotDensityScatter(data.rp, np.sqrt(data.area / np.pi))

if 0:
ax = None

xlimits = [-40, 300]
ylimits = [-0.1, 1.1]

def plotPathList(paths):
global ax
paths = list(paths)
print(paths)
fit_data = []

data_list = []
for index, file in enumerate(paths):
output_file = Path(str(file).replace("_result.txt", "_evaluated.csv"))

# load the data and the config
data = getData(file)
config = getConfig(file)

""" evaluating data"""
if not output_file.exists():
#refetchTimestamps(data, config)

getVelocity(data, config)

# take the mean of all values of each cell
data = data.groupby(['cell_id']).mean()

correctCenter(data, config)

data = filterCells(data, config)

# reset the indices
data.reset_index(drop=True, inplace=True)

getStressStrain(data, config)

#data = data[(data.stress < 50)]
data.reset_index(drop=True, inplace=True)

data["area"] = data.long_axis * data.short_axis * np.pi
data.to_csv(output_file, index=False)

data = pd.read_csv(output_file)

#data = data[(data.area > 0) * (data.area < 2000) * (data.stress < 250)]
#data.reset_index(drop=True, inplace=True)

data_list.append(data)


data = pd.concat(data_list)
data.reset_index(drop=True, inplace=True)

fitStiffness(data, config)

plotDensityScatter(data.rp, data.angle)
#plotStressStrainFit(data, config)
#plotBinnedData(data.stress, data.strain, [0, 10, 20, 30, 40, 50, 75, 100, 125, 150, 200, 250])
#plt.title(f'{config["fit"]["p"][0] * config["fit"]["p"][1]:.2f}')
fit_data.append(config["fit"]["p"][0] * config["fit"]["p"][1])

return fit_data

def plotPathList2(paths):
global ax
paths = list(paths)
print(paths)
fit_data = []

data_list = []
for index, file in enumerate(paths):
output_file = Path(str(file).replace("_result.txt", "_evaluated.csv"))

# load the data and the config
data = getData(file)
config = getConfig(file)

""" evaluating data"""
if not output_file.exists():
#refetchTimestamps(data, config)

getVelocity(data, config)

# take the mean of all values of each cell
data = data.groupby(['cell_id']).mean()

correctCenter(data, config)

data = filterCells(data, config)

# reset the indices
data.reset_index(drop=True, inplace=True)

getStressStrain(data, config)

#data = data[(data.stress < 50)]
data.reset_index(drop=True, inplace=True)

data["area"] = data.long_axis * data.short_axis * np.pi
data.to_csv(output_file, index=False)

data = pd.read_csv(output_file)

#data = data[(data.area > 0) * (data.area < 2000) * (data.stress < 250)]
#data.reset_index(drop=True, inplace=True)

data_list.append(data)


data = pd.concat(data_list)
data.reset_index(drop=True, inplace=True)

fitStiffness(data, config)

plotDensityScatter(data.rp, np.sqrt(data.area/np.pi))
#plotStressStrainFit(data, config)
#plotBinnedData(data.stress, data.strain, [0, 10, 20, 30, 40, 50, 75, 100, 125, 150, 200, 250])
#plt.title(f'{config["fit"]["p"][0] * config["fit"]["p"][1]:.2f}')
fit_data.append(config["fit"]["p"][0] * config["fit"]["p"][1])

return fit_data

#""" loading data """
## get the results file (by config parameter or user input dialog)
datasets = [
{
"datafiles": [
Path(r"\\131.188.117.96\biophysDS\emirzahossein\data\microscope4_baslercamera\2020_may\2020_05_22_alginateDMEM2%"),
# Path(r"Z:\emirzahossein\data\microscope4_baslercamera\2020_july\07_07_2020_alginate2%_rpmi_no_fcs_time"),
],
"rows": 1,
"cols": 12,
"range": range(2, 11, 2),
"fitfunc": "line",
"repetition": 1,
}
]

rows = 1
cols = 3
#row_index = 0
data_index = -1
dataset = datasets[0]
datafiles = dataset["datafiles"]
for data_index, datafile in enumerate(datafiles):
data_index += 1
paths = []
pressures = []
ax = None
datafiles = dataset["datafiles"]
#
for index, file in enumerate(Path(datafile).glob("**/*_result.txt")):
config = getConfig(file)
paths.append(file)
pressures.append(config['pressure_pa'] / 100_000)

paths = np.array(paths)
pressures = np.array(pressures)

unique_pressures = np.unique(pressures)
unique_pressures = [1, 2, 3]#unique_pressures[unique_pressures > 0.5]
print(unique_pressures)

fit_data = []
index = 1

plt.subplot(1, 2, 1)
f = plotPathList(paths[pressures == 1])

plt.subplot(1, 2, 2)
f = plotPathList2(paths[pressures == 1])


#plt.legend()
#% start: automatic generated code from pylustrator
Expand Down
Binary file modified figures/figure_margination_histogram.pdf
Binary file not shown.
Binary file modified figures/figure_margination_histogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion figures/figure_margination_histogram.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from scripts.helper_functions import load_all_data
from deformationcytometer.evaluation.helper_functions import load_all_data
import numpy as np
from scipy import stats

Expand Down
62 changes: 1 addition & 61 deletions figures/figure_margination_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
# the file 'all_data.txt' located at the same directory as this script
"""
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from scripts.helper_functions import getInputFile, getConfig, getData, getInputFolder
from scripts.helper_functions import refetchTimestamps, getVelocity, filterCells, correctCenter, getStressStrain, fitStiffness
from scripts.helper_functions import initPlotSettings, plotVelocityProfile, plotStressStrain, plotMessurementStatus
from scripts.helper_functions import storeEvaluationResults, plotDensityScatter, plotStressStrainFit, plotBinnedData
import numpy as np
import pandas as pd
import glob
from pathlib import Path

import pylustrator
pylustrator.start()

Expand All @@ -29,59 +22,6 @@
pylustrator.load("strain_vs_stress_clean_3different_pressures.py", offset=[0, 1])
#pylustrator.load("figure_angle_and_size.py", offset=[0, 1])

if 0:
# % start: automatic generated code from pylustrator
plt.figure(1).ax_dict = {ax.get_label(): ax for ax in plt.figure(1).axes}
import matplotlib as mpl
plt.figure(1).set_size_inches(16.250000/2.54, 6.420000/2.54, forward=True)
plt.figure(1).axes[0].set_position([0.096051, 0.498363, 0.185083, 0.484703])
plt.figure(1).axes[0].set_xlim(-90.0, 90.0)
plt.figure(1).axes[0].set_xticklabels(["", "", ""])
plt.figure(1).axes[0].set_xticks([-75.0, 0.0, 75.0])
plt.figure(1).axes[0].texts[1].set_position([-0.374725, 0.955913])
plt.figure(1).axes[0].get_xaxis().get_label().set_text("")
plt.figure(1).axes[0].get_yaxis().get_label().set_text("strain")
plt.figure(1).axes[1].set_position([0.302915, 0.498363, 0.185083, 0.484703])
plt.figure(1).axes[1].set_xlim(-90.0, 90.0)
plt.figure(1).axes[1].set_xticklabels(["", "", ""])
plt.figure(1).axes[1].set_xticks([-75.0, 0.0, 75.0])
plt.figure(1).axes[1].texts[1].set_position([-0.186918, 0.950514])
plt.figure(1).axes[1].get_xaxis().get_label().set_text("")
plt.figure(1).axes[2].set_position([0.509781, 0.498363, 0.185083, 0.484703])
plt.figure(1).axes[2].set_xlim(-90.0, 90.0)
plt.figure(1).axes[2].set_xticklabels(["", "", ""])
plt.figure(1).axes[2].set_xticks([-75.0, 0.0, 75.0])
plt.figure(1).axes[2].texts[1].set_position([-0.161016, 0.950514])
plt.figure(1).axes[2].get_xaxis().get_label().set_text("")
plt.figure(1).axes[3].set_position([0.096051, 0.194818, 0.185083, 0.248456])
plt.figure(1).axes[3].set_ylim(0.0, 0.011)
plt.figure(1).axes[3].yaxis.labelpad = -5.854955
plt.figure(1).axes[3].texts[0].set_visible(False)
plt.figure(1).axes[3].texts[1].set_position([-0.374725, 1.077305])
plt.figure(1).axes[3].texts[1].set_text("e")
plt.figure(1).axes[4].set_position([0.302915, 0.194818, 0.185083, 0.248456])
plt.figure(1).axes[4].set_ylim(0.0, 0.011)
plt.figure(1).axes[4].texts[0].set_visible(False)
plt.figure(1).axes[4].texts[1].set_position([-0.186918, 0.959572])
plt.figure(1).axes[4].texts[1].set_text("f")
plt.figure(1).axes[5].set_position([0.509780, 0.192011, 0.185083, 0.248456])
plt.figure(1).axes[5].set_ylim(0.0, 0.011)
plt.figure(1).axes[5].texts[0].set_visible(False)
plt.figure(1).axes[5].texts[1].set_position([-0.161009, 0.970868])
plt.figure(1).axes[5].texts[1].set_text("g")
plt.figure(1).axes[6].set_position([0.791249, 0.498363, 0.185083, 0.481965])
plt.figure(1).axes[6].set_xlim(-90.0, 90.0)
plt.figure(1).axes[6].set_xticklabels(["", "", ""])
plt.figure(1).axes[6].set_xticks([-75.0, 0.0, 75.0])
plt.figure(1).axes[6].texts[0].set_text("d")
plt.figure(1).axes[6].get_xaxis().get_label().set_text("")
plt.figure(1).axes[7].set_position([0.791249, 0.192011, 0.185083, 0.248456])
plt.figure(1).axes[7].set_ylim(0.0, 30.0)
plt.figure(1).axes[7].set_yticklabels(["0", "10", "20", "30"])
plt.figure(1).axes[7].set_yticks([0.0, 10.0, 20.0, 30.0])
plt.figure(1).axes[7].texts[0].set_position([-0.232258, 1.192373])
plt.figure(1).axes[7].texts[0].set_text("h")
#% end: automatic generated code from pylustrator

#% start: automatic generated code from pylustrator
plt.figure(1).ax_dict = {ax.get_label(): ax for ax in plt.figure(1).axes}
Expand Down
3 changes: 1 addition & 2 deletions figures/figure_stress_vs_radialposition.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from scripts.helper_functions import plotDensityScatter
from scripts.helper_functions import load_all_data
from deformationcytometer.evaluation.helper_functions import plotDensityScatter, load_all_data
import numpy as np

import pylustrator
Expand Down
Loading

0 comments on commit 2ea0f09

Please sign in to comment.