Skip to content

Commit

Permalink
Merge pull request #402 from jadball/master
Browse files Browse the repository at this point in the history
Parameterise box-beam notebooks with papermill
  • Loading branch information
jadball authored Feb 17, 2025
2 parents 4427139 + 2d51719 commit 53147d3
Show file tree
Hide file tree
Showing 10 changed files with 2,355 additions and 883 deletions.
461 changes: 461 additions & 0 deletions ImageD11/nbGui/TDXRD/0_segment_frelon.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,66 @@
},
"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' )"
"exec(open('/data/id11/nanoscope/install_ImageD11_from_git.py').read())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"parameters"
]
},
"outputs": [],
"source": [
"# this cell is tagged with 'parameters'\n",
"# to view the tag, select the cell, then find the settings gear icon (right or left sidebar) and look for Cell Tags\n",
"\n",
"PYTHONPATH = setup_ImageD11_from_git( ) # ( os.path.join( os.environ['HOME'],'Code'), 'ImageD11_git' )\n",
"\n",
"# desination of H5 files\n",
"# replace below with e.g.:\n",
"# dset_path = '/data/visitor/expt1234/20240101/PROCESSED_DATA/sample/dataset/sample_dataset.h5'\n",
"\n",
"dset_path = ''\n",
"\n",
"phase_str = 'Fe'\n",
"\n",
"# path to parameters .json/.par\n",
"parfile = ''\n",
"\n",
"# peak filtration options\n",
"cf_strong_frac = 0.9837\n",
"cf_strong_dsmax = 1.01\n",
"cf_strong_dstol = 0.01\n",
"\n",
"# indexing options\n",
"indexer_ds_tol = 0.01\n",
"\n",
"# we have to choose which rings we want to generate orientations on\n",
"# generally we want two or three low-multiplicity rings that are isolated from other phases\n",
"# take a look at the ring assignment output from a few cells above, and choose two or three\n",
"rings_for_gen = [0, 1]\n",
"\n",
"# now we want to decide which rings to score our found orientations against\n",
"# generally we can just exclude dodgy rings (close to other phases, only a few peaks in etc)\n",
"rings_for_scoring = [0, 1, 2, 3]\n",
"\n",
"# the sequence of hkl tolerances the indexer will iterate through\n",
"hkl_tols_seq = [0.01, 0.02, 0.03, 0.04]\n",
"# the sequence of minpks fractions the indexer will iterate through\n",
"fracs = [0.9, 0.75]\n",
"# the max number of UBIs we can find per pair of rings\n",
"max_grains = 1000\n",
"\n",
"# makemap parameters\n",
"makemap_hkl_tol_seq = [0.05, 0.025, 0.01]\n",
"symmetry = \"cubic\"\n",
"\n",
"absolute_minpks = 120\n",
"\n",
"dset_prefix = 'ff'"
]
},
{
Expand Down Expand Up @@ -59,21 +117,6 @@
"from ImageD11.peakselect import select_ring_peaks_by_intensity"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# desination of H5 files\n",
"# replace below with e.g.:\n",
"# dset_path = '/data/visitor/expt1234/20240101/PROCESSED_DATA/sample/dataset/sample_dataset.h5'\n",
"\n",
"dset_path = ''"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -87,11 +130,24 @@
"ds = ImageD11.sinograms.dataset.load(dset_path)\n",
"sample = ds.sample\n",
"dataset = ds.dset\n",
"rawdata_path = ds.dataroot\n",
"processed_data_root_dir = ds.analysisroot\n",
"\n",
"print(ds)\n",
"print(ds.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# also set our parameters for indexing\n",
"ds.parfile = parfile\n",
"ds.save()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -111,7 +167,6 @@
"outputs": [],
"source": [
"# now let's select a phase to index from our parameters json\n",
"phase_str = 'Fe'\n",
"\n",
"ucell = ds.phases.unitcells[phase_str]\n",
"\n",
Expand Down Expand Up @@ -175,10 +230,6 @@
"# if the blue line does not look elbow-shaped in the logscale plot, try changing the \"doplot\" parameter (the y scale of the logscale plot) until it does\n",
"\n",
"\n",
"cf_strong_frac = 0.9837\n",
"cf_strong_dsmax = 1.01\n",
"cf_strong_dstol = 0.01\n",
"\n",
"cf_strong = select_ring_peaks_by_intensity(cf_3d, frac=cf_strong_frac, dsmax=cf_strong_dsmax, doplot=0.8, dstol=cf_strong_dstol)\n",
"print(f\"Got {cf_strong.nrows} strong peaks for indexing\")\n",
"cf_strong_path = f'{sample}_{dataset}_3d_peaks_strong.flt'\n",
Expand Down Expand Up @@ -246,7 +297,6 @@
"\n",
"# USER: set a tolerance in d-space (for assigning peaks to powder rings)\n",
"\n",
"indexer_ds_tol = 0.05\n",
"indexer.ds_tol = indexer_ds_tol\n",
"\n",
"# change the log level so we can see what the ring assigments look like\n",
Expand Down Expand Up @@ -292,23 +342,9 @@
"outputs": [],
"source": [
"# now we are indexing!\n",
"# we have to choose which rings we want to generate orientations on\n",
"# generally we want two or three low-multiplicity rings that are isolated from other phases\n",
"# take a look at the ring assignment output from a few cells above, and choose two or three\n",
"rings_for_gen = [0, 1]\n",
"\n",
"# now we want to decide which rings to score our found orientations against\n",
"# generally we can just exclude dodgy rings (close to other phases, only a few peaks in etc)\n",
"rings_for_scoring = [0, 1, 2, 3]\n",
"\n",
"# the sequence of hkl tolerances the indexer will iterate through\n",
"hkl_tols_seq = [0.01, 0.02, 0.03, 0.04]\n",
"# the sequence of minpks fractions the indexer will iterate through\n",
"fracs = [0.9, 0.75]\n",
"# the tolerance in g-vector angle\n",
"cosine_tol = np.cos(np.radians(90 - ds.ostep))\n",
"# the max number of UBIs we can find per pair of rings\n",
"max_grains = 1000\n",
"\n",
"grains, indexer = utils.do_index(cf=cf_strong,\n",
" dstol=indexer.ds_tol,\n",
Expand Down Expand Up @@ -359,33 +395,9 @@
"tmp_map_path = f'{sample}_{dataset}_grains.map'\n",
"\n",
"new_flt_path = f'{sample}_{dataset}_3d_peaks_strong_all_rings.flt.new' # flt file containing assignments from makemap\n",
"unindexed_flt_path = f'{sample}_{dataset}_3d_peaks_strong_all_rings.flt.unindexed' # remaining unassigned peaks from makemap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"ImageD11.grain.write_grain_file(tmp_ubi_path, grains)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"omegas_sorted = np.sort(ds.omega)[0]\n",
"omega_step = np.round(np.diff(omegas_sorted).mean(), 3)\n",
"omega_slop = omega_step/2\n",
"unindexed_flt_path = f'{sample}_{dataset}_3d_peaks_strong_all_rings.flt.unindexed' # remaining unassigned peaks from makemap\n",
"\n",
"makemap_hkl_tol_seq = [0.05, 0.025, 0.01]"
"ImageD11.grain.write_grain_file(tmp_ubi_path, grains)"
]
},
{
Expand Down Expand Up @@ -416,7 +428,9 @@
},
"outputs": [],
"source": [
"symmetry = \"cubic\"\n",
"# now run makemap in a loop\n",
"\n",
"omega_slop = ds.ostep/2\n",
"\n",
"for inc, makemap_tol in enumerate(makemap_hkl_tol_seq):\n",
" print(f\"Running makemap {inc+1}/{len(makemap_hkl_tol_seq)}\")\n",
Expand All @@ -426,6 +440,15 @@
" makemap_output = !makemap.py -p {oldparfile} -u {tmp_map_path} -U {tmp_map_path} -f {cf_strong_allrings_path} -F {unindexed_flt_path} -s {symmetry} -t {makemap_hkl_tol_seq[inc]} --omega_slop={omega_slop} --no_sort"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"utils.plot_grain_histograms(new_flt_path, tmp_map_path, oldparfile, omega_slop, tol=makemap_hkl_tol_seq[-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -491,19 +514,11 @@
},
"outputs": [],
"source": [
"# find the spike\n",
"absolute_minpks = 120"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# filter out grains with fewer than 15 peaks\n",
"# filter out grains with fewer than absolute_minpks peaks\n",
"\n",
"# most grains should have a high number of peaks\n",
"# choose absolute_minpks such that the low-peak grains are removed\n",
"\n",
"grains_filtered = [grain for grain in grains2 if float(grain.npks) > absolute_minpks]"
]
},
Expand Down Expand Up @@ -551,6 +566,15 @@
"grains_filtered = ImageD11.grain.read_grain_file(map_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"utils.plot_grain_histograms(final_new_flt_path, map_path, oldparfile, omega_slop, tol=makemap_hkl_tol_seq[-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -643,37 +667,27 @@
"metadata": {},
"outputs": [],
"source": [
"# change to 0 to allow all cells to be run automatically\n",
"if 1:\n",
" raise ValueError(\"Hello!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now that we are happy with our indexing parameters, we can run the below cell to do this in bulk for many samples/datasets\n",
"# Now that we're happy with our indexing parameters, we can run the below cell to do this in bulk for many samples/datasets\n",
"# by default this will do all samples in sample_list, all datasets with a prefix of dset_prefix\n",
"# you can add samples and datasets to skip in skips_dict\n",
"\n",
"# you can optionally skip samples\n",
"# skips_dict = {\n",
"# \"FeAu_0p5_tR\": [\"ff6\",]\n",
"# }\n",
"# otherwise by default skip nothing:\n",
"skips_dict = {\n",
" \"FeAu_0p5_tR\": []\n",
" ds.sample: []\n",
"}\n",
"\n",
"# USER: Change this prefix to match the naming convention of your datasets\n",
"# e.g if your slices are formatted like [\"ff_z0\", \"ff_z1\", \"ff_z2\"] etc, then the prefix could be \"ff\" or \"ff_z\"\n",
"sample_list = [ds.sample, ]\n",
"\n",
"dset_prefix = \"ff\"\n",
"samples_dict = utils.find_datasets_to_process(rawdata_path, skips_dict, dset_prefix, sample_list)\n",
"\n",
"sample_list = [\"FeAu_0p5_tR\"]\n",
" \n",
"samples_dict = utils.find_datasets_to_process(ds.dataroot, skips_dict, dset_prefix, sample_list)\n",
"print(samples_dict)\n",
"\n",
"# manual override example:\n",
"# samples_dict = {\"FeAu_0p5_tR\": [\"ff1\", \"ff2\"]}"
" \n",
"# manual override:\n",
"# samples_dict = {\"FeAu_0p5_tR_nscope\": [\"top_100um\", \"top_150um\"]}"
]
},
{
Expand All @@ -698,7 +712,10 @@
" print(f\"Found existing grains file for {dataset} in {sample}, skipping\")\n",
" continue\n",
" \n",
" ds.parfile = parfile\n",
" ds.save()\n",
" ds.phases = ds.get_phases_from_disk()\n",
" \n",
" ucell = ds.phases.unitcells[phase_str]\n",
" sample = ds.sample\n",
" dataset = ds.dset\n",
Expand Down
Loading

0 comments on commit 53147d3

Please sign in to comment.