コード例 #1
0
def train_test_data(file_path,
                    batch_size,
                    input_length,
                    test_percent=0.2,
                    scale_factor=1):
    '''
        Prepare data for training and testing, split it into training and testing examples
        :batch_size: batch size for training/testing, type: int
        :input_length: input length to feed into the recurrent network, the remaining will be the labels, type: int
        :return: (encoder input, expected decoder output), type: tuple, shape: [seq_len, batch_size, out_dim] for training set
                 and test_set values to see how the model behaves on unseen data
        '''
    # converting mean values from the CSV file into a smoothened numpy array
    data_dir = file_path
    df = pandas.read_csv(data_dir, converters={"value_mean":
                                               float})["value_mean"].values
    df = scale_factor * smooth(df, window_len=5)
    # divide into train and test sets
    train_count = int((1 - test_percent) * len(df))
    df_train = df[:train_count]
    df_test = df[train_count:]
    X_batch = []
    Y_batch = []
    for _ in range(batch_size):
        seq_1 = df_train
        X = np.array([seq_1[:input_length]])  # size: [out_dim, seq_len]
        Y = np.array([seq_1[input_length:]])
        X = X.T  # size: [seq_len, out_dim]
        Y = Y.T
        X_batch.append(X)
        Y_batch.append(Y)

    TEST = np.array([df_test])  # size: [out_dim, seq_len]
    TEST = TEST.T  # size: [seq_len, out_dim]
    TEST = TEST.reshape(
        1, TEST.shape[0],
        1)  # size: [batch_size=1, seq_len=(test-set-length), out_dim=1]

    X_batch = np.array(X_batch)  # size: [batch_size, seq_len, out_dim]
    Y_batch = np.array(Y_batch)
    print("X batch", X_batch.shape)
    print("Y batch", Y_batch.shape)
    print("TEST data", TEST.shape)
    X_batch = np.transpose(X_batch,
                           (1, 0, 2))  # size: [seq_len, batch_size, out_dim]
    Y_batch = np.transpose(Y_batch, (1, 0, 2))
    TEST = np.transpose(TEST, (1, 0, 2))
    return (X_batch, Y_batch), TEST
コード例 #2
0
def pre_data(file_path, batch_size, seq_len):
    '''
    Prepare data for training and testing

    :batch_size: batch size for training/testing, type: int
    :seq_len: sequence length, type: int
    :return: (encoder input, expected decoder output), type: tuple, shape: [seq_len, batch_size, out_dim]
    '''
    # converting mean values from the CSV file into a smoothened numpy array
    data_dir = file_path
    df = pandas.read_csv(data_dir, converters={"value_mean":
                                               float})["value_mean"].values
    df = smooth(df, window_len=5)
    # print(df.shape)
    # pl.plot(df)
    # pl.show()
    X_batch = []
    Y_batch = []
    for _ in range(batch_size):
        # offset = np.random.random_sample() * math.pi
        # t = np.linspace(start=offset, stop=offset+4*math.pi, num=2*seq_len)
        # print(t)
        # seq_1 = np.sin(t)
        seq_1 = df
        # seq_2 = np.cos(t)
        x1 = seq_1[:seq_len]
        y1 = seq_1[seq_len:]
        # x2 = seq_2[:seq_len]
        # y2 = seq_2[seq_len:]
        X = np.array([x1])  # size: [out_dim, seq_len]
        Y = np.array([y1])
        X = X.T  # size: [seq_len, out_dim]
        Y = Y.T
        X_batch.append(X)
        Y_batch.append(Y)

    X_batch = np.array(X_batch)  # size: [batch_size, seq_len, out_dim]
    Y_batch = np.array(Y_batch)
    print("X batch", X_batch.shape)
    print("Y batch", Y_batch.shape)
    X_batch = np.transpose(X_batch,
                           (1, 0, 2))  # size: [seq_len, batch_size, out_dim]
    Y_batch = np.transpose(Y_batch, (1, 0, 2))
    return X_batch, Y_batch
コード例 #3
0
# u, v are the 3-d arrays to store velocity data
u = np.zeros(ArraySize)
v = np.zeros(ArraySize)

for i in range(TimeLength[0]):
    u[:, :, i] = Data['u_original'][i][0]
    v[:, :, i] = Data['v_original'][i][0]

# Calculate the time-averaged velocity using by moving average
# us, vs represent the time-averaged velocity
# smooth is an user-defined function in smooth.py
us = np.zeros(ArraySize)
vs = np.zeros(ArraySize)
for i in range(FieldSize[0]):
    for j in range(FieldSize[1]):
        us[i, j, :] = smooth(u[i, j, :], TimeLength[0], 15)
        vs[i, j, :] = smooth(v[i, j, :], TimeLength[0], 15)

# ut, vt represent the turbulent velocity
ut = u - us
vt = v - vs

# An example shown below
# Calculate the turbulent kinetic energy dissipation rate
delta = Data['x'][0][0][0][1]-Data['x'][0][0][0][0]  # grid step distance
nu = 1.01E-6                                         # kinetic viscosity
Dissipation = dis_compute(ut, vt, delta, nu, ArraySize)

# Results visualization
plt.imshow(Dissipation[:, :, 900])
plt.show()
コード例 #4
0
ファイル: Test.py プロジェクト: YipLab/OCT_Capillary_Biofilm
patata
ZPLog2Mn = ZProjLog2.mean(axis=1)
Mx = ZPLog2Mn.max()
Md = np.median(ZPLog2Mn)
RPos = np.arange(len(ZPLog2Mn))
RPosNaN = RPos + np.nan
ValidRPos = np.where(ZPLog2Mn > ((Mx + Md) / 2), RPos, RPosNaN)
ValidRPos = ValidRPos[~np.isnan(ValidRPos)].astype(int)
MinMax = ValidRPos.min() - len(ZPLog2Mn) / 20
MaxMax = min(ValidRPos.max() + len(ZPLog2Mn) / 20, len(ZPLog2Mn))
##pp.plot(RPos[MinMax:MaxMax],ZPLog2Mn[MinMax:MaxMax],'xb')

Segmnt = ZProjLog2[MinMax:MaxMax, :]
SegmntMx = Segmnt.argmax(axis=0)
SegmntSM = smooth(Segmnt.argmax(axis=0), window_len=11)  #Segmnt.argmax(axis=0)
MaxPadd = SegmntSM[0:2 * ExtSrch]
MinPadd = SegmntSM[len(SegmntSM) - 2 * ExtSrch:len(SegmntSM)]
SegmntSMPadd = np.zeros(len(SegmntSM) + 4 * ExtSrch)
PosMaxCtr = SegmntSM.argmax() + 2 * ExtSrch
PosMinCtr = SegmntSM.argmin() + 2 * ExtSrch
SegmntSMPadd[0:2 * ExtSrch] = MinPadd
SegmntSMPadd[2 * ExtSrch:len(SegmntSM) + 2 * ExtSrch] = SegmntSM
SegmntSMPadd[len(SegmntSM) + 2 * ExtSrch:len(SegmntSMPadd)] = MaxPadd
#pp.plot(SegmntSMPadd)
MinPos = posMinArr(SegmntSMPadd, 750)
MinCut = np.where(np.diff(MinPos) != 1)[0][0]
MinPos1 = MinPos[0:MinCut + 1].mean()
MinPos2 = MinPos[MinCut + 1:len(MinPos)].mean()
if MinPos1 > (len(SegmntSMPadd) - MinPos2):
    MinPos = MinPos1
コード例 #5
0
def Centress(ZProjLog2):

    ##MaxHough=20
    ##HistS=100
    ##Padd=100
    ##ZPLog2Mn=ZProjLog2.mean(axis=1)
    ##Mx=ZPLog2Mn.max()
    ##Md=np.median(ZPLog2Mn)
    ##RPos=np.arange(len(ZPLog2Mn))
    ##RPosNaN=RPos+np.nan
    ##ValidRPos=np.where(ZPLog2Mn>((Mx+Md)/2),RPos,RPosNaN)
    ##ValidRPos=ValidRPos[~np.isnan(ValidRPos)].astype(int)
    ##MinMax=ValidRPos.min()-len(ZPLog2Mn)/20
    ##MaxMax=min(ValidRPos.max()+len(ZPLog2Mn)/20,len(ZPLog2Mn))
    ##pp.plot(RPos[MinMax:MaxMax],ZPLog2Mn[MinMax:MaxMax],'xb')

    MinMax,MaxMax=MinMaxProj(ZProjLog2)
    Segmnt=ZProjLog2[MinMax:MaxMax,:]
    SegmntMx=Segmnt.argmax(axis=0)
    SegmntSM=smooth(Segmnt.argmax(axis=0),window_len=11)#Segmnt.argmax(axis=0)
    MaxPadd=SegmntSM[0:2*ExtSrch]
    MinPadd=SegmntSM[len(SegmntSM)-2*ExtSrch:len(SegmntSM)]
    SegmntSMPadd=np.zeros(len(SegmntSM)+4*ExtSrch)
    PosMaxCtr=SegmntSM.argmax()+2*ExtSrch
    PosMinCtr=SegmntSM.argmin()+2*ExtSrch
    SegmntSMPadd[0:2*ExtSrch]=MinPadd
    SegmntSMPadd[2*ExtSrch:len(SegmntSM)+2*ExtSrch]=SegmntSM
    SegmntSMPadd[len(SegmntSM)+2*ExtSrch:len(SegmntSMPadd)]=MaxPadd
    #pp.plot(SegmntSMPadd)
    MinPos=posMinArr(SegmntSMPadd,750)
    MinCut=np.where(np.diff(MinPos)!=1)[0][0]
    MinPos1=MinPos[0:MinCut+1].mean()
    MinPos2=MinPos[MinCut+1:len(MinPos)].mean()
    if MinPos1>(len(SegmntSMPadd)-MinPos2):
        MinPos=MinPos1
    else:
        MinPos=MinPos2
    
    MinPosReal=MinPos-2*ExtSrch

    MaxPos=posMaxArr(SegmntSMPadd,750)
    MaxCut=np.where(np.diff(MaxPos)!=1)[0][0]
    MaxPos1=MaxPos[0:MaxCut+1].mean()
    MaxPos2=MaxPos[MaxCut+1:len(MaxPos)].mean()
    if MaxPos1>(len(SegmntSMPadd)-MaxPos2):
        MaxPos=MaxPos1
    else:
        MaxPos=MaxPos2

    MaxPosReal=MaxPos-2*ExtSrch
    MaxAng=MaxPosReal*2*np.pi/len(SegmntSM)
    MinAng=MinPosReal*2*np.pi/len(SegmntSM)
    AvgAng=(MaxAng-np.pi+MinAng)/2##180=np.pi convert both angles to the same reference
    RadShift=(SegmntMx[MaxPosReal]-SegmntMx[MinPosReal])/2
    #SegmntMx[MinPosReal]
    #SegmntMx[MaxPosReal]
    #pp.plot(MaxPos,SegmntSMPadd[MaxPos],'+b')
    if AvgAng<(np.pi/2):
        Rh=RadShift*np.cos(AvgAng)
        Rv=RadShift*np.sin(AvgAng)
    elif AvgAng<np.pi:
        Rh=-RadShift*np.cos(np.pi-AvgAng)
        Rv=RadShift*np.sin(np.pi-AvgAng)
    elif AvgAng<(3*np.pi/4):
        Rh=-RadShift*np.cos(0.75*np.pi-AvgAng)
        Rv=-RadShift*np.sin(0.75*np.pi-AvgAng)
    else:
        Rh=RadShift*np.cos(2*np.pi-AvgAng)
        Rv=-RadShift*np.sin(2*np.pi-AvgAng)
    Rh=int(np.round(Rh))
    Rv=int(np.round(Rv))
    return Rh,Rv,SegmntSMPadd
コード例 #6
0
ファイル: Test.py プロジェクト: YipLab/OCT_Capillary_Biofilm
patata
ZPLog2Mn=ZProjLog2.mean(axis=1)
Mx=ZPLog2Mn.max()
Md=np.median(ZPLog2Mn)
RPos=np.arange(len(ZPLog2Mn))
RPosNaN=RPos+np.nan
ValidRPos=np.where(ZPLog2Mn>((Mx+Md)/2),RPos,RPosNaN)
ValidRPos=ValidRPos[~np.isnan(ValidRPos)].astype(int)
MinMax=ValidRPos.min()-len(ZPLog2Mn)/20
MaxMax=min(ValidRPos.max()+len(ZPLog2Mn)/20,len(ZPLog2Mn))
##pp.plot(RPos[MinMax:MaxMax],ZPLog2Mn[MinMax:MaxMax],'xb')


Segmnt=ZProjLog2[MinMax:MaxMax,:]
SegmntMx=Segmnt.argmax(axis=0)
SegmntSM=smooth(Segmnt.argmax(axis=0),window_len=11)#Segmnt.argmax(axis=0)
MaxPadd=SegmntSM[0:2*ExtSrch]
MinPadd=SegmntSM[len(SegmntSM)-2*ExtSrch:len(SegmntSM)]
SegmntSMPadd=np.zeros(len(SegmntSM)+4*ExtSrch)
PosMaxCtr=SegmntSM.argmax()+2*ExtSrch
PosMinCtr=SegmntSM.argmin()+2*ExtSrch
SegmntSMPadd[0:2*ExtSrch]=MinPadd
SegmntSMPadd[2*ExtSrch:len(SegmntSM)+2*ExtSrch]=SegmntSM
SegmntSMPadd[len(SegmntSM)+2*ExtSrch:len(SegmntSMPadd)]=MaxPadd
#pp.plot(SegmntSMPadd)
MinPos=posMinArr(SegmntSMPadd,750)
MinCut=np.where(np.diff(MinPos)!=1)[0][0]
MinPos1=MinPos[0:MinCut+1].mean()
MinPos2=MinPos[MinCut+1:len(MinPos)].mean()
if MinPos1>(len(SegmntSMPadd)-MinPos2):
    MinPos=MinPos1