; docformat = 'rst' ;+ ; Batch file containing all the code intended to be typed at the command line ; from the "Performance" chapter. ;- ; Timing file = filepath('endocell.jpg', subdir=['examples', 'data']) read_jpeg, file, cell .compile adapt_hist_equal resolve_all profiler, /system profiler ahe_cell = adapt_hist_equal(cell) profiler, /report, data=data profiler, /clear print, 'Name', 'Count', 'Only time', 'Time', 'System', format='(A-20, A7, A12, A10, A9)' print, data[sort(-data.time)], format='(A-20, I7, F12.5, F10.5, I9)' ; Vectorization ones = fltarr(100) + 1.0 endoFilename = file_which('endocell.jpg') endo = read_image(endoFilename) tvscl, endo gt 170 tv, endo * (endo gt 120 and endo lt 180) all = bytarr(n) if (ind[0] ne -1L) then all[ind] = 1B complement = where(all eq 0, nComplement) d = dist(20) ind = where(d gt 13, count) print, ind print, array_indices(d, ind) arr = fix(randomu(0L, 6, 3) * 10) print, arr print, total(arr, 2, /preserve_type) print, total(arr, 1, /preserve_type) print, max(arr, dimension=2, min=min) - min y = findgen(3, 5) print, y x = fltarr(3, 5) + 1. a = 2. blas_axpy, y, a, x print, y blas_axpy, y, a, fltarr(3) + 1., 1, [0, 2] print, y blas_axpy, y, a, fltarr(3) + 1., 1, [0, 1], 2, [0, 2, 3] print, y a = 10 print, a[0] multi = indgen(2, 3) print, multi[4] print, multi print, indgen(6) print, multi[0, 2] print, multi[4] ; Vectorization exercises filename = filepath('people.jpg', subdir=['examples', 'data']) ali = read_image(filename) help, ali arr = randomu(seed, 1000) ind = mg_sample(1000, 10) ; Logic print, mg_convert_type(1, 2) print, mg_convert_type(5, 6) ; Array and matrix techniques a = indgen(4, 5) print, a a[*, 4] = indgen(4) a[0, 4] = reform(indgen(4), 4, 1) print, a seed = 0L arr = randomu(seed, 3, 5) print, arr v = randomu(seed, 5) print, v repeatV = rebin(reform(v, 1, 5), 3, 5) print, repeatV print, arr * repeatV print, arr * ((fltarr(3) + 1.0) # v) restore, filename=filepath('cow10.sav', subdir=['examples', 'data']) verts = transpose([[x], [y], [z]]) nverts = (size(points, /dimensions))[1] pt = [1, 1, 0] distances = sqrt(total((verts - rebin(reform(pt, 3, 1), 3, nverts))^2, 1)) tv, rebin(reform(bindgen(256), 256, 1), 256, 8) tv, rebin(reform(bindgen(256), 1, 256), 8, 256) print, reform(rebin([-1, 0, 1], 3, 9), 27) print, reform(rebin([-1, 0, 1], 9, 3), 27) print, rebin([-1, 0, 1], 27) arr = findgen(5) print, arr[[-1, 0, 3, 4, 5]] compile_opt strictarrsubs print, arr[[-1, 0, 3, 4, 5]] im = findgen(3, 4, 3) x_loc = (lindgen(4, 3) + 2L) mod 4 y_loc = (lindgen(4, 3) + 1L) mod 3 p = transpose([[[x_loc]], [[y_loc]]], [2, 0, 1]) x = rebin(reform(lindgen(3), [3, 1, 1]), [3, 4, 3]) y = rebin(p[0, *, *], [3, 4, 3]) z = rebin(p[1, *, *], [3, 4, 3]) im2 = im[x, y, z] print, im2[2, 3, 1], im[2, p[0, 3, 1], p[1, 3, 1]] print, (intarr(3) + 1) # indgen(4) print, indgen(4) # indgen(5) x = findgen(10) / 9. - 0.5 y = findgen(20) / 19. - 0.5 surface, x^2 # (fltarr(20) + 1.) + (fltarr(10) + 1.) # y^2, x, y ; The clever use of HISTOGRAM im = read_image(file_which('endocell.jpg')) plot, histogram(im), psym=10, xstyle=9, ystyle=8, charsize=0.75 d = randomu(0L, 100) print, histogram(d, min=0., max=1., nbins=10) print, histogram(d, min=0., max=0.1 * (10 - 1) + 0., nbins=10) print, histogram(d, min=0., max=1., binsize=0.1) seed = 0L a = fix(15 * randomu(seed, 6)) b = fix(15 * randomu(seed, 6)) print, a, b print, where(histogram([a, b], omin=omin)) + omin a = [0] b = [999999] minAB = min(a, max=maxA) > min(b, max=maxB) maxAB = maxA < maxB ahist = histogram(a, min=minAB, max=maxAB) bhist = histogram(b, min=minAB, max=maxAB) print, where((ahist ne 0L) and (bhist ne 0L), count) + minAB vec = randomu(0L, 10) removeInd = [0, 3, 4, 6, 9, 9] h = histogram(removeInd, min=0, max=n_elements(vec) - 1L) keepInd = where(h eq 0, count) print, keepInd seed = 0L values = randomu(seed, 4) print, values repeats = [2, 0, 1, 2] resultInd = [0, total(repeats, /cumulative, /preserve_type)] print, resultInd startBlocks = histogram(resultInd) print, startBlocks blocks = total(startBlocks, /cumulative, /preserve_type) - 1L print, blocks print, values[blocks[0:n_elements(startBlocks) - 2L]] seed = 0L arr = fix(randomu(seed, 4) * 10) print, arr h = histogram(arr, reverse_indices=r) print, h print, r print, indgen(n_elements(r)), r, format='(13I6)' a = fix(100 * randomu(seed, 10000)) i = 50 ind = where(a eq i, count) if (count ne 0L) then a[ind] = 0 h = histogram(a, reverse_indices=r) if (r[i] lt r[i + 1]) then a[r[r[i] : r[i+1]-1]] = 0 .run mg_where_vs_histogram print, mg_n_smallest(randomu(seed, 1000), 3) x = findgen(11) / 10. print, x print, value_locate([0.2, 0.4, 0.8], x) d = randomu(12345678L, 20) print, d cutoffs = [0.3, 0.4, 0.8] bins = value_locate(cutoffs, d) + 1L print, bins h = histogram(bins, reverse_indices=r) print, h print, d[r[r[0]:r[1] - 1]] print, d[r[r[1]:r[2] - 1]] print, d[r[r[2]:r[3] - 1]] print, d[r[r[3]:r[4] - 1]] ; Memory usage startMemory = memory(/current) image = 255B - image print, memory(/highwater) - startMemory startMemory = memory(/current) image = 255B - temporary(image) print, memory(/highwater) - startMemory image = image - smooth(image, 5) image = image - smooth(temporary(image), 5) image = temporary(image) - smooth(image, 5) s = { image: bytscl(dist(256)) } s.image++ s.image = s.image + 1B ; Object-oriented techniques a = [0, 5, 6] a = [a, 8] print, a restore, filename=filepath('cow10.sav', subdir=['examples', 'data']) verts = transpose([[x], [y], [z]]) nverts = (size(points, /dimensions))[1] pt = [1, 1, 0] distances = sqrt(total((verts - rebin(reform(pt, 3, 1), 3, nverts))^2, 1)) im = findgen(3, 4, 3) x_loc = (lindgen(4, 3) + 2L) mod 4 y_loc = (lindgen(4, 3) + 1L) mod 3 p = transpose([[[x_loc]], [[y_loc]]], [2, 0, 1]) sz = size(im, /dimensions) nx = sz[1] ny = sz[2] x = rebin(reform(lindgen(3), [3, 1, 1]), [3, nx, ny]) y = rebin(p[0, *, *], [3, nx, ny]) z = rebin(p[1, *, *], [3, nx, ny]) im2 = im[x, y, z]