Best Python code snippet using localstack_python
Femur_angles.py
Source:Femur_angles.py
...18 except:19 pass20 check_list = check_obj_list(['Anatomical Point P:Cor', 'Femur Center', "Mechanical Axis", 'Hip Center'])21 if len(check_list) == 0:22 copy_object(bpy.data.objects['Anatomical Point P:Cor'], 'Anatomical Point P:Cor for valgus angle')23 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for valgus angle')24 make_axis([bpy.data.objects['Anatomical Point P:Cor for valgus angle'], bpy.data.objects['Femur Center for valgus angle']], "Valgus angle L1")25 copy_object(bpy.data.objects["Mechanical Axis"], 'Mechanical Axis for valgus angle')26 # coord_values = get_coordinates(['Anatomical Point P:Cor', 'Femur Center', 'Hip Center'])27 # angle = angle_triangle(coord_values[0], coord_values[1], coord_values[2], coord_values[3], coord_values[4], coord_values[5], coord_values[6], coord_values[7], coord_values[8])28 # print("valgus angle = " + angle) 29 30 join_obj([bpy.data.objects['Mechanical Axis for valgus angle'], bpy.data.objects["Valgus angle L1"]], "Valgus Angle")31 remove_doubles(bpy.data.objects["Valgus Angle"])32 move_to_collection("Measurements", bpy.data.objects["Valgus Angle"])33 else:34 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))35 return {'FINISHED'}36class FEMUR_OT_NSACoronal(Operator):37 """ """38 bl_label = "NSA in Coronal Plane"39 bl_idname = "object.nsacoronal"40 bl_options = {"REGISTER", "UNDO"}41 42 def execute(self, context):43 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])44 unhide_list(["NSA in Coronal Plane", 'Hip Center', 'Neck Center P:Cor', 'GT P:Cor', 'Femur Center'])45 46 try:47 delete_obj(bpy.data.objects["NSA in Coronal Plane"])48 except:49 pass50 51 check_list = check_obj_list(['Hip Center', 'Neck Center P:Cor', 'GT P:Cor', 'Femur Center'])52 if len(check_list) == 0:53 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for NSACoronal L1')54 copy_object(bpy.data.objects['Neck Center P:Cor'], 'Neck Center P:Cor for NSACoronal L1')55 copy_object(bpy.data.objects['GT P:Cor'], 'GT P:Cor for NSACoronal L2')56 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for NSACoronal L2')57 58 make_axis([bpy.data.objects['Hip Center for NSACoronal L1'], bpy.data.objects['Neck Center P:Cor for NSACoronal L1']], "NSACoronal Line1")59 make_axis([bpy.data.objects['GT P:Cor for NSACoronal L2'], bpy.data.objects['Femur Center for NSACoronal L2']], "NSACoronal Line2")60 61 join_obj([bpy.data.objects["NSACoronal Line1"], bpy.data.objects["NSACoronal Line2"]], "NSAcoronal point")62 vertex_group(bpy.data.objects["NSAcoronal point"])63 copy_object(bpy.data.objects["NSAcoronal point"], "NSAcoronal point copy")64 65 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for NSAcoronal')66 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for NSAcoronal')67 make_axis([bpy.data.objects['Hip Center for NSAcoronal'], bpy.data.objects["NSAcoronal point"]], "NSA in Coronal Plane 1")68 make_axis([bpy.data.objects['Femur Center for NSAcoronal'], bpy.data.objects["NSAcoronal point copy"]], "NSA in Coronal Plane 2")69 join_obj([bpy.data.objects["NSA in Coronal Plane 1"], bpy.data.objects["NSA in Coronal Plane 2"]], "NSA in Coronal Plane")70 remove_doubles(bpy.data.objects["NSA in Coronal Plane"])71 move_to_collection("Measurements", bpy.data.objects["NSA in Coronal Plane"])72 else:73 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))74 return {'FINISHED'}75 76class FEMUR_OT_NSASagittal(Operator):77 """ """78 bl_label = "NSA in Sagittal Plane"79 bl_idname = "object.nsasagittal"80 bl_options = {"REGISTER", "UNDO"}81 82 def execute(self, context):83 84 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])85 unhide_list(["NSA in Sagittal Plane", 'Hip Center', 'Neck Center P:Sag', 'Femur Center', 'GT P:Sag'])86 87 try:88 delete_obj(bpy.data.objects["NSA in Sagittal Plane"])89 except:90 pass91 check_list = check_obj_list(['Hip Center', 'Neck Center P:Sag', 'Femur Center', 'GT P:Sag'])92 if len(check_list) == 0:93 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for NSASagittal L1')94 copy_object(bpy.data.objects['Neck Center P:Sag'], 'Neck Center P:Sag for NSASagittal L1')95 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for NSASagittal L2')96 copy_object(bpy.data.objects['GT P:Sag'], 'GT P:Sag for NSASagittal L2')97 make_axis([bpy.data.objects['Hip Center for NSASagittal L1'] , bpy.data.objects['Neck Center P:Sag for NSASagittal L1']], "NSASagittal L1")98 make_axis([bpy.data.objects['Femur Center for NSASagittal L2'] , bpy.data.objects['GT P:Sag for NSASagittal L2']], "NSASagittal L2")99 100 join_obj([bpy.data.objects['NSASagittal L1'], bpy.data.objects['NSASagittal L2']], "NSASagittal point")101 102 vertex_group(bpy.data.objects["NSASagittal point"])103 copy_object(bpy.data.objects["NSASagittal point"], "NSASagittal point copy")104 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for NSASagittal')105 copy_object(bpy.data.objects['GT P:Sag'], 'GT P:Sag for NSASagittal')106 make_axis([bpy.data.objects["NSASagittal point"], bpy.data.objects['Hip Center for NSASagittal']], "NSA in Sagittal Plane 1")107 make_axis([bpy.data.objects["NSASagittal point copy"], bpy.data.objects['GT P:Sag for NSASagittal']], "NSA in Sagittal Plane 2")108 join_obj([bpy.data.objects["NSA in Sagittal Plane 1"], bpy.data.objects["NSA in Sagittal Plane 2"]], "NSA in Sagittal Plane")109 remove_doubles(bpy.data.objects["NSA in Sagittal Plane"])110 move_to_collection("Measurements", bpy.data.objects["NSA in Sagittal Plane"])111 else:112 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))113 return {'FINISHED'}114class FEMUR_OT_mLDFA(Operator):115 """ """116 bl_label = "mLDFA"117 bl_idname = "object.mldfa"118 bl_options = {"REGISTER", "UNDO"}119 120 def execute(self, context):121 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])122 unhide_list(["mLDFA", 'Lateral Distal P:Cor', 'Medial Distal P:Cor', 'Mechanical Axis']) 123 124 try:125 delete_obj(bpy.data.objects["mLDFA"])126 except:127 pass128 129 check_list = check_obj_list(['Lateral Distal P:Cor', 'Medial Distal P:Cor', 'Mechanical Axis'])130 if len(check_list) == 0:131 copy_object(bpy.data.objects['Lateral Distal P:Cor'], 'Lateral Distal P:Cor for mLDFA L1')132 copy_object(bpy.data.objects['Medial Distal P:Cor'], 'Medial Distal P:Cor for mLDFA L1')133 make_axis([bpy.data.objects['Lateral Distal P:Cor for mLDFA L1'], bpy.data.objects['Medial Distal P:Cor for mLDFA L1']], "mLDFA Line1")134 135 copy_object(bpy.data.objects['Mechanical Axis'], 'Mechanical Axis for mLDFA L2')136 join_obj([bpy.data.objects['mLDFA Line1'], bpy.data.objects['Mechanical Axis for mLDFA L2']], "mLDFA point")137 138 vertex_group(bpy.data.objects["mLDFA point"])139 copy_object(bpy.data.objects["mLDFA point"], "mLDFA point copy")140 141 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for mLDFA')142 copy_object(bpy.data.objects['Medial Distal P:Cor'], 'Medial Distal P:Cor for mLDFA')143 make_axis([bpy.data.objects["mLDFA point"], bpy.data.objects['Hip Center for mLDFA']], "mLDFA 1")144 make_axis([bpy.data.objects["mLDFA point copy"], bpy.data.objects['Medial Distal P:Cor for mLDFA']], "mLDFA 2")145 join_obj([bpy.data.objects["mLDFA 1"], bpy.data.objects["mLDFA 2"]], "mLDFA")146 remove_doubles(bpy.data.objects["mLDFA"])147 move_to_collection("Measurements", bpy.data.objects["mLDFA"])148 else:149 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))150 return {'FINISHED'}151class FEMUR_OT_mLPFA(Operator):152 """ """153 bl_label = "mLPFA"154 bl_idname = "object.mlpfa"155 bl_options = {"REGISTER", "UNDO"}156 157 def execute(self, context):158 159 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])160 unhide_list(['mLPFA', 'GT P:Cor', 'Hip Center', 'Femur Center'])161 162 try:163 delete_obj(bpy.data.objects['mLPFA'])164 except:165 pass166 check_list = check_obj_list(['GT P:Cor', 'Hip Center','Femur Center'])167 if len(check_list) == 0:168 copy_object(bpy.data.objects['GT P:Cor'], 'GT P:Cor for mLPFA')169 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for mLPFA 1')170 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for mLPFA 2')171 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for mLPFA')172 make_axis([bpy.data.objects['GT P:Cor for mLPFA'], bpy.data.objects['Hip Center for mLPFA 1']], "mLPFA 1")173 make_axis([bpy.data.objects['Femur Center for mLPFA'], bpy.data.objects['Hip Center for mLPFA 2']], "mLPFA 2")174 join_obj([bpy.data.objects["mLPFA 1"], bpy.data.objects["mLPFA 2"]], "mLPFA")175 remove_doubles(bpy.data.objects["mLPFA"])176 move_to_collection("Measurements", bpy.data.objects['mLPFA'])177 else:178 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))179 return {'FINISHED'}180class FEMUR_OT_aMPFA(Operator):181 """ """182 bl_label = "aMPFA"183 bl_idname = "object.ampfa"184 bl_options = {"REGISTER", "UNDO"}185 186 def execute(self, context):187 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])188 unhide_list(['aMPFA', 'Hip Center', 'GT P:Cor', 'Femur Center', 'Anatomical Point P:Cor'])189 190 try:191 delete_obj(bpy.data.objects['aMPFA'])192 except:193 pass194 195 check_list = check_obj_list(['Hip Center', 'GT P:Cor', 'Femur Center', 'Anatomical Point P:Cor'])196 if len(check_list) == 0:197 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for aMPFA L1')198 copy_object(bpy.data.objects['GT P:Cor'], 'GT P:Cor for aMPFA L1')199 make_axis([bpy.data.objects['Hip Center for aMPFA L1'], bpy.data.objects['GT P:Cor for aMPFA L1']], "aMPFA Line1")200 201 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for aMPFA L2')202 copy_object(bpy.data.objects['Anatomical Point P:Cor'], 'Anatomical Point P:Cor for aMPFA L2')203 make_axis([bpy.data.objects['Femur Center for aMPFA L2'], bpy.data.objects['Anatomical Point P:Cor for aMPFA L2']], "aMPFA Line2")204 205 join_obj([bpy.data.objects['aMPFA Line1'], bpy.data.objects['aMPFA Line2']], "aMPFA point")206 207 vertex_group(bpy.data.objects["aMPFA point"])208 copy_object(bpy.data.objects["aMPFA point"], "aMPFA point copy")209 210 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for aMPFA')211 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for aMPFA')212 make_axis([bpy.data.objects["aMPFA point"], bpy.data.objects['Hip Center for aMPFA']], 'aMPFA 1')213 make_axis([bpy.data.objects["aMPFA point copy"], bpy.data.objects['Femur Center for aMPFA']], 'aMPFA 2')214 join_obj([bpy.data.objects['aMPFA 1'], bpy.data.objects['aMPFA 2']], 'aMPFA')215 remove_doubles(bpy.data.objects['aMPFA'])216 move_to_collection("Measurements", bpy.data.objects['aMPFA'])217 else:218 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))219 return {'FINISHED'}220class FEMUR_OT_aLDFA(Operator):221 """ """222 bl_label = "aLDFA"223 bl_idname = "object.aldfa"224 bl_options = {"REGISTER", "UNDO"}225 226 def execute(self, context):227 228 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])229 unhide_list(['aLDFA', 'Medial Distal P:Cor', 'Lateral Distal P:Cor', 'Anatomical Point P:Cor', 'Femur Center'])230 231 try:232 delete_obj(bpy.data.objects['aLDFA'])233 except:234 pass235 check_list = check_obj_list(['Medial Distal P:Cor', 'Lateral Distal P:Cor', 'Anatomical Point P:Cor', 'Femur Center'])236 if len(check_list) == 0:237 copy_object(bpy.data.objects['Medial Distal P:Cor'],'Medial Distal P:Cor for aLDFA L1')238 copy_object(bpy.data.objects['Lateral Distal P:Cor'], 'Lateral Distal P:Cor for aLDFA L1')239 make_axis([bpy.data.objects['Medial Distal P:Cor for aLDFA L1'], bpy.data.objects['Lateral Distal P:Cor for aLDFA L1']], "aLDFA L1")240 copy_object(bpy.data.objects['Anatomical Point P:Cor'], 'Anatomical Point P:Cor for aLDFA L2')241 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for aLDFA L2')242 make_axis([bpy.data.objects['Anatomical Point P:Cor for aLDFA L2'], bpy.data.objects['Femur Center for aLDFA L2']], "aLDFA L2")243 join_obj([bpy.data.objects["aLDFA L1"],bpy.data.objects["aLDFA L2"]], "aLDFA point") 244 vertex_group(bpy.data.objects["aLDFA point"])245 copy_object(bpy.data.objects["aLDFA point"], "aLDFA point copy")246 247 copy_object(bpy.data.objects['Anatomical Point P:Cor'], 'Anatomical Point P:Cor for aLDFA')248 copy_object(bpy.data.objects['Medial Distal P:Cor'], 'Medial Distal P:Cor for aLDFA')249 make_axis([bpy.data.objects["aLDFA point"], bpy.data.objects['Anatomical Point P:Cor for aLDFA']],"aLDFA 1")250 make_axis([bpy.data.objects["aLDFA point copy"], bpy.data.objects['Medial Distal P:Cor for aLDFA']],"aLDFA 2")251 join_obj([bpy.data.objects["aLDFA 1"], bpy.data.objects["aLDFA 2"]], "aLDFA")252 remove_doubles(bpy.data.objects["aLDFA"])253 move_to_collection("Measurements", bpy.data.objects['aLDFA'])254 else:255 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))256 return {'FINISHED'}257class FEMUR_OT_MidshaftVarusInCoronalPlane(Operator):258 """ """259 bl_label = "Midshaft Varus Coronal Plane"260 bl_idname = "object.midshaftvaruscoronalplane"261 bl_options = {"REGISTER", "UNDO"}262 263 def execute(self, context):264 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])265 unhide_list(["Midshaft Varus Coronal Plane", 'GT P:Cor', 'Midshaft Femur Center P:Cor', 'Femur Center'])266 267 try:268 delete_obj(bpy.data.objects["Midshaft Varus Coronal Plane"])269 except:270 pass271 check_list = check_obj_list(['GT P:Cor', 'Midshaft Femur Center P:Cor', 'Femur Center'])272 if len(check_list) == 0:273 copy_object(bpy.data.objects['GT P:Cor'], 'GT P:Cor for MidshaftVarusInCoronalPlane')274 copy_object(bpy.data.objects['Midshaft Femur Center P:Cor'], 'Midshaft Center P:Cor for MidshaftVarusInCoronalPlane 1')275 copy_object(bpy.data.objects['Midshaft Femur Center P:Cor'], 'Midshaft Center P:Cor for MidshaftVarusInCoronalPlane 2')276 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for MidshaftVarusInCoronalPlane')277 make_axis([bpy.data.objects['GT P:Cor for MidshaftVarusInCoronalPlane'], bpy.data.objects['Midshaft Center P:Cor for MidshaftVarusInCoronalPlane 1']], "MidshaftVarusInCoronalPlane 1")278 make_axis([bpy.data.objects['Femur Center for MidshaftVarusInCoronalPlane'], bpy.data.objects['Midshaft Center P:Cor for MidshaftVarusInCoronalPlane 2']], "MidshaftVarusInCoronalPlane 2")279 join_obj([bpy.data.objects["MidshaftVarusInCoronalPlane 1"], bpy.data.objects["MidshaftVarusInCoronalPlane 2"]], "Midshaft Varus Coronal Plane")280 remove_doubles(bpy.data.objects["Midshaft Varus Coronal Plane"])281 move_to_collection("Measurements", bpy.data.objects["Midshaft Varus Coronal Plane"])282 else:283 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))284 return {'FINISHED'}285class FEMUR_OT_MidshaftBowInSagittalPlane(Operator):286 """ """287 bl_label = "Midshaft Bow Sagittal Plane"288 bl_idname = "object.midshaftbowsagittalplane"289 bl_options = {"REGISTER", "UNDO"}290 291 def execute(self, context):292 293 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])294 unhide_list(["Midshaft Bow Sagittal Plane", 'GT P:Sag', 'Midshaft Femur Center P:Sag', 'Femur Center'])295 296 try:297 delete_obj(bpy.data.objects["Midshaft Bow Sagittal Plane"])298 except:299 pass300 check_list = check_obj_list(['GT P:Sag', 'Midshaft Femur Center P:Sag', 'Femur Center'])301 if len(check_list) == 0:302 copy_object(bpy.data.objects['GT P:Sag'], 'GT P:Sag for MidshaftBowInSagittalPlane')303 copy_object(bpy.data.objects['Midshaft Femur Center P:Sag'], 'Midshaft Center P:Sag for MidshaftBowInSagittalPlane 1')304 copy_object(bpy.data.objects['Midshaft Femur Center P:Sag'], 'Midshaft Center P:Sag for MidshaftBowInSagittalPlane 2')305 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for MidshaftBowInSagittalPlane')306 make_axis([bpy.data.objects['GT P:Sag for MidshaftBowInSagittalPlane'], bpy.data.objects['Midshaft Center P:Sag for MidshaftBowInSagittalPlane 1']], "Midshaft Bow Sagittal Plane 1")307 make_axis([bpy.data.objects['Femur Center for MidshaftBowInSagittalPlane'], bpy.data.objects['Midshaft Center P:Sag for MidshaftBowInSagittalPlane 2']], "Midshaft Bow Sagittal Plane 2")308 join_obj([bpy.data.objects["Midshaft Bow Sagittal Plane 1"], bpy.data.objects["Midshaft Bow Sagittal Plane 2"]], "Midshaft Bow Sagittal Plane")309 remove_doubles(bpy.data.objects["Midshaft Bow Sagittal Plane"])310 move_to_collection("Measurements", bpy.data.objects["Midshaft Bow Sagittal Plane"]) 311 else:312 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))313 return {'FINISHED'}314class FEMUR_OT_DistalFemurValgusCoronalPlane(Operator):315 """ """316 bl_label = "Distal Femur Valgus Coronal Plane"317 bl_idname = "object.distalfemurvalguscoronalplane"318 bl_options = {"REGISTER", "UNDO"}319 320 def execute(self, context):321 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])322 unhide_list(["Distal Femur Valgus Coronal Plane", 'Midshaft Femur Center P:Cor', 'Femur Center', 'Hip Center'])323 324 try:325 delete_obj(bpy.data.objects["Distal Femur Valgus Coronal Plane"])326 except:327 pass328 check_list = check_obj_list(['Midshaft Femur Center P:Cor', 'Femur Center', 'Hip Center'])329 if len(check_list) == 0:330 copy_object(bpy.data.objects['Midshaft Femur Center P:Cor'], 'Midshaft Center P:Cor for DistalValgusCoronalPlane')331 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for DistalFemurValgusCoronalPlane 1')332 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for DistalFemurValgusCoronalPlane 2')333 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for DistalFemurValgusCoronalPlane')334 make_axis([bpy.data.objects['Femur Center for DistalFemurValgusCoronalPlane 1'], bpy.data.objects['Midshaft Center P:Cor for DistalValgusCoronalPlane']], "DistalFemurValgusCoronalPlane 1")335 make_axis([bpy.data.objects['Femur Center for DistalFemurValgusCoronalPlane 2'], bpy.data.objects['Hip Center for DistalFemurValgusCoronalPlane']], "DistalFemurValgusCoronalPlane 2")336 join_obj([bpy.data.objects["DistalFemurValgusCoronalPlane 1"], bpy.data.objects["DistalFemurValgusCoronalPlane 2"]], "Distal Femur Valgus Coronal Plane")337 remove_doubles(bpy.data.objects["Distal Femur Valgus Coronal Plane"])338 move_to_collection("Measurements", bpy.data.objects["Distal Femur Valgus Coronal Plane"]) 339 else:340 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))341 return {'FINISHED'}342class FEMUR_OT_DistalFemurFlexionSagittalPlane(Operator):343 """ """344 bl_label = "Distal Femur Flexion Sagittal Plane"345 bl_idname = "object.distalfemurflexionsagittalplane"346 bl_options = {"REGISTER", "UNDO"}347 348 def execute(self, context):349 350 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])351 unhide_list(["Distal Femur Flexion Sagittal Plane", 'Midshaft Femur Center P:Sag', 'Femur Center', 'Hip Center'])352 353 try:354 delete_obj(bpy.data.objects["Distal Femur Flexion Sagittal Plane"])355 except:356 pass357 check_list = check_obj_list(['Midshaft Femur Center P:Sag', 'Femur Center', 'Hip Center'])358 if len(check_list) == 0:359 copy_object(bpy.data.objects['Midshaft Femur Center P:Sag'], 'Midshaft Center P:Sag for DistalFlexionSagittalPlane')360 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for DistalFemurFlexionSagittalPlane 1')361 copy_object(bpy.data.objects['Femur Center'], 'Femur Center for DistalFemurFlexionSagittalPlane 2')362 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for DistalFemurFlexionSagittalPlane')363 make_axis([bpy.data.objects['Femur Center for DistalFemurFlexionSagittalPlane 1'], bpy.data.objects['Midshaft Center P:Sag for DistalFlexionSagittalPlane']], "Distal Femur Flexion Sagittal Plane 1")364 make_axis([bpy.data.objects['Femur Center for DistalFemurFlexionSagittalPlane 2'], bpy.data.objects['Hip Center for DistalFemurFlexionSagittalPlane']], "Distal Femur Flexion Sagittal Plane 2")365 join_obj([bpy.data.objects["Distal Femur Flexion Sagittal Plane 1"], bpy.data.objects["Distal Femur Flexion Sagittal Plane 2"]], "Distal Femur Flexion Sagittal Plane")366 remove_doubles(bpy.data.objects["Distal Femur Flexion Sagittal Plane"])367 move_to_collection("Measurements", bpy.data.objects["Distal Femur Flexion Sagittal Plane"]) 368 else:369 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))370 return {'FINISHED'}371class FEMUR_OT_ProximalFemurVarusCoronalPlane(Operator):372 """ """373 bl_label = "Proximal Femur Varus Coronal Plane"374 bl_idname = "object.proximalfemurvaruscoronalplane"375 bl_options = {"REGISTER", "UNDO"}376 377 def execute(self, context):378 379 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])380 unhide_list(["Proximal Femur Varus Coronal Plane", 'Midshaft Femur Center P:Cor', 'GT P:Cor', 'Neck Center P:Cor', 'Hip Center'])381 382 try:383 delete_obj(bpy.data.objects["Proximal Femur Varus Coronal Plane"])384 except:385 pass386 check_list = check_obj_list(['Midshaft Femur Center P:Cor', 'GT P:Cor', 'Neck Center P:Cor', 'Hip Center'])387 if len(check_list) == 0:388 copy_object(bpy.data.objects['Midshaft Femur Center P:Cor'], 'Midshaft Femur Center P:Cor for ProximalVarusCoronalPlane L1')389 copy_object(bpy.data.objects['GT P:Cor'],'GT P:Cor for ProximalVarusCoronalPlane L1' )390 make_axis([bpy.data.objects['Midshaft Femur Center P:Cor for ProximalVarusCoronalPlane L1'], bpy.data.objects['GT P:Cor for ProximalVarusCoronalPlane L1']], "ProximalVarusCoronalPlane L1")391 392 copy_object(bpy.data.objects['Neck Center P:Cor'], 'Neck Center P:Cor for ProximalVarusCoronalPlane L2')393 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for ProximalVarusCoronalPlane L2')394 make_axis([bpy.data.objects['Neck Center P:Cor for ProximalVarusCoronalPlane L2'], bpy.data.objects['Hip Center for ProximalVarusCoronalPlane L2']], "ProximalVarusCoronalPlane L2")395 396 join_obj([bpy.data.objects["ProximalVarusCoronalPlane L1"], bpy.data.objects["ProximalVarusCoronalPlane L2"]], "Proximal Femur Varus Coronal Plane point")397 398 vertex_group(bpy.data.objects["Proximal Femur Varus Coronal Plane point"])399 copy_object(bpy.data.objects["Proximal Femur Varus Coronal Plane point"], "Proximal Femur Varus Coronal Plane point copy")400 401 copy_object(bpy.data.objects['Hip Center'], 'Hip Center ProximalVarusCoronalPlane')402 copy_object(bpy.data.objects['Midshaft Femur Center P:Cor'], 'Midshaft Femur Center P:Cor ProximalVarusCoronal')403 make_axis([bpy.data.objects["Proximal Femur Varus Coronal Plane point"], bpy.data.objects['Hip Center ProximalVarusCoronalPlane']], "ProximalFemurVarusCoronalPlane 1")404 make_axis([bpy.data.objects["Proximal Femur Varus Coronal Plane point copy"], bpy.data.objects['Midshaft Femur Center P:Cor ProximalVarusCoronal']], "ProximalFemurVarusCoronalPlane 2")405 join_obj([bpy.data.objects["ProximalFemurVarusCoronalPlane 1"], bpy.data.objects["ProximalFemurVarusCoronalPlane 2"]], "Proximal Femur Varus Coronal Plane")406 remove_doubles(bpy.data.objects["Proximal Femur Varus Coronal Plane"])407 408 move_to_collection("Measurements", bpy.data.objects["Proximal Femur Varus Coronal Plane"]) 409 else:410 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))411 return {'FINISHED'}412class FEMUR_OT_ProximalFemurFlexionSagittalPlane(Operator):413 """ """414 bl_label = "Proximal Femur Flexion Sagittal Plane"415 bl_idname = "object.proximalfemurflexionsagittalplane"416 bl_options = {"REGISTER", "UNDO"}417 418 def execute(self, context):419 420 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])421 unhide_list(["Proximal Femur Flexion Sagittal Plane", 'Midshaft Femur Center P:Sag', 'GT P:Sag', 'Neck Center P:Sag', 'Hip Center'])422 423 try:424 delete_obj(bpy.data.objects["Proximal Femur Flexion Sagittal Plane"])425 except:426 pass427 check_list = check_obj_list(['Midshaft Femur Center P:Sag', 'GT P:Sag', 'Neck Center P:Sag', 'Hip Center'])428 if len(check_list) == 0:429 copy_object(bpy.data.objects['Midshaft Femur Center P:Sag'], 'Midshaft Femur Center P:Sag ProximalFlexionSagittalPlane L1')430 copy_object(bpy.data.objects['GT P:Sag'], 'GT P:Sag ProximalFlexionSagittalPlane L1')431 make_axis([bpy.data.objects['Midshaft Femur Center P:Sag ProximalFlexionSagittalPlane L1'], bpy.data.objects['GT P:Sag ProximalFlexionSagittalPlane L1']], "Proximal Femur Flexion Sagittal Plane L1")432 433 copy_object(bpy.data.objects['Neck Center P:Sag'], 'Neck Center P:Sag for ProximalFemurFlexionSagittalPlane L2')434 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for ProximalFemurFlexionSagittalPlane L2')435 make_axis([bpy.data.objects['Neck Center P:Sag for ProximalFemurFlexionSagittalPlane L2'], bpy.data.objects['Hip Center for ProximalFemurFlexionSagittalPlane L2']], "Proximal Femur Flexion Sagittal Plane L2")436 437 join_obj([bpy.data.objects["Proximal Femur Flexion Sagittal Plane L1"], bpy.data.objects["Proximal Femur Flexion Sagittal Plane L2"]], "Proximal Femur Flexion Sagittal Plane point")438 439 vertex_group(bpy.data.objects['Proximal Femur Flexion Sagittal Plane point'])440 copy_object(bpy.data.objects['Proximal Femur Flexion Sagittal Plane point'], 'Proximal Femur Flexion Sagittal Plane point copy')441 442 copy_object(bpy.data.objects['Hip Center'], 'Hip Center for ProximalFemurFlexionSagittalPlane')443 copy_object(bpy.data.objects['GT P:Sag'], 'GT P:Sag for ProximalFemurFlexionSagittalPlane')444 make_axis([bpy.data.objects['Proximal Femur Flexion Sagittal Plane point'], bpy.data.objects['Hip Center for ProximalFemurFlexionSagittalPlane']], "ProximalFemurFlexionSagittalPlane 1")445 make_axis([bpy.data.objects['Proximal Femur Flexion Sagittal Plane point copy'], bpy.data.objects['GT P:Sag for ProximalFemurFlexionSagittalPlane']], "ProximalFemurFlexionSagittalPlane 2")446 join_obj([bpy.data.objects["ProximalFemurFlexionSagittalPlane 1"], bpy.data.objects["ProximalFemurFlexionSagittalPlane 2"]], "Proximal Femur Flexion Sagittal Plane")447 remove_doubles(bpy.data.objects["Proximal Femur Flexion Sagittal Plane"])448 move_to_collection("Measurements", bpy.data.objects["Proximal Femur Flexion Sagittal Plane"]) 449 else:450 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))451 return {'FINISHED'}452class FEMUR_OT_CondylarDifferenceCoronalPlane(Operator):453 """ """454 bl_label = "Condylar difference of knee in coronal plane"455 bl_idname = "object.condylardifferencecoronalplane"456 bl_options = {"REGISTER", "UNDO"}457 458 def execute(self, context):459 460 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])461 unhide_list(["Condylar difference in coronal plane", 'Lateral Distal P:Cor', "Sagittal Plane", 'Medial Distal P:Cor', 'Distal Plane'])462 463 try:464 delete_obj(bpy.data.objects["Condylar difference in coronal plane"])465 except:466 pass467 468 check_list = check_obj_list(['Lateral Distal P:Cor', "Sagittal Plane", 'Medial Distal P:Cor', 'Distal Plane'])469 if len(check_list) == 0:470 save_orientation_InEditMode(bpy.data.objects['Distal Plane'], "FACE")471 copy_object(bpy.data.objects['Lateral Distal P:Cor'], "Lateral Distal P:Cor_Sag")472 shrinkwrap_obj(bpy.data.objects["Lateral Distal P:Cor_Sag"], bpy.data.objects["Sagittal Plane"])473 474 cursor_to_obj(bpy.data.objects['Lateral Distal P:Cor_Sag'])475 add_plane("CondylarDifferenceCoronalPlane P1")476 transform_to_orientation()477 copy_object(bpy.data.objects['Medial Distal P:Cor'], "Medial Distal P:Cor_Sag")478 shrinkwrap_obj(bpy.data.objects["Medial Distal P:Cor_Sag"], bpy.data.objects["Sagittal Plane"])479 480 cursor_to_obj(bpy.data.objects["Medial Distal P:Cor_Sag"])481 add_plane("CondylarDifferenceCoronalPlane P2")482 transform_to_orientation()483 484 join_obj([bpy.data.objects["CondylarDifferenceCoronalPlane P1"], bpy.data.objects["CondylarDifferenceCoronalPlane P2"]], "Condylar difference in coronal plane")485 486 delete_obj(bpy.data.objects['Lateral Distal P:Cor_Sag'])487 delete_obj(bpy.data.objects['Medial Distal P:Cor_Sag'])488 move_to_collection("Measurements", bpy.data.objects["Condylar difference in coronal plane"]) 489 else:490 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))491 return {'FINISHED'}492class FEMUR_OT_CondylarDifferenceTransversePlane(Operator):493 """ """494 bl_label = "Posterior Condylar difference in transverse plane"495 bl_idname = "object.condylardifferencetransverseplane"496 bl_options = {"REGISTER", "UNDO"}497 498 def execute(self, context):499 500 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])501 unhide_list(["Posterior Condylar difference in transverse plane", 'Posterior Medial Point P:Dis', 'Posterior Lateral Point P:Dis', "Sagittal Plane",'Coronal Plane'])502 503 try:504 delete_obj(bpy.data.objects["Posterior Condylar difference in transverse plane"])505 except:506 pass507 check_list = check_obj_list(['Posterior Medial Point P:Dis', 'Posterior Lateral Point P:Dis', "Sagittal Plane", 'Coronal Plane'])508 if len(check_list) == 0:509 save_orientation_InEditMode(bpy.data.objects['Coronal Plane'], "FACE")510 copy_object(bpy.data.objects['Posterior Medial Point P:Dis'], "Posterior Medial Point P:Cor_Sag")511 shrinkwrap_obj(bpy.data.objects["Posterior Medial Point P:Cor_Sag"], bpy.data.objects["Sagittal Plane"])512 513 cursor_to_obj(bpy.data.objects["Posterior Medial Point P:Cor_Sag"])514 add_plane("Posterior Condylar difference in transverse plane P1")515 transform_to_orientation()516 copy_object(bpy.data.objects['Posterior Lateral Point P:Dis'], "Posterior Lateral Point P:Dis_Sag")517 shrinkwrap_obj(bpy.data.objects["Posterior Lateral Point P:Dis_Sag"], bpy.data.objects["Sagittal Plane"])518 519 cursor_to_obj(bpy.data.objects["Posterior Lateral Point P:Dis_Sag"])520 add_plane("Posterior Condylar difference in transverse plane P2")521 transform_to_orientation()522 523 join_obj([bpy.data.objects["Posterior Condylar difference in transverse plane P1"], bpy.data.objects["Posterior Condylar difference in transverse plane P2"]], "Posterior Condylar difference in transverse plane")524 525 delete_obj(bpy.data.objects['Posterior Medial Point P:Cor_Sag'])526 delete_obj(bpy.data.objects['Posterior Lateral Point P:Dis_Sag'])527 move_to_collection("Measurements", bpy.data.objects["Posterior Condylar difference in transverse plane"]) 528 else:529 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))530 return {'FINISHED'}531class FEMUR_OT_RotationalAngle(Operator):532 """ """533 bl_label = "Rotational Angle"534 bl_idname = "object.rotationalangle"535 bl_options = {"REGISTER", "UNDO"}536 def execute(self, context): 537 538 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])539 unhide_list(["Rotational Angle", 'Medial Epicondyle P:Dis', 'Lateral Epicondyle P:Dis', 'Posterior Medial Point P:Dis', 'Posterior Lateral Point P:Dis'])540 541 try:542 delete_obj(bpy.data.objects["Rotational Angle"])543 except:544 pass545 check_list = check_obj_list(['Medial Epicondyle P:Dis', 'Lateral Epicondyle P:Dis', 'Posterior Medial Point P:Dis', 'Posterior Lateral Point P:Dis'])546 if len(check_list) == 0:547 copy_object(bpy.data.objects['Medial Epicondyle P:Dis'], 'Medial Epicondyle P:Dis for RotationalAngle L1')548 copy_object(bpy.data.objects['Lateral Epicondyle P:Dis'], 'Lateral Epicondyle P:Dis for RotationalAngle L1')549 make_axis([bpy.data.objects['Medial Epicondyle P:Dis for RotationalAngle L1'], bpy.data.objects['Lateral Epicondyle P:Dis for RotationalAngle L1']], "RotationalAngle L1")550 551 copy_object(bpy.data.objects['Posterior Medial Point P:Dis'], 'Posterior Medial Point P:Dis for RotationalAngle L2')552 copy_object(bpy.data.objects['Posterior Lateral Point P:Dis'], 'Posterior Lateral Point P:Dis for RotationalAngle L2')553 make_axis([bpy.data.objects['Posterior Medial Point P:Dis for RotationalAngle L2'], bpy.data.objects['Posterior Lateral Point P:Dis for RotationalAngle L2']], "RotationalAngle L2")554 join_obj([bpy.data.objects["RotationalAngle L1"], bpy.data.objects["RotationalAngle L2"]], "Rotational Angle point")555 556 vertex_group(bpy.data.objects["Rotational Angle point"])557 copy_object(bpy.data.objects["Rotational Angle point"], "Rotational Angle point copy")558 559 copy_object(bpy.data.objects['Posterior Medial Point P:Dis'], 'Posterior Medial Point P:Dis for RotationalAngle')560 copy_object(bpy.data.objects['Medial Epicondyle P:Dis'], 'Medial Epicondyle P:Dis for RotationalAngle')561 make_axis([bpy.data.objects["Rotational Angle point"], bpy.data.objects['Posterior Medial Point P:Dis for RotationalAngle']], "RotationalAngle 1")562 make_axis([bpy.data.objects["Rotational Angle point copy"], bpy.data.objects['Medial Epicondyle P:Dis for RotationalAngle']], "RotationalAngle 2")563 join_obj([bpy.data.objects["RotationalAngle 1"], bpy.data.objects["RotationalAngle 2"]], "Rotational Angle")564 remove_doubles(bpy.data.objects["Rotational Angle"])565 move_to_collection("Measurements", bpy.data.objects["Rotational Angle"]) 566 else:567 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))568 return {'FINISHED'}569class FEMUR_OT_FemurTorsionalAngle(Operator):570 """ """571 bl_label = "Femur torsional angle"572 bl_idname = "object.femurtorsionalangle"573 bl_options = {"REGISTER", "UNDO"}574 575 def execute(self, context): 576 577 check_create_collection(["Landmarks", "Axes", "Planes", "Projections", "Measurements"])578 unhide_list(['Femur torsional angle', 'Hip Center P:Dis', 'Neck Center P:Dis', 'Posterior Medial Point P:Dis', 'Posterior Lateral Point P:Dis'])579 580 try:581 delete_obj(bpy.data.objects['Femur torsional angle'])582 except:583 pass584 check_list = check_obj_list(['Hip Center P:Dis', 'Neck Center P:Dis', 'Posterior Medial Point P:Dis', 'Posterior Lateral Point P:Dis'])585 if len(check_list) == 0:586 copy_object(bpy.data.objects['Hip Center P:Dis'], 'Hip Center P:Dis for FemurTorsionalAngle L1')587 copy_object(bpy.data.objects['Neck Center P:Dis'], 'Neck Center P:Dis for FemurTorsionalAngle L1')588 make_axis([bpy.data.objects['Hip Center P:Dis for FemurTorsionalAngle L1'], bpy.data.objects['Neck Center P:Dis for FemurTorsionalAngle L1']],"FemurTorsionalAngle L1")589 copy_object(bpy.data.objects['Posterior Medial Point P:Dis'], 'Posterior Medial Point P:Dis for FemurTorsionalAngle L2')590 copy_object(bpy.data.objects['Posterior Lateral Point P:Dis'], 'Posterior Lateral Point P:Dis for FemurTorsionalAngle L2')591 make_axis([bpy.data.objects['Posterior Medial Point P:Dis for FemurTorsionalAngle L2'], bpy.data.objects['Posterior Lateral Point P:Dis for FemurTorsionalAngle L2']],"FemurTorsionalAngle L2")592 593 join_obj([bpy.data.objects["FemurTorsionalAngle L1"], bpy.data.objects["FemurTorsionalAngle L2"]], 'Femur torsional angle point') 594 595 vertex_group(bpy.data.objects['Femur torsional angle point'])596 copy_object(bpy.data.objects['Femur torsional angle point'], 'Femur torsional angle point copy')597 598 copy_object(bpy.data.objects['Hip Center P:Dis'], 'Hip Center P:Dis for FemurTorsionalAngle')599 copy_object(bpy.data.objects['Posterior Medial Point P:Dis'], 'Posterior Medial Point P:Dis for FemurTorsionalAngle')600 make_axis([bpy.data.objects['Femur torsional angle point'], bpy.data.objects['Hip Center P:Dis for FemurTorsionalAngle']], "FemurTorsionalAngle 1")601 make_axis([bpy.data.objects['Femur torsional angle point copy'], bpy.data.objects['Posterior Medial Point P:Dis for FemurTorsionalAngle']], "FemurTorsionalAngle 2")602 join_obj([bpy.data.objects["FemurTorsionalAngle 1"], bpy.data.objects["FemurTorsionalAngle 2"]], "Femur torsional angle")603 remove_doubles(bpy.data.objects['Femur torsional angle'])604 move_to_collection("Measurements", bpy.data.objects['Femur torsional angle']) 605 else:606 self.report({'ERROR'}, "missing items:" + ", ".join(check_list))...
tree.py
Source:tree.py
1from copy import deepcopy2import numpy as np3import random4#########################5# BASE CLASSES #6#########################7class Node:8 def __init__(self):9 self.left = None10 self.right = None11 self.value = None12 self.parent = None13 def eval(self, X):14 raise NotImplementedError15 def __deepcopy__(self, memodict={}):16 raise NotImplementedError17class Tree:18 def __init__(self):19 self.root = None20 def getRoot(self):21 return self.root22 def setRoot(self, root):23 self.root = root24 def evalTree(self, X):25 return self.root.eval(X)26 def __str__(self):27 if(self.root != None):28 return self.root.__str__()29 def _getListOfNodes(self, node_list, node, position, depth):30 if (node == None):31 return depth-132 max_depth_left = self._getListOfNodes(node_list, node.left, 'left', depth+1)33 max_depth_right = self._getListOfNodes(node_list, node.right, 'right', depth+1)34 max_depth = max([max_depth_left, max_depth_right])35 node_list.append({36 'position': position,37 'node': node,38 'cur_depth': depth,39 'node_depth': max_depth - depth})40 return max_depth41 def getListOfNodes(self):42 node_list = []43 self._getListOfNodes(node_list, self.root.left, 'left', 1)44 self._getListOfNodes(node_list, self.root.right, 'right', 1)45 return node_list46 def _getMaxDepth(self, node, depth):47 if (node == None):48 return depth-149 50 max_depth_left = self._getMaxDepth(node.left, depth+1)51 max_depth_right = self._getMaxDepth(node.right, depth+1)52 return max(max_depth_left, max_depth_right)53 def getMaxDepth(self):54 max_depth_left = self._getMaxDepth(self.root.left, 1)55 max_depth_right = self._getMaxDepth(self.root.right, 1)56 return max(max_depth_left, max_depth_right)57 58 def __deepcopy__(self, memodict={}):59 copy_object = Tree()60 copy_object.root = deepcopy(self.root)61 return copy_object62#########################63# FUNCTIONS #64#########################65class Function(Node):66 def __init__(self, parent, n_features, rng):67 super().__init__()68 self.type = 'Function'69 self.n_features = n_features70 self.parent = parent71 self.rng = rng72class Sum(Function):73 def __init__(self, parent, n_features, rng):74 super().__init__(parent, n_features, rng)75 def eval(self, X):76 if (self.left == None or self.right == None):77 raise Exception('Left or Right value not set for Sum')78 79 return np.add(self.left.eval(X), self.right.eval(X))80 def __str__(self):81 return '({} + {})'.format(self.left.__str__(), self.right.__str__())82 def __deepcopy__(self, memodict={}):83 copy_object = Sum(None, self.n_features, self.rng)84 copy_object.parent = None85 copy_object.left = deepcopy(self.left)86 copy_object.right = deepcopy(self.right)87 copy_object.left.parent = copy_object88 copy_object.right.parent = copy_object89 return copy_object90class Multiply(Function):91 def __init__(self, parent, n_features, rng):92 super().__init__(parent, n_features, rng)93 def eval(self, X):94 if (self.left == None or self.right == None):95 raise Exception('Left or Right value not set for Multiply')96 97 return np.multiply(self.left.eval(X), self.right.eval(X))98 def __str__(self):99 return '({} * {})'.format(self.left.__str__(), self.right.__str__())100 def __deepcopy__(self, memodict={}):101 copy_object = Multiply(None, self.n_features, self.rng)102 copy_object.parent = None103 copy_object.left = deepcopy(self.left)104 copy_object.right = deepcopy(self.right)105 copy_object.left.parent = copy_object106 copy_object.right.parent = copy_object107 return copy_object108class Subtraction(Function):109 def __init__(self, parent, n_features, rng):110 super().__init__(parent, n_features, rng)111 def eval(self, X):112 if (self.left == None or self.right == None):113 raise Exception('Left or Right value not set for Subtraction')114 115 return np.subtract(self.left.eval(X), self.right.eval(X))116 def __str__(self):117 return '({} - {})'.format(self.left.__str__(), self.right.__str__())118 def __deepcopy__(self, memodict={}):119 copy_object = Subtraction(None, self.n_features, self.rng)120 copy_object.parent = None121 copy_object.left = deepcopy(self.left)122 copy_object.right = deepcopy(self.right)123 copy_object.left.parent = copy_object124 copy_object.right.parent = copy_object125 return copy_object126 127class Division(Function):128 def __init__(self, parent, n_features, rng):129 super().__init__(parent, n_features, rng)130 def eval(self, X):131 if (self.left == None or self.right == None):132 raise Exception('Left or Right value not set for Division')133 134 with np.errstate(divide='ignore', invalid='ignore'):135 result = np.true_divide(self.left.eval(X), np.round(self.right.eval(X), 2))136 result[np.isinf(result)] = 1137 result[np.isnan(result)] = 1138 return result139 def __str__(self):140 return '({} / {})'.format(self.left.__str__(), self.right.__str__())141 def __deepcopy__(self, memodict={}):142 copy_object = Division(None, self.n_features, self.rng)143 copy_object.parent = None144 copy_object.left = deepcopy(self.left)145 copy_object.right = deepcopy(self.right)146 copy_object.left.parent = copy_object147 copy_object.right.parent = copy_object148 return copy_object149#########################150# TERMINALS #151#########################152class Terminal(Node):153 def __init__(self, parent, n_features, rng):154 super().__init__()155 self.type = 'Terminal'156 self.n_features = n_features157 self.parent = parent158 self.rng = rng159class Value(Terminal):160 def __init__(self, parent, n_features, rng):161 super().__init__(parent, n_features, rng)162 self.value = round(rng.uniform(-2, 2), 2)163 def eval(self, X):164 return np.full(X.shape[0], self.value)165 166 def __str__(self):167 return str(self.value)168 def __deepcopy__(self, memodict={}):169 copy_object = Value(None, self.n_features, self.rng)170 copy_object.value = self.value171 copy_object.parent = None172 copy_object.left = None173 copy_object.right = None174 return copy_object175class Variable(Terminal):176 def __init__(self, parent, n_features, rng):177 super().__init__(parent, n_features, rng)178 # Value here represents the i position of the X vector179 self.value = rng.randint(0, n_features)180 def eval(self, X):181 return X[:,self.value]182 def __str__(self):183 return 'X{}'.format(self.value)184 def __deepcopy__(self, memodict={}):185 copy_object = Variable(None, self.n_features, self.rng)186 copy_object.value = self.value187 copy_object.parent = None188 copy_object.left = None189 copy_object.right = None190 return copy_object191class Sin(Terminal):192 def __init__(self, parent, n_features, rng):193 super().__init__(parent, n_features, rng)194 self.value = rng.randint(0, n_features)195 def eval(self, X):196 return np.sin(X[:,self.value])197 def __str__(self):198 return '(sin(X{}))'.format(self.value)199 def __deepcopy__(self, memodict={}):200 copy_object = Sin(None, self.n_features, self.rng)201 copy_object.value = self.value202 copy_object.parent = None203 copy_object.left = None204 copy_object.right = None205 return copy_object206class Cos(Terminal):207 def __init__(self, parent, n_features, rng):208 super().__init__(parent, n_features, rng)209 self.value = rng.randint(0, n_features)210 def eval(self, X):211 return np.cos(X[:,self.value])212 def __str__(self):213 return '(cos(X{}))'.format(self.value)214 def __deepcopy__(self, memodict={}):215 copy_object = Cos(None, self.n_features, self.rng)216 copy_object.value = self.value217 copy_object.parent = None218 copy_object.left = None219 copy_object.right = None220 return copy_object221# X = np.array([[1, 2, 3], [3, 4, 5], [6, 7, 8]])222# a = Sum()223# a.left = Multiply()224# a.left.left = Variable(0)225# a.left.right = Value(2)226# a.right = Variable(1)227# t = Tree()228# t.setRoot(a)229# print(t.evalTree(X))...
test_game_state.py
Source:test_game_state.py
...3from unittest import IsolatedAsyncioTestCase4from jsonschema import validate5import copy6import json7def copy_object(data):8 return copy.deepcopy(data)9def create_mock_tick_packet(tick_number, events):10 return{"type": "tick", "payload": {11 "tick": tick_number, "events": copy_object(events)}}12mock_state = {"agent_state": {"0": {"coordinates": [6, 7], "hp": 3, "inventory": {"bombs": 3}, "blast_diameter": 3, "number": 0, "invulnerability": 0}, "1": {"coordinates": [6, 6], "hp": 3, "inventory": {"bombs": 3}, "blast_diameter": 3, "number": 1, "invulnerability": 0}}, "entities": [{"x": 8, "y": 4, "type": "m"}, {"x": 2, "y": 5, "type": "m"}, {"x": 2, "y": 6, "type": "m"}, {"x": 2, "y": 7, "type": "m"}, {"x": 1, "y": 5, "type": "m"}, {"x": 6, "y": 8, "type": "m"}, {"x": 4, "y": 4, "type": "m"}, {"x": 8, "y": 6, "type": "m"}, {"x": 3, "y": 3, "type": "m"}, {"x": 0, "y": 5, "type": "m"}, {"x": 8, "y": 2, "type": "m"}, {"x": 1, "y": 8, "type": "m"}, {"x": 2, "y": 2, "type": "m"}, {"x": 5, "y": 6, "type": "m"}, {"x": 3, "y": 0, "type": "m"}, {"x": 2, "y": 8, "type": "m"}, {"x": 8, "y": 1, "type": "m"}, {13 "x": 6, "y": 2, "type": "w"}, {"x": 2, "y": 3, "type": "w"}, {"x": 3, "y": 5, "type": "w"}, {"x": 7, "y": 6, "type": "w"}, {"x": 4, "y": 3, "type": "w"}, {"x": 0, "y": 3, "type": "w"}, {"x": 5, "y": 3, "type": "w"}, {"x": 3, "y": 2, "type": "w"}, {"x": 5, "y": 5, "type": "w"}, {"x": 8, "y": 7, "type": "w"}, {"x": 3, "y": 7, "type": "w"}, {"x": 1, "y": 7, "type": "w"}, {"x": 4, "y": 0, "type": "w"}, {"x": 3, "y": 6, "type": "w"}, {"x": 0, "y": 0, "type": "w"}, {"x": 7, "y": 5, "type": "w"}, {"x": 3, "y": 4, "type": "w"}, {"x": 0, "y": 8, "type": "w"}, {"x": 8, "y": 3, "type": "w"}, {"x": 6, "y": 0, "type": "w"}, {"x": 1, "y": 4, "type": "o"}, {"x": 0, "y": 4, "type": "o"}, {"x": 3, "y": 8, "type": "o"}, {"x": 0, "y": 2, "type": "o"}, {"x": 4, "y": 7, "type": "o"}], "world": {"width": 9, "height": 9}, "connection": {"id": 7, "role": "agent", "agent_number": 0},14 "tick": 0,15 "config": {16 "tick_rate_hz": 20,17 "game_duration_ticks": 10,18 "fire_spawn_interval_ticks": 519}}20mock_state_packet = {"type": "game_state",21 "payload": copy_object(mock_state)}22mock_tick_spawn_packet = create_mock_tick_packet(33, [23 {"type": "entity_spawned", "data": {"x": 5, "y": 4, "type": "a", "expires": 73}}])24mock_tick_expired_packet = create_mock_tick_packet(69, [25 {"type": "entity_expired", "data": [5, 4]}])26mock_agent_state_payload = {"coordinates": [6, 7], "hp": 3, "inventory": {27 "bombs": 2}, "blast_diameter": 3, "number": 0, "invulnerability": 0}28mock_tick_agent_state_packet = create_mock_tick_packet(50, [{"type": "agent_state",29 "data": mock_agent_state_payload}])30mock_tick_agent_action_packet = create_mock_tick_packet(31 5, [{"type": "agent", "data": [0, {"type": "move", "move": "left"}]}])32class TestGameState(IsolatedAsyncioTestCase):33 def setUp(self):34 self.client = GameState("")35 self.maxDiff = None36 def assert_object_equal(self, first, second):37 j1 = json.dumps(first, sort_keys=True, indent=4)38 j2 = json.dumps(second, sort_keys=True, indent=4)39 self.assertEqual(j1, j2)40 def test_mocks_are_valid_with_latest_schema(self):41 with open('validation.schema.json') as f:42 schema = json.load(f)43 validate(instance=mock_state_packet, schema=schema.get(44 "definitions").get("ValidServerPacket"))45 validate(instance=mock_tick_spawn_packet, schema=schema.get(46 "definitions").get("ValidServerPacket"))47 validate(instance=mock_tick_agent_action_packet, schema=schema.get(48 "definitions").get("ValidServerPacket"))49 validate(instance=mock_tick_agent_state_packet, schema=schema.get(50 "definitions").get("ValidServerPacket"))51 async def test_initial_game_state_constructor(self):52 self.assertTrue(self.client._state == None)53 self.assertTrue(self.client._connection_string == "")54 self.assertTrue(self.client._tick_callback == None)55 async def test_on_game_state_payload(self):56 await self.client._on_data(mock_state_packet)57 expected = copy_object(mock_state)58 self.assertEqual(self.client._state, expected)59 async def test_on_game_entity_spawn_packet(self):60 await self.client._on_data(copy_object(mock_state_packet))61 await self.client._on_data(copy_object(mock_tick_spawn_packet))62 expected = copy_object(mock_state)63 expected["entities"].append(64 {"x": 5, "y": 4, "type": "a", "expires": 73})65 self.assert_object_equal(self.client._state, expected)66 async def test_on_game_entity_expired_packet(self):67 await self.client._on_data(copy_object(mock_state_packet))68 await self.client._on_data(copy_object(mock_tick_spawn_packet))69 await self.client._on_data(copy_object(mock_tick_expired_packet))70 expected = copy_object(mock_state)71 self.assert_object_equal(self.client._state, expected)72 async def test_on_agent_state_packet(self):73 await self.client._on_data(copy_object(mock_state_packet))74 await self.client._on_data(copy_object(mock_tick_agent_state_packet))75 expected = copy_object(mock_state)76 expected["agent_state"]["0"] = mock_agent_state_payload77 self.assert_object_equal(78 self.client._state, expected)79 async def test_on_agent_move_packet(self):80 await self.client._on_data(copy_object(mock_state_packet))81 await self.client._on_data(copy_object(mock_tick_agent_action_packet))82 expected = copy_object(mock_state)83 expected["agent_state"]["0"]["coordinates"] = [5, 7]84 self.assert_object_equal(85 self.client._state, expected)86if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!