Debian Codegraph @

planetary_system_stacker/Test_programs/Test_for_Rolf.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 Created on Fri Sep 21 15:39:35 2018
4
5 @author: Michal Powalko [email protected]
6 """
7
8 import time
9 import numpy as np
10
11
12 # Currently by Rolf
13 tic = time.time()
14 buffer_Rolf = np.empty([200, 1920, 1080])
15 mem_alloc_Rolf = buffer_Rolf.nbytes / 1024**3
16 print('Preallocated {0:.3f} [GB] memory for importing the data in {1:.3f} [s]...Rolf'.format(mem_alloc_Rolf, time.time() - tic))
17
18 # Proposal from Michal
19 # "buffer" is a matrix to HOLD "input data" - and this consist of 8bit data = "uint8". Specifing the "dtype"
20 # as "np.uint8" You can save al lot of mememory and gain speed (in case of many frames)
21 # The results of "mean/median" will be saved under another variable, where the resolution might be adjustet
22 # The only disadvantageous would be, when working with input files with greater color resolution than 8bit
23
24 tic = time.time()
25 buffer_Michal = np.empty([200, 1920, 1080], dtype = np.uint8)
26 mem_alloc_Michal = buffer_Michal.nbytes / 1024**3
27 print('Preallocated {0:.3f} [GB] memory for importing the data in {1:.3f} [s]...Michal :)'.format(mem_alloc_Michal, time.time() - tic))
28
29 #------------------------------------------------------------------------------------------------------------------------
30
31 # Currently by Rolf
32 tic = time.time()
33 mean_frame_Rolf = np.mean(buffer_Rolf, axis=0)
34 print('Calculated "mean" from imported frames in {0:.3f} [s]...Rolf'.format(time.time() - tic))
35
36
37 # Proposal from Michal
38 # Taking advantage of "variable.npCommand" might save some time
39 # Disadvantage may be luck of some internal check in numpy "sanity check of input data", but I have never had a problem with this
40 buffer_Rolf2 = np.empty([200, 1920, 1080])
41 tic = time.time()
42 mean_frame_Michal = buffer_Rolf2.mean(axis=0)
43 print('Calculated "mean" from imported frames in {0:.3f} [s]...Michal'.format(time.time() - tic))
44
45 #------------------------------------------------------------------------------------------------------------------------
46
47 # Proposal from Michal
48 # Calculating Sum as well
49 # Having read: http://www.stark-labs.com/craig/resources/Articles-&-Reviews/BitDepthStacking.pdf
50 # building a sum from 8bits we can gaint the bit resolution. To prevent the resolution it is necessary to either:
51 # - saving stacked uint8 frames with "float"resolution (to keep the "after comma values)
52 # - or to sum to uint>8, like uint16 or even uint32
53 tic = time.time()
54 sum_frame = np.sum(buffer_Rolf, axis=0, dtype=np.uint32)
55 print('Calculated "sum" from imported frames in {0:.3f} [s]...'.format(time.time() - tic))
56
57 #------------------------------------------------------------------------------------------------------------------------
58
59 # Proposal from Michal
60 # Calculating Median as well
61
62 tic = time.time()
63 median_frame = np.median(buffer_Rolf, axis=0)
64 print('Calculated "median" from imported frames in {0:.3f} [s]...'.format(time.time() - tic))
65
66 # We can boost performance by setting "overwrite_input=True" --> the original matrix will be "reshaped" instead of creating another one
67 # "variable.median" is not available :(
68 # https://docs.scipy.org/doc/numpy/reference/generated/numpy.median.html
69 buffer_Rolf2 = np.empty([200, 1920, 1080])
70 tic = time.time()
71 median_frame2 = np.median(buffer_Rolf2, axis=0, overwrite_input=True)
72 print('Calculated "median" from imported frames in {0:.3f} [s]...with "overwrite_input=True"'.format(time.time() - tic))
73
74
75 #------------------------------------------------------------------------------------------------------------------------
76 ## TIPS
77
78 # setting "dtype=np.uint32" inside numpy function is much faster then "().astype(np.uint32)"
79
80 # "variable.npCommand" is slightly faster than "npCommand(variable)"
81
82 # Downsampling to 8bit
83 # This may be tricky --> ".astype(np.uint)" will perform "floor" instead of "round".
84 # This might lead to situation, where "9,9999" will be "rounded" to "9"
85 downsample_ipnut = 10 * np.random.rand(3,2)
86 downsample_wrong = downsample_ipnut.astype(np.uint8)
87 downsample_good = downsample_ipnut.round().astype(np.uint8)