簡體   English   中英

暫停 python 腳本以保存輸出

[英]pause python script to save output

我在 python 中運行一個代碼來計算鄰接矩陣的一些度量,我遍歷所有鄰接矩陣,迭代地寫入一個 numpy 數組,然后在循環結束時保存數組的輸出。 由於我可以將計算的進度可視化,我發現其中一個矩陣的度量計算花費的時間太長(例如,不是通常的 5 秒,而是幾個小時)。 這是我的腳本:


longmat = scipy.io.loadmat('longmat.mat')
longmat = longmat['longmat']
matlength = longmat.shape
densval = np.arange(0, 1, 0.01)
betti = 0
output_long = np.zeros((264, 168, 12), dtype=float, order='F')

for z in range(matlength[2]): 
    for s in range(matlength[1]):
        temp = longmat[:, s, z] #vector of values
        tempr = scipy.spatial.distance.squareform(temp,checks=True)

        for dens in range(len(densval)):
            G_tda = gd.graph_density(densval[dens], tempr)
            B = bet.Betti_k(G_tda, betti, verbose=False)
            output_long[dens, s, z] = B
            if B < 2:
                break

scipy.io.savemat('longout.mat', {'output_long': output_long})

我想暫停腳本並將數組輸出保存到這一點,並且想知道這是否可能。 非常感謝您的幫助!

喬吉特

你需要一個if語句。

while true: # or a for loop
    #do the calculations here
    if condition: # condition is the metric before the one that takes too long to calculate
        open("test.txt", "a").write(output) # save the output to the file

這只有在您知道哪個指標需要很長時間才能計算時才有可能。

我已經做了研究,試圖找到關於是否可以暫停 python 腳本並以某種方式保存輸出的答案,但似乎不可能。 我應該事先在腳本中添加一個子句,以便在計算需要這么長時間的情況下中斷並保存輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM