From c62e3102d28015c45ea5cdc1616f09752de0937e Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 20 Nov 2024 12:11:28 +0100 Subject: [PATCH 1/6] Print failure bugfix --- ImageD11/nbGui/S3DXRD/run_astra_recon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ImageD11/nbGui/S3DXRD/run_astra_recon.py b/ImageD11/nbGui/S3DXRD/run_astra_recon.py index 8fc54b18..f89be004 100755 --- a/ImageD11/nbGui/S3DXRD/run_astra_recon.py +++ b/ImageD11/nbGui/S3DXRD/run_astra_recon.py @@ -9,7 +9,7 @@ def main(h5name, dsfile, group_name): print('Reconstructing grain sinograms') for inc, gs in enumerate(grainsinos): gs.recon(method="astra", astra_method="EM_CUDA") - print('Reconstructed ' + inc+1 + '/' + len(grainsinos)) + print('Reconstructed ' + str(inc+1) + '/' + str(len(grainsinos))) # mask recon after running print('Masking reconstructions') From e4e8b739101e3c9e4a8a15f4bb4ff85f296f24d4 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 20 Nov 2024 16:53:33 +0100 Subject: [PATCH 2/6] Improved powder/sx calibration notebooks --- ImageD11/nbGui/build_par_file.ipynb | 173 ----- ImageD11/nbGui/calibration/powder_calib.ipynb | 625 ++++++++++++++++++ .../{ => calibration}/silicon_calib.ipynb | 103 ++- ImageD11/nbGui/fit_cen_trans.ipynb | 125 ---- ImageD11/nbGui/peaksearch_CeO2_powder.ipynb | 491 -------------- 5 files changed, 712 insertions(+), 805 deletions(-) delete mode 100755 ImageD11/nbGui/build_par_file.ipynb create mode 100644 ImageD11/nbGui/calibration/powder_calib.ipynb rename ImageD11/nbGui/{ => calibration}/silicon_calib.ipynb (74%) delete mode 100644 ImageD11/nbGui/fit_cen_trans.ipynb delete mode 100644 ImageD11/nbGui/peaksearch_CeO2_powder.ipynb diff --git a/ImageD11/nbGui/build_par_file.ipynb b/ImageD11/nbGui/build_par_file.ipynb deleted file mode 100755 index dd45fcf9..00000000 --- a/ImageD11/nbGui/build_par_file.ipynb +++ /dev/null @@ -1,173 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "3ff7a14d-db7c-4c71-9de9-12bda564f827", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "from xfab.structure import build_atomlist" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "22ae1142-2cc4-40bf-801c-96a03ab82af1", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "import pyFAI" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "1c02aaae-6c95-4aaa-b2b8-93154688b999", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "2024-02-13 16:57:27,125 - xfab.structure - WARNING - No _atom_type_symbol found in CIF\n", - "WARNING:xfab.structure:No _atom_type_symbol found in CIF\n", - "2024-02-13 16:57:27,126 - xfab.structure - WARNING - No dispersion factors for Si in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for Si in cif file - set to zero\n", - "2024-02-13 16:57:27,126 - xfab.structure - WARNING - No dispersion factors for Si in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for Si in cif file - set to zero\n", - "2024-02-13 16:57:27,127 - xfab.structure - WARNING - No dispersion factors for Si in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for Si in cif file - set to zero\n", - "2024-02-13 16:57:27,127 - xfab.structure - WARNING - No dispersion factors for O in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for O in cif file - set to zero\n", - "2024-02-13 16:57:27,128 - xfab.structure - WARNING - No dispersion factors for O in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for O in cif file - set to zero\n", - "2024-02-13 16:57:27,128 - xfab.structure - WARNING - No dispersion factors for O in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for O in cif file - set to zero\n", - "2024-02-13 16:57:27,128 - xfab.structure - WARNING - No dispersion factors for O in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for O in cif file - set to zero\n", - "2024-02-13 16:57:27,129 - xfab.structure - WARNING - No dispersion factors for O in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for O in cif file - set to zero\n", - "2024-02-13 16:57:27,129 - xfab.structure - WARNING - No dispersion factors for O in cif file - set to zero\n", - "WARNING:xfab.structure:No dispersion factors for O in cif file - set to zero\n" - ] - } - ], - "source": [ - "# Import the CIF, save the results in unit_cell\n", - "\n", - "cif_path = 'SiO2_mp-7000_conventional_standard.cif'\n", - "\n", - "mylist = build_atomlist()\n", - "mylist.CIFread(cif_path)\n", - "unit_cell = mylist.atomlist.cell\n", - "cell_lattice = mylist.atomlist.sgname[0]" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "id": "7c8234be-a3d9-4695-bdac-56a613a78753", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# import calibration results from PONI\n", - "\n", - "poni_path = \"CeO2_20240210_1348_175mm.poni\"\n", - "poni_results = pyFAI.load(poni_path).getImageD11()" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "id": "e536b2ec-6047-4a0d-a342-f21bee268276", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# build parameter string\n", - "\n", - "par_string = f\"\"\"cell__a {unit_cell[0]}\n", - "cell__b {unit_cell[1]}\n", - "cell__c {unit_cell[2]}\n", - "cell_alpha {unit_cell[3]}\n", - "cell_beta {unit_cell[4]}\n", - "cell_gamma {unit_cell[5]}\n", - "cell_lattice_[P,A,B,C,I,F,R] {cell_lattice}\n", - "chi 0.0\n", - "distance {poni_results[\"distance\"]}\n", - "fit_tolerance 0.1\n", - "min_bin_prob 1e-05\n", - "no_bins 10000\n", - "o11 {poni_results[\"o11\"]}\n", - "o12 {poni_results[\"o12\"]}\n", - "o21 {poni_results[\"o21\"]}\n", - "o22 {poni_results[\"o22\"]}\n", - "omegasign 1.0\n", - "t_x 0\n", - "t_y 0\n", - "t_z 0\n", - "tilt_x {poni_results[\"tilt_x\"]}\n", - "tilt_y {poni_results[\"tilt_y\"]}\n", - "tilt_z {poni_results[\"tilt_z\"]}\n", - "wavelength {poni_results[\"wavelength\"]*10}\n", - "wedge 0.0\n", - "weight_hist_intensities 0\n", - "y_center {poni_results[\"y_center\"]}\n", - "y_size {poni_results[\"y_size\"]}\n", - "z_center {poni_results[\"z_center\"]}\n", - "z_size {poni_results[\"z_size\"]}\"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "id": "f3dde877-751f-438d-9c64-eb3c0da72c77", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# Save it to file\n", - "\n", - "par_path = \"output.par\"\n", - "with open(par_path, \"w\") as par_file:\n", - " par_file.writelines(par_string)" - ] - }, - { - "cell_type": "raw", - "id": "59c34763-f1b4-4881-91cf-2c1757e5184e", - "metadata": {}, - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (main)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.6" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/ImageD11/nbGui/calibration/powder_calib.ipynb b/ImageD11/nbGui/calibration/powder_calib.ipynb new file mode 100644 index 00000000..0533d665 --- /dev/null +++ b/ImageD11/nbGui/calibration/powder_calib.ipynb @@ -0,0 +1,625 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "bd148401-0fc3-480c-b62e-bce5edc7183d", + "metadata": {}, + "source": [ + "# Fit the far-field diffraction geometry using a powder calibrant with dx dy spatial files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1fbe01a0-bff9-48e5-8c09-c57ba21f139c", + "metadata": {}, + "outputs": [], + "source": [ + "exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())\n", + "PYTHONPATH = setup_ImageD11_from_git( ) # os.path.join( os.environ['HOME'],'Code'), 'ImageD11_git' )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c11df2b-ed9b-47b3-8166-e8f92fe42449", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import copy\n", + "import silx.io\n", + "import scipy.ndimage\n", + "import numpy as np\n", + "import fabio\n", + "import matplotlib.pyplot as plt\n", + "import h5py\n", + "import matplotlib\n", + "import ImageD11.columnfile\n", + "import ImageD11.unitcell\n", + "import ImageD11.transformer\n", + "import ImageD11.frelon_peaksearch\n", + "from ImageD11 import parameters\n", + "from ImageD11.cImageD11 import s_1, s_I, s_sI, s_fI\n", + "from ImageD11.nbGui import segmenter_gui\n", + "from ImageD11.nbGui import fit_geometry\n", + "%matplotlib ipympl" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "388f0e0c-a13a-4a6d-9601-1af79ba47550", + "metadata": {}, + "outputs": [], + "source": [ + "# Set up the file paths. Edit this if you are not at ESRF or not using the latest data policy.\n", + "dataroot, analysisroot = segmenter_gui.guess_ESRF_paths() \n", + "\n", + "if len(dataroot)==0:\n", + " print(\"Please fix in the dataroot and analysisroot folder names above!!\")\n", + " \n", + "print('dataroot =',repr(dataroot))\n", + "print('analysisroot =',repr(analysisroot))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c414a6f-8782-48a0-80dd-3d30879d3a93", + "metadata": {}, + "outputs": [], + "source": [ + "# List the samples available:\n", + "segmenter_gui.printsamples(dataroot)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3e25f1a9-86ca-4e07-889f-a31e47a2660b", + "metadata": {}, + "outputs": [], + "source": [ + "# USER: Decide which sample\n", + "sample = 'CeO2'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1720b7d-c065-4df4-a3d3-2974a5a6659a", + "metadata": {}, + "outputs": [], + "source": [ + "# List the datasets for that sample:\n", + "segmenter_gui.printdatasets( dataroot, sample )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66ee1f9f-eaa2-4417-99f5-f55cf40d2194", + "metadata": {}, + "outputs": [], + "source": [ + "# USER: Decide which dataset\n", + "dataset = \"rot\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f9ac02f-3380-4d3f-b5e3-7df8d2e25f05", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# USER: which detector are you using?\n", + "detector = 'frelon3'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28228c64-f8ab-44fb-b071-ba42f32589b0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# USER: Which scan is the calibration?\n", + "scan = '1.1'" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e32fccc1-2162-4cf8-9751-f5945a5bc11d", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "ds = ImageD11.sinograms.dataset.DataSet(dataroot=dataroot,\n", + " analysisroot=analysisroot,\n", + " sample=sample,\n", + " dset=dataset,\n", + " detector=detector)\n", + "\n", + "frames = silx.io.get_data( f\"silx:{ds.masterfile}::{scan}/measurement/{ds.detector}\")\n", + "print(frames.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc56ea91-4d31-4d6b-8ef9-5e484bc0a9d1", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# now specify where the dark scan is\n", + "# if we didn't collect one, change the below to False\n", + "\n", + "have_dark_scan = False\n", + "\n", + "if have_dark_scan:\n", + " \n", + " dark_sample = 'CeO2'\n", + " dark_dataset = \"dark\"\n", + " dark_scan = '1.1'\n", + " \n", + " ds_dark = ImageD11.sinograms.dataset.DataSet(dataroot=dataroot,\n", + " analysisroot=analysisroot,\n", + " sample=dark_sample,\n", + " dset=dark_dataset,\n", + " detector=detector)\n", + "\n", + " dark = silx.io.get_data(f\"silx:{ds_dark.masterfile}::{dark_scan}/measurement/{ds_dark.detector}\" )[:].mean(axis=0, dtype=np.float32)\n", + " print(dark.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5a40d445-7bc8-49ca-b114-80e57a68dcfa", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Try to determine which spatial calibration files to use depending on the detector\n", + "\n", + "if 'frelon' in detector:\n", + " serial = silx.io.get_data( f\"silx:{ds.masterfile}::{scan}/instrument/{detector}/camera_settings/camera_serial\" )\n", + " o11 = '1'\n", + " print(\"Detector serial\", serial )\n", + " if serial == \"21\":\n", + " flat = silx.io.get_data( f\"fabio:/data/id11/3dxrd/inhouse/Frelon21/F21_flat_july18.edf\" )\n", + " e2dxfile = \"/data/id11/3dxrd/inhouse/Frelon21/F21dxnew.edf\"\n", + " e2dyfile = \"/data/id11/3dxrd/inhouse/Frelon21/F21dynew.edf\"\n", + " maskfile = \"/data/id11/3dxrd/inhouse/Frelon21/F21_flat_Oct2016.msk\"\n", + " pixelsize = float(fabio.open(e2dxfile).header['PSize_1'].split()[0])*1e6 # microns\n", + "\n", + " elif serial == \"36\": \n", + " flat = silx.io.get_data( f\"fabio:/data/id11/3dxrd/inhouse/Frelon36/F36_Nov2023.edf\" )\n", + " flat = flat / flat[500:-500, 500:-500].mean()\n", + " e2dxfile = \"/data/id11/3dxrd/inhouse/Frelon36/f36dx.edf\"\n", + " e2dyfile = \"/data/id11/3dxrd/inhouse/Frelon36/f36dy.edf\"\n", + " maskfile = \"/data/id11/3dxrd/inhouse/Frelon36/frelon36_mask_20240117.edf\"\n", + " pixelsize = float(fabio.open(e2dxfile).header['PSize_1'].split()[0])*1e6 # microns\n", + "\n", + " elif serial == \"29\":\n", + " flat = silx.io.get_data( f\"fabio:/data/id11/3dxrd/inhouse/Frelon4M/c29_Agfilt_flat-dark.edf\" )\n", + " e2dxfile = \"/data/id11/3dxrd/inhouse/Frelon4M/F4M_EO_dx.edf\"\n", + " e2dyfile = \"/data/id11/3dxrd/inhouse/Frelon4M/F4M_EO_dy.edf\"\n", + " maskfile = \"/data/id11/3dxrd/inhouse/Frelon4M/c29_Agfilt_flat-dark-mask.edf\"\n", + " pixelsize = float(fabio.open(e2dxfile).header['PSize_1'].split()[0])*1e6 # microns\n", + " else:\n", + " print('Not sure what Frelon your detector is! You should manually provide the flat field, e2dxfile, e2dyfile, pixelsize, maskfile')\n", + " print('Pixel size', pixelsize )\n", + " print( \"Average flat, should be close to 1 : \",flat[100:-100, 100:-100].mean() )\n", + " \n", + " if have_dark_scan:\n", + " cor = ( frames - dark[ None, :, :] ).mean( axis = 0, dtype = np.float32 ) / flat\n", + " else:\n", + " cor = frames.mean( axis = 0, dtype = np.float32 ) / flat\n", + " \n", + "elif 'eiger' in detector: # eiger\n", + " cor = frames.mean( axis = 0, dtype = np.float32 )\n", + " o11 = '-1'\n", + " e2dxfile = \"/data/id11/nanoscope/Eiger/e2dx_E-08-0144_20240205.edf\"\n", + " e2dyfile = \"/data/id11/nanoscope/Eiger/e2dy_E-08-0144_20240205.edf\"\n", + " maskfile = \"/data/id11/nanoscope/Eiger/eiger_mask_E-08-0144_20240205.edf\"\n", + " pixelsize = float(silx.io.get_data(f\"silx:{ds.masterfile}::{scan}/instrument/{ds.detector}/x_pixel_size\" )) * 1e6 # microns\n", + "else:\n", + " print('Unknown detector! Must manually specify')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "49341b2b-b7dc-4b15-968e-469e0130a1ca", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def guess_median_bg( img, size=91 ):\n", + " m0 = scipy.ndimage.median_filter( cor, (size,1))\n", + " m1 = scipy.ndimage.median_filter( cor, (1,size))\n", + " bg = np.where(m0 lowcut ) )\n", + "\n", + "bg = guess_median_bg( cor )\n", + "\n", + "signal = ((cor - bg )*mask).clip(0,None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a91deedf-4f61-401b-adef-f9e32d82bbcb", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "f,(a,b) = plt.subplots( 2, 2, figsize=(8, 6), constrained_layout=True )\n", + "f.colorbar( a[1].imshow( cor * mask, norm='log', vmin=0.5, vmax=500, interpolation='nearest' ) )\n", + "f.colorbar( b[1].imshow( signal, norm='log', vmin=0.5, vmax=500, interpolation='nearest' ) )\n", + "a[1].set_title('Correction')\n", + "a[0].plot( (cor * mask)[ 1024, : ] )\n", + "a[0].plot( (cor * mask)[ :, 1024 ] )\n", + "a[0].set(xlabel='pixel', ylabel='intensity')\n", + "b[0].plot( signal[ 1024, : ] )\n", + "b[0].plot( signal[ :, 1024 ] )\n", + "b[1].set_title('Signal to peaksearch')\n", + "b[0].set(xlabel='pixel', ylabel='intensity', ylim=(0, None) );" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8bf70797-1bd2-42af-b1c4-337b08b3cc6f", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Old way, using command line:\n", + "# !powderimagetopeaks.py CeO2_for_S5_blue_rot_clean.edf CeO2_for_S5_blue_rot_clean0000.edf CeO2_for_S5_blue_rot_clean0001.edf 1024 1024\n", + "def powderimagetopeaks( img, ci = 1024, cj = 1024 ):\n", + " # cuts into 1 degree bins\n", + " si, sj = img.shape\n", + " i, j = np.mgrid[0:si,0:sj]\n", + " phi = np.arctan2( i - ci, j - cj ) * 361 / np.pi\n", + " return (phi%2).astype(int) == 0" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21201753-7e66-4cf5-ade9-78021460eabf", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "peak_mask = powderimagetopeaks(signal)\n", + "\n", + "worker = ImageD11.frelon_peaksearch.worker(None, None)\n", + "worker.threshold = 0\n", + "\n", + "pks = np.concatenate( (\n", + " worker.peaksearch( signal * peak_mask ), # even degrees\n", + " worker.peaksearch( signal * (1-peak_mask)), # odd degrees\n", + " ), axis = 0 )\n", + "print(f'Found {pks.shape[0]} peaks')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "29f1bca0-3896-432e-b421-f9fcebd9708e", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "colfile = ImageD11.columnfile.colfile_from_dict( {\n", + " \"s_raw\" : pks[ :, s_sI ] / pks[ :, s_I ],\n", + " \"f_raw\" : pks[ :, s_fI ] / pks[ :, s_I ],\n", + " \"Number_of_pixels\" : pks[ :, s_1 ], \n", + " \"sum_intensity\" : pks[ :, s_I ],\n", + " \"omega\" : np.zeros( len(pks), float ),\n", + "} )" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0363424c-a306-41ae-af0c-821b058b7a44", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "f, a = plt.subplots(1,2, figsize=(8,5), constrained_layout=True)\n", + "a[0].plot( colfile.f_raw, colfile.s_raw, \".\", ms = 1)\n", + "a[0].set( ylabel=\"detector slow direction\", xlabel=\"detector fast direction\", aspect='equal')\n", + "a[1].plot( colfile.Number_of_pixels, colfile.sum_intensity, \".\", ms =1 )\n", + "a[1].set( xlabel=\"Number of pixels\", ylabel=\"sum intensity\", yscale='log', xscale='log');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "234b7390-8007-4cd5-87cd-3dda68305a23", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "colfile = ImageD11.blobcorrector.eiger_spatial( e2dxfile, e2dyfile )(colfile)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff605eec-589d-4f53-b4cc-621a626836f0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def auto_guess_distance(masterfile, scan):\n", + " \"\"\"\n", + " Automatically guess the distance from the masterfile\n", + " \"\"\"\n", + " possible_distance_motors = ['ffdtx1', 'frelx']\n", + " distance_um = None\n", + " for mot in possible_distance_motors:\n", + " try:\n", + " distance_um = float(silx.io.get_data(f\"silx:{ds.masterfile}::{scan}/instrument/positioners/{mot}\" )) * 1e3 # microns\n", + " except ValueError:\n", + " continue\n", + " if distance_um is None:\n", + " raise ValueError(\"Couldn't find distance!\")\n", + " \n", + " return distance_um" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df82ca77-40c2-4aab-8dc9-632a14815420", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# guess the detector distance in um\n", + "# you can also manually specify\n", + "distance_guess = auto_guess_distance(ds.masterfile, scan) # detector distance in um\n", + "\n", + "# 'Ag': 25.514, 'Sn': 29.2001, 'Nd': 43.5689, 'Gd': 50.2391, 'Hf': 65.3508, 'W' : 69.525, 'Pt': 78.3948, 'Pb': 88.0045\n", + "# here enter the lattice parameters and spacegroup of the calibrant\n", + "# at ID11 we use CeO2 674b\n", + "\n", + "a_calibrant = 5.4115260\n", + "spacegroup_calibrant = 225\n", + "econst = 12.398423 # energy conversion - don't change\n", + "energy = 43.5689 # the energy in keV" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64349042-2503-4663-a0b7-416135bf2255", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# get initial parameters\n", + "\n", + "trans = ImageD11.transformer.transformer()\n", + "initial_parameters = trans.parameterobj\n", + "initial_parameters.set('cell__a', a_calibrant)\n", + "initial_parameters.set('cell__b', a_calibrant)\n", + "initial_parameters.set('cell__c', a_calibrant)\n", + "initial_parameters.set('cell_lattice_[P,A,B,C,I,F,R]', spacegroup_calibrant)\n", + "initial_parameters.set('distance', distance_guess)\n", + "initial_parameters.set('o11', int(o11))\n", + "initial_parameters.set('wavelength', econst/energy)\n", + "initial_parameters.set('y_size', pixelsize)\n", + "initial_parameters.set('z_size', pixelsize)\n", + "\n", + "initial_parameters.get_parameters()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "36f69f3b-3d6a-4b20-8ae4-8c9a3f686773", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "ui = ImageD11.nbGui.fit_geometry.FitGeom( )\n", + "ui.setfiltered(colfile)\n", + "ui.parameterobj = copy.deepcopy(initial_parameters)\n", + "ui.fitGui()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67ab3776-062f-429f-91e7-879610243db0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# now save the parameter file to disk\n", + "\n", + "final_par_file = 'powder_pars.par'\n", + "\n", + "ui.saveparameters(final_par_file)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dfe6e775-47fa-45a4-8baf-150d8f91b993", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "with open(final_par_file, 'r') as final_pars_in:\n", + " final_pars_string = final_pars_in.read()\n", + "\n", + "print(final_pars_string)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "72209985-dc15-46ee-a53f-09e84a0a8886", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# now we make a new-style json parameter file\n", + "# for now we just use the calibrant parameters\n", + "\n", + "geometry_par_file = 'geometry.par'\n", + "calibrant_par_file = 'calibrant.par'\n", + "\n", + "geometry_pars = '\\n'.join([line for line in final_pars_string.splitlines() if not line.startswith('cell')])\n", + "calibrant_pars = '\\n'.join([line for line in final_pars_string.splitlines() if line.startswith('cell')])\n", + "\n", + "with open(geometry_par_file, \"w\") as pars:\n", + " pars.write(geometry_pars)\n", + "\n", + "with open(calibrant_par_file, \"w\") as pars:\n", + " pars.write(calibrant_pars)\n", + "\n", + "json_par_file = 'pars.json'\n", + "\n", + "with open(json_par_file, \"w\") as pars:\n", + " pars.write(\"\"\"{\n", + " \"geometry\": {\n", + " \"file\":\"geometry.par\"\n", + " },\n", + " \"phases\": {\n", + " \"calibrant\": {\n", + " \"file\": \"calibrant.par\"\n", + " }\n", + " }\n", + "}\"\"\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73c7da13-4d48-4d21-917f-0fede00df2d0", + "metadata": {}, + "outputs": [], + "source": [ + "# prepare silicon start parameters\n", + "# for single-crystal calibration\n", + "\n", + "si_pars = parameters.read_par_file(json_par_file)\n", + "si_pars.set('cell__a', 5.43094) # the lattice spacing of Si\n", + "si_pars.set('cell__b', 5.43094)\n", + "si_pars.set('cell__c', 5.43094)\n", + "si_pars.set('cell_lattice_[P,A,B,C,I,F,R]', 227)\n", + "del si_pars.parameters['filename']\n", + "del si_pars.parameters['phase_name']\n", + "\n", + "si_start_par_file ='si_start.par'\n", + "si_pars.saveparameters(si_start_par_file)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3bf22454-7fab-4c86-9a69-0389589a6a3e", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (main)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/ImageD11/nbGui/silicon_calib.ipynb b/ImageD11/nbGui/calibration/silicon_calib.ipynb similarity index 74% rename from ImageD11/nbGui/silicon_calib.ipynb rename to ImageD11/nbGui/calibration/silicon_calib.ipynb index 2ff05707..29abac98 100755 --- a/ImageD11/nbGui/silicon_calib.ipynb +++ b/ImageD11/nbGui/calibration/silicon_calib.ipynb @@ -8,7 +8,7 @@ "\n", "Uses the older ImageD11 calibration fitting routines\n", "\n", - "Last updated 24/7/2024 by @jonwright" + "Last updated 20/11/2024 by @jadball" ] }, { @@ -20,13 +20,15 @@ "outputs": [], "source": [ "exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())\n", - "PYTHONPATH = setup_ImageD11_from_git( os.path.join( os.environ['HOME'],'git'), 'ImageD11_clean' )" + "PYTHONPATH = setup_ImageD11_from_git( ) # os.path.join( os.environ['HOME'],'Code'), 'ImageD11_git' )" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "%matplotlib ipympl\n", @@ -49,20 +51,34 @@ "outputs": [], "source": [ "# 'Ag': 25.514, 'Sn': 29.2001, 'Nd': 43.5689, 'Gd': 50.2391, 'Hf': 65.3508, 'W' : 69.525, 'Pt': 78.3948, 'Pb': 88.0045\n", + "# here enter the lattice parameters and spacegroup of the calibrant\n", + "# at ID11 we use NIST SRM SiO2 670c\n", + "\n", "a_silicon = 5.43094\n", - "econst = 12.398423\n", - "energy = 43.5689" + "econst = 12.398423 # energy conversion - don't change\n", + "energy = 43.5689 # the energy in keV" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# NOTE: before you run this notebook, you need to segment the silicon dataset\n", + "# use 0_segment_and_label.ipynb to do this" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "# Load some peaks for your silicone\n", - "# colf = ImageD11.columnfile.columnfile('Silicon/Silicon_100ms/Silicon_100ms_peaks_2d.h5')\n", - "dset = ImageD11.sinograms.dataset.load(\"WAu_siliconAttrz25_dataset.h5\")\n", + "# change this to the path to the Si dataset file in PROCESSED_DATA\n", + "\n", + "dset = ImageD11.sinograms.dataset.load(\"fakepath\")\n", "colf = dset.get_cf_2d()" ] }, @@ -82,7 +98,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "f, a = pl.subplots(1,2,figsize=(12,6), constrained_layout=True)\n", @@ -95,12 +113,61 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "colf.writefile(\"si.flt\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def auto_guess_distance(masterfile, scan):\n", + " \"\"\"\n", + " Automatically guess the distance from the masterfile\n", + " \"\"\"\n", + " possible_distance_motors = ['ffdtx1', 'frelx']\n", + " distance_um = None\n", + " for mot in possible_distance_motors:\n", + " try:\n", + " distance_um = float(silx.io.get_data(f\"silx:{ds.masterfile}::{scan}/instrument/positioners/{mot}\" )) * 1e3 # microns\n", + " except ValueError:\n", + " continue\n", + " if distance_um is None:\n", + " raise ValueError(\"Couldn't find distance!\")\n", + " \n", + " return distance_um" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# guess the detector distance in um\n", + "# you can also manually specify\n", + "distance_guess = auto_guess_distance(ds.masterfile, ds.scans[-1]) # detector distance in um\n", + "# distance_guess = 140000 # 140 mm" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, + "source": [ + "# Note: If you followed powder_calib.ipynb first, you should have a much better starting point than the below suggested!\n", + "# currently the below cell is only set up for the Eiger\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -118,7 +185,7 @@ "cell_gamma 90.0\n", "cell_lattice_[P,A,B,C,I,F,R] 227\n", "chi 0.0\n", - "distance 151000.0\n", + "distance {distance_guess}\n", "fit_tolerance 0.05\n", "o11 -1\n", "o12 0\n", @@ -133,9 +200,9 @@ "tilt_z 0.0\n", "wavelength {econst/energy}\n", "wedge 0.0\n", - "y_center 1050.4779244096353\n", + "y_center 1024.0\n", "y_size 75.0\n", - "z_center 1111.1724398511979\n", + "z_center 1024.0\n", "z_size 75.0\"\"\")" ] }, @@ -206,7 +273,7 @@ ] }, { - "cell_type": "raw", + "cell_type": "markdown", "metadata": {}, "source": [ "# TODO : modernize the rest ..." @@ -262,7 +329,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "!cp 0.par si.par\n", @@ -294,7 +363,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [] + }, "outputs": [], "source": [ "g = ImageD11.grain.read_grain_file('si.map')[0]\n", diff --git a/ImageD11/nbGui/fit_cen_trans.ipynb b/ImageD11/nbGui/fit_cen_trans.ipynb deleted file mode 100644 index e58629a1..00000000 --- a/ImageD11/nbGui/fit_cen_trans.ipynb +++ /dev/null @@ -1,125 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%matplotlib notebook\n", - "import ipywidgets, pylab as pl\n", - "from ImageD11 import transformer" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# input data from peaksearch and starting parameter file:\n", - "trans=transformer.transformer()\n", - "trans.loadfiltered(\"github/FABLE-3DXRD/ImageD11/test/nac_demo/peaks.out_merge_t200\")\n", - "trans.loadfileparameters(\"github/FABLE-3DXRD/ImageD11/test/nac_demo/nac.prm\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": false - }, - "outputs": [], - "source": [ - "def parCallBack( arg ):\n", - " trans.parameterobj.parameters.update( { arg['owner'].description : arg['new'] } )\n", - " drawPlot()\n", - " \n", - "def fixVaryCallBack( arg ):\n", - " name = arg['owner'].description.split(\" \")[1]\n", - " vars = trans.getvars()\n", - " if arg.new and not (name in vars):\n", - " vars.append( name )\n", - " if name in vars and not arg.new:\n", - " vars.remove(name)\n", - " trans.parameterobj.set_varylist(vars)\n", - " \n", - "def fitCallBack(arg):\n", - " \"\"\" fit call back - runs fit \"\"\"\n", - " lo, hi = ax1.get_xlim()\n", - " trans.fit( lo, hi )\n", - " for i, pname in enumerate(vars):\n", - " layout[i,0].value = trans.parameterobj.get(pname)\n", - " drawPlot()\n", - " \n", - "def drawPlot():\n", - " tth, eta = trans.compute_tth_eta()\n", - " pt1.set_data( tth, eta )\n", - "\n", - "# Things to be edited in the UI\n", - "vars = \"y_center z_center distance tilt_y tilt_z\".split()\n", - "steps = (1, 1, 100, 0.01, 0.01, 0)\n", - "nv = len(vars)\n", - "\n", - "# Draw the widgets:\n", - "layout = ipywidgets.GridspecLayout(nv+1,2)\n", - "for i,( pname, pstep ) in enumerate( zip( vars, steps ) ) :\n", - " layout[i,0] = ipywidgets.FloatText( description=pname, \n", - " value = trans.parameterobj.parameters.get(pname),\n", - " step=pstep)\n", - " layout[i,0].observe( parCallBack , names='value' )\n", - " layout[i,1] = ipywidgets.ToggleButton( description=\"Vary \"+pname, \n", - " value = pname in trans.getvars() )\n", - " layout[i,1].observe( fixVaryCallBack, names='value' )\n", - " \n", - "layout[nv,0] = ipywidgets.FloatText( description='fit_tolerance', \n", - " value = trans.parameterobj.parameters.get(\"fit_tolerance\"), step=0,)\n", - "layout[nv,0].observe( parCallBack , names='value' )\n", - "\n", - "layout[nv,1] = ipywidgets.Button(description=\"Run Fit (blocks)\")\n", - "layout[nv,1].on_click( fitCallBack )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": false - }, - "outputs": [], - "source": [ - "# Draw plot\n", - "fig1 = pl.figure(1, figsize=(9,6))\n", - "ax1 = fig1.add_subplot()\n", - "tth, eta = trans.compute_tth_eta()\n", - "trans.addcellpeaks()\n", - "pt1, = ax1.plot( tth, eta, \",\")\n", - "ax1.set(xlabel=\"tth\", ylabel=\"eta\")\n", - "ax1.plot( trans.theorytth, [0,]*len(trans.theorytth), \"r|\", ms=360, alpha=0.2 )\n", - "# Add controls\n", - "display(layout)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.3" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/ImageD11/nbGui/peaksearch_CeO2_powder.ipynb b/ImageD11/nbGui/peaksearch_CeO2_powder.ipynb deleted file mode 100644 index 5ce524cd..00000000 --- a/ImageD11/nbGui/peaksearch_CeO2_powder.ipynb +++ /dev/null @@ -1,491 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "07afc3da-8ce0-4717-ac4c-f2d746148cd4", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())\n", - "PYTHONPATH = setup_ImageD11_from_git( )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ed9d32b5-bf86-4633-9928-afae3df0226d", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "%matplotlib widget\n", - "import h5py, hdf5plugin\n", - "import numpy as np, matplotlib.pyplot as plt\n", - "import silx.io\n", - "import scipy.ndimage, fabio\n", - "import ImageD11.frelon_peaksearch\n", - "import ImageD11.blobcorrector\n", - "import ipywidgets\n", - "import ImageD11.transformer" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cbfde13c-0b61-440a-b105-27a30194cf0b", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "masterfile = \"/data/id11/3dxrd/ihma233/id11/CeO2/CeO2_align/CeO2_align.h5\"\n", - "scan = \"39.1\"\n", - "detector = \"frelon3\"\n", - "energy = 43.35\n", - "print(energy)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bc515a1e-23f9-4e71-a298-143fa872ad1c", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "darkscan = \"34.1\" # first 7 frames:\n", - "dark = silx.io.get_data( f\"silx:{masterfile}::{darkscan}/measurement/{detector}\" )[0:7].mean(axis=0, dtype=np.float32)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8e4ff2dc-5bd6-4ae2-9258-c19330d90c3c", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "frames = silx.io.get_data( f\"silx:{masterfile}::{scan}/measurement/{detector}\" )\n", - "frames.shape" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "39f5d07a-328a-4f0f-be5a-210222657c5a", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "if detector.find('frelon')>=0:\n", - " \n", - " serial = silx.io.get_data( f\"silx:{masterfile}::{darkscan}/instrument/{detector}/camera_settings/camera_serial\" )\n", - " print(\"Detector serial\", serial )\n", - " if serial == \"21\":\n", - " flat = silx.io.get_data( f\"fabio:/data/id11/3dxrd/inhouse/Frelon21/F21_flat_july18.edf\" )\n", - " e2dxfile = \"/data/id11/3dxrd/inhouse/Frelon21/F21dxnew.edf\"\n", - " e2dyfile = \"/data/id11/3dxrd/inhouse/Frelon21/F21dynew.edf\"\n", - " pixelsize = float(fabio.open(e2dxfile).header['PSize_1'].split()[0])*1e6 # microns\n", - " print('Pixel size', pixelsize )\n", - " if serial == \"36\": \n", - " flat = silx.io.get_data( f\"fabio:/data/id11/3dxrd/inhouse/Frelon36/F36_Nov2023.edf\" )\n", - " flat = flat / flat[500:-500, 500:-500].mean()\n", - " e2dxfile = \"fabio:/data/id11/3dxrd/inhouse/Frelon36/f36dx.edf\"\n", - " e2dyfile = f\"fabio:/data/id11/3dxrd/inhouse/Frelon36/f36dy.edf\"\n", - " pixelsize = float(fabio.open(e2dxfile).header['PSize_1'].split()[0])*1e6 # microns\n", - " if serial == \"29\":\n", - " flat = silx.io.get_data( f\"fabio:/data/id11/3dxrd/inhouse/Frelon4M/c29_Agfilt_flat-dark.edf\" )\n", - " e2dxfile = \"fabio:/data/id11/3dxrd/inhouse/Frelon4M/F4M_EO_dx.edf\"\n", - " e2dyfile = \"fabio:/data/id11/3dxrd/inhouse/Frelon4M/F4M_EO_dy.edf\"\n", - " pixelsize = float(fabio.open(e2dxfile).header['PSize_1'].split()[0])*1e6 # microns\n", - " \n", - " print( \"Average flat, should be close to 1 : \",flat[100:-100, 100:-100].mean() )\n", - " \n", - " cor = ( frames - dark[ None, :, :] ).mean( axis = 0, dtype = np.float32 ) / flat\n", - " \n", - "else: # eiger\n", - " \n", - " cor = frames.mean( axis = 0, dtype = np.float32 )\n", - " \n", - " e2dxfile = \"/data/id11/nanoscope/Eiger/e2dx_E-08-0144_20240205.edf\"\n", - " e2dyfile = \"/data/id11/nanoscope/Eiger/e2dy_E-08-0144_20240205.edf\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2d4d3e39-6615-41da-9a49-7c3687306957", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "def guess_median_bg( img, size=91 ):\n", - " m0 = scipy.ndimage.median_filter( cor, (size,1))\n", - " m1 = scipy.ndimage.median_filter( cor, (1,size))\n", - " bg = np.where(m0 lowcut )\n", - "\n", - "bg = guess_median_bg( cor )\n", - "\n", - "signal = ((cor - bg )*mask).clip(0,None)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "631b7295-1b53-4fac-994d-62967e242662", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "f,(a,b) = plt.subplots( 2, 2, figsize=(8, 6), constrained_layout=True )\n", - "f.colorbar( a[1].imshow( cor * mask ) )\n", - "f.colorbar( b[1].imshow( signal ) )\n", - "a[0].plot( cor[ 1024, : ] )\n", - "a[0].plot( cor[ :, 1024 ] )\n", - "a[0].set(xlabel='pixel', ylabel='intensity')\n", - "b[0].plot( signal[ 1024, : ] )\n", - "b[0].plot( signal[ :, 1024 ] )\n", - "b[0].set(xlabel='pixel', ylabel='intensity', ylim=(0, None) );" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0dd443d3-9be7-4c84-a1cc-502d3c4aab0a", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# Old way, using command line:\n", - "# !powderimagetopeaks.py CeO2_for_S5_blue_rot_clean.edf CeO2_for_S5_blue_rot_clean0000.edf CeO2_for_S5_blue_rot_clean0001.edf 1024 1024\n", - "def powderimagetopeaks( img, ci = 1024, cj = 1024 ):\n", - " # cuts into 1 degree bins\n", - " si, sj = img.shape\n", - " i, j = np.mgrid[0:si,0:sj]\n", - " phi = np.arctan2( i - ci, j - cj ) * 361 / np.pi\n", - " return (phi%2).astype(int) == 0" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dbc62cc4-03aa-4f33-a4a8-431b4e6fa00d", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# old way:\n", - "# !peaksearch.py -n CeO2_for_S5_blue_rot_clean -f 0 -l 1 -p N -s /data/id11/3dxrd/inhouse/Frelon36/frelon36.spline -t 5000 -t 10000 -t 20000 -t 40000 -o CeO2_for_S5_blue_rot_clean.spt " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ebe6849c-0b51-4ec2-95b8-2caefc9b75c5", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "worker = ImageD11.frelon_peaksearch.worker(None, None)\n", - "pkmsk = powderimagetopeaks( signal )\n", - "pks = np.concatenate( (\n", - " worker.peaksearch( signal * pkmsk ), # even degrees\n", - " worker.peaksearch( signal * (1-pkmsk)), # odd degrees\n", - " ) , axis = 0 )\n", - "pks.shape" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8d7cefae-e1f3-4bad-9a07-454b592a8ccc", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "from ImageD11.cImageD11 import s_1, s_I, s_sI, s_fI\n", - "colfile = ImageD11.columnfile.colfile_from_dict( {\n", - " \"s_raw\" : pks[ :, s_sI ] / pks[ :, s_I ],\n", - " \"f_raw\" : pks[ :, s_fI ] / pks[ :, s_I ],\n", - " \"Number_of_pixels\" : pks[ :, s_1 ], \n", - " \"sum_intensity\" : pks[ :, s_I ],\n", - " \"omega\" : np.zeros( len(pks), float ),\n", - "} )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1fa38247-7d08-45cd-aeec-4618ac178917", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "f, a = plt.subplots(1,2, figsize=(8,5), constrained_layout=True)\n", - "a[0].plot( colfile.f_raw, colfile.s_raw, \".\", ms = 1)\n", - "a[0].set( ylabel=\"detector slow direction\", xlabel=\"detector fast direction\", aspect='equal')\n", - "a[1].plot( colfile.Number_of_pixels, colfile.sum_intensity, \".\", ms =1 )\n", - "a[1].set( xlabel=\"Number of pixels\", ylabel=\"sum intensity\", yscale='log', xscale='log');" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2a543865-9505-4b25-bab3-f130546bcbff", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# old way\n", - "# !spatialfix.py frelon36_spline_dx.edf frelon36_spline_dy.edf CeO2_for_S5_blue_rot_clean_t5000.flt CeO2_for_S5_blue_rot_clean_spline_t5000.flt" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "abec9195-69d1-44f8-9238-e549ee68fbf6", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "colfile = ImageD11.blobcorrector.eiger_spatial( e2dxfile, e2dyfile )(colfile)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bad00d35-5618-4a79-969b-ef2328e74447", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# input data from peaksearch and starting parameter file:\n", - "trans=ImageD11.transformer.transformer()\n", - "trans.colfile = colfile\n", - "trans.setxyomcols(\"sc\", \"fc\", \"omega\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "62bc178a-d49a-4242-b4a6-9566c324fd6a", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "distance = 210 * 1e3 # micron\n", - "wavelength = 12.3984 / energy" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ce7a07c4-b353-4be7-989f-22a8150848f9", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "wavelength, energy" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f9ba5882-5236-47bf-b85c-0982c4db89c0", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "trans.parameterobj.set( 'y_size', pixelsize )\n", - "trans.parameterobj.set( 'z_size', pixelsize )\n", - "trans.parameterobj.set( 'distance', distance )\n", - "trans.parameterobj.set( 'wavelength', wavelength )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "639cd3aa-b35d-41a7-acb5-335042c68fc0", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# unit cell\n", - "a = 5.4115260\n", - "[ trans.parameterobj.set( f'cell__{x}', a ) for x in 'abc']\n", - "[ trans.parameterobj.set( f'cell_{x}', 90 ) for x in ('alpha','beta','gamma')]\n", - "trans.parameterobj.set('cell_lattice_[P,A,B,C,I,F,R]', \"F\" )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "20a04c19-5065-4edd-8241-df851a686c6d", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "def parCallBack( arg ):\n", - " trans.parameterobj.parameters.update( { arg['owner'].description : arg['new'] } )\n", - " drawPlot()\n", - " \n", - "def fixVaryCallBack( arg ):\n", - " name = arg['owner'].description.split(\" \")[1]\n", - " vars = trans.getvars()\n", - " if arg.new and not (name in vars):\n", - " vars.append( name )\n", - " if name in vars and not arg.new:\n", - " vars.remove(name)\n", - " trans.parameterobj.set_varylist(vars)\n", - " \n", - "def fitCallBack(arg):\n", - " \"\"\" fit call back - runs fit \"\"\"\n", - " lo, hi = ax1.get_xlim()\n", - " trans.fit( lo, hi )\n", - " for i, pname in enumerate(vars):\n", - " layout[i,0].value = trans.parameterobj.get(pname)\n", - " drawPlot()\n", - " \n", - "def drawPlot():\n", - " tth, eta = trans.compute_tth_eta()\n", - " pt1.set_data( tth, eta )\n", - " fig.canvas.update\n", - "\n", - "# Things to be edited in the UI\n", - "vars = \"y_center z_center distance tilt_y tilt_z\".split()\n", - "steps = (1, 1, 100, 0.01, 0.01, 0)\n", - "nv = len(vars)\n", - "\n", - "# Draw the widgets:\n", - "layout = ipywidgets.GridspecLayout(nv+1,2)\n", - "for i,( pname, pstep ) in enumerate( zip( vars, steps ) ) :\n", - " layout[i,0] = ipywidgets.FloatText( description=pname, \n", - " value = trans.parameterobj.parameters.get(pname),\n", - " step=pstep)\n", - " layout[i,0].observe( parCallBack , names='value' )\n", - " layout[i,1] = ipywidgets.ToggleButton( description=\"Vary \"+pname, \n", - " value = pname in trans.getvars() )\n", - " layout[i,1].observe( fixVaryCallBack, names='value' )\n", - " \n", - "layout[nv,0] = ipywidgets.FloatText( description='fit_tolerance', \n", - " value = trans.parameterobj.parameters.get(\"fit_tolerance\"), step=0,)\n", - "layout[nv,0].observe( parCallBack , names='value' )\n", - "\n", - "layout[nv,1] = ipywidgets.Button(description=\"Run Fit (blocks)\")\n", - "layout[nv,1].on_click( fitCallBack )\n", - "\n", - "\n", - "# Draw plot\n", - "fig1 = plt.figure(figsize=(9,6))\n", - "ax1 = fig1.add_subplot()\n", - "tth, eta = trans.compute_tth_eta()\n", - "trans.addcellpeaks()\n", - "pt1, = ax1.plot( tth, eta, \",\")\n", - "ax1.set(xlabel=\"tth\", ylabel=\"eta\")\n", - "ax1.plot( trans.theorytth, [0,]*len(trans.theorytth), \"r|\", ms=360, alpha=0.2 )\n", - "# Add controls\n", - "display(layout)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "36bbea15-894b-4424-abe9-1e34702da583", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "import pprint\n", - "pprint.pprint( trans.parameterobj.parameters )" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c0c4bd4e-b7d6-4234-8442-283f314c6a54", - "metadata": {}, - "outputs": [], - "source": [ - "trans.parameterobj.saveparameters('CeO2_fitted.par')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f2a17baf-133e-4a87-b900-8448bccc0eee", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "12.3985 / trans.parameterobj.get('wavelength')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a29f6ee3-95f5-4e97-b03b-4fdd6deb4c71", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (main)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.6" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 548b205ae8538955a0db198825d98f6f93f08e23 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 20 Nov 2024 16:53:56 +0100 Subject: [PATCH 3/6] Handle zero scans in size calculation --- ImageD11/sinograms/dataset.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ImageD11/sinograms/dataset.py b/ImageD11/sinograms/dataset.py index 9844fc4d..671675b8 100644 --- a/ImageD11/sinograms/dataset.py +++ b/ImageD11/sinograms/dataset.py @@ -436,6 +436,10 @@ def guess_shape(self): elif len(self.scans) > 1: s0 = len(self.scans) s1 = npts // s0 + else: + # no scans + s0 = 0 + s1 = 0 self.shape = s0, s1 if np.prod(self.shape) != npts: print("Warning: irregular scan - might be bugs in here") From 39dfae0fbc011a6dfaa0031208c531406f4cadb6 Mon Sep 17 00:00:00 2001 From: James Ball Date: Wed, 20 Nov 2024 16:54:52 +0100 Subject: [PATCH 4/6] New setfiltered method for transformer so we can use colfiles in memory --- ImageD11/transformer.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/ImageD11/transformer.py b/ImageD11/transformer.py index 9aff6c3d..fdf03f88 100644 --- a/ImageD11/transformer.py +++ b/ImageD11/transformer.py @@ -212,12 +212,12 @@ def getvars(self): def setvars(self, varlist): """ set the things to refine """ self.parameterobj.varylist = varlist - - def loadfiltered(self, filename): + + def setfiltered(self, colfile): """ - Read in 3D peaks from peaksearch + Set self.colfile as colfile """ - self.colfile = columnfile.columnfile(filename) + self.colfile = colfile if ( ("sc" in self.colfile.titles) and ("fc" in self.colfile.titles) and ("omega" in self.colfile.titles)): @@ -229,6 +229,13 @@ def loadfiltered(self, filename): if "spot3d_id" not in self.colfile.titles: self.colfile.addcolumn(list(range(self.colfile.nrows)), "spot3d_id") + + def loadfiltered(self, filename): + """ + Read in 3D peaks from peaksearch + """ + colfile = columnfile.columnfile(filename) + self.setfiltered(colfile) def setxyomcols(self, xname, yname, omeganame): self.xname = xname From e2ca1e4a79d80e107f073b8ecc09fce87300f867 Mon Sep 17 00:00:00 2001 From: James Ball Date: Thu, 21 Nov 2024 08:09:15 +0100 Subject: [PATCH 5/6] Fix typo --- ImageD11/nbGui/calibration/powder_calib.ipynb | 2 +- ImageD11/nbGui/calibration/silicon_calib.ipynb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ImageD11/nbGui/calibration/powder_calib.ipynb b/ImageD11/nbGui/calibration/powder_calib.ipynb index 0533d665..af7183f2 100644 --- a/ImageD11/nbGui/calibration/powder_calib.ipynb +++ b/ImageD11/nbGui/calibration/powder_calib.ipynb @@ -427,7 +427,7 @@ " distance_um = None\n", " for mot in possible_distance_motors:\n", " try:\n", - " distance_um = float(silx.io.get_data(f\"silx:{ds.masterfile}::{scan}/instrument/positioners/{mot}\" )) * 1e3 # microns\n", + " distance_um = float(silx.io.get_data(f\"silx:{masterfile}::{scan}/instrument/positioners/{mot}\" )) * 1e3 # microns\n", " except ValueError:\n", " continue\n", " if distance_um is None:\n", diff --git a/ImageD11/nbGui/calibration/silicon_calib.ipynb b/ImageD11/nbGui/calibration/silicon_calib.ipynb index 29abac98..e8d7b1d2 100755 --- a/ImageD11/nbGui/calibration/silicon_calib.ipynb +++ b/ImageD11/nbGui/calibration/silicon_calib.ipynb @@ -137,7 +137,7 @@ " distance_um = None\n", " for mot in possible_distance_motors:\n", " try:\n", - " distance_um = float(silx.io.get_data(f\"silx:{ds.masterfile}::{scan}/instrument/positioners/{mot}\" )) * 1e3 # microns\n", + " distance_um = float(silx.io.get_data(f\"silx:{masterfile}::{scan}/instrument/positioners/{mot}\" )) * 1e3 # microns\n", " except ValueError:\n", " continue\n", " if distance_um is None:\n", From 09f4c70c6a08a246cad8db044ef85fbb5da5089f Mon Sep 17 00:00:00 2001 From: James Ball Date: Thu, 21 Nov 2024 17:04:33 +0100 Subject: [PATCH 6/6] Fix y0 bug, a bit of cleanup --- .../S3DXRD/select_for_index_unknown.ipynb | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/ImageD11/nbGui/S3DXRD/select_for_index_unknown.ipynb b/ImageD11/nbGui/S3DXRD/select_for_index_unknown.ipynb index 9e11d6cc..a356754d 100644 --- a/ImageD11/nbGui/S3DXRD/select_for_index_unknown.ipynb +++ b/ImageD11/nbGui/S3DXRD/select_for_index_unknown.ipynb @@ -90,7 +90,7 @@ }, "outputs": [], "source": [ - "ds.parfile = aroot + \"/LMGO_pseudo_cubic.par\"\n", + "# ds.parfile = aroot + \"/LMGO_pseudo_cubic.par\"\n", "cf_4d = ds.get_cf_4d()\n", "ds.update_colfile_pars( cf_4d )" ] @@ -98,12 +98,20 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "tags": [] - }, + "metadata": {}, "outputs": [], "source": [ - "\" \".join(cf_4d.titles)" + "ds.phases = ds.get_phases_from_disk()\n", + "ds.phases.unitcells" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "major_phase_str = 'Al'" ] }, { @@ -129,9 +137,8 @@ }, "outputs": [], "source": [ - "# fixme - removing the main phase - could skip this part if there is no main phase\n", - "a = 3.90725\n", - "ucell = ImageD11.unitcell.unitcell( [2*a, 2*a, 2*a, 90, 90, 90], \"P\")\n", + "ucell = ds.phases.unitcells[major_phase_str]\n", + "\n", "ucell.makerings( dsbinedges[-1] )\n", "ucell.lattice_parameters" ] @@ -269,7 +276,8 @@ " return pair\n", "\n", "om = np.linspace( cring.omega.min(), cring.omega.max(), 90 )\n", - "fitline, = ax[1].plot( om, np.zeros_like(om), '-')\n", + "fitlinel, = ax[0].plot( om, np.zeros_like(om), '-')\n", + "fitliner, = ax[1].plot( om, np.zeros_like(om), '-')\n", "\n", "### GLOBALS, also y0\n", "p1 = None # index of first peak in cring\n", @@ -305,7 +313,8 @@ " [ -sj, -cj] ] )\n", " xy = x, y = np.linalg.inv(R).dot( [ yi, yj ] )\n", " ycalc = ImageD11.sinograms.geometry.x_y_y0_omega_to_dty(om, x, y, y0)\n", - " fitline.set_ydata( ycalc )\n", + " fitlinel.set_ydata( ycalc )\n", + " fitliner.set_ydata( ycalc )\n", " peak_ycalc = ImageD11.sinograms.geometry.x_y_y0_omega_to_dty(cring.omega, x, y, y0)\n", " \n", "def onpick( evt ):\n", @@ -523,8 +532,8 @@ "sino, obinedge, ybinedge = np.histogram2d( cring.omega, cring.dty, weights = np.log(cring.sum_intensity),\n", " bins = (np.linspace(ds.obinedges.min(), ds.obinedges.max(), nbins_angle), ds.ybinedges) )\n", "obincen = 0.5*(obinedge[:-1] + obinedge[1:])\n", - "recon = ImageD11.sinograms.roi_iradon.iradon( sino.T, obincen, filter_name='shepp-logan', \n", - " workers=ImageD11.cImageD11.cores_available())" + "recon = ImageD11.sinograms.roi_iradon.run_iradon(sino.T, obincen, filter_name='shepp-logan', \n", + " workers=ImageD11.cImageD11.cores_available(), shift=-y0/ds.ystep, pad=0)" ] }, { @@ -557,10 +566,10 @@ " if evt.inaxes == ax[1]:\n", " y = evt.xdata\n", " x = evt.ydata\n", - " ycalc = ImageD11.sinograms.geometry.dtycalc( om, x, y, 0 )\n", + " ycalc = ImageD11.sinograms.geometry.dtycalc( om, x, y, y0 )\n", " global ycalcall, pos\n", " pos = y,x\n", - " ycalcall = ImageD11.sinograms.geometry.dtycalc( cring.omega, x, y, 0 )\n", + " ycalcall = ImageD11.sinograms.geometry.dtycalc( cring.omega, x, y, y0 )\n", " fitline1.set_ydata( ycalc + 1 )\n", " fitline2.set_ydata( ycalc - 1 )\n", " location.set_xdata( [y,] )\n", @@ -664,6 +673,8 @@ "for c in cells:\n", " plt.plot( c.ringds, np.full( len(c.ringds), o) ,\"|\", ms=10, lw =.1, label=(\"%.4f \"*6)%tuple(c.lattice_parameters) )\n", " o *= 2\n", + "o *= 2\n", + "plt.plot( ucell.ringds, np.full( len(ucell.ringds), o) ,\"|\", ms=10, lw =.1, label=(\"%.4f \"*6)%tuple(ucell.lattice_parameters) )\n", "plt.semilogy()\n", "plt.xlim(0.1,0.6)\n", "plt.legend(loc='upper left');"