Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
rcolyer committed May 16, 2019
0 parents commit aa9417c
Show file tree
Hide file tree
Showing 10 changed files with 658 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__pycache__/
*.py[cod]

plots/*
log/*

82 changes: 82 additions & 0 deletions CompressiveSim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/python3
"""
Ryan A. Colyer
Compressive Fluorescence Lifetime Imaging Microscopy simulation testbed.
This simulation is for working out analysis ideas under a controlled system,
prior to implementation with the experimental data.
This approach is based on a previous MATLAB simulation done as undergraduate
research by Sarah Grant.
"""

import library.RunTime as rt
import library.MakeImages as make_img
import library.Simulator as cssim
import library.CompressivePlots as cp
import numpy as np

run_timer = rt.RunTime()


randseed = None
freq = 2*((32/17)*100e6)/8
frames_per_s = 60
sim_cps = 20000
frame_count = 200

print('randseed', randseed)
print('freq', freq)
print('frames_per_s', frames_per_s)

np.random.seed(None)
img = make_img.MakeCellImage(freq)
total_brightness = np.sum(img.Intensity())

print('total_brightness', total_brightness)
print('Input', img.Intensity())


def Make_g_array(sim):
g_arr = []
labels = []
index = 'a'
for frame_count in range(800,3200+1,800):
g_row = []
l_row = []
for sim_cps in range(80000,320000+1,80000):
sim.SimAndOutput(sim_cps, frame_count)
g_row.append(sim.g_res)
l_row.append('('+index+') '+str(sim_cps)+' cps, '+str(frame_count)+' frames')
index = chr(ord(index)+1)
g_arr.append(g_row)
labels.append(l_row)

cp.PlotArray(g_arr, labels, 'plots/g_array.png', display=False, zscale=[0,1])

def Make_g_noise_data(sim):
frame_count = 2400
cps = []
sdarr = []
print('# cps, stdev')
for sim_cps in range(20000,320000+1,20000):
gvals = []
for i in range(10):
sim.SimulateImage(sim_cps, frame_count)
gvals.append(sim.g_res[8][8])
sd = np.std(gvals, ddof=1)
sdarr.append(sd)
cps.append(sim_cps)
print(str(sim_cps)+', '+str(sd))


sim = cssim.CSSimulator(img, total_brightness, freq, frames_per_s, run_timer)

#Make_g_array(sim)

#Make_g_noise_data(sim)

sim.SimAndOutput(sim_cps=320000, frame_count=3200)

169 changes: 169 additions & 0 deletions library/CompressivePlots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"""
Ryan A. Colyer
Generate comparison plots of Compressive Sensing results
"""

import numpy as np
import matplotlib.pyplot as plt

def Plot2(a, Lab_a, b, Lab_b, filename=None, display=True):
plt.figure(figsize=(8, 4))
plt.set_cmap('nipy_spectral')

plt.subplot(1,2,1)
plt.imshow(np.flipud(a))
plt.title(Lab_a)
plt.colorbar()
plt.axis('off')

plt.subplot(1,2,2)
plt.imshow(np.flipud(b))
plt.title(Lab_b)
plt.colorbar()
plt.axis('off')

plt.subplots_adjust(hspace=0.07, wspace=0.07, top=0.91, bottom=0.02, left=0.005, right=0.99)

if filename:
plt.savefig(filename, dpi=300)

if display:
plt.show()

plt.close()


def Plot3(a, Lab_a, b, Lab_b, c, Lab_c, filename=None, display=True):
plt.figure(figsize=(11, 4))
plt.set_cmap('nipy_spectral')

plt.subplot(1,3,1)
plt.imshow(np.flipud(a))
plt.title(Lab_a)
plt.colorbar()
plt.axis('off')

plt.subplot(1,3,2)
plt.imshow(np.flipud(b))
plt.title(Lab_b)
plt.colorbar()
plt.axis('off')

plt.subplot(1,3,3)
plt.imshow(np.flipud(c))
plt.title(Lab_c)
plt.colorbar()
plt.axis('off')

plt.subplots_adjust(hspace=0.07, wspace=0.07, top=0.95, bottom=0.02, left=0.005, right=0.99)

if filename:
plt.savefig(filename, dpi=300)

if display:
plt.show()

plt.close()


def Plot4(a, Lab_a, b, Lab_b, c, Lab_c, d, Lab_d, filename=None, display=True, zscale=[None, None, None, None]):
plt.figure(figsize=(8, 7))
plt.set_cmap('nipy_spectral')

plt.subplot(2,2,1)
if zscale[0]:
plt.pcolor(a, vmin=zscale[0][0], vmax=zscale[0][1])
else:
plt.pcolor(a)
plt.title(Lab_a)
plt.colorbar()
plt.axis('off')

plt.subplot(2,2,2)
if zscale[1]:
plt.pcolor(b, vmin=zscale[1][0], vmax=zscale[1][1])
else:
plt.pcolor(b)
plt.title(Lab_b)
plt.colorbar()
plt.axis('off')

plt.subplot(2,2,3)
if zscale[2]:
plt.pcolor(c, vmin=zscale[2][0], vmax=zscale[2][1])
else:
plt.pcolor(c)
plt.title(Lab_c)
plt.colorbar()
plt.axis('off')

plt.subplot(2,2,4)
if zscale[3]:
plt.pcolor(d, vmin=zscale[3][0], vmax=zscale[3][1])
else:
plt.pcolor(d)
plt.title(Lab_d)
plt.colorbar()
plt.axis('off')

plt.subplots_adjust(hspace=0.07, wspace=0.07, top=0.95, bottom=0.02, left=0.005, right=0.99)

if filename:
plt.savefig(filename, dpi=300)

if display:
plt.show()

plt.close()


def PlotArray(data_arr, label_arr, filename=None, display=True, zscale=None):
w = len(data_arr[0])
h = len(data_arr)
plt.figure(figsize=(3*w+1, 3.5*h))
plt.set_cmap('nipy_spectral')

index = 1
for (x,y) in ((x,y) for y in range(h) for x in range(w)):
plt.subplot(w, h, index)
plt.title(label_arr[y][x])
if zscale:
plt.pcolor(data_arr[y][x], vmin=zscale[0], vmax=zscale[1])
else:
plt.pcolor(data_arr[y][x])

# if x == w-1:
# plt.colorbar()

plt.axis('off')

index += 1

plt.subplots_adjust(hspace=0.07, wspace=0.07, top=0.95, bottom=0.02, left=0.005, right=0.99)

if filename:
plt.savefig(filename, dpi=300)

if display:
plt.show()

plt.close()


def IntensityComparison(original, result, filename=None, display=True):
Plot2(original, '(a) Simulation Input', result, '(b) Compressive Sensing', filename, display)

def IGSComparison(original, intensity, bigG, bigS, filename=None, display=True):
Plot4(original, '(a) Simulation Input', intensity, '(b) CS Intensity', bigG, '(c) CS BigG', bigS, '(d) CS BigS', filename, display)

def IgsComparison(original, intensity, g, s, filename=None, display=True):
Plot4(original, '(a) Simulation Input', intensity, '(b) CS Intensity', g, '(c) CS g', s, '(d) CS s', filename, display, zscale=[None,None,[0,1],[0,1]])

def ITauPComparison(original, intensity, taup, filename=None, display=True):
Plot3(original, '(a) Simulation Input', intensity, '(b) CS Intensity', taup, '(c) CS TauP', filename, display)

def ITauMComparison(original, intensity, taum, filename=None, display=True):
Plot3(original, '(a) Simulation Input', intensity, '(b) CS Intensity', taum, '(c) CS TauM', filename, display)

65 changes: 65 additions & 0 deletions library/CompressiveSensing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
Ryan A. Colyer
Compressive Sensing Analysis tools.
"""

import numpy as np
import random
from library.Phasor import Phasor
from sklearn.linear_model import Lasso

# Stores the data for one compressive FLIM frame, consisting of a mask,
# the intensity, and the phasor data.
class Frame:
def __init__(self, mask):
self.mask = mask
self.data = Phasor()

def Add(self, photon):
self.data += Phasor(photon)


# A randomly generated mask.
class Mask:
def __init__(self, width, height):
self.width = width
self.height = height
self.data = [[np.random.randint(0,2) for x in range(width)] for y in range(height)]

def IsOn(self, x, y):
return 1 == self.data[y][x]

def IsOff(self, x, y):
return not self.IsOn(x, y)


# Reconstruct the base image with L1 penalization using Lasso.
class Reconstruction:
def __init__(self, alpha=0.001):
self.model = Lasso(alpha)

def ModelFit(self, frames, data):
height = frames[0].mask.height
width = frames[0].mask.width
masks = self.GetMasks(frames)
self.model.fit(masks, data)
self.result = self.model.coef_.reshape(height, width)
return self.result

def GetMasks(self, frames):
return np.array([np.array(f.mask.data).ravel() for f in frames])

def FitIntensity(self, frames):
counts = np.array([f.data.count for f in frames])
return self.ModelFit(frames, counts)

def FitG(self, frames):
bigGs = np.array([f.data.big_G for f in frames])
return self.ModelFit(frames, bigGs)

def FitS(self, frames):
bigSs = np.array([f.data.big_S for f in frames])
return self.ModelFit(frames, bigSs)

Loading

0 comments on commit aa9417c

Please sign in to comment.