Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PnccdDetector APIs #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions examples/scripts/Example3D_MPI_HDF5.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import skopi as sk
import skopi.gpu as sg
from skopi.detector.pnccd import PnccdDetector
from mpi4py import MPI
import numpy as np
import argparse
Expand Down Expand Up @@ -37,7 +38,7 @@ def main():
print("Running %d parallel MPI processes" % (comm.size))

t_start = MPI.Wtime()

orientations = np.zeros((2*number,4))
particle = sk.Particle()

Expand All @@ -46,51 +47,51 @@ def main():
orientations = sk.geometry.get_uniform_quat(num_pts=number).astype(np.float64)
elif orient== 0:
orientations = sk.geometry.get_random_quat(num_pts=number).astype(np.float64)
print "O=",orientations.shape
print "ODtype=", orientations.dtype
print("O=",orientations.shape)
print("ODtype=", orientations.dtype)
#sys.exit(0)
print("Reading PDB file...")
particle.read_pdb(pdb, ff='WK')
# reading beam and detector files
beam= sk.Beam(beam)
#beam.set_wavelength(1.0e-10)
print beam.get_wavelength()
det = sk.PnccdDetector(geom=geom, beam=beam)
print(beam.get_wavelength())
det = PnccdDetector(geom=geom, beam=beam)
print("Broadcasting input to processes...")

data = {'particle': particle, 'orientations': orientations, 'detector': det}

dct = comm.bcast(data,root=0)

if rank==0:
pattern_shape = det.pedestals.shape
fin = h5.File(os.path.join(outDir,'test_saveHDF5_parallel_intens_combined.h5'),'w')
if savePhotons == 1:
fph = h5.File(os.path.join(outDir,'test_saveHDF5_parallel_photons_combined.h5'),'w')

if savePhotons == 1:
dset_photons = fph.create_dataset('imgPhot', shape=(number,)+pattern_shape,dtype=np.int32, chunks=(1,)+pattern_shape, compression="gzip", compression_opts=4)
dset_intens = fin.create_dataset('imgIntens', shape=(number,)+pattern_shape,dtype=np.float32, chunks=(1,)+pattern_shape, compression="gzip", compression_opts=4)

if savePhotons == 1:
fph.create_dataset('orientation', data=orientations, compression="gzip", compression_opts=4)
fph.create_dataset('orientation', data=orientations, compression="gzip", compression_opts=4)
fin.create_dataset('orientation', data=orientations, compression="gzip", compression_opts=4)

print("Done creating HDF5 file and datasets...")

c = 0
while c < number:
status1 = MPI.Status()
result = comm.recv(source=MPI.ANY_SOURCE,status=status1) # (index,photImg)
i = status1.Get_source()

dd = det.add_correction(result[1])
print("Rank 0: Received image %d from rank %d" % (result[0],i))
dset_intens[result[0],:,:,:] = dd #result[1]
#photoImg = det.add_correction_and_quantization(pattern=result[1])
if savePhotons == 1:
photoImg = det.add_quantization(pattern=dd)
dset_photons[result[0],:,:,:] = photoImg
photoImg = det.add_quantization(pattern=dd)
dset_photons[result[0],:,:,:] = photoImg
c += 1

else: # slave
Expand All @@ -104,14 +105,14 @@ def main():
sliceOne = np.zeros((pattern_shape)) #left out dtype=np.float32
mesh_length = 128
mesh,voxel_length = det.get_reciprocal_mesh(voxel_number_1d=mesh_length)
print "MeshDtype=",mesh.dtype
print("MeshDtype=",mesh.dtype)

intensVol = sg.diffraction.calculate_diffraction_pattern_gpu(mesh, particle, return_type='intensity')
# lft out mesh.astype(np.float32)
for i in range((rank-1),number,sz-1):
# transform quaternion (set of orientations) into 3D rotation
rotmat = sk.geometry.quaternion2rot3d(ori[i,:])

intensSlice = slave_calc_intensity(rot3d = rotmat,
pixel_momentum = pixel_momentum,
pattern_shape = pattern_shape,
Expand All @@ -123,7 +124,7 @@ def main():
# astype(np.int32)
print("Sending slice %d from rank %d" % (i,rank))
comm.ssend((i,intensSlice),dest=0)

if rank==0:
t_end = MPI.Wtime()
print("Finishing constructing %d patterns in %f seconds" % (number,t_end-t_start))
Expand All @@ -143,7 +144,7 @@ def main():
#plt.imshow(diffImgAssemb)
#plt.colorbar()
#ax1.colorbar()

#ax2 = fig.add_subplot(2,1,2)
#ax2.imshow(np.log(photImgAssem+1), interpolation='none')
#ax2.colorbar()
Expand All @@ -167,7 +168,7 @@ def slave_calc_intensity(rot3d, pixel_momentum, pattern_shape, volume, voxel_len
"""
pixel_num = np.prod(pattern_shape)
sliceOne = np.zeros((pattern_shape)) # dtype=np.float32
print "SliceOneDtype=",sliceOne.dtype
print("SliceOneDtype=",sliceOne.dtype)
rotated_pixel_position = np.zeros((1,)+pattern_shape+(3,))

# new pixel position in reciprocal space
Expand All @@ -180,12 +181,12 @@ def slave_calc_intensity(rot3d, pixel_momentum, pattern_shape, volume, voxel_len
intensSlice = sk.geometry.extract_slice(local_index = index,
local_weight = weight,
volume = volume)

#intensSliceWCorrection = sk.detector.add_linear_correction(intensSlice)

return intensSlice
# local index, weight int32, float32

def parse_input_arguments(args):
del args[0]
parse = argparse.ArgumentParser()
Expand All @@ -203,5 +204,5 @@ def parse_input_arguments(args):

if __name__ == '__main__':
main()


3 changes: 2 additions & 1 deletion examples/scripts/ExampleAggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import matplotlib.pyplot as plt
import skopi as sk
from skopi.particlePlacement import *
from skopi.detector.pnccd import PnccdDetector
import time, os

def drawSphere(xCenter, yCenter, zCenter, r):
Expand Down Expand Up @@ -46,7 +47,7 @@ def drawSphere(xCenter, yCenter, zCenter, r):
print('focus area = {}'.format(beam._focus_area))

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)
increase_factor = 0.5
print('BEFORE: detector distance = {} m'.format(det.distance))
print('>>> Increasing the detector distance by a factor of {}'.format(increase_factor))
Expand Down
20 changes: 14 additions & 6 deletions examples/scripts/ExampleChaperone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@
import matplotlib.pyplot as plt
import h5py as h5
import skopi as sk
from skopi.detector.pnccd import PnccdDetector
import time, os

# Create a particle object
input_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../input')
script_dir = os.path.dirname(os.path.abspath(__file__))
pdb_dir = '../input/pdb/3iyf.pdb'
pdb_path = os.path.join(script_dir, pdb_dir)
particleOp = sk.Particle()
particleOp.read_pdb(input_dir+'/3iyf.pdb', ff='WK')
particleOp.read_pdb(pdb_path, ff='WK')
#particleOp.rotate_randomly()

#exit()

pdb_dir = '../input/pdb/3j03.pdb'
pdb_path = os.path.join(script_dir, pdb_dir)
particleCl = sk.Particle()
particleCl.read_pdb(input_dir+'/3j03.pdb', ff='WK')
particleCl.read_pdb(pdb_path, ff='WK')

# Load beam
beam = sk.Beam(input_dir+'/beam/amo86615.beam')
beam_file = '../input/beam/amo86615.beam'
beam_path = os.path.join(script_dir, beam_file)
beam = sk.Beam(beam_path)

# Load and initialize the detector
det = sk.PnccdDetector(geom = input_dir+'/lcls/amo86615/PNCCD::CalibV1/Camp.0:pnCCD.1/geometry/0-end.data',
beam = beam)
geom_file = '../input/lcls/amo86615/PNCCD::CalibV1/Camp.0:pnCCD.1/geometry/0-end.data'
geom_path = os.path.join(script_dir, geom_file)
det = PnccdDetector(geom = geom_path, beam = beam)

tic = time.time()
patternOp = det.get_photons(device='gpu', particle=particleOp)
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleFXS.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import h5py as h5
import time, os
import skopi as sk
from skopi.detector.pnccd import PnccdDetector

# Parameter(s)
numOp = 1
Expand All @@ -37,7 +38,7 @@
print('AFTER : # of photons per pulse = {}'.format(beam.get_photons_per_pulse()))

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)
increase_factor = 0.5
print('BEFORE: detector distance = {} m'.format(det.distance))
print('>>> Increasing the detector distance by a factor of {}'.format(increase_factor))
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleHolography.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import skopi as sk
from skopi.detector.pnccd import PnccdDetector
import time, os

# Input files
Expand All @@ -29,7 +30,7 @@
print('AFTER: # of photons per pulse = {}'.format(beam.get_photons_per_pulse()))

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)
increase_factor = 0.5
print('BEFORE: detector distance = {} m'.format(det.distance))
print('>>> Increasing the detector distance by a factor of {}'.format(increase_factor))
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleHydration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import h5py as h5
import time, os
import skopi as sk
from skopi.detector.pnccd import PnccdDetector

# Input files
input_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../input')
Expand All @@ -30,7 +31,7 @@
print('photon energy = {} eV'.format(beam.photon_energy))

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)
increase_factor = 0.5
print('BEFORE: detector distance = {} m'.format(det.distance))
print('>>> Increasing the detector distance by a factor of {}'.format(increase_factor))
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleHydrocarbons.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time, os

import skopi as sk
from skopi.detector.pnccd import PnccdDetector

numCyclohexane = 500
numDinitro = 500
Expand All @@ -27,7 +28,7 @@
geom = os.path.join(pwd,'../input/lcls/amo86615/PNCCD::CalibV1/Camp.0:pnCCD.1/geometry/0-end.data')

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam = beam)
det = PnccdDetector(geom=geom, beam = beam)

tic = time.time()
patternC = det.get_photons(device='gpu', particle=particleC)
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleMPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import argparse
import skopi as sk
from skopi.util import asnumpy, xp
from skopi.detector.pnccd import PnccdDetector


# set up MPI environment
Expand Down Expand Up @@ -38,7 +39,7 @@ def main():
beam = sk.Beam(beam)

# load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)

# create particle object(s)
particle = sk.Particle()
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleSASE.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import h5py as h5
import time, os
import skopi as sk
from skopi.detector.pnccd import PnccdDetector

# Input files
input_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../input')
Expand All @@ -32,7 +33,7 @@
print('AFTER : photon energy = {} eV'.format(beam.photon_energy))

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)
increase_factor = 0.5
print('BEFORE: detector distance = {} m'.format(det.distance))
print('>>> Increasing the detector distance by a factor of {}'.format(increase_factor))
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/ExampleSPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import h5py as h5
import time, os
import skopi as sk
from skopi.detector.pnccd import PnccdDetector

# Parameter(s)
num = 1
Expand All @@ -34,7 +35,7 @@
print('photon energy = {} eV'.format(beam.photon_energy))

# Load and initialize the detector
det = sk.PnccdDetector(geom=geom, beam=beam)
det = PnccdDetector(geom=geom, beam=beam)
increase_factor = 0.5
print('BEFORE: detector distance = {} m'.format(det.distance))
print('>>> Increasing the detector distance by a factor of {}'.format(increase_factor))
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/cspi_generate_synthetic_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import h5py as h5

import skopi as sk
from skopi.detector.pnccd import PnccdDetector


"""
Expand Down Expand Up @@ -99,7 +100,7 @@ def main():

# Geometry of detector
print("Load detector geometry: {}".format(geom_file))
det = sk.PnccdDetector(geom=geom_file, beam=beam)
det = PnccdDetector(geom=geom_file, beam=beam)


# Simulate the SPI Experiment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

import skopi as sk
from skopi.util import asnumpy, xp
from skopi.detector.pnccd import PnccdDetector


def main():
Expand Down Expand Up @@ -131,7 +132,7 @@ def main():
beam.set_photons_per_pulse(beam_fluence_increase_factor * beam.get_photons_per_pulse())

# Load geometry of detector
det = sk.PnccdDetector(geom=geom_file, beam=beam)
det = PnccdDetector(geom=geom_file, beam=beam)

# Get the shape of the diffraction pattern
diffraction_pattern_height = det.detector_pixel_num_x.item()
Expand Down
18 changes: 11 additions & 7 deletions examples/scripts/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
import h5py as h5
import time

from matplotlib.backends.qt_compat import QtWidgets, QtCore, is_pyqt5
if is_pyqt5():
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
from matplotlib.backends.backend_qt4agg import (
from matplotlib.backends.qt_compat import QtWidgets, QtCore
## from matplotlib.backends.qt_compat import QtWidgets, QtCore, is_pyqt5
## if is_pyqt5():
## from matplotlib.backends.backend_qt5agg import (
## FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
## else:
## from matplotlib.backends.backend_qt4agg import (
## FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from matplotlib.colors import LogNorm
from mpl_toolkits.mplot3d import Axes3D

import skopi as sk
import skopi.gpu as sg
from skopi.detector.pnccd import PnccdDetector


# Set default matplotlib parameters
Expand All @@ -45,7 +49,7 @@ def __init__(self, pdb_file, colors=False, debug=False):
beam = sk.Beam('../input/beam/amo86615.beam')

# Load and initialize the detector
self.det = sk.PnccdDetector(
self.det = PnccdDetector(
geom='../input/lcls/amo86615/PNCCD::CalibV1/'
'Camp.0:pnCCD.1/geometry/0-end.data',
beam=beam)
Expand Down
Loading