# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: py37
#     language: python
#     name: py37
# ---

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from os import listdir
from keras.preprocessing import sequence
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.optimizers import Adam
from keras.models import load_model
from keras.callbacks import ModelCheckpoint

# +
#  Example代码分析
### (1)由几个部分构成
### (2)功能是什么
### (3)输入和输出都是什么(类型和格式)
### (4)输入数据可以用什么工具生成
# -

df1 = pd.read_csv('../data/MovementAAL/dataset/MovementAAL_RSS_1.csv')
df2 = pd.read_csv('../data/MovementAAL/dataset/MovementAAL_RSS_2.csv')

df1.head(5), df2.head(5)

df1.shape, df2.shape

path = '../data/MovementAAL/dataset/MovementAAL_RSS_'
sequences = list()
for i in range(1,315):
    file_path = path + str(i) + '.csv'
    #print(file_path)
    df = pd.read_csv(file_path, header=0)
    values = df.values
    #print(values)
    sequences.append(values)
targets = pd.read_csv('../data/MovementAAL/dataset/MovementAAL_target.csv')
targets = targets.values[:,1]

#我们现在有一个列表“序列”，其中包含来自运动传感器的数据和“目标”，
#其中包含csv文件的标签。当我们打印序列[0]时，从第一个csv文件中获取传感器的值：
sequences[0]

#如前所述，数据集是在三对不同的房间中收集的——因此有三组。
#此信息可用于将数据集划分为训练集、测试集和验证集。我们现在将加载DatasetGroup csv文件：
groups = pd.read_csv('../data/MovementAAL/groups/MovementAAL_DatasetGroup.csv', header=0)
groups = groups.values[:,1]

print(groups)

#让我们找出最小长度、最大长度和平均长度：
len_sequences = list()
for one_seq in sequences:
    len_sequences.append(len(one_seq))
pd.Series(len_sequences).describe()

#Padding the sequence with the values in last row to max length
to_pad = 129
new_seq = list()
for one_seq in sequences:
    len_one_seq = len(one_seq)
    #print("one_seq", one_seq)
    last_val = one_seq[-1]
    #print("last_val", last_val)
    n = to_pad - len_one_seq
    to_concat = np.repeat(one_seq[-1], n).reshape(4, n).transpose()
    #print(np.repeat(one_seq[-1], n))
    #print(np.repeat(one_seq[-1], n).reshape(4, n))
    new_one_seq = np.concatenate([one_seq, to_concat])
    new_seq.append(new_one_seq)
# print(new_seq)
final_seq = np.stack(new_seq) #数组堆叠
print(final_seq)
#truncate the sequence to length 60
from keras.preprocessing import sequence
seq_len = 60
final_seq=sequence.pad_sequences(final_seq, maxlen=seq_len, padding='post', dtype='float', truncating='post')



StarcoderdataJupyterScriptsDedupFiltered