diff --git a/.gitignore b/.gitignore index 221a58901f03894b128b24ad8afbe23032c9fd45..a3b8a68577ea77305d2462b4e8bd46bbb702c7ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,11 @@ +env/ testing/output.mp4 testing/abc.png +*.o +*.so +*.pyc +*.egg-info +fastXPCS/fastXPCS/cython/welford.c +fastXPCS/fastXPCS/cython/fxpcs_sparse.c +fastXPCS/fastXPCS/cython/dense.c diff --git a/fastXPCS/__init__.py b/fastXPCS/fastXPCS/__init__.py similarity index 100% rename from fastXPCS/__init__.py rename to fastXPCS/fastXPCS/__init__.py diff --git a/fastXPCS/algos.py b/fastXPCS/fastXPCS/algos.py similarity index 100% rename from fastXPCS/algos.py rename to fastXPCS/fastXPCS/algos.py diff --git a/fastXPCS/processOnCPU.py b/fastXPCS/fastXPCS/processOnCPU.py similarity index 100% rename from fastXPCS/processOnCPU.py rename to fastXPCS/fastXPCS/processOnCPU.py diff --git a/install_env.sh b/install_env.sh new file mode 100644 index 0000000000000000000000000000000000000000..d84734af8a95bbe8307d40271c9f140316cf9a6d --- /dev/null +++ b/install_env.sh @@ -0,0 +1,23 @@ + +python3 -m pip install --upgrade pip + +python3 -m venv env +source env/bin/activate + +python3 -m pip install Cython +python3 -m pip install numpy +python3 -m pip install scipy +python3 -m pip install matplotlib +python3 -m pip install pillow + +cd fastXPCS + +#python setup.py build_ext --inplace +#python setup.py develop + +python3 -m pip install -e . +cd .. + + + + diff --git a/run_test.sh b/run_test.sh new file mode 100644 index 0000000000000000000000000000000000000000..5086dc94104014252128458c37a7f6073bcfa09e --- /dev/null +++ b/run_test.sh @@ -0,0 +1,3 @@ +source env/bin/activate + +python3 testing/test.py \ No newline at end of file diff --git a/testing/test.py b/testing/test.py index ecc4de0e430a7b253eb473d6fa91fefae8749fa9..a111fdd9bcea9d679236510ad8e5fda1ab05fd7a 100644 --- a/testing/test.py +++ b/testing/test.py @@ -6,6 +6,9 @@ import numpy as np import matplotlib.pyplot as pl # get some data +from fastXPCS.algos import TTCdata, do_sparse_train +from fastXPCS.fxpcs_sparse import sparsify, sparsify2 +from PIL import Image dataShape = (352, 512, 128) photonEnergy = 13.3 # in ADU @@ -44,6 +47,7 @@ def getDummyData(shape, chanceOfPhotonPerPixel = 0.01, correlationPerBunch=0.5, p=[q**0, q**1, q**2, q**3, q**4, q**5, q**6]/np.sum([q**0, q**1, q**2, q**3, q**4, q**5, q**6])),(shape[1], shape[2])) def evolveImageFrom(image, c=correlationPerBunch): + print(c) randMask = np.reshape(np.random.choice( [0, 1], size=numPixelPrImage, @@ -54,7 +58,8 @@ def getDummyData(shape, chanceOfPhotonPerPixel = 0.01, correlationPerBunch=0.5, data[0,:,:] = np.reshape(getRandomImage(), (shape[1], shape[2])) for pulseNr in range(1,dataShape[0]): - data[pulseNr, :, :] = evolveImageFrom(data[pulseNr-1,:,:]) + correlationPerBunch += (1-correlationPerBunch)*0.001 + data[pulseNr, :, :] = evolveImageFrom(data[pulseNr-1,:,:], correlationPerBunch ) # if we do not returned photnoized data, then add a gaussian random variable if (~photonized ): @@ -70,15 +75,26 @@ data = getDummyData( correlationPerBunch=0.5, photonized=False, energy=photonEnergy, - sigma=0.12) + sigma=0.012) # function to do the xpcs the same way as in the xpcs class # in the xpcs calng addon # input : -image, the data set, e.g., data def xpcs(image, energy): - image /= 1 + + + _ttcdata = TTCdata() + _ttcdata.user_q_mask = np.ones(shape=(1,512,128)).astype(np.uint32) + _ttcdata.current_cal_mask = np.ones(shape=(1,512,128)).astype(np.uint32) + print(_ttcdata.user_q_mask.shape) + print(_ttcdata.current_cal_mask.shape) + _sparse_array = None + _cal_mask = None + + image /= energy image = np.around(image) + print(f" {np.sum(image==0)} num zeros {image.size}") flat_image_shape = image.reshape(image.shape[0], -1).shape print(flat_image_shape) @@ -90,24 +106,59 @@ def xpcs(image, energy): # lit, i.e., have a value larger than 0 lit_mask = reshaped_data > 0 # get the values of those that have a value larger than 0 - lit_pixels = reshaped_data[lit_mask].astype(np.uint32) + lit_pixels = np.ascontiguousarray(reshaped_data[lit_mask].astype(np.uint32)) # get the indices of those that - lit_indices = indices[:, lit_mask].astype(np.uint32) + lit_indices = np.ascontiguousarray(indices[:, lit_mask].astype(np.uint32)) # initalize a sparse array sparse_array = np.empty_like(lit_pixels, dtype=np.uint32) - # call function to fill sparse array with indices and value via bitpacking + # call function to fill sparse array with indices and value via bitpacking (from Felix) + # note that aliaksandr.leonau@xfel.eu mentioned that using build in sparse capabilities from + # numpy/scipy gives comparable or better results. sparsify2(lit_pixels, lit_indices, sparse_array) + image = image.astype(np.int32) + ## sparsifing again? + + start = time.time() + sparse_ref = sparsify(image.reshape(image.shape[0], -1)) + + _ttcdata.current_image = image.astype(np.int32) + _ttcdata.current_image_sparse = sparse_ref + _ttcdata.current_cal_mask = np.isnan(image) + + do_sparse_train(_ttcdata) + elapsed = time.time() - start + print(f"TTCF in: {elapsed:.3f}") + + output = _ttcdata.ttc_on.astype(np.int32) + return output + # do stuff t = time.time() -xpcs(data, photonEnergy) +out = xpcs(data, photonEnergy) elapsed = time.time() - t print(f"elapsed {elapsed}") - +print(out.shape) +print(np.min(out)) +print(np.max(out)) +def plot(data): + data = data.astype(np.float32) + cmap = pl.get_cmap('viridis') + norm = pl.Normalize(data.min(), data.max()) + print("blib") + # The norm instance scales data to a 0-1 range, cmap makes it RGB + rgb = cmap(norm(data)) + print("blub") + # MPL uses a 0-1 float RGB representation, so we'll scale to 0-255 + rgb = (255 * rgb).astype(np.uint8) + print("blab") + Image.fromarray(rgb).save('test.png') + +plot(out[0,:,:]) # cpu code with python from xpcs