Skip to content

Commit

Permalink
fix: #1 small bug recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
RainBoltz committed Dec 12, 2023
1 parent 53fa4fc commit b2ae63d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 89 deletions.
75 changes: 41 additions & 34 deletions cnn_example/series2gaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,72 @@
from tqdm import tqdm, trange

# 主函式


def GenerateGAF(all_ts, window_size, rolling_length, fname, normalize_window_scaling=1.0, method='summation', scale='[0,1]'):

# 取得時間序列長度
n = len(all_ts)
n = len(all_ts)

# 我們要避免微觀被過度放大, 所以移動的 window_size 是原本的 normalize_window_scaling 倍
moving_window_size = int(window_size * normalize_window_scaling)

# 根據我們的滾動大小,將資料切成一組一組
n_rolling_data = int(np.floor((n - moving_window_size)/rolling_length))
n_rolling_data = (n - moving_window_size) // rolling_length + 1

# 最終的 GAF
gramian_field = []

# 紀錄價格,用來畫圖
#Prices = []
# Prices = []

# 開始從第一筆資料前進
for i_rolling_data in trange(n_rolling_data, desc="Generating...", ascii=True):

# 起始位置
start_flag = i_rolling_data*rolling_length

# 整個窗格的資料先從輸入時間序列中取出來
full_window_data = list(all_ts[start_flag : start_flag+moving_window_size])
full_window_data = list(
all_ts[start_flag: start_flag + moving_window_size])

# 紀錄窗格的資料,用來畫圖
#Prices.append(full_window_data[-int(window_size*(normalize_window_scaling-1)):])
# Prices.append(full_window_data[-int(window_size*(normalize_window_scaling-1)):])

# 因為等等要做cos/sin運算, 所以先標準化時間序列
rescaled_ts = np.zeros((moving_window_size, moving_window_size), float)
min_ts, max_ts = np.min(full_window_data), np.max(full_window_data)
diff = max_ts - min_ts
if scale == '[0,1]':
diff = max_ts - min_ts
if diff != 0:
rescaled_ts = (full_window_data - min_ts) / diff
if scale == '[-1,1]':
diff = max_ts - min_ts
if diff != 0:
rescaled_ts = (2 * full_window_data - diff) / diff

# 留下原始 window_size 長度的資料
rescaled_ts = rescaled_ts[-int(window_size*(normalize_window_scaling-1)):]

rescaled_ts = rescaled_ts[-int(window_size *
(normalize_window_scaling-1)):]

# 計算 Gramian Angular Matrix
this_gam = np.zeros((window_size, window_size), float)
sin_ts = np.sqrt(np.clip(1 - rescaled_ts**2, 0, 1))
if method == 'summation':
# cos(x1+x2) = cos(x1)cos(x2) - sin(x1)sin(x2)
this_gam = np.outer(rescaled_ts, rescaled_ts) - np.outer(sin_ts, sin_ts)
this_gam = np.outer(rescaled_ts, rescaled_ts) - \
np.outer(sin_ts, sin_ts)
if method == 'difference':
# sin(x1-x2) = sin(x1)cos(x2) - cos(x1)sin(x2)
this_gam = np.outer(sin_ts, rescaled_ts) - np.outer(rescaled_ts, sin_ts)

this_gam = np.outer(sin_ts, rescaled_ts) - \
np.outer(rescaled_ts, sin_ts)

gramian_field.append(this_gam)

# 清理記憶體占用
del this_gam

# 輸出 Gramian Angular Field
np.array(gramian_field).dump('%s_gaf.pkl'%fname)
np.asarray(gramian_field, dtype="float").dump('%s_gaf.pkl' % fname)

# 清理記憶體占用
del gramian_field
Expand All @@ -76,7 +81,7 @@ def PlotHeatmap(all_img, save_dir='output_img'):
# 建立輸出資料夾
if not os.path.exists(save_dir):
os.makedirs(save_dir)

# 取得資料總長度
total_length = all_img.shape[0]

Expand All @@ -88,27 +93,29 @@ def PlotHeatmap(all_img, save_dir='output_img'):
this_fname = str(img_no).zfill(fname_zero_padding_size)
plt.imshow(all_img[img_no], cmap='hot', interpolation='nearest')
plt.axis('off')
plt.savefig("%s/%s.png"%(save_dir, this_fname), bbox_inches='tight', pad_inches=0, transparent=True)
plt.savefig("%s/%s.png" % (save_dir, this_fname),
bbox_inches='tight', pad_inches=0, transparent=True)
plt.clf()


#
#
# DEMO
#
#
if __name__=='__main__':
if __name__ == '__main__':

random_series = np.random.uniform(low=50.0, high=150.0, size=(200,))
random_series = np.random.uniform(low=110.0, high=150.0, size=(200,))

timeSeries = list(random_series)
windowSize = 50
rollingLength = 10
fileName = 'demo_%02d_%02d'%(windowSize, rollingLength)
GenerateGAF(all_ts = timeSeries,
window_size = windowSize,
rolling_length = rollingLength,
fname = fileName,
normalize_window_scaling = 2.0)

ts_img = np.load('%s_gaf.pkl'%fileName)
PlotHeatmap(ts_img)
rollingLength = 50
fileName = 'demo_%02d_%02d' % (windowSize, rollingLength)
GenerateGAF(all_ts=timeSeries,
window_size=windowSize,
rolling_length=rollingLength,
fname=fileName,
normalize_window_scaling=1.0)

ts_img = np.load(f'{fileName}_gaf.pkl', allow_pickle=True)
PlotHeatmap(ts_img)
109 changes: 54 additions & 55 deletions series2gaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,111 +4,110 @@
import matplotlib.pyplot as plt
from tqdm import tqdm, trange

# 主函式

def GenerateGAF(all_ts, window_size, rolling_length, fname, normalize_window_scaling=1.0, method='summation', scale='[0,1]'):

# 取得時間序列長度
n = len(all_ts)
# 我們要避免微觀被過度放大, 所以移動的 window_size 是原本的 normalize_window_scaling 倍
# get length of time series
n = len(all_ts)

# to make sure the normalizing size for moving window is considered
moving_window_size = int(window_size * normalize_window_scaling)
# 根據我們的滾動大小,將資料切成一組一組
n_rolling_data = int(np.floor((n - moving_window_size)/rolling_length))
# 最終的 GAF

# calculate the steps of rolling
n_rolling_data = (n - moving_window_size) // rolling_length + 1

# record the Gramian Angular Field
gramian_field = []

# 紀錄價格,用來畫圖
#Prices = []

# 開始從第一筆資料前進
# start to generate GAF
for i_rolling_data in trange(n_rolling_data, desc="Generating...", ascii=True):

# 起始位置
# start position index
start_flag = i_rolling_data*rolling_length

# 整個窗格的資料先從輸入時間序列中取出來
full_window_data = list(all_ts[start_flag : start_flag+moving_window_size])

# 紀錄窗格的資料,用來畫圖
#Prices.append(full_window_data[-int(window_size*(normalize_window_scaling-1)):])

# 因為等等要做cos/sin運算, 所以先標準化時間序列

# get the data in the window
full_window_data = list(
all_ts[start_flag: start_flag + moving_window_size])

# normalize the data in the window to [0,1] or [-1,1]
rescaled_ts = np.zeros((moving_window_size, moving_window_size), float)
min_ts, max_ts = np.min(full_window_data), np.max(full_window_data)
diff = max_ts - min_ts
if scale == '[0,1]':
diff = max_ts - min_ts
if diff != 0:
rescaled_ts = (full_window_data - min_ts) / diff
if scale == '[-1,1]':
diff = max_ts - min_ts
if diff != 0:
rescaled_ts = (2 * full_window_data - diff) / diff

# 留下原始 window_size 長度的資料
rescaled_ts = rescaled_ts[-int(window_size*(normalize_window_scaling-1)):]

# 計算 Gramian Angular Matrix
# if normalize_window_scaling > 1, ignore the data out of the original window size
rescaled_ts = rescaled_ts[-int(window_size *
(normalize_window_scaling-1)):]

# calculate the Gramian Angular Matrix
this_gam = np.zeros((window_size, window_size), float)
sin_ts = np.sqrt(np.clip(1 - rescaled_ts**2, 0, 1))
if method == 'summation':
# cos(x1+x2) = cos(x1)cos(x2) - sin(x1)sin(x2)
this_gam = np.outer(rescaled_ts, rescaled_ts) - np.outer(sin_ts, sin_ts)
this_gam = np.outer(rescaled_ts, rescaled_ts) - \
np.outer(sin_ts, sin_ts)
if method == 'difference':
# sin(x1-x2) = sin(x1)cos(x2) - cos(x1)sin(x2)
this_gam = np.outer(sin_ts, rescaled_ts) - np.outer(rescaled_ts, sin_ts)

this_gam = np.outer(sin_ts, rescaled_ts) - \
np.outer(rescaled_ts, sin_ts)

gramian_field.append(this_gam)

# 清理記憶體占用
# garbage collection
del this_gam

# 輸出 Gramian Angular Field
np.array(gramian_field).dump('%s_gaf.pkl'%fname)

# 清理記憶體占用
# dump Gramian Angular Field to pickle file
np.asarray(gramian_field, dtype="float").dump('%s_gaf.pkl' % fname)

# garbage collection
del gramian_field


def PlotHeatmap(all_img, save_dir='output_img'):

# 建立輸出資料夾
# create output directory
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 取得資料總長度

# get total length of images
total_length = all_img.shape[0]

# 計算輸出圖片名稱的補零數量
# get zero padding size for naming
fname_zero_padding_size = int(np.ceil(np.log10(total_length)))

# 輸出圖片
# start to plot
for img_no in trange(total_length, desc="Output Heatmaps...", ascii=True):
this_fname = str(img_no).zfill(fname_zero_padding_size)
plt.imshow(all_img[img_no], cmap='hot', interpolation='nearest')
plt.axis('off')
plt.savefig("%s/%s.png"%(save_dir, this_fname), bbox_inches='tight', pad_inches=0, transparent=True)
plt.savefig("%s/%s.png" % (save_dir, this_fname),
bbox_inches='tight', pad_inches=0, transparent=True)
plt.clf()


#
#
# DEMO
#
#
if __name__=='__main__':
if __name__ == '__main__':

random_series = np.random.uniform(low=50.0, high=150.0, size=(200,))
random_series = np.random.uniform(low=110.0, high=150.0, size=(200,))

timeSeries = list(random_series)
windowSize = 50
rollingLength = 10
fileName = 'demo_%02d_%02d'%(windowSize, rollingLength)
GenerateGAF(all_ts = timeSeries,
window_size = windowSize,
rolling_length = rollingLength,
fname = fileName,
normalize_window_scaling = 2.0)

ts_img = np.load('%s_gaf.pkl'%fileName)
PlotHeatmap(ts_img)
rollingLength = 50
fileName = 'demo_%02d_%02d' % (windowSize, rollingLength)
GenerateGAF(all_ts=timeSeries,
window_size=windowSize,
rolling_length=rollingLength,
fname=fileName,
normalize_window_scaling=1.0)

ts_img = np.load(f'{fileName}_gaf.pkl', allow_pickle=True)
PlotHeatmap(ts_img)

0 comments on commit b2ae63d

Please sign in to comment.