剖模, server desktop分支
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
profiler/mynumpy.py

199 lines
6.0 KiB

import numpy as np
from pathlib import Path
TOKEN = ";"
SAVE_EXT_NAME = ".csv"
class MyNumpy(object):
def __init__(self,):
# self.first_ndarray = np.array([])
# self.two_d_ndarray = np.ndarray([])
pass
def row_stack_nd_nd(self, nd1:np.ndarray, nd2:np.ndarray):
if nd1.ndim == 1:
np.stack( (nd1, nd2), 0) # 两个一位数组 ,0 上下叠加,1:转置水平叠加
pass
else:
np.vstack( (nd1, nd2)) # 垂直上下叠加
pass
pass
def col_stack_nd_nd(self,nd1:np.ndarray, nd2:np.ndarray):
if nd1.ndim == 1:
np.stack( (nd1, nd2), 1) # 两个一位数组 ,0 上下叠加,1:转置水平叠加
pass
else:
np.hstack( (nd1, nd2.reshape(-1,1)) ) # 垂直上下叠加
pass
def get_time_wl_data_from_csv(self, fpath:Path, sn,fname):
data =[]
file_name = fpath.joinpath( fname + SAVE_EXT_NAME)
try:
readout = np.loadtxt(file_name, dtype = float, delimiter = TOKEN)
except IOError:
# self.makeMsg( 2, file_name + " 文件不存在 " )
readout = np.array([])
raise Exception( "IOError : 文件不存在")
except Exception as e :
# self.makeMsg( 2, "警告: %s" %e )
readout = np.array([])
raise Exception( e)
first_column = readout[1:,0]
header = readout[0,1:]
data = readout[1:,1:]
return first_column,header,data
pass
def get_header_data_from_csv(self, fpath:Path, sn,fname):
data =[]
file_name = fpath.joinpath( fname + SAVE_EXT_NAME)
try:
readout = np.loadtxt(file_name, dtype = float, delimiter = TOKEN)
except IOError:
# self.makeMsg( 2, file_name + " 文件不存在 " )
readout = np.array([])
raise Exception( "IOError : 文件不存在")
except Exception as e :
# self.makeMsg( 2, "警告 %s" % e )
readout = np.array([])
header = readout[0,:]
data = readout[1:,:]
return header,data
pass
def combine_col_row_data (self, col, row, data ):
"""
@description : 结合 时间 波长 数据存到 csv
考虑 data 是一维的情况不考虑为空
col header data 皆为 ndarray 格式
col data 二维 ,header 一维二维都可以 尽量用2维
"""
print( "__combine_col_row_data and return" )
# print( col.shape )
# print( row.shape )
# print( data.shape )
new_d = np.array( [ ] )
# 处理数据维度不符
if col.shape[0] != data.shape[0]:
raise Exception("ndarray shape 不对不能合并!")
return new_d
if row.shape[1] != col.shape[1] + data.shape[1]:
raise Exception("ndarray shape 不对不能合并!")
return new_d
# 给新数据 拼接上新的时间 column
try:
new_d = np.column_stack( ( col, data) )
except Exception as e :
raise Exception("合并数据col data 失败!")
# 拼接上波长作为 头部
try:
new_d = np.row_stack( ( row ,new_d ) )
except Exception as e :
raise Exception("合并数据row data 失败!")
# print( " new_d %s " % new_d.shape)
return new_d
pass
def combine_col_row_data_to_csv(self,col,row,data, path_name:Path):
"""
@description : 结合 时间 波长 数据存到 csv
考虑 data 是一维的情况不考虑为空
col header data 皆为 ndarray 格式
col data 二维 ,header 一维二维都可以 尽量用2维
"""
print( "__combine_col_row_data_to_csv" )
# print( col.shape )
# print( row.shape )
# print( data.shape )
new_d = np.array( [ ] )
# 处理数据维度不符
if col.shape[0] != data.shape[0]:
return new_d
if row.shape[0] != col.shape[1] + data.shape[1]:
return new_d
# 给新数据 拼接上新的时间 column
try:
new_d = np.column_stack( ( col, data) )
except Exception as e :
# self.makeMsg ( 2, "警告:拼接出错。 " + e )
raise Exception("合并数据col data 失败!")
# 拼接上波长作为 头部
try:
new_d = np.row_stack( ( row ,new_d ) )
except Exception as e :
# self.makeMsg ( 2, "警告:拼接出错。 " + e )
raise Exception("合并数据row data 失败!")
# print( " new_d %s " % new_d.shape)
try:
np.savetxt( path_name, new_d, fmt='%8f', delimiter=TOKEN)
except Exception as e:
# self.makeMsg(2, "警告:%s %s " % (e, path_name) )
raise Exception("保存文件 失败!")
pass
pass
if __name__ == '__main__':
mnp = MyNumpy()
a = np.arange(0, 60, 5)
a = a.reshape(3, 4)
print(a)
aa = np.array([1,2,3])
bb = np.array([4,5,6])
cc = np.array([7,8,9])
s = np.stack( (aa,bb) ,0 ) # 行合并
ss = np.vstack( (s,cc) )
sss = np.stack( (aa,bb) ,1 )
ssss = np.hstack( (sss,cc.reshape(-1,1))) # 列合并,reshape(-1,1)
aa = np.stack( (aa,bb) ,0 ) # 行合并
print(s)
print(ss)
print(sss)
print(cc)
print(cc.reshape(-1,1))
print(ssss)
print(aa)
pass
'''
np.stack( (aa,bb) ,0 ) 增加了维度
np.stack( (aa,bb) ,1 )
np.vstack( (s,cc) ) # 不增加维度
np.column_stack( (sss,cc.T))
'''