Finally remove the scourge of from __future__ import ... and remove any lingering suggestion that this code is python 2 compatible

This commit is contained in:
Abe Levitan
2021-08-05 11:08:13 -04:00
parent 960740f39f
commit 3e6072acb7
33 changed files with 25 additions and 311 deletions
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
from CDTools import tools
from CDTools import datasets
from CDTools import models
-2
View File
@@ -25,8 +25,6 @@ dataset before attempting to do so
"""
from __future__ import division, print_function, absolute_import
# I don't believe that __all__ really needed, but it's nice to define it
# to be explicit that import * is safe
__all__ = ['CDataset','Ptycho2DDataset','PolarizedPtycho2DDataset']
+17 -31
View File
@@ -13,29 +13,15 @@ of the following functions:
"""
from __future__ import division, print_function, absolute_import
import numpy as np
import torch as t
from copy import copy
import h5py
import pathlib
from CDTools.tools import data as cdtdata
from CDTools.tools import plotting
from torch.utils import data as torchdata
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider
from matplotlib import ticker
__all__ = ['CDataset']
#
# This loads and stores all the kinds of metadata that are common to
# All different kinds of diffraction experiments
# Other datasets can subclass this and not worry about loading and
# saving that metadata.
#
class CDataset(torchdata.Dataset):
""" The base dataset class which all other datasets subclass
@@ -47,7 +33,7 @@ class CDataset(torchdata.Dataset):
storage of the metadata portions of .cxi files, as well as the tools
needed to allow for easy mixing of data on the CPU and GPU.
"""
def __init__(self, entry_info=None, sample_info=None,
wavelength=None,
detector_geometry=None, mask=None,
@@ -55,9 +41,9 @@ class CDataset(torchdata.Dataset):
"""The __init__ function allows construction from python objects.
The detector_geometry dictionary is defined to have the
The detector_geometry dictionary is defined to have the
entries defined by the outputs of data.get_detector_geometry.
Parameters
----------
@@ -71,13 +57,13 @@ class CDataset(torchdata.Dataset):
A dictionary containing the various detector geometry
parameters
mask : array
A mask for the detector, defined as 1 for live pixels, 0
A mask for the detector, defined as 1 for live pixels, 0
for dead
background : array
An initial guess for the not-previously-subtracted
An initial guess for the not-previously-subtracted
detector background
"""
# Force pass-by-value-like behavior to stop strangeness
self.entry_info = copy(entry_info)
self.sample_info = copy(sample_info)
@@ -91,38 +77,38 @@ class CDataset(torchdata.Dataset):
self.background = t.tensor(background, dtype=t.float32)
else:
self.background = None
self.get_as(device='cpu')
def to(self,*args,**kwargs):
def to(self, *args, **kwargs):
"""Sends the relevant data to the given device and dtype
This function sends the stored mask and background to the
specified device and dtype
Accepts the same parameters as torch.Tensor.to
"""
# The mask should always stay a uint8, but it should switch devices
mask_kwargs = copy(kwargs)
try:
mask_kwargs.pop('dtype')
except KeyError as r:
except KeyError:
pass
if self.mask is not None:
self.mask = self.mask.to(*args,**mask_kwargs)
self.mask = self.mask.to(*args,**mask_kwargs)
if self.background is not None:
self.background = self.background.to(*args,**kwargs)
self.background = self.background.to(*args,**kwargs)
def get_as(self, *args, **kwargs):
"""Sets the dataset to return data on the given device and dtype
Oftentimes there isn't room to store an entire dataset on a GPU,
but it is still worth running the calculation on the GPU even with
the overhead incurred by transferring data back and forth. In that
case, get_as can be used instead of to, to declare a set of
case, get_as can be used instead of to, to declare a set of
device and dtype that the data should be returned as, whenever it
is accessed through the __getitem__ function (as it would be in
any reconstructions).
@@ -1,16 +1,10 @@
from __future__ import division, print_function, absolute_import
import numpy as np
import torch as t
from copy import copy
import h5py
import pathlib
from CDTools.datasets import CDataset, Ptycho2DDataset
from CDTools.datasets import Ptycho2DDataset
from CDTools.tools import data as cdtdata
from CDTools.tools import plotting
from torch.utils import data as torchdata
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider
from matplotlib import ticker
__all__ = ['PolarizedPtycho2DDataset']
@@ -18,7 +12,7 @@ __all__ = ['PolarizedPtycho2DDataset']
class PolarizedPtycho2DDataset(Ptycho2DDataset):
"""The standard dataset for a 2D ptychography scan
Subclasses datasets.CDataset
Subclasses datasets.Ptycho2DDataset
This class loads and saves 2D ptychography scan data from .cxi files.
It should save and load files compatible with most reconstruction
@@ -122,7 +116,7 @@ class PolarizedPtycho2DDataset(Ptycho2DDataset):
# perhaps there is a way but I couldn't figure it out.
@classmethod
def from_cxi(cls, cxi_file):
"""Generates a new CDataset from a .cxi file directly
"""Generates a new PolarizedPtycho2DDataset from a .cxi file directly
This generates a new PolarizedPtycho2DDataset from a .cxi file storing
a 2D ptychography scan.
+1 -7
View File
@@ -1,17 +1,11 @@
from __future__ import division, print_function, absolute_import
import numpy as np
import torch as t
from copy import copy
import h5py
import pathlib
from CDTools.datasets import CDataset
from CDTools.tools import data as cdtdata
from CDTools.tools import plotting
from torch.utils import data as torchdata
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider
from matplotlib import ticker
__all__ = ['Ptycho2DDataset']
@@ -125,7 +119,7 @@ class Ptycho2DDataset(CDataset):
# perhaps there is a way but I couldn't figure it out.
@classmethod
def from_cxi(cls, cxi_file):
"""Generates a new CDataset from a .cxi file directly
"""Generates a new Ptycho2DDataset from a .cxi file directly
This generates a new Ptycho2DDataset from a .cxi file storing
a 2D ptychography scan.
+3 -1
View File
@@ -22,7 +22,7 @@ defining a new ptychography model before attempting to do so.
# I don't believe that __all__ really needed, but it's nice to define it
# to be explicit that import * is safe
#__all__ = ['CDIModel', 'SimplePtycho', 'FancyPtycho', 'Bragg2DPtycho', 'SMatrixPtycho', 'RPI']
__all__ = ['CDIModel', 'SimplePtycho', 'FancyPtycho', 'PolarizedFancyPtycho', 'Bragg2DPtycho', 'Multislice2DPtycho', 'RPI']
from CDTools.models.base import CDIModel
from CDTools.models.simple_ptycho import SimplePtycho
@@ -31,5 +31,7 @@ from CDTools.models.polarized_fancy_ptycho import PolarizedFancyPtycho
from CDTools.models.bragg_2d_ptycho import Bragg2DPtycho
from CDTools.models.multislice_2d_ptycho import Multislice2DPtycho
from CDTools.models.rpi import RPI
# Still needs to be updated for the new complex numbers
#from CDTools.models.s_matrix_ptycho import SMatrixPtycho
-5
View File
@@ -28,8 +28,6 @@ loss
"""
from __future__ import division, print_function, absolute_import
import torch as t
from torch.utils import data as torchdata
from matplotlib import pyplot as plt
@@ -39,11 +37,8 @@ import numpy as np
import threading
import queue
import time
#import pytorch_warmup
from .complex_adam import MyAdam
from .complex_lbfgs import MyLBFGS
from matplotlib.backends.backend_pdf import PdfPages
__all__ = ['CDIModel']
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import torch as t
from CDTools.models import CDIModel
from CDTools.datasets import Ptycho2DDataset
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import torch as t
from CDTools.models import CDIModel
from CDTools.datasets import Ptycho2DDataset
-5
View File
@@ -1,14 +1,9 @@
from __future__ import division, print_function, absolute_import
import torch as t
from CDTools.models import CDIModel
from CDTools.datasets import Ptycho2DDataset
from CDTools import tools
from CDTools.tools import plotting as p
from CDTools.tools.interactions import RPI_interaction
from CDTools.tools import initializers
from matplotlib import pyplot as plt
from datetime import datetime
import numpy as np
from copy import copy
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import torch as t
from CDTools.models import CDIModel
from CDTools.datasets import Ptycho2DDataset
-4
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
import torch as t
from CDTools.models import CDIModel
from CDTools.datasets import Ptycho2DDataset
@@ -7,10 +5,8 @@ from CDTools import tools
from CDTools.tools import plotting as p
from copy import copy
from torch.utils import data as torchdata
from matplotlib import pyplot as plt
from datetime import datetime
import numpy as np
from .complex_adam import MyAdam
__all__ = ['SimplePtycho']
-1
View File
@@ -1 +0,0 @@
from __future__ import division, print_function, absolute_import
-97
View File
@@ -1,97 +0,0 @@
from __future__ import division, print_function, absolute_import
import numpy as np
import torch as t
from matplotlib import pyplot as plt
import pickle
import argparse
from CDTools.tools import cmath, plotting
from CDTools.tools.analysis import *
def make_argparser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('file', help='The reconstruction file to calculate metrics for')
parser.add_argument('--use-probe', '-up', action='store_true', help='Use the probe instead of the object to align the reconstructions')
return parser
if __name__ == '__main__':
args = make_argparser().parse_args()
with open(args.file, 'rb') as f:
dataset = pickle.load(f)
# This converts from a list of dictionaries to a dictionary of lists
# It's safe to assume that all elements have the same set of keys
if type(dataset) == type([]):
dataset = {key: [element[key] for element in dataset]
for key in dataset[0]}
calc_prtf = True
else:
# If it's a length-one reconstruction
dataset = {key: [dataset[key]] for key in dataset}
calc_prtf = False
# Orthogonalize the probes
dataset['probe'] = [orthogonalize_probes(p) for p in dataset['probe']]
print('hi')
synth_probe, synth_obj, aligned_objs = synthesize_reconstructions(
dataset['probe'], dataset['obj'], args.use_probe)
print('hey')
if calc_prtf:
freqs, prtf = calc_consistency_prtf(synth_obj, aligned_objs, dataset['basis'][0], nbins=200)
# Either plot the only probe, or plot the dominant probe
if len(synth_probe.shape) == 2:
plotting.plot_phase(synth_probe,basis=dataset['basis'][0])
plotting.plot_amplitude(synth_probe,basis=dataset['basis'][0])
plotting.plot_colorized(synth_probe,basis=dataset['basis'][0])
else:
# Plot as many probes as exist
try:
for i in range(0,50):
plotting.plot_phase(synth_probe[i],basis=dataset['basis'][0])
plt.title('Probe ' + str(i+1) + 'Phase')
plotting.plot_amplitude(synth_probe[i],basis=dataset['basis'][0])
plt.title('Probe ' + str(i+1) + ' Amplitude')
plotting.plot_colorized(synth_probe[i],basis=dataset['basis'][0])
plt.title('Probe ' + str(i+1) + ' Colorized')
except IndexError:
pass
plotting.plot_amplitude(aligned_objs[0][300:-300,300:-300],basis=dataset['basis'][0])
plotting.plot_colorized(aligned_objs[0][300:-300,300:-300],basis=dataset['basis'][0])
plotting.plot_phase(aligned_objs[0][300:-300,300:-300],basis=dataset['basis'][0])
plotting.plot_amplitude(synth_obj[300:-300,300:-300],basis=dataset['basis'][0])
plotting.plot_colorized(synth_obj[300:-300,300:-300],basis=dataset['basis'][0])
plotting.plot_phase(synth_obj[300:-300,300:-300],basis=dataset['basis'][0])
try:
real_translations = dataset['translation'][0]
real_translations -= np.min(real_translations,axis=0)[None,:]
real_translations = real_translations
plotting.plot_translations(real_translations)
plotting.plot_nanomap(real_translations,dataset['weights'][0])
except:
pass
plt.figure()
plt.imshow(np.sqrt(dataset['background'][0]))
if calc_prtf:
plt.figure()
plt.plot(freqs*1e-6, prtf)
plt.xlabel('Spatial Frequency (cycles/um)')
plt.ylabel('Consistency Based PRTF')
plt.grid()
plt.show()
-96
View File
@@ -1,96 +0,0 @@
from __future__ import division, print_function, absolute_import
import h5py
import numpy as np
import os
from PyQt5 import QtWidgets
from matplotlib import pyplot as plt
import signal
import sys
import argparse
#
# The plotting was broken by the move to CDTools, at some point this should
# be fixed
#
def view_cxi(filename):
"""Opens a popup window displaying the contents of ``filename``.
relevant attributes."""
signal.signal(signal.SIGINT, signal.SIG_DFL)
class Viewer(QtWidgets.QMainWindow):
def __init__(self, datafile):
self.datafile = datafile
QtWidgets.QMainWindow.__init__(self)
self.tree = QtWidgets.QTreeWidget(self)
self.tree.setColumnWidth(0, 200)
self.setCentralWidget(self.tree)
self.buildTree()
self.tree.itemClicked.connect(self.handleClick)
def handleClick(self,item,column):
if(item.text(column) == 'Click to print to console'):
data = self.data_full[str(item.text(2))]
print(np.asarray(data))
if(item.text(column) == 'Click to display'):
data = self.datasets[str(item.text(2))]
plt.plot(data)
plt.title(item.text(0).capitalize())
plt.show()
def closeWindow(self):
pass
def buildTree(self):
self.datasets = {}
self.data_full = {}
self.tree.setColumnCount(2)
self.f = h5py.File(self.datafile, 'r')
item = QtWidgets.QTreeWidgetItem(['/'])
self.tree.addTopLevelItem(item)
self.buildBranch(self.f,item)
def buildBranch(self,group,item):
for g in group.keys():
lst = [g]
self.data_full[group[g].name] = group[g]
if(isinstance(group[g],h5py.Group)):
child = QtWidgets.QTreeWidgetItem(lst)
self.buildBranch(group[g],child)
item.addChild(child)
else:
if len(group[g].shape)>2:
lst.append('Click to print to console')
lst.append(group[g].name)
self.datasets[group[g].name] = group[g]
item.addChild(QtWidgets.QTreeWidgetItem(lst))
if len(group[g].shape)==2 or len(group[g].shape)==1:
lst.append('Click to display')
lst.append(group[g].name)
self.datasets[group[g].name] = group[g]
item.addChild(QtWidgets.QTreeWidgetItem(lst))
else:
lst.append('Click to print to console')
lst.append(group[g].name)
self.datasets[group[g].name] = group[g]
item.addChild(QtWidgets.QTreeWidgetItem(lst))
filename = os.path.expanduser(filename)
app = QtWidgets.QApplication(sys.argv)
viewer = Viewer(filename)
viewer.setFixedSize(500, 500)
viewer.show()
app.exec_()
def make_argparser():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('file', help='The cxi file to view')
return parser
if __name__ == '__main__':
args = make_argparser().parse_args()
view_cxi(args.file)
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.analysis.analysis import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.atoms.atoms import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.data.data import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.initializers.initializers import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.interactions.interactions import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.losses.losses import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.measurements.measurements import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.plotting.plotting import *
-2
View File
@@ -1,3 +1 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools.propagators.propagators import *
-3
View File
@@ -1,11 +1,8 @@
from __future__ import division, print_function, absolute_import
from CDTools.datasets import *
from CDTools.tools import data as cdtdata
import numpy as np
import torch as t
import h5py
import pytest
import datetime
-3
View File
@@ -1,6 +1,3 @@
from __future__ import division, print_function, absolute_import
import pytest
import numpy as np
from scipy import fftpack as ffts
import torch as t
+1 -6
View File
@@ -1,13 +1,8 @@
from __future__ import division, print_function, absolute_import
import pytest
import numpy as np
import torch as t
from CDTools.tools import image_processing, initializers, interactions
from CDTools.tools import image_processing, interactions
from scipy import ndimage
from scipy.signal import fftconvolve
def test_centroid():
# Test single im
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools import initializers
from CDTools.datasets import Ptycho2DDataset
import numpy as np
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools import interactions
import numpy as np
import torch as t
-2
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools import losses
import numpy as np
import torch as t
-3
View File
@@ -1,9 +1,6 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools import measurements
import torch as t
import numpy as np
import pytest
def test_intensity():
-3
View File
@@ -1,9 +1,6 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools import plotting
from CDTools.tools import initializers
import numpy as np
import pytest
import torch as t
import scipy.misc
import matplotlib.pyplot as plt
-3
View File
@@ -1,5 +1,3 @@
from __future__ import division, print_function, absolute_import
from CDTools.tools import initializers
from CDTools.tools import propagators
from CDTools.tools import image_processing
@@ -8,7 +6,6 @@ import numpy as np
import torch as t
import pytest
import scipy.misc
from scipy.fftpack import fftshift, ifftshift
from scipy import stats
from matplotlib import pyplot as plt