Skip to content

Commit 55ffd31

Browse files
TKD_DED
Adding routines for transmission kikuchi diffraction / direct electron diffraction analysis paper. Work by Tianbi Zhang and Ben Britton (UBC)
1 parent bdc3d67 commit 55ffd31

15 files changed

Lines changed: 1775 additions & 1 deletion

decks/AstroEBSD_PatternMatch_2021.m

Lines changed: 494 additions & 0 deletions
Large diffs are not rendered by default.

decks/TKD_HDR_pattern_astro.m

Lines changed: 374 additions & 0 deletions
Large diffs are not rendered by default.

decks/TKD_HDR_stereo_reproject.m

Lines changed: 668 additions & 0 deletions
Large diffs are not rendered by default.

gen/loading/h5_pixet.m

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
function [rawdata,numfiles] = h5_pixet(h5filenames,path1,multframe_option,multframe_num)
2+
%H5_PIXET read patterns from a h5 pixet output
3+
% (c) Tianbi Zhang and Ben Britton, 2023
4+
5+
[~,numfiles] = size(h5filenames);
6+
7+
rawdata = zeros(254^2,numfiles);
8+
9+
for i=1:numfiles
10+
filename = fullfile(path1, h5filenames{i});
11+
if exist(filename,'file') == false
12+
error(['The input file ' filename ' cannot be found']);
13+
end
14+
15+
if multframe_option == true
16+
rawdata(:,i) = rawhdfmult(filename, multframe_num);
17+
else
18+
if i~=numfiles
19+
rawdata(:,i) = rawhdfint(filename) ./ multframe_num; % due to data formet issue, use a special function here
20+
else
21+
rawdata(:,i) = rawhdfint_special(filename) ./ multframe_num;
22+
end
23+
end
24+
25+
end
26+
27+
end
28+
29+
function event_data_vector = rawhdfint(h5filename)
30+
31+
% Read width and height values from the HDF5 file. Both should be 256
32+
% because of the DED geometry
33+
width = h5read(h5filename,'/Frame_0/Width');
34+
height = h5read(h5filename,'/Frame_0/Height');
35+
36+
% Extract pixel-by-pixel intensity from the "Event" and "iTOT" data.
37+
% Change "Event" to "iToT", "ToT", "ToA" etc. based on data structure.
38+
% Please check the file under HDF5 viewer or use h5info() first.
39+
event_data = h5read(h5filename,'/Frame_0//SubFrames/Event/Data');
40+
event_data_matrix = double(reshape(event_data,width,height));
41+
42+
event_data_matrix = event_data_matrix';
43+
event_data_matrix = imcrop(event_data_matrix, [2 2 253 253]);
44+
45+
%TZ QUERY
46+
%what are these pixels?
47+
pix_cleanxval=[11,26,240,44];
48+
pix_cleanyval=[60,208,22,47];
49+
event_data_matrix=pix_clean(event_data_matrix,pix_cleanxval,pix_cleanyval);
50+
51+
event_data_vector = double(reshape(event_data_matrix, [],1));
52+
53+
end
54+
55+
function event_data_vector = rawhdfint_special(h5filename)
56+
% Read width and height values from the HDF5 file. Both should be 256
57+
% because of the DED geometry
58+
width = h5read(h5filename,'/Frame_0/Width');
59+
height = h5read(h5filename,'/Frame_0/Height');
60+
61+
% Extract pixel-by-pixel intensity from the "Event" and "iTOT" data.
62+
% Change "Event" to "iToT", "ToT", "ToA" etc. based on data structure.
63+
% Please check the file under HDF5 viewer or use h5info() first.
64+
event_data = h5read(h5filename,'/Frame_0/Data');
65+
event_data_matrix = double(reshape(event_data,width,height));
66+
67+
event_data_matrix = event_data_matrix';
68+
event_data_matrix = imcrop(event_data_matrix, [2 2 253 253]);
69+
70+
pix_cleanxval=[11,26,240,44];
71+
pix_cleanyval=[60,208,22,47];
72+
event_data_matrix=pix_clean(event_data_matrix,pix_cleanxval,pix_cleanyval);
73+
74+
event_data_vector = double(reshape(event_data_matrix, [],1));
75+
end
76+
77+
function h5matrix=pix_clean(h5matrix,xval,yval)
78+
%clean specific pixels fro the array to 0
79+
for n=1:numel(xval)
80+
h5matrix(xval(n),yval(n)) = 0;
81+
end
82+
83+
end
84+
85+
86+
function event_data_vector = rawhdfmult(h5filename, framenum)
87+
width = h5read(h5filename,'/Frame_0/Width');
88+
height = h5read(h5filename,'/Frame_0/Height');
89+
90+
event_data = double(zeros(65536,1));
91+
92+
for i=0:(framenum-1)
93+
frame_event_key = strcat('/Frame_',num2str(i),'/SubFrames/Event/Data');
94+
event_data_this_frame = double(h5read(h5filename, frame_event_key));
95+
96+
event_data = event_data + event_data_this_frame;
97+
end
98+
99+
event_data_matrix = double(reshape(event_data,width,height));
100+
101+
event_data_matrix = event_data_matrix';
102+
event_data_matrix = imcrop(event_data_matrix, [2 2 253 253]);
103+
104+
%TZ QUERY
105+
pix_cleanxval=[11,26,240,44];
106+
pix_cleanyval=[60,208,22,47];
107+
event_data_matrix=pix_clean(event_data_matrix,pix_cleanxval,pix_cleanyval);
108+
109+
110+
event_data_matrix = event_data_matrix / framenum;
111+
112+
event_data_vector = double(reshape(event_data_matrix, [],1));
113+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function [fillingdomains, satdomains] = TKD_cor_saturated(rawdata,numfiles,sat_index)
2+
%TKD_COR_SATURATED Summary of this function goes here
3+
% Detailed explanation goes here
4+
%% Identify saturated domains
5+
satdomains = cell(1,numfiles);
6+
for i=1:numfiles
7+
satdomains{i} = find(rawdata(:,i) >= sat_index);
8+
end
9+
10+
fillingdomains = cell(1,numfiles);
11+
for i=1:numfiles
12+
if i==1
13+
fillingdomains{i} = find(rawdata(:,i) < sat_index);
14+
else
15+
fillingdomains{i} = setdiff(satdomains{i-1}, satdomains{i});
16+
end
17+
end
18+
end
19+

modules/ded_tkd/TKD_flatfield.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function [fused_flat,fusedpattern,normfactor, ref_curve] = TKD_flatfield(rawdata,normalizeddata,angle_matrix,fillingdomains,numfiles)
2+
%TKD_FLATFIELD fit to the radial function, and then flat field with it
3+
4+
5+
[normfactor, ref_curve] = TKD_referencecurvefit(rawdata,normalizeddata,angle_matrix,numfiles);
6+
7+
% Stitch the pattern
8+
[fusedpattern] = TKD_patternfuse(rawdata,normalizeddata,fillingdomains);
9+
10+
%% Flat field
11+
npix=sqrt(size(rawdata,1));
12+
13+
normfactor = reshape(normfactor, [npix npix]);
14+
fused_flat = fusedpattern ./ normfactor;
15+
16+
end
17+

modules/ded_tkd/TKD_normalize.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function [normalizeddata] = TKD_normalize(fillingdomains,rawdata,numfiles)
2+
%TKD_NORMALIZE Summary of this function goes here
3+
% Detailed explanation goes here
4+
5+
normalizeddata = 0*rawdata;
6+
7+
% Calculate normalization factors
8+
eventratio = zeros(length(fillingdomains{1}),numfiles-1);
9+
for j=2:numfiles
10+
reference_data = rawdata(:,1);
11+
current_data = rawdata(:,j);
12+
eventratio(:,j-1) = reference_data(fillingdomains{1})./ current_data(fillingdomains{1});
13+
end
14+
15+
eventratio(isinf(eventratio)) = NaN; % account for dead pixels
16+
17+
clear reference_data current_data;
18+
19+
scale_factor = mean(eventratio, 1 ,"omitnan");
20+
21+
for i=1:numfiles
22+
if i==1
23+
normalizeddata(:,i) = rawdata(:,i);
24+
else
25+
normalizeddata(:,i)= rawdata(:,i) .* scale_factor(i-1);
26+
end
27+
end
28+
end
29+

modules/ded_tkd/TKD_patternfuse.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function [fusedpattern] = TKD_patternfuse(rawdata,normalizeddata,fillingdomains)
2+
%TKD_PATTERNFUSE Summary of this function goes here
3+
% Detailed explanation goes here
4+
numfiles=size(rawdata,2);
5+
6+
fusedpattern = zeros(254^2,1);
7+
8+
for i=1:numfiles
9+
current_data = normalizeddata(:,i);
10+
fusedpattern(fillingdomains{i}) = current_data(fillingdomains{i});
11+
end
12+
13+
fusedpattern = reshape(fusedpattern, 254,254);
14+
stitched_mask = medfilt2(fusedpattern, [3 3]);
15+
fusedpattern(rawdata(:,1)==0) = stitched_mask(rawdata(:,1)==0);
16+
17+
18+
19+
end
20+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function [normfactor, ref_curve] = TKD_referencecurvefit(rawdata,normalizeddata,angle_matrix,numfiles)
2+
%fit to the angular data to get the scattering profile
3+
4+
%% Fit reference curve to a smoothing function
5+
notdeadpixel = find(rawdata(:,1) ~=0);
6+
7+
ref_curve = normalizeddata(:,numfiles);
8+
9+
normfactor = smooth(angle_matrix(:), ref_curve,200,'sgolay',2);
10+
11+
end

modules/ded_tkd/TKD_scattergrid.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function [angle_matrix,xgrid,ygrid] = TKD_scattergrid(PCx,PCy,DD,npix)
2+
%TKD Scattering Grid
3+
%calculates the scattered grid pixel positions
4+
5+
[xgrid,ygrid] = meshgrid(1:npix,1:npix);
6+
angle_matrix = atan(sqrt((xgrid - PCx).^2 + (ygrid - PCy).^2) * 55 / 1000 ./ DD) / pi * 180;
7+
8+
end
9+

0 commit comments

Comments
 (0)