Finish fleshing out the examples

This commit is contained in:
Abe Levitan
2019-09-04 18:23:34 -04:00
parent 0377ea90be
commit 99d02f95cf
10 changed files with 158 additions and 105 deletions
+16 -34
View File
@@ -1,71 +1,53 @@
from __future__ import division, print_function, absolute_import
import CDTools
from CDTools.tools import cmath
from CDTools import tools
from CDTools.tools.plotting import *
from matplotlib import pyplot as plt
import pickle
from time import time
import datetime
import torch as t
import numpy as np
# This file is too large to be distributed via Github.
# Please contact Abe Levitan (alevitan@mit) if you would like access
filename = '/media/Data Bank/CSX_6_17/Processed_CXIs/79511_p.cxi'
dataset = CDTools.datasets.Ptycho_2D_Dataset.from_cxi(filename)
# In this dataset, the edges of the patterns are too noisy and are
# masked off anyway. We can easily just remove this data instead of
# leaving it to float.
# In this dataset, the edges of the patterns are masked off anyway
# We can easily just remove this data instead of leaving it to float.
dataset.patterns = dataset.patterns[:,70:-70,70:-70]
dataset.mask = dataset.mask[70:-70,70:-70]
# This model definition includes lots of parameters.
# In this case:
# This model definition includes lots of tweaks, described below.
#
# randomize_ang defines the initial random phase noise's extent
# translations_scale defines how aggressive the position reconstruction is
# n_modes is the number of incoherent modes
# propagation_distance is the distance to propagate from the SHARP-style guess of the probe's focal spot (in this case, the value comes from knowledge of the experimental geometry).
model = CDTools.models.FancyPtycho.from_dataset(dataset,
randomize_ang = np.pi/4,
translation_scale = 4,
n_modes=2,
propagation_distance=-73e-6)
# Uncomment these to use on the CPU
# default is CPU with 32-bit floats
# Move to the GPU
model.to(device='cuda')
dataset.get_as(device='cuda')
# We can run the first phase of phase retrieval while leaving
# the probe positions fixed (whether this is good is debatable)
# model.translation_offsets.requires_grad = False
for i, loss in enumerate(model.Adam_optimize(15, dataset, batch_size=15)):
print(i,loss)
# We turn off position reconstruction for the first phase
model.translation_offsets.requires_grad = False
for i, loss in enumerate(model.Adam_optimize(10, dataset, batch_size=15)):
model.inspect(dataset)
# And we turn it on for the second phase, as we also lower the learning rate
# model.translation_offsets.requires_grad = True
for i, loss in enumerate(model.Adam_optimize(15, dataset, batch_size=15, lr=0.0005)):
print(i,loss)
# And we turn it on for the second phase
model.translation_offsets.requires_grad = True
for i, loss in enumerate(model.Adam_optimize(20, dataset, batch_size=15)):
model.inspect(dataset)
print(i,loss)
# The third phase lowers the rate further
for i, loss in enumerate(model.Adam_optimize(10, dataset, batch_size=15, lr=0.00005)):
print(i,loss)
for i, loss in enumerate(model.Adam_optimize(10, dataset, batch_size=15, lr=0.0005)):
model.inspect(dataset)
print(i,loss)
model.inspect(dataset)
model.compare(dataset)
plt.show()
+9 -18
View File
@@ -1,23 +1,12 @@
from __future__ import division, print_function, absolute_import
import numpy as np
from matplotlib import pyplot as plt
import pickle
from CDTools.tools import cmath, plotting
from CDTools.tools.analysis import *
from CDTools.tools import plotting
from CDTools.tools import analysis
#
# Note that much of this functionality is duplicated by the convenience
# script. Try running:
#
# python -m CDTools.scripts.synthesize example_reconstructions/gold_balls_ensemble.pickle
#
# Which will perform much of the same analysis on any saved reconstruction
# ensemble
#
with open('example_reconstructions/gold_balls_ensemble.pickle', 'rb') as f:
dataset = pickle.load(f)
@@ -29,12 +18,13 @@ if type(dataset) == type([]):
for key in dataset[0]}
# Now we synthesize the object using the tool from CDTools
synth_probe, synth_obj, aligned_objs = synthesize_reconstructions(
# Now we synthesize an average reconstruction
synth_probe, synth_obj, aligned_objs = analysis.synthesize_reconstructions(
dataset['probe'], dataset['obj'])
# And then we calculate the consistency PRTF from this
freqs, prtf = calc_consistency_prtf(synth_obj, aligned_objs, dataset['basis'][0])
freqs, prtf = analysis.calc_consistency_prtf(synth_obj, aligned_objs, dataset['basis'][0])
# Plot the first mode in detail
plotting.plot_phase(synth_probe[0],basis=dataset['basis'][0])
@@ -44,12 +34,13 @@ plotting.plot_colorized(synth_probe[0],basis=dataset['basis'][0])
# Just plot the colorized version of the subdominant modes
plotting.plot_colorized(synth_probe[1],basis=dataset['basis'][0])
plotting.plot_colorized(synth_probe[2],basis=dataset['basis'][0])
# And now we plot the object
plotting.plot_amplitude(synth_obj,basis=dataset['basis'][0])
plotting.plot_colorized(synth_obj,basis=dataset['basis'][0])
plotting.plot_phase(synth_obj,basis=dataset['basis'][0])
# Now plot the PRTF
# Finally, plot the consistency PRTF
plt.figure()
plt.plot(freqs*1e-6, prtf)
plt.xlabel('Spatial Frequency (cycles/um)')
@@ -1,12 +1,10 @@
from __future__ import division, print_function, absolute_import
import CDTools
import numpy as np
import pickle
from matplotlib import pyplot as plt
# Load the data
filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi'
dataset = CDTools.datasets.Ptycho_2D_Dataset.from_cxi(filename)
results = []
@@ -14,23 +12,23 @@ results = []
for idx in range(25):
print('Starting Reconstruction', idx)
model = CDTools.models.FancyPtycho.from_dataset(dataset,n_modes=3,randomize_ang=0.1*np.pi)
# Create a new model each time
model = CDTools.models.FancyPtycho.from_dataset(dataset,n_modes=3,
randomize_ang=0.1*np.pi)
# default is CPU with 32-bit floats
# Work on the GPU
model.to(device='cuda')
dataset.get_as(device='cuda')
# Run the reconstruction
for i, loss in enumerate(model.Adam_optimize(30, dataset, batch_size=100)):
print(i,loss)
# Here we see how to liveplot the results - this call will create
# or update a readout of the various parameters being reconstructed
# And add the results to the ensemble
results.append(model.save_results(dataset))
# Save out the ensemble
with open('example_reconstructions/gold_balls_ensemble.pickle', 'wb') as f:
pickle.dump(results,f)
model.inspect(dataset)
model.compare(dataset)
plt.show()
+9 -14
View File
@@ -1,36 +1,31 @@
from __future__ import division, print_function, absolute_import
import CDTools
import numpy as np
import pickle
from matplotlib import pyplot as plt
import pickle
# First, we load an example dataset from a .cxi file
filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi'
dataset = CDTools.datasets.Ptycho_2D_Dataset.from_cxi(filename)
# Next, we create a ptychography model from the dataset
# Note that we explicitly as for two incoherent probe modes
model = CDTools.models.FancyPtycho.from_dataset(dataset, n_modes=2)
# default is CPU with 32-bit floats
# Let's do this reconstruction on the GPU, shall we?
model.to(device='cuda')
dataset.get_as(device='cuda')
for i, loss in enumerate(model.Adam_optimize(30, dataset, batch_size=100)):
print(i,loss)
# Here we see how to liveplot the results - this call will create
# or update a readout of the various parameters being reconstructed
# And we liveplot the updates to the model as they happen
model.inspect(dataset)
print(i,loss)
# And we save the reconstruction out to a file
with open('example_reconstructions/gold_balls.pickle', 'wb') as f:
pickle.dump(model.save_results(dataset),f)
# Finally, we plot the results
model.inspect(dataset)
dataset.inspect()
model.compare(dataset)
plt.show()
+12
View File
@@ -0,0 +1,12 @@
from __future__ import division, print_function, absolute_import
import CDTools
from matplotlib import pyplot as plt
# First, we load an example dataset from a .cxi file
filename = 'example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi'
dataset = CDTools.datasets.Ptycho_2D_Dataset.from_cxi(filename)
# And we take a look at the data
dataset.inspect()
plt.show()
-1
View File
@@ -12,7 +12,6 @@ model = CDTools.models.SimplePtycho.from_dataset(dataset)
# Now, we run a short reconstruction from the dataset!
for i, loss in enumerate(model.Adam_optimize(10, dataset)):
model.inspect(dataset)
print(i, loss)
# Finally, we plot the results
@@ -1,44 +1,37 @@
from __future__ import division, print_function, absolute_import
import CDTools
from CDTools.tools import cmath
from CDTools import tools
from CDTools.tools.plotting import *
from matplotlib import pyplot as plt
import pickle
from time import time
import datetime
import h5py
import torch as t
import numpy as np
# This file is too large to be distributed via Github.
# Please contact Abe Levitan (alevitan@mit) if you would like access
filename = '/media/Data Bank/CSX_10_18/Processed_CXIs/110531_p.cxi'
dataset = CDTools.datasets.Ptycho_2D_Dataset.from_cxi(filename)
# This model definition includes lots of tweaks, described below.
#
# randomize_ang defines the initial random phase noise's extent
# translations_scale defines how aggressive the position reconstruction is
# scattering_mode overrides any sample normal information stored in the .cxi file
model = CDTools.models.FancyPtycho.from_dataset(dataset,
randomize_ang = np.pi/4,
padding=0,
translation_scale=10,
scattering_mode='reflection')
# Uncomment these to use on the CPU
# default is CPU with 32-bit floats
# Move to the GPU
model.to(device='cuda')
dataset.get_as(device='cuda')
# Run the reconstruction
for i, loss in enumerate(model.Adam_optimize(250, dataset,batch_size=5)):
print(i,loss)
model.inspect(dataset)
model.inspect(dataset)
dataset.inspect()
model.compare(dataset)
plt.show()