| Conditions | 17 |
| Total Lines | 85 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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:
Complex classes like savu.plugins.unregistered.i18_loaders.base_i18_multi_modal_loader.BaseI18MultiModalLoader.set_motors() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Copyright 2014 Diamond Light Source Ltd. |
||
| 68 | def set_motors(self, data_obj, ltype): |
||
| 69 | # now lets extract the map, if there is one! |
||
| 70 | # to begin with |
||
| 71 | data_obj.data_mapping = DataMapping() |
||
| 72 | logging.debug("========="+ltype+"=========") |
||
| 73 | motors = self.parameters['scan_pattern'] |
||
| 74 | data_obj.data_mapping.set_axes(self.parameters['scan_pattern']) |
||
| 75 | nAxes = len(data_obj.get_shape()) |
||
| 76 | #logging.debug nAxes |
||
| 77 | cts = 0 |
||
| 78 | chk = 0 |
||
| 79 | chk1 = 0 |
||
| 80 | motor_type = [] |
||
| 81 | labels = [] |
||
| 82 | fast_axis = self.parameters["fast_axis"] |
||
| 83 | logging.debug("axes input are:"+str(motors)) |
||
| 84 | unknown_count = 0 |
||
| 85 | for ii in range(nAxes): |
||
| 86 | # find the rotation axis |
||
| 87 | try: |
||
| 88 | data_axis = self.parameters[motors[ii]]# get that out the file |
||
| 89 | logging.debug("the data axis is %s" % str(data_axis)) |
||
| 90 | if motors[ii]=="rotation": |
||
| 91 | data_obj.data_mapping._is_tomo = True |
||
| 92 | motor_type.append('rotation') |
||
| 93 | label = 'rotation_angle' |
||
| 94 | units = 'degrees' |
||
| 95 | logging.debug(ltype + " reader: %s", "is a tomo scan") |
||
| 96 | elif motors[ii] in ["x","y"]: |
||
| 97 | cts += 1 # increase the order of the map |
||
| 98 | motor_type.append('translation') |
||
| 99 | if (motors[ii]==fast_axis): |
||
| 100 | label='x' |
||
| 101 | else: |
||
| 102 | label='y' |
||
| 103 | units = 'mm' |
||
| 104 | except KeyError as e: |
||
| 105 | #print("exception was ",str(e)) |
||
| 106 | #print("found no motor") |
||
| 107 | motor_type.append('None') |
||
| 108 | #now the detector axes |
||
| 109 | if ltype =='fluo': |
||
| 110 | label = 'energy' |
||
| 111 | units = 'counts' |
||
| 112 | elif ltype =='xrd': |
||
| 113 | if chk==0: |
||
| 114 | label = 'detector_x' |
||
| 115 | elif chk==1: |
||
| 116 | label = 'detector_y' |
||
| 117 | units = 'index' |
||
| 118 | chk=chk+1 |
||
| 119 | |||
| 120 | except IndexError: |
||
| 121 | ''' |
||
| 122 | some additional singleton dimensions have been added in the latest mapping project stuff on I18 |
||
| 123 | This fixes that. |
||
| 124 | ''' |
||
| 125 | if ltype =='xrd': |
||
| 126 | if chk1 == 0: |
||
| 127 | label = 'detector_x' |
||
| 128 | elif chk1 == 1: |
||
| 129 | label = 'detector_y' |
||
| 130 | units = 'pixels' |
||
| 131 | chk1=chk1+1 |
||
| 132 | else: |
||
| 133 | label = 'unknown_%s' % unknown_count |
||
| 134 | units = 'unknown' |
||
| 135 | unknown_count += 1 |
||
| 136 | except: |
||
| 137 | raise |
||
| 138 | |||
| 139 | labels.append(label+'.'+units) |
||
| 140 | if not motors: |
||
| 141 | logging.debug("%s reader: No maps found!", ltype) |
||
| 142 | #logging.debug labels |
||
| 143 | data_obj.set_axis_labels(*tuple(labels)) |
||
| 144 | data_obj.data_mapping.set_motors(motors) |
||
| 145 | data_obj.data_mapping.set_motor_type(motor_type) |
||
| 146 | if (cts): |
||
| 147 | data_obj.data_mapping._is_map = cts |
||
| 148 | else: |
||
| 149 | logging.debug("'%s' reader: No translations found!", ltype) |
||
| 150 | pass |
||
| 151 | logging.debug("axis labels:"+str(labels)) |
||
| 152 | logging.debug("motor_type:"+str(motor_type)) |
||
| 153 | |||
| 197 |