mirror of
https://github.com/cdtools-developers/cdtools.git
synced 2026-09-11 14:02:38 +02:00
Refactor the PRTF calculation, fix a small bug creating a slight offset in the frequency labels, and write a test
This commit is contained in:
@@ -4,50 +4,11 @@ import torch as t
|
||||
from matplotlib import pyplot as plt
|
||||
import pickle
|
||||
import argparse
|
||||
from scipy import fftpack
|
||||
|
||||
from CDTools.tools import cmath, plotting
|
||||
from CDTools.tools import image_processing as ip
|
||||
from CDTools.tools.analysis import *
|
||||
|
||||
|
||||
|
||||
|
||||
def calc_prtf(synth_obj, objects, basis, obj_slice=None):
|
||||
if obj_slice is None:
|
||||
obj_slice = np.s_[(objects[0].shape[0]//8)*3:(objects[0].shape[0]//8)*5,
|
||||
(objects[0].shape[1]//8)*3:(objects[0].shape[1]//8)*5]
|
||||
|
||||
synth_obj = cmath.complex_to_torch(synth_obj[obj_slice])
|
||||
|
||||
synth_fft = cmath.cabssq(cmath.fftshift(t.fft(synth_obj,2))).numpy()
|
||||
|
||||
prtfs = []
|
||||
for obj in objects:
|
||||
obj = cmath.complex_to_torch(obj[obj_slice])
|
||||
single_fft = cmath.cabssq(cmath.fftshift(t.fft(obj,2))).numpy()
|
||||
|
||||
|
||||
di = np.linalg.norm(basis[:,0])
|
||||
dj = np.linalg.norm(basis[:,1])
|
||||
|
||||
i_freqs = fftpack.fftshift(fftpack.fftfreq(single_fft.shape[0],d=di))
|
||||
j_freqs = fftpack.fftshift(fftpack.fftfreq(single_fft.shape[1],d=dj))
|
||||
|
||||
Js,Is = np.meshgrid(j_freqs,i_freqs)
|
||||
Is = Is - np.mean(Is)
|
||||
Js = Js - np.mean(Js)
|
||||
Rs = np.sqrt(Is**2+Js**2)
|
||||
|
||||
single_ints, bins = np.histogram(Rs,bins=100,weights=single_fft)
|
||||
synth_ints, bins = np.histogram(Rs,bins=100,weights=synth_fft)
|
||||
|
||||
prtfs.append(synth_ints/single_ints)
|
||||
|
||||
return bins[:-1], np.mean(prtfs,axis=0)
|
||||
|
||||
|
||||
|
||||
def make_argparser():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
|
||||
@@ -56,6 +17,7 @@ def make_argparser():
|
||||
return parser
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
args = make_argparser().parse_args()
|
||||
@@ -66,7 +28,7 @@ if __name__ == '__main__':
|
||||
synth_probe, synth_obj, aligned_objs = synthesize_reconstructions(
|
||||
dataset['probe'], dataset['obj'], args.use_probe)
|
||||
|
||||
freqs, prtf = calc_prtf(synth_obj, aligned_objs, dataset['basis'])
|
||||
freqs, prtf = calc_consistency_prtf(synth_obj, aligned_objs, dataset['basis'])
|
||||
|
||||
|
||||
plotting.plot_phase(dataset['probe'][0][0],basis=1e6*dataset['basis'])
|
||||
@@ -74,10 +36,12 @@ if __name__ == '__main__':
|
||||
plotting.plot_colorized(dataset['probe'][0][0],basis=1e6*dataset['basis'])
|
||||
|
||||
|
||||
plotting.plot_phase(synth_probe[1],basis=1e6*dataset['basis'])
|
||||
plotting.plot_amplitude(synth_probe[1],basis=1e6*dataset['basis'])
|
||||
plotting.plot_colorized(synth_probe[1],basis=1e6*dataset['basis'])
|
||||
|
||||
try:
|
||||
plotting.plot_phase(synth_probe[1],basis=1e6*dataset['basis'])
|
||||
plotting.plot_amplitude(synth_probe[1],basis=1e6*dataset['basis'])
|
||||
plotting.plot_colorized(synth_probe[1],basis=1e6*dataset['basis'])
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
plotting.plot_amplitude(synth_obj,basis=1e6*dataset['basis'])
|
||||
@@ -86,12 +50,14 @@ if __name__ == '__main__':
|
||||
|
||||
|
||||
plt.figure()
|
||||
plt.show()
|
||||
exit()
|
||||
real_translations = dataset['basis'].dot(dataset['translation'][0].transpose())
|
||||
real_translations -= np.min(real_translations,axis=1)[:,None]
|
||||
plt.plot(real_translations[0]*1e6,real_translations[1]*1e6,'k.')
|
||||
plt.plot(real_translations[0]*1e6,real_translations[1]*1e6,'b-',linewidth=0.5)
|
||||
plt.figure()
|
||||
try:
|
||||
real_translations = dataset['basis'].dot(dataset['translation'][0].transpose())
|
||||
real_translations -= np.min(real_translations,axis=1)[:,None]
|
||||
plt.plot(real_translations[0]*1e6,real_translations[1]*1e6,'k.')
|
||||
plt.plot(real_translations[0]*1e6,real_translations[1]*1e6,'b-',linewidth=0.5)
|
||||
plt.figure()
|
||||
except:
|
||||
pass
|
||||
|
||||
plt.plot(freqs*1e-6, prtf)
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user