def task_1_ShowBase(): strOf_FuncName = "task_1_ShowBase" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 2 ###################''' # class MyApp(ShowBase): # # def __init__(self): # ShowBase.__init__(self) # # # Load the environment model. # self.scene = self.loader.loadModel("models/environment") # # Reparent the model to render. # self.scene.reparentTo(self.render) # # Apply scale and position transforms on the model. # self.scene.setScale(0.25, 0.25, 0.25) # # #code:20210708_162726 # # valOf_Pos_X, valOf_Pos_Y, valOf_Pos_Z = -100, 42, 0 # # valOf_Pos_X, valOf_Pos_Y, valOf_Pos_Z = -400, 42, 0 # # valOf_Pos_X, valOf_Pos_Y, valOf_Pos_Z = -400, 420, 0 # # valOf_Pos_X, valOf_Pos_Y, valOf_Pos_Z = -400, 42, 100 # valOf_Pos_X, valOf_Pos_Y, valOf_Pos_Z = -800, 42, 0 # # valOf_Pos_X = -100 # # # valOf_Pos_X = -50 # # valOf_Pos_Y = 42 # # valOf_Pos_Z = 0 # self.scene.setPos(valOf_Pos_Y, valOf_Pos_Y, valOf_Pos_Z) # # self.scene.setPos(-8, 42, 0) '''################### step : 3 ###################''' app = MyApp() print() print ("[%s:%d] 'MyApp' ==> instantiated" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) app.run()
def task_1_Bullet_Hello_World(): strOf_FuncName = "task_1_Bullet_Hello_World" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 2 ###################''' base.cam.setPos(0, -10, 0) base.cam.lookAt(0, 0, 0) # World world = BulletWorld() world.setGravity(Vec3(0, 0, -9.81)) # Plane shape = BulletPlaneShape(Vec3(0, 0, 1), 1) node = BulletRigidBodyNode('Ground') node.addShape(shape) np = render.attachNewNode(node) np.setPos(0, 0, -2) world.attachRigidBody(node) # Box shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5)) node = BulletRigidBodyNode('Box') node.setMass(1.0) node.addShape(shape) np = render.attachNewNode(node) np.setPos(0, 0, 2) world.attachRigidBody(node) model = loader.loadModel('models/box.egg') model.flattenLight() model.reparentTo(np) # Update def update(task): dt = globalClock.getDt() world.doPhysics(dt) return task.cont taskMgr.add(update, 'update') base.run() '''###################
def test_13_Scale(): strOf_FuncName = "test_13_Scale" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 1.1 load : data ###################''' '''################### step : 2 prep ###################''' #ref https://www.w3schools.com/python/python_ml_multiple_regression.asp df = pandas.read_csv("cars2.csv") X = df[['Weight', 'Volume']] scale = StandardScaler() '''################### step : 3 scale ###################''' scaledX = scale.fit_transform(X) '''################### step : 4 report ###################''' #debug # message print ("[%s:%d] scaledX ==>" % ( os.path.basename(libs.thisfile()) , libs.linenum() ) ) print(scaledX)
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) '''################### step : 2 prep ###################''' aryOf_Numbers = [1, 2, 3, 4, 5] print() print( "[%s:%d] aryOf_Numbers =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(aryOf_Numbers) print() '''################### step : 3 transposition ###################''' #code:20210728_132436 # pointOf_Tsp = 0 pointOf_Tsp = 3 print("[%s:%d] pointOf_Tsp => %d" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), pointOf_Tsp)) print( "[%s:%d] calling ... => tsp()" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print() #code:20210728_132743 aryOf_Numbers = tsp(aryOf_Numbers, pointOf_Tsp) # tsp(aryOf_Numbers, pointOf_Tsp) print( "[%s:%d] tsp() => complete" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print( "[%s:%d] aryOf_Numbers is now ... =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(aryOf_Numbers) print()
def build_VTK_File(): fname_In = "data/paradata.%s.vtk" % (libs.get_TimeLabel_Now()) fout = open(fname_In, "w") grid = 10 dim = grid*2+1 points = dim**3 r = 0.8 fout.write("# vtk DataFile Version 1.0\n") fout.write("test\n") fout.write("ASCII\n") fout.write("DATASET STRUCTURED_POINTS\n") #ref sprintf https://blog.udemy.com/ruby-sprintf/ fout.write( "DIMENSIONS %d %d %d\n" % (dim, dim, dim)) # fout.write( sprintf("DIMENSIONS %d %d %d\n",dim, dim, dim)) fout.write("ORIGIN 0.0 0.0 0.0\n") fout.write("ASPECT_RATIO 1.0 1.0 1.0\n") fout.write("\n") fout.write( "POINT_DATA %s\n" % (points)) fout.write("SCALARS scalars float\n") fout.write("LOOKUP_TABLE default\n") '''################### data ###################''' for ix in range(-grid, grid + 1) : for iy in range(-grid, grid + 1) : for iz in range(-grid, grid + 1) : x = ix * 1.0 / grid y = iy * 1.0 / grid z = iz * 1.0 / grid # x = ix.to_f/grid # y = iy.to_f/grid # z = iz.to_f/grid v = r*r - (x*x + y*y + z*z) if v < 0: v = 0 fout.write("%d\n" % (v)) else: fout.write("%.7f\n" % (v)) fout.close()
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) '''################### step : 2 build : string ###################''' strOf_Coordinates = "(a_i^%d)*(a_j^%d)" # strOf_Coordinates = "(a_i^k)*(a_i^l)" # strOf_Coordinates = "a_i^k*a_i^l" strOf_Formula = "" # strOf_Formula = strOf_Coordinates # wedge #ref https://wiki.python.org/moin/ForLoop for x in range(1, 4): for y in range(1, 4): #ref https://stackoverflow.com/questions/5309978/sprintf-like-functionality-in-python strOf_Formula += strOf_Coordinates % (x, y) strOf_Formula += "(" strOf_Formula += "e->_%d∧e->_%d" % (x, y) # strOf_Formula += "e->_%d" % (x) strOf_Formula += ")" strOf_Formula += "+" #/for x in range(1, 4): print() print( "[%s:%d] strOf_Formula ==> " % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(strOf_Formula) print() '''###################
def test_5__Numbering(): '''################### prep : get root ###################''' fpath = cons31.FPath.dpath_In_CSV.value \ + "/" \ + cons31.FPath.fname_In_XML.value ### backup fname_Out_Backup = "%s.copy.%s.mm" % (fpath, libs.get_TimeLabel_Now()) copyfile(fpath, fname_Out_Backup) '''################### parse ###################''' tree = ET.parse(fpath) tree = libmt.add_Numbering__Through(tree) # tree = libmt.add_Numbering(tree) '''################### append : child ###################''' # #ref https://stackoverflow.com/questions/31259847/python-appending-children-to-an-already-created-xml-files-root-using-xml-dom # data1 = ET.Element("node", {"TEXT": "something_" + libs.get_TimeLabel_Now()}) # # data1 = ET.Element("node", {"TEXT": "something_v001.0002.ma"}) # # data2 = ET.Element("attribute", {"NAME": "created" # # , "VALUE" : "18/01/23" # }) # # data1.append(data2) # # g2[0].append(data1) # '''################### save xml ###################''' label = "add-numbering-through" fpath_Out = fpath # fpath_Out = "new.%s.%s.mm" % (label, libs.get_TimeLabel_Now()) tree.write(fpath_Out) print() print ("[%s:%d] mm => written : %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), fpath_Out))
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) # path var print( "[%s:%d] PATH variable : " % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(sys.path) '''################### step : 2 sys.argv ###################''' print() print( "[%s:%d] sys.argv ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(sys.argv) '''################### step : 2 : 1 arg : 2nd arg ###################''' arg_2 = sys.argv[1] print() print( "[%s:%d] sys.argv[1] ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(arg_2) print("type(arg_2) => ", type(arg_2))
def test_2__WriteFile(): ### write file dpath = "./blog/data" # dpath = "./blog" fname_Out = "test.%s.txt" % (libs.get_TimeLabel_Now()) # fname_Out = "./test.%s.txt" % (libs.get_TimeLabel_Now()) fpath = "%s/%s" % (dpath, fname_Out) f = open(fpath, "w") # f = open(fname_Out, "w") # f = open("test.txt", "w") f.write("yes\n") f.close() return fpath
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) # path var print( "[%s:%d] PATH variable : " % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(sys.path)
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 2 ###################''' fig = plt.figure() # syntax for 3-D projection ax = plt.axes(projection ='3d') # defining all 3 axes x = [1,1,-1, 2,2,2,2] y = [-1,1,1, -1,1,2,-2] z = [1,-1,1, 1/2,-1/2,-1/4,1/4] # z = np.linspace(0, 1, 100) # x = z * np.sin(25 * z) # y = z * np.cos(25 * z) # plotting ax.plot3D(x, y, z, 'green') ax.set_title('3D line plot geeks for geeks') plt.show() '''###################
def test_2_statistics_StdDev(): strOf_FuncName = "test_2_statistics_StdDev" '''################### step : 1 std ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) # from scipy import stats speed = [99,86,87,88,111,86,103,87,94,78,77,85,86] x_std = numpy.std(speed) print() print("speed : ", speed) print() print("x_std : ", x_std) '''################### step : 2 variance ###################''' x_var = numpy.var(speed) print() print("x_var : ", x_var)
def test_3_stats_Percentile(): strOf_FuncName = "test_2_statistics_StdDev" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) #code:20210411_171845 ages = [5,31,43,48,50,41,7,11,15,39,80,82,32,2,8,6,25,36,27,61,31] numOf_Percentile = 75 '''################### step : 2 percentile ###################''' x_Percentile = numpy.percentile(ages, numOf_Percentile) print() print("ages : ", ages) print() print("x_Percentile : ", x_Percentile) '''###################
def exec_prog(): # upto : 20180109_161654 '''###################################### get data : raw csv rows ######################################''' #ref enum https://qiita.com/methane/items/8612bdefd8fa4238cc44 #ref https://docs.python.org/3.5/library/enum.html fname_In = cons.FPath.dpath_In_CSV.value \ + "/" \ + cons.FPath.fname_In_CSV.value # fname_In = cons.FPath.fpath_In_CSV.value # fname_In = cons.FPath.fname_In_CSV.value # fname_In = "../data/49_11_file-io.USDJPY.Period-H1.Days-720.Bars-17280.20171231_233725.csv" #=> '''### ###''' header_Length = 2 # header_Length = 3 # skip_Header = True skip_Header = False result = libfx.get_ChartData_CSV(\ fname_In, header_Length, skip_Header) ### Validate if result == None: #if result == None print ("[%s:%d] get_ChartData_CSV => Returned 'None'" % \ (libs.thisfile(), libs.linenum())) # print "[%s:%d] get_ChartData_CSV => Returned 'None'" % \ # (libs.thisfile(), libs.linenum()) return #/if result == None ### report print() print("[%s:%d] CSV rows => %d" % (os.path.basename(libs.thisfile()), libs.linenum(), len(result))) print() print("[%s:%d] row[%d] => %s" % (os.path.basename(libs.thisfile()), libs.linenum(), 0, result[0])) print() '''###################################### Conv : CSV rows ---> array of BarData class instances ######################################''' # aryOf_BarDatas = libfx.conv_CSVRows_2_BarDatas(result) aryOf_BarDatas = libfx.conv_CSVRows_2_BarDatas(result[header_Length:]) ### Validate if aryOf_BarDatas == None: #if aryOf_BarDatas == None print("[%s:%d] aryOf_BarDatas => None" % (os.path.basename(libs.thisfile()), libs.linenum())) print() return #/if aryOf_BarDatas == None '''################### get : high-lows ###################''' id_Start = cons.BarData.HighLowDiff_ID_Start.value id_End = cons.BarData.HighLowDiff_ID_End.value # typeOf_Data = cons.typeOf_Data_OPENCLOSE # typeOf_Data = "OpenClose" result_HighLowDiffs = libfx.get_HighLowDiffs(aryOf_BarDatas, id_Start, id_End) # result = libfx.get_HighLowDiffs(aryOf_BarDatas, typeOf_Data, id_Start, id_End) print ("[%s:%d] result => %s" % \ (libs.thisfile(), libs.linenum(), result_HighLowDiffs)) # print "[%s:%d] result[%s] => %s" % \ # (libs.thisfile(), libs.linenum(), cons.LABEL_OC, result_HighLowDiffs[cons.LABEL_OC]) # print "[%s:%d] result[%s] => %s" % \ # (libs.thisfile(), libs.linenum(), cons.LABEL_HL, result_HighLowDiffs[cons.LABEL_HL]) print() '''################### add : data ###################''' whole_Data = {} whole_Data['data'] = result_HighLowDiffs print() print("[%s:%d] whole data =>" % (os.path.basename(libs.thisfile()), libs.linenum())) print(whole_Data) '''################### build : meta info ###################''' dictOf_MetaInfo = libfx.get_BarData_MetaInfo(fname_In, header_Length) print() print ("[%s:%d] dictOf_MetaInfo => %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), dictOf_MetaInfo)) print() ### add : meta info whole_Data['meta'] = dictOf_MetaInfo '''################### write to file ###################''' # [82_1.py:163] dictOf_MetaInfo => {'PAIR': 'USDJPY', 'PERIOD': 'H1', 'DAYS': '720 # ', 'SHIFT': '1'} # fname_Out_HighLowDiffs = \ fpath_Out_HighLowDiffs = \ cons.FPath.fpath_Out_HighLowDiff.value \ + "/" \ + "_HighLowDiff_." \ + cons.Label_ColNames.PAIR.value + "-" \ + dictOf_MetaInfo[cons.Label_ColNames.PAIR.value] \ + "." \ + cons.Label_ColNames.PERIOD.value + "-" \ + dictOf_MetaInfo[cons.Label_ColNames.PERIOD.value] \ + "." \ + cons.Label_ColNames.DAYS.value + "-" \ + dictOf_MetaInfo[cons.Label_ColNames.DAYS.value] \ + "." \ + cons.Label_ColNames.SHIFT.value + "-" \ + dictOf_MetaInfo[cons.Label_ColNames.SHIFT.value] \ + "." \ + libs.get_TimeLabel_Now() \ + ".csv" # + ".txt" print ("[%s:%d] fname out => %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), fpath_Out_HighLowDiffs)) ### write to file f_Out = open(fpath_Out_HighLowDiffs, "w") '''################### meta info ###################''' f_Out.write( '\t'.join( [cons.Label_ColNames.PAIR.value, \ cons.Label_ColNames.PERIOD.value, \ cons.Label_ColNames.DAYS.value, \ cons.Label_ColNames.SHIFT.value, \ "id_Start", \ "id_End", \ "source csv" ]) ) f_Out.write('\n') f_Out.write( '\t'.join( [ dictOf_MetaInfo[cons.Label_ColNames.PAIR.value], \ dictOf_MetaInfo[cons.Label_ColNames.PERIOD.value], \ dictOf_MetaInfo[cons.Label_ColNames.DAYS.value], \ dictOf_MetaInfo[cons.Label_ColNames.SHIFT.value], \ str(id_Start), \ str(id_End), \ cons.FPath.fname_In_CSV.value ]) ) f_Out.write('\n') f_Out.write("\t\t\t\t" \ + aryOf_BarDatas[id_Start - 1].dateTime_Local \ + '\t' \ + aryOf_BarDatas[id_End - 1].dateTime_Local ) f_Out.write('\n') '''################### High, low, diff ###################''' # f_Out.write('\t'.join([cons.BarData.LABEL_OC.value].extend(result_HighLowDiffs[cons.BarData.LABEL_OC.value]))) # tmp = [cons.BarData.LABEL_OC.value] # tmp = result_HighLowDiffs[cons.BarData.LABEL_OC.value] # tmp = [cons.BarData.LABEL_OC.value].extend(['aaa']) # tmp = [cons.BarData.LABEL_OC.value] \ # .extend(result_HighLowDiffs[cons.BarData.LABEL_OC.value]) ### OC tmp = [cons.BarData.LABEL_OC.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_OC.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### HL tmp = [cons.BarData.LABEL_HL.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_HL.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### RSI tmp = [cons.BarData.LABEL_RSI.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_RSI.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### MFI tmp = [cons.BarData.LABEL_MFI.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_MFI.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### BB_MAIN tmp = [cons.BarData.LABEL_BB_MAIN.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_BB_MAIN.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### BB_1S tmp = [cons.BarData.LABEL_BB_1S.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_BB_1S.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### BB_2S tmp = [cons.BarData.LABEL_BB_2S.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_BB_2S.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### BB_M1S tmp = [cons.BarData.LABEL_BB_M1S.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_BB_M1S.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') ### BB_M2S tmp = [cons.BarData.LABEL_BB_M2S.value] tmp.extend(result_HighLowDiffs[cons.BarData.LABEL_BB_M2S.value]) f_Out.write('\t'.join([str(x) for x in tmp])) # f_Out.write('\t'.join(tmp)) f_Out.write('\n') # print() # print ("[%s:%d] OC data => " % (os.path.basename(libs.thisfile()), libs.linenum())) # print(tmp) # print() '''################### file : close ###################''' f_Out.close() print() print ("[%s:%d] file closed => %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), fpath_Out_HighLowDiffs)) print() # #test # print() # print ("[%s:%d] aryOf_BarDatas[id_Start(%d)] => %s" % \ # (os.path.basename(libs.thisfile()), libs.linenum(), # id_Start, aryOf_BarDatas[id_Start])) # # print(aryOf_BarDatas[id_Start].dateTime_Local) # # print(aryOf_BarDatas[id_Start]) # # print() '''################### Report ###################''' print("[%s:%d] exec_prog => done" % (os.path.basename(libs.thisfile()), libs.linenum()))
def task_1_PyBullet_Hello_World(): strOf_FuncName = "task_1_PyBullet_Hello_World" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 2 tut codes https://docs.google.com/document/d/10sXEhzFRSnvFcl3XxNGhnD4N2SedqwdAvK3dsihxVUA/edit# ###################''' physicsClient = p.connect(p.GUI)#or p.DIRECT for non-graphical version p.setAdditionalSearchPath(pybullet_data.getDataPath()) #optionally p.setGravity(0,0,-10) print() print ("[%s:%d] tut : first 3 lines" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) print("type(physicsClient).__name__ ==> ", type(physicsClient).__name__) print() # separator line '''################### step : 2 : 2 ###################''' planeId = p.loadURDF("plane.urdf") startPos = [0,0,1] startOrientation = p.getQuaternionFromEuler([0,0,0]) boxId = p.loadURDF("r2d2.urdf",startPos, startOrientation) print ("[%s:%d] tut : next 4 lines" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) print("type(planeId).__name__ ==> ", type(planeId).__name__) print() # separator line #n:20210715_180327 #set the center of mass frame (loadURDF sets base link frame) startPos/Ornp.resetBasePositionAndOrientation(boxId, startPos, startOrientation) for i in range (10000): p.stepSimulation() time.sleep(1./240.) #debug:20210717_174601 print ("[%s:%d] for loop ==> ended" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) print() # separator line cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId) print("cubePos = %d, cubeOrn = %d" % (cubePos, cubeOrn)) # print(cubePos,cubeOrn) p.disconnect() print ("[%s:%d] pybullet ==> disconnected" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) print() # separator line '''###################
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) print() '''################### step : 2 data : load ###################''' #mark:20210421_164025 iris_dataset = load_iris() #debug print("[%s:%d] load_iris => done" % (os.path.basename(libs.thisfile()), libs.linenum())) # print("iris_dataset['DESCR'] ==>") # print(iris_dataset['DESCR']) # print(iris_dataset) '''################### step : 3 data : train ###################''' X_train, X_test, y_train, y_test = train_test_split(iris_dataset["data"], iris_dataset["target"], random_state=0) #debug print("[%s:%d] train_test_split => done" % (os.path.basename(libs.thisfile()), libs.linenum())) # print("X_train ==>") # print(X_train) # # print() # print("X_test ==>") # print(X_test) '''################### step : 4 data : neighbor ###################''' '''################### step : 4 : 1 data : setup ###################''' kn = KNeighborsClassifier(n_neighbors=1) #debug print("[%s:%d] KNeighborsClassifier => done" % (os.path.basename(libs.thisfile()), libs.linenum())) print("kn ==>") print(kn) '''################### step : 4 : 2 data : fit ###################''' kn.fit(X_train, y_train) #debug print("[%s:%d] KNeighborsClassifier : fit => done" % (os.path.basename(libs.thisfile()), libs.linenum())) print("kn ==>") print(kn) '''################### step : 4 : 3 data : prep ###################''' x_new = np.array([[5, 2.9, 1, 0.2]]) #debug print("[%s:%d] np.array => done" % (os.path.basename(libs.thisfile()), libs.linenum())) print("x_new ==>") print(x_new) '''################### step : 5 data : predict ###################''' prediction = kn.predict(x_new) #debug print("[%s:%d] predict => done" % (os.path.basename(libs.thisfile()), libs.linenum())) # print("prediction ==>") # print(prediction) '''################### step : 6 results ###################''' prediction = kn.predict(x_new) #debug print("[%s:%d] results => " % (os.path.basename(libs.thisfile()), libs.linenum())) print("probe data =>") print(x_new) print() print("Predicted target value: {}\n".format(prediction)) print("Predicted feature name: {}\n".format( iris_dataset["target_names"][prediction])) print("Test score: {:.2f}".format(kn.score(X_test, y_test))) #debug print("[%s:%d] X_test => " % (os.path.basename(libs.thisfile()), libs.linenum())) print("X_test =>") print(X_test) print() print("y_test =>") print(y_test) '''###################
def test_S_14_No_2_Scipy(): strOf_FuncName = "test_S_14_No_2_Scipy" '''################### step : 1 opening, vars ###################''' #ref https://stackoverflow.com/questions/56711424/how-can-i-count-time-in-python-3 t_start = time.time() print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) print() '''################### step : 2 data : prep ###################''' x = np.array([[1, 2], [3, 4]]) y = np.array([[5, 6], [7, 8]]) #debug print("[%s:%d] x, y :" % (os.path.basename(libs.thisfile()), libs.linenum())) print(x) print(y) v = np.array([9, 10]) w = np.array([11, 12]) '''################### step : 2 : 1 inner prod ###################''' valOf_InnerProd = np.dot(v, w) #debug print("[%s:%d] valOf_InnerProd :" % (os.path.basename(libs.thisfile()), libs.linenum())) print(v) print(w) print(valOf_InnerProd) print() '''################### step : 2 : 2 vector prod ###################''' valOf_Vector_Prod = np.dot(x, v) #debug print("[%s:%d] valOf_Vector_Prod :" % (os.path.basename(libs.thisfile()), libs.linenum())) print(x) print(v) print(valOf_Vector_Prod) print() '''################### step : 2 : 3 prod : matrix ###################''' valOf_Prod_Matrix = np.dot(x, y) #debug print("[%s:%d] valOf_Prod_Matrix :" % (os.path.basename(libs.thisfile()), libs.linenum())) print(x) print(y) print(valOf_Prod_Matrix) print() '''################### step : 2 : 1 data : store ###################''' '''################### step : 6 results ###################''' '''################### step : 6 : 1 time ###################''' t_end = time.time() #debug # print ("[%s:%d] time => %s" #ref https://www.pythonpool.com/python-float-to-string/#5_Using_NumPy print("[%s:%d] time => %.03f sec" % (os.path.basename(libs.thisfile()), libs.linenum(), (t_end - t_start))) print() '''###################
def main_20210506_164920(): strOf_FuncName = "main_20210506_164920()" '''################### step : 1 opening, vars ###################''' #ref https://stackoverflow.com/questions/56711424/how-can-i-count-time-in-python-3 t_start = time.time() print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) print() '''################### step : 2 vars ###################''' #code:20210506_170631 dataset = np.array([['Asset Flip', 100, 1000], ['Text Based', 500, 3000], ['Visual Novel', 1500, 5000], ['2D Pixel Art', 3500, 8000], ['2D Vector Art', 5000, 6500], ['Strategy', 6000, 7000], ['First Person Shooter', 8000, 15000], ['Simulator', 9500, 20000], ['Racing', 12000, 21000], ['RPG', 14000, 25000], ['Sandbox', 15500, 27000], ['Open-World', 16500, 30000], ['MMOFPS', 25000, 52000], ['MMORPG', 30000, 80000]]) print( "[%s:%d] dataset =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) # print the dataset print(dataset) print() '''################### step : 3 data : select ###################''' # select all rows by : and column 1 # by 1:2 representing features X = dataset[:, 1:2].astype(int) print( "[%s:%d] X = dataset[:, 1:2] =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) # print X print(X) print() '''################### step : 4 data : select ###################''' #code:20210506_171149 # select all rows by : and column 2 # by 2 to Y representing labels y = dataset[:, 2].astype(int) print( "[%s:%d] y = dataset[:, 2] =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) # print y print(y) print() '''################### step : 5 regressor ###################''' # create a regressor object regressor = DecisionTreeRegressor(random_state=0) # fit the regressor with X and Y data regressor.fit(X, y) print( "[%s:%d] regressor =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) # print y print(regressor) print() '''################### step : 6 predict ###################''' #code:20210506_172247 valOf_Target_Val = 3750 # test the output by changing values, like 3750 y_pred = regressor.predict( valOf_Target_Val) #=> 3,7550 as a val for 2nd column # y_pred = regressor.predict(3750) #=> 3,7550 as a val for 2nd column print("[%s:%d] y_pred => (for target : %d" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), valOf_Target_Val)) # print the predicted price print("Predicted price: % d\n" % y_pred) print() #n:20210506_172536 '''################### step : 2 : 1 data : store ###################''' '''################### step : 6 results ###################''' '''################### step : 6 : 1 time ###################''' t_end = time.time() #debug # print ("[%s:%d] time => %s" #ref https://www.pythonpool.com/python-float-to-string/#5_Using_NumPy print("[%s:%d] time => %.03f sec" % (os.path.basename(libs.thisfile()), libs.linenum(), (t_end - t_start))) print() '''###################
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) '''################### step : 2 prep ###################''' '''################### step : 3 ###################''' a = math.pi cos_a = math.cos(a) print() print("[%s:%d] a = %.03f / cos(a) = %.03f" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), a, cos_a)) '''################### step : 4 ###################''' '''################### step : 4 : 1 prep : data ###################''' '''################### step : 4 : 1 : prep : data : basics ###################''' #ref https://numpy.org/doc/stable/reference/generated/numpy.linspace.html # numOf_Data_X = 12 numOf_Data_X = 24 valOf_Linspace_Start = -math.pi / 2 valOf_Linspace_End = math.pi / 2 # valOf_Linspace_Start = 0 # valOf_Linspace_End = math.pi #ref https://matplotlib.org/stable/tutorials/introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py data_x = np.linspace(valOf_Linspace_Start, valOf_Linspace_End, numOf_Data_X) '''################### step : 4 : 1 : 2 prep : data : cosine values ###################''' k = 1 data_x_1 = [(2 * k + 1) * x for x in data_x] data_y = np.cos(data_x) data_y_1 = np.cos(data_x_1) data_y_1_final = [math.pow(-1, k) / (2 * k + 1) * x for x in data_y_1] print() print("[%s:%d] numOf_Data_X = %d" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), numOf_Data_X)) print() # print ("[%s:%d] data_x =>" % ( # os.path.basename(os.path.basename(libs.thisfile())) # , libs.linenum() # ) # ) # # print(data_x) # # print ("[%s:%d] data_y =>" % ( # os.path.basename(os.path.basename(libs.thisfile())) # , libs.linenum() # ) # ) # # print(data_y) '''################### step : 4 : 2 plot ###################''' '''################### step : 4 : 2 : 1 plot : prep savefig ###################''' strOf_Time_Label = libs.get_TimeLabel_Now() #ref C:\WORKS_2\WS\WS_Others.JVEMV6\JVEMV6\73_ai\1_start\1_1.py fig = plt.figure() dpath_PlotImage = "./data" fname_PlotImage = "plot_image_%s_[test]" % (strOf_Time_Label) fpath_PlotImage = os.path.join(dpath_PlotImage, fname_PlotImage) # title # plt.title("cos(a)\n numOf_Data_X = %d\n x [%.02f, %.02f]" # plt.title("cos(a), data_y_1\n numOf_Data_X = %d\n x [%.02f, %.02f]" plt.title("cos(a), data_y_1_final\n numOf_Data_X = %d\n x [%.02f, %.02f]" % (numOf_Data_X, valOf_Linspace_Start, valOf_Linspace_End)) # axis #ref https://matplotlib.org/stable/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py plt.axis([-math.pi / 2, math.pi / 2, -1.5, 1.5]) # plt.axis([- math.pi/2, math.pi/2, -1.0, 1.0]) '''################### step : 4 : 2 : 2 plot : set data ###################''' plt.plot(data_x, data_y + data_y_1_final, 'y-o') plt.plot(data_x, data_y_1_final, 'b-o') plt.plot(data_x, data_y, 'r-o') # plt.plot(data_x, data_y_1, 'r-o') # plt.plot(data_x, data_y, 'r-o') # plt.show() '''################### step : 4 : 2 : 3 plot : save image ###################''' fig.savefig(fpath_PlotImage) print("[%s:%d] plot image saved => %s" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), fpath_PlotImage))
def test_2_1(): '''################### ops ###################''' init_Val = 10 a = [10 for x in range(10)] b = [10 for x in range(10)] # de-homogenize #ref mean https://stackoverflow.com/questions/9039961/finding-the-average-of-a-list answered Jan 28 '12 at 3:59 a[-1] = 5 b[-1] = 5 # a[-1] = 9 # b[-1] = 9 '''################### write file ###################''' # fname_Out = "data/pval.a-%d.%s.txt" % \ label = "test_2_1" fname_Out = "data/pval.%s.%s.txt" % \ ( label , libs.get_TimeLabel_Now() ) # fname_Out = "/data/pval.%s.txt" % (libs.get_TimeLabel_Now()) fout = open(fname_Out, "w") '''################### correl : basic ###################''' msg = "[%s:%d] a=\n" % (os.path.basename(libs.thisfile()), libs.linenum()) fout.write(msg) # fout.write("a=\n") fout.write(','.join([str(x) for x in a])) # fout.write(','.join(a)) fout.write('\n') fout.write('\n') msg = "[%s:%d] b=\n" % (os.path.basename(libs.thisfile()), libs.linenum()) fout.write(msg) # fout.write("b=\n") fout.write(','.join([str(x) for x in b])) # fout.write(','.join(b)) fout.write('\n') fout.write('\n') '''################### correl : basic ###################''' lo_Final = [] for i in range(10): # tmp list lo_Tmp = [] ### copy a #ref copy https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list answered Apr 10 '10 at 8:55 a_ = copy.copy(a) lo_Tmp.append(i) a1 = stats.pearsonr(a_, b) print() print("[%s:%d] i => %d / a1 =>" % \ (os.path.basename(libs.thisfile()), libs.linenum() ,i ), file=sys.stderr) print(a1) print("a_ =>") print(a_) print("b =>") print(b) a_[i] /= 2.0 a2 = stats.pearsonr(a_, b) print() print("a1 => ") print(a1) print("a_ =>") print(a_) print("b =>") print(b) a_[i] = 0 a3 = stats.pearsonr(a_, b) ### append lo_Tmp.append(a1) lo_Tmp.append(a2) lo_Tmp.append(a3) #debug print() print("[%s:%d] lo_Tmp =>" % \ (os.path.basename(libs.thisfile()), libs.linenum() ), file=sys.stderr) print(lo_Tmp) ### append lo_Final.append(lo_Tmp) '''################### write ###################''' fout.write("index\t=1.0\t/2.0\t=0") fout.write("\n") for item in lo_Final: #debug print() print("[%s:%d] item =>" % \ (os.path.basename(libs.thisfile()), libs.linenum() ), file=sys.stderr) print(item) # print(item[0]) # print('\t') # print('\n') fout.write("%d\t%.10f\t%.10f\t%.10f" % \ # fout.write("%d\t%.4f\t%.4f\t%.4f" % \ ( item[0], item[1][0], item[2][0], item[3][0] ) ) # print("%.4f %.4f %.4f" % (item[1][0], item[2][0], item[3][0])) fout.write('\n') '''################### file : close ###################''' fout.close() # msg = "[%s:%d] \na[%d]\tcorrel" % \ # ( # os.path.basename(libs.thisfile()), libs.linenum() # , index_a # ) # fout.write(msg) # # fout.write('\n') # fout.write('\n') return None
def test_2(): '''################### prep : get root ###################''' fpath = cons31.FPath.dpath_In_CSV.value \ + "/" \ + cons31.FPath.fname_In_XML.value tree = ET.parse(fpath) root = tree.getroot() '''################### nodes : g-1 ###################''' print() print ("[%s:%d] root.tag => '%s' / root.attrib => '%s'" % \ (os.path.basename(libs.thisfile()), libs.linenum() , root.tag, root.attrib)) g1 = root[0] print() print ("[%s:%d] g1.tag => '%s' / g1.attrib => '%s'" % \ (os.path.basename(libs.thisfile()), libs.linenum() , g1.tag, g1.attrib)) '''################### nodes : g-2 ###################''' g2 = [] lenOf_g2 = len(g1) for i in range(lenOf_g2): g2.append(g1[i]) #/for i in range(lenOf_g2): #debug for item in g2: attrib_Created = item.get('CREATED') #ref append child https://stackoverflow.com/questions/31259847/python-appending-children-to-an-already-created-xml-files-root-using-xml-dom "answered Jul 8 '15 at 11:26" data2 = ET.Element("attribute", { "NAME": "created", "VALUE": attrib_Created }) item.append(data2) # print() # print ("[%s:%d] item.tag = '%s' | item.get('CREATED') = '%s'" % \ # (os.path.basename(libs.thisfile()), libs.linenum() # , item.tag, item.get('CREATED'))) #/for item in g2: '''################### append : child ###################''' # #ref https://stackoverflow.com/questions/31259847/python-appending-children-to-an-already-created-xml-files-root-using-xml-dom # data1 = ET.Element("node", {"TEXT": "something_" + libs.get_TimeLabel_Now()}) # # data1 = ET.Element("node", {"TEXT": "something_v001.0002.ma"}) # # data2 = ET.Element("attribute", {"NAME": "created" # # , "VALUE" : "18/01/23" # }) # # data1.append(data2) # # g2[0].append(data1) # '''################### save xml ###################''' label = "add_Attrib_Created" fpath_Out = "new.%s.%s.mm" % (label, libs.get_TimeLabel_Now()) tree.write(fpath_Out) print() print("[%s:%d] xml => written : %s" % (os.path.basename(libs.thisfile()), libs.linenum(), fpath_Out))
def exec_prog(): # '''################### prep : text ###################''' # fin = open("article.txt", "r", encoding='CP932') # # fin = open("article.txt", "r", encoding='shift-jis') # # fin = open("article.txt", "r", encoding='UTF-8') # # fin = open("article.txt", "r") # # text = fin.read() # # fin.close() # # print(text) # regex #ref https://stackoverflow.com/questions/4995892/python-split-string-on-regex p = re.compile('[。、]') ary = p.split(text) # ary = text.split("。") # ary of num aryOf_TokenLen = [] for item in ary: lenOf_Item = len(item) # print ("len => %d [%s]" % (lenOf_Item, item)) if lenOf_Item > 5: aryOf_TokenLen.append(lenOf_Item) # aryOf_TokenLen.append(lenOf_Item) ### report print ("[%s:%d] average => %.3f" % \ (os.path.basename(libs.thisfile()), libs.linenum(), #ref sum https://stackoverflow.com/questions/4362586/sum-a-list-of-numbers-in-python sum(aryOf_TokenLen) / len(aryOf_TokenLen))) # aryOf_TokenLen.sum() / len(aryOf_TokenLen))) print(aryOf_TokenLen) '''################### write to file ###################''' fname = "data/report.%s.txt" % (libs.get_TimeLabel_Now()) f = open(fname, "w") #ref sort https://www.tutorialspoint.com/python/list_sort.htm aryOf_TokenLen__Sorted = aryOf_TokenLen.sort() # get max len_Max = -1 for item in aryOf_TokenLen: # get max if len_Max < item: len_Max = item for i in range(item): f.write("*") # return f.write("\n") f.close() print ("[%s:%d] file => closed : %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), fname)) print ("[%s:%d] max len => %d" % \ (os.path.basename(libs.thisfile()), libs.linenum(), len_Max)) '''################### Out : histogram ###################''' aryOf_Histogram = {} #test print() print(range(len_Max)) # init for i in range(len_Max + 1): aryOf_Histogram[i] = 0 # for i in range(len_Max) : aryOf_Histogram[i] = 0 for item in aryOf_TokenLen: aryOf_Histogram[item] += 1 ### report print() print(aryOf_Histogram) ### write to file fname = "data/report.histogram.%s.txt" % (libs.get_TimeLabel_Now()) f = open(fname, "w") for i in range(len_Max + 1): f.write("%d : " % (i)) for j in range(aryOf_Histogram[i]): f.write("*") # return f.write("\n") f.close() print ("[%s:%d] file => closed : %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), fname)) '''################### Out : raw data ###################''' ### write to file fname = "data/report.rawdata.%s.txt" % (libs.get_TimeLabel_Now()) f = open(fname, "w") for i in aryOf_TokenLen: f.write("%d" % i) # return f.write("\n") f.close() print ("[%s:%d] file => closed : %s" % \ (os.path.basename(libs.thisfile()), libs.linenum(), fname)) # '''################### # Out : sentences # ###################''' # ### write to file # fname = "data/report.sentences.%s.txt" % (libs.get_TimeLabel_Now()) # # f = open(fname, "wb") # # f = open(fname, "w") # # for i in ary : # # # f.write(i) #=> UnicodeEncodeError: 'cp932' codec can't encode character '\u2003' # # f.write("%s" % i) #=> UnicodeEncodeError: 'cp932' codec can't encode character '\u2003' # # f.write(i.encode('utf-8')) # # f.write(i.decode('utf-8')) #=> AttributeError: 'str' object has no attribute 'decode' # # f.write(i.encode('utf-8')) #=> TypeError: write() argument must be str, not bytes # # f.write("%s" % i.encode('utf-8')) #=> TypeError: write() argument must be str, not bytes # f.write(i.encode('utf-8')) #=> # # f.write(i.decode('ascii', 'ignore')) #=> AttributeError: 'str' object has no attribute 'decode' # # return # f.write("\n") # # f.close() # # print ("[%s:%d] file => closed : %s" % \ # (os.path.basename(libs.thisfile()), libs.linenum(), fname)) # '''################### Report ###################''' print("[%s:%d] exec_prog => done" % (os.path.basename(libs.thisfile()), libs.linenum()))
def test_4(): '''################### prep : get root ###################''' fpath = cons31.FPath.dpath_In_CSV.value \ + "/" \ + cons31.FPath.fname_In_XML.value ### backup fname_Out_Backup = "%s.copy.%s.mm" % (fpath, libs.get_TimeLabel_Now()) copyfile(fpath, fname_Out_Backup) '''################### parse ###################''' tree = ET.parse(fpath) tree = libmt.add_Node_Attribute_Created(tree) # root = tree.getroot() # # '''################### # nodes : g-1 # ###################''' # print() # print ("[%s:%d] root.tag => '%s' / root.attrib => '%s'" % \ # (os.path.basename(libs.thisfile()), libs.linenum() # , root.tag, root.attrib)) # # g1 = root[0] # # print() # print ("[%s:%d] g1.tag => '%s' / g1.attrib => '%s'" % \ # (os.path.basename(libs.thisfile()), libs.linenum() # , g1.tag, g1.attrib)) # # '''################### # nodes : g-2 # ###################''' # g2 = [] # # lenOf_g2 = len(g1) # # for i in range(lenOf_g2): # # g2.append(g1[i]) # # #/for i in range(lenOf_g2): # # #debug # for item in g2: # # for subitem in item: # # if subitem.tag == 'attribute' : #if subitem.tag == 'attribute' # # item.remove(subitem) # # print() # print ("[%s:%d] item.tag = '%s', subitem.tag = '%s'" % \ # (os.path.basename(libs.thisfile()), libs.linenum() # , item.tag, subitem.tag)) #/if subitem.tag == 'attribute' #/for subitem in item: # attrib_Created = item.get('CREATED') # # #ref append child https://stackoverflow.com/questions/31259847/python-appending-children-to-an-already-created-xml-files-root-using-xml-dom "answered Jul 8 '15 at 11:26" # data2 = ET.Element("attribute" # , {"NAME": "created" # , "VALUE" : attrib_Created # }) # item.remove('attribute') # item.append(data2) # print() # print ("[%s:%d] item.tag = '%s' | item.get('CREATED') = '%s'" % \ # (os.path.basename(libs.thisfile()), libs.linenum() # , item.tag, item.get('CREATED'))) #/for item in g2: '''################### append : child ###################''' # #ref https://stackoverflow.com/questions/31259847/python-appending-children-to-an-already-created-xml-files-root-using-xml-dom # data1 = ET.Element("node", {"TEXT": "something_" + libs.get_TimeLabel_Now()}) # # data1 = ET.Element("node", {"TEXT": "something_v001.0002.ma"}) # # data2 = ET.Element("attribute", {"NAME": "created" # # , "VALUE" : "18/01/23" # }) # # data1.append(data2) # # g2[0].append(data1) # '''################### save xml ###################''' label = "add-attribute-CREATED" fpath_Out = fpath # fpath_Out = "new.%s.%s.mm" % (label, libs.get_TimeLabel_Now()) tree.write(fpath_Out) print() print("[%s:%d] xml => written : %s" % (os.path.basename(libs.thisfile()), libs.linenum(), fpath_Out))
<usage> ''' if __name__ == "__main__": '''################### validate : help option ###################''' triangle = [(0.2, 0.2), (0.8, 0.2), (0.5, 0.8)] #初期三角形の座標 fig = plt.figure(figsize=(5, 5)) ax = fig.add_subplot(1, 1, 1) p = pat.Polygon(xy=triangle, fc="white", ec="black") ax.add_patch(p) produce_fractal1(triangle, 6) fname_Out = "./fractal." + libs.get_TimeLabel_Now() + ".png" fig.savefig(fname_Out) #画像の保存 # fig.savefig("./fractal.png") #画像の保存 '''################### Report ###################''' print("[%s:%d] file gen-ed => %s" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), fname_Out)) '''################### validate : help option ###################''' '''################### get options ###################''' '''###################
def main_202105XX(): strOf_FuncName = "main_202105XX()" '''################### step : 1 opening, vars ###################''' #ref https://stackoverflow.com/questions/56711424/how-can-i-count-time-in-python-3 t_start = time.time() print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) print() '''################### step : 2 data : prep ###################''' # Building Phase data = importdata() print( "[%s:%d] importdata ==> comp." % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print() '''################### step : 2 : 1 data : store ###################''' X, Y, X_train, X_test, y_train, y_test = splitdataset(data) #n:20210502_171207 #debug print( "[%s:%d] type(X) ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(type(X)) print() #debug:20210503_160446 print( "[%s:%d] X.shape ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) #ref https://note.nkmk.me/en/python-numpy-ndarray-ndim-shape-size/ print(X.shape) print() # (625, 4) #debug:20210503_160757 print( "[%s:%d] X[:3] ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(X[:3]) print() # [[1 1 1 1] # [1 1 1 2] # [1 1 1 3]] #code:20210503_162445 clf_gini = train_using_gini(X_train, X_test, y_train) #debug:20210503_162611 print( "[%s:%d] clf_gini ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(clf_gini) print() # DecisionTreeClassifier(class_weight=None,... #code:20210503_162857 clf_entropy = tarin_using_entropy(X_train, X_test, y_train) #debug:20210503_163047 print( "[%s:%d] clf_entropy ==>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(clf_entropy) print() '''################### step : 2 : 2 data : graphviz, png file ###################''' data = tree.export_graphviz(clf_gini, out_file=None) # data = tree.export_graphviz(dtree, out_file=None, feature_names=features) #debug:20210503_164342 print( "[%s:%d] export_graphviz ==> comp" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) # print(clf_entropy) print() # graph = pydotplus.graph_from_dot_data(data) strOf_Time_Label = libs.get_TimeLabel_Now() dpath_PlotImage = "./data/s-18" # dpath_PlotImage = "./data/s-9" fname_PlotImage = "decisiontree.%s.png" % (strOf_Time_Label) # fname_PlotImage = "mydecisiontree.%s.png" % (strOf_Time_Label) # fname_PlotImage = "plot_image_%s" % (strOf_Time_Label) fpath_PlotImage = os.path.join(dpath_PlotImage, fname_PlotImage) graph.write_png(fpath_PlotImage) #debug:20210503_165123 print("[%s:%d] decisiontree png file ==> comp : %s" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), fpath_PlotImage)) print() '''################### step : 2 : 2.2 data : graphviz, png file ###################''' data_entropy = tree.export_graphviz(clf_entropy, out_file=None) #debug:20210503_165911 print( "[%s:%d] export_graphviz (clf_entropy) ==> comp" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) # print(clf_entropy) print() # graph = pydotplus.graph_from_dot_data(data_entropy) strOf_Time_Label = libs.get_TimeLabel_Now() dpath_PlotImage = "./data/s-18" # dpath_PlotImage = "./data/s-9" fname_PlotImage = "decisiontree.%s.[clf_entropy].png" % (strOf_Time_Label) fpath_PlotImage = os.path.join(dpath_PlotImage, fname_PlotImage) graph.write_png(fpath_PlotImage) print("[%s:%d] decisiontree png file ==> comp : %s" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), fpath_PlotImage)) print() '''################### step : 3 : 1 prediction : gini ###################''' #n:20210503_170550 # Prediction using gini y_pred_gini = prediction(X_test, clf_gini) cal_accuracy(y_test, y_pred_gini) '''################### step : 3 : 1 prediction : gini ###################''' #code:20210505_164310 # Prediction using entropy y_pred_entropy = prediction(X_test, clf_entropy) cal_accuracy(y_test, y_pred_entropy) '''################### step : 2 : 1 data : store ###################''' '''################### step : 6 results ###################''' '''################### step : 6 : 1 time ###################''' t_end = time.time() #debug # print ("[%s:%d] time => %s" #ref https://www.pythonpool.com/python-float-to-string/#5_Using_NumPy print("[%s:%d] time => %.03f sec" % (os.path.basename(libs.thisfile()), libs.linenum(), (t_end - t_start))) print() '''###################
def test_1(): '''################### ops ###################''' a = np.arange(1, 11) b = np.arange(1, 11) pval = stats.pearsonr(a, b) print(a) '''################### write file ###################''' index_a = -2 fname_Out = "data/pval.a[%d].%s.txt" % \ ( index_a , libs.get_TimeLabel_Now() ) # fname_Out = "/data/pval.%s.txt" % (libs.get_TimeLabel_Now()) fout = open(fname_Out, "w") '''################### correl : basic ###################''' msg = "[%s:%d] a=\n" % (os.path.basename(libs.thisfile()), libs.linenum()) fout.write(msg) # fout.write("a=\n") fout.write(','.join([str(x) for x in a])) # fout.write(','.join(a)) fout.write('\n') fout.write('\n') msg = "[%s:%d] b=\n" % (os.path.basename(libs.thisfile()), libs.linenum()) fout.write(msg) # fout.write("b=\n") fout.write(','.join([str(x) for x in b])) # fout.write(','.join(b)) fout.write('\n') fout.write('\n') correl = stats.pearsonr(a, b) msg = "[%s:%d] correl=%.4f\n" % \ (os.path.basename(libs.thisfile()), libs.linenum() , correl[0] ) fout.write(msg) fout.write('\n') fout.write('\n') '''################### correl : basic ###################''' # index_a = -2 test_1_2(fout, a, b) # test_1_1(fout, a, b, index_a) # test_1_1(fout, a, b) '''################### file : close ###################''' fout.close() return None
def test_2(): strOf_FuncName = "test_2" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 2 prep ###################''' '''################### step : 2 : 1 prep : data : x ###################''' #mark:20211030_153553 # numOf_Data_X = 24 numOf_Data_X = 36 valOf_Linspace_Start = - math.pi / 2 valOf_Linspace_End = math.pi / 2 #ref https://matplotlib.org/stable/tutorials/introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py data_x = np.linspace( valOf_Linspace_Start , valOf_Linspace_End , numOf_Data_X) # print() # # print ("[%s:%d] data_x =>" % ( # os.path.basename(os.path.basename(libs.thisfile())) # , libs.linenum() # ) # ) # # print(data_x) '''################### step : 2 : 2 prep : data : y --> cos(x) ###################''' #mark:20211030_154118 k = 3 # k = 1 # k = 2 coord_1 = math.pow(-1, k) / (2*k+1) data_y = [math.cos(x) for x in data_x] data_y_with_coord = [coord_1 * y for y in data_y] # y vals --> add #mark:20211030_155934 data_y_sum = data_y + data_y_with_coord #mark:20211030_162412 lenOf_data_y = len(data_y) data_y_sum_2 = [data_y[i] + data_y_with_coord[i] for i in range(lenOf_data_y)] #debug:20211030_160239 print() print ("[%s:%d] len(data_y) => %d / len(data_y_with_coord) => %d" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , len(data_y) , len(data_y_with_coord) ) ) print ("[%s:%d] len(data_y_sum) => %d / len(data_y_sum_2) => %d" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , len(data_y_sum) , len(data_y_sum_2) ) ) print() # #debug # return # print() # # print ("[%s:%d] data_y =>" % ( # os.path.basename(os.path.basename(libs.thisfile())) # , libs.linenum() # ) # ) # # print(data_y) # # print() # # print ("[%s:%d] data_y_with_coord =>" % ( # os.path.basename(os.path.basename(libs.thisfile())) # , libs.linenum() # ) # ) # # print(data_y_with_coord) '''################### step : 3 plot ###################''' '''################### step : 3 : 1 plot : prep ###################''' # folder strOf_Time_Label = libs.get_TimeLabel_Now() # dpath_data = "./data" dpath_data = "data" dname_PlotImage = "plot_%s" % (strOf_Time_Label) # dname_PlotImage = "plot_image_%s" % (strOf_Time_Label) dpath_PlotImage = os.path.join(dpath_data, dname_PlotImage) # file name #mark:20211030_163128 fname_Plot = "plot_%s_(k=%d)" % (strOf_Time_Label, k) # fname_Plot = "plot_%s" % (strOf_Time_Label) fpath_PlotImage = os.path.join(dpath_PlotImage, fname_Plot) #ref https://www.askpython.com/python/examples/create-a-directory-in-python res = os.mkdir(dpath_PlotImage) print() print ("[%s:%d] os.mkdir for => : %s" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , dpath_PlotImage ) ) print(res) '''################### step : 3 : 2 plot : plot ###################''' #ref C:\WORKS_2\WS\WS_Others.JVEMV6\JVEMV6\73_ai\1_start\1_1.py fig = plt.figure() # title # plt.title("cos(a), data_y_1_final\n numOf_Data_X = %d\n x [%.02f, %.02f]" # plt.title("cos(a), data_y_with_coord\n numOf_Data_X = %d\n x [%.02f, %.02f]" plt.title("cos(a), data_y_with_coord\n numOf_Data_X = %d\n x [%.02f, %.02f] / k = %d" % ( numOf_Data_X , valOf_Linspace_Start , valOf_Linspace_End , k ) ) # axis #ref https://matplotlib.org/stable/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py plt.axis([- math.pi/2, math.pi/2, -1.5, 1.5]) # plot #mark:20211030_154535 plt.plot(data_x, data_y,'b-o') plt.plot(data_x, data_y_with_coord,'r-o') #mark:20211030_160000 plt.plot(data_x, data_y_sum_2,'y-+') # plt.plot(data_x, data_y_sum,'y-+') # grid #ref https://www.w3schools.com/python/matplotlib_grid.asp plt.grid() # save image res = fig.savefig(fpath_PlotImage) print() print ("[%s:%d] result for 'fig.savefig' : %s" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , fpath_PlotImage ) ) print(res) '''################### step : 4 ###################''' '''###################
def test_2_2(): '''################### ops ###################''' init_Val = 10 a = [10 for x in range(10)] b = [10 for x in range(10)] # de-homogenize #ref mean https://stackoverflow.com/questions/9039961/finding-the-average-of-a-list answered Jan 28 '12 at 3:59 # a[-1] = 5 # b[-1] = 5 val = 5 a[-1] = val b[-1] = val # a[-1] = 9 # b[-1] = 9 '''################### write file ###################''' # fname_Out = "data/pval.a-%d.%s.txt" % \ label = "test_2_2(val-%d)" % (val) fname_Out = "data/pval.%s.%s.txt" % \ ( label , libs.get_TimeLabel_Now() ) # fname_Out = "/data/pval.%s.txt" % (libs.get_TimeLabel_Now()) fout = open(fname_Out, "w") '''################### correl : basic ###################''' msg = "[%s:%d] a=\n" % (os.path.basename(libs.thisfile()), libs.linenum()) fout.write(msg) # fout.write("a=\n") fout.write(','.join([str(x) for x in a])) # fout.write(','.join(a)) fout.write('\n') fout.write('\n') msg = "[%s:%d] b=\n" % (os.path.basename(libs.thisfile()), libs.linenum()) fout.write(msg) # fout.write("b=\n") fout.write(','.join([str(x) for x in b])) # fout.write(','.join(b)) fout.write('\n') fout.write('\n') '''################### correl : basic ###################''' lo_Final = [] for i in range(10): # tmp list lo_Tmp = [] ### copy a #ref copy https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list answered Apr 10 '10 at 8:55 a_ = copy.copy(a) lo_Tmp.append(i) lo_Tmp.append(a[0]) a1 = stats.pearsonr(a_, b) lo_Tmp.append(a1) ### append lo_Final.append(lo_Tmp) # decrement a[0] -= 1 '''################### write ###################''' fout.write("index\ta[0]\tcorrel") fout.write("\n") for item in lo_Final: fout.write("%d\t%d\t%.10f" % \ # fout.write("%d\t%.4f\t%.4f\t%.4f" % \ ( item[0], item[1], item[2][0] ) ) # print("%.4f %.4f %.4f" % (item[1][0], item[2][0], item[3][0])) fout.write('\n') '''################### file : close ###################''' fout.close() # msg = "[%s:%d] \na[%d]\tcorrel" % \ # ( # os.path.basename(libs.thisfile()), libs.linenum() # , index_a # ) # fout.write(msg) # # fout.write('\n') # fout.write('\n') return None
def test_1(): strOf_FuncName = "test_1" '''################### step : 1 opening, vars ###################''' print() print ("[%s:%d] starting : %s (time=%s)" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , strOf_FuncName , libs.get_TimeLabel_Now() ) ) '''################### step : 2 prep ###################''' '''################### step : 2 : 1 prep : data : x ###################''' #mark:20211030_153553 # numOf_Data_X = 24 numOf_Data_X = 36 valOf_Linspace_Start = - math.pi / 2 valOf_Linspace_End = math.pi / 2 #ref https://matplotlib.org/stable/tutorials/introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py data_x = np.linspace( valOf_Linspace_Start , valOf_Linspace_End , numOf_Data_X) print() print ("[%s:%d] data_x =>" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) print(data_x) '''################### step : 2 : 2 prep : data : y --> cos(x) ###################''' data_y = [math.cos(x) for x in data_x] print() print ("[%s:%d] data_y =>" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() ) ) print(data_y) '''################### step : 3 plot ###################''' '''################### step : 3 : 1 plot : prep ###################''' # folder strOf_Time_Label = libs.get_TimeLabel_Now() # dpath_data = "./data" dpath_data = "data" dname_PlotImage = "plot_%s" % (strOf_Time_Label) # dname_PlotImage = "plot_image_%s" % (strOf_Time_Label) dpath_PlotImage = os.path.join(dpath_data, dname_PlotImage) # file name fname_Plot = "plot_%s" % (strOf_Time_Label) fpath_PlotImage = os.path.join(dpath_PlotImage, fname_Plot) #ref https://www.askpython.com/python/examples/create-a-directory-in-python res = os.mkdir(dpath_PlotImage) print() print ("[%s:%d] os.mkdir for => : %s" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , dpath_PlotImage ) ) print(res) '''################### step : 3 : 2 plot : plot ###################''' #ref C:\WORKS_2\WS\WS_Others.JVEMV6\JVEMV6\73_ai\1_start\1_1.py fig = plt.figure() # title plt.title("cos(a), data_y_1_final\n numOf_Data_X = %d\n x [%.02f, %.02f]" % ( numOf_Data_X , valOf_Linspace_Start , valOf_Linspace_End ) ) # axis #ref https://matplotlib.org/stable/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py plt.axis([- math.pi/2, math.pi/2, -1.5, 1.5]) # plot plt.plot(data_x, data_y,'b-o') # grid #ref https://www.w3schools.com/python/matplotlib_grid.asp plt.grid() # save image res = fig.savefig(fpath_PlotImage) print() print ("[%s:%d] result for 'fig.savefig' : %s" % ( os.path.basename(os.path.basename(libs.thisfile())) , libs.linenum() , fpath_PlotImage ) ) print(res) '''################### step : 4 ###################''' '''###################
def test_2_Multiple_Tsp(): strOf_FuncName = "test_2_Multiple_Tsp" '''################### step : 1 opening, vars ###################''' print() print("[%s:%d] starting : %s (time=%s)" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum(), strOf_FuncName, libs.get_TimeLabel_Now())) '''################### step : 2 prep ###################''' aryOf_Numbers__Orig = [1, 2, 3, 4, 5] #ref https://www.geeksforgeeks.org/array-copying-in-python/ aryOf_Numbers__Copy = aryOf_Numbers__Orig.copy() # aryOf_Numbers__Copy = aryOf_Numbers__Orig print() print( "[%s:%d] aryOf_Numbers__Orig =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(aryOf_Numbers__Orig) print() '''################### step : 3 transposition ###################''' #code:20210728_132436 # pointOf_Tsp = 0 pointsOf_Tsp = [3, 1, 4, 1] # pointsOf_Tsp = [1,3,4,1] # pointsOf_Tsp = [1,1,3,4] # pointsOf_Tsp = [1,3,1,4] # pointOf_Tsp = 3 print( "[%s:%d] pointsOf_Tsp =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print(pointsOf_Tsp) print() cntr = 0 for pointOf_Tsp in pointsOf_Tsp: '''################### counter ###################''' cntr += 1 print("[%s:%d] ----------------- for : %d" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum(), cntr)) print() print("[%s:%d] calling ... => tsp()" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum())) print() #code:20210728_132743 aryOf_Numbers__Copy = tsp(aryOf_Numbers__Copy, pointOf_Tsp) # aryOf_Numbers = tsp(aryOf_Numbers, pointOf_Tsp) # tsp(aryOf_Numbers, pointOf_Tsp) print("[%s:%d] tsp() => complete" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum())) print("[%s:%d] aryOf_Numbers__Copy is now ... =>" % (os.path.basename( os.path.basename(libs.thisfile())), libs.linenum())) print(aryOf_Numbers__Copy) print() #/for pointOf_Tsp in pointsOf_Tsp: print( "[%s:%d] aryOf_Numbers : compare =>" % (os.path.basename(os.path.basename(libs.thisfile())), libs.linenum())) print("orig / tsp / pointsOf_Tsp") print(aryOf_Numbers__Orig) print(aryOf_Numbers__Copy) print(pointsOf_Tsp)