Conditions | 9 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
34 | def analyse_mean_norm(self, laser_data, signal_start=0.0, signal_end=200e-9, norm_start=300e-9, |
||
35 | norm_end=500e-9): |
||
36 | """ |
||
37 | |||
38 | @param laser_data: |
||
39 | @param signal_start: |
||
40 | @param signal_end: |
||
41 | @param norm_start: |
||
42 | @param norm_end: |
||
43 | @return: |
||
44 | """ |
||
45 | # Get number of lasers |
||
46 | num_of_lasers = laser_data.shape[0] |
||
47 | # Get counter bin width |
||
48 | bin_width = self.fast_counter_settings.get('bin_width') |
||
49 | |||
50 | if not isinstance(bin_width, float): |
||
51 | return np.zeros(num_of_lasers), np.zeros(num_of_lasers) |
||
52 | |||
53 | # Convert the times in seconds to bins (i.e. array indices) |
||
54 | signal_start_bin = round(signal_start / bin_width) |
||
55 | signal_end_bin = round(signal_end / bin_width) |
||
56 | norm_start_bin = round(norm_start / bin_width) |
||
57 | norm_end_bin = round(norm_end / bin_width) |
||
58 | |||
59 | # initialize data arrays for signal and measurement error |
||
60 | signal_data = np.empty(num_of_lasers, dtype=float) |
||
61 | error_data = np.empty(num_of_lasers, dtype=float) |
||
62 | |||
63 | # loop over all laser pulses and analyze them |
||
64 | for ii, laser_arr in enumerate(laser_data): |
||
65 | # calculate the sum and mean of the data in the normalization window |
||
66 | tmp_data = laser_arr[norm_start_bin:norm_end_bin] |
||
67 | reference_sum = np.sum(tmp_data) |
||
68 | reference_mean = (reference_sum / len(tmp_data)) if len(tmp_data) != 0 else 0.0 |
||
69 | |||
70 | # calculate the sum and mean of the data in the signal window |
||
71 | tmp_data = laser_arr[signal_start_bin:signal_end_bin] |
||
72 | signal_sum = np.sum(tmp_data) |
||
73 | signal_mean = (signal_sum / len(tmp_data)) if len(tmp_data) != 0 else 0.0 |
||
74 | |||
75 | # Calculate normalized signal while avoiding division by zero |
||
76 | if reference_mean > 0 and signal_mean >= 0: |
||
77 | signal_data[ii] = signal_mean / reference_mean |
||
78 | else: |
||
79 | signal_data[ii] = 0.0 |
||
80 | |||
81 | # Calculate measurement error while avoiding division by zero |
||
82 | if reference_sum > 0 and signal_sum > 0: |
||
83 | # calculate with respect to gaussian error 'evolution' |
||
84 | error_data[ii] = signal_data[ii] * np.sqrt(1 / signal_sum + 1 / reference_sum) |
||
85 | else: |
||
86 | error_data[ii] = 0.0 |
||
87 | |||
88 | return signal_data, error_data |
||
89 | |||
128 |