def read_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=5): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents({}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found, please check names of the collection and database') except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find({'time': {'$gt': inittime, '$lt': termtime}}): device_no = int(volt['device_no']) v = volt['voltage'] time = volt['time'] times[device_no].append(time) volts[device_no].append(v) return times, volts
def feature_to_matrix_file(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0, action_num=0, interval=1, rate=1): # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 # print(interval,rate) client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) # ntags = 1 tag_acc = 0 title = config['volt_collection'][6:] + "" + action + "_features" fig = plt.figure(title, figsize=(6, 8)) fig.suptitle(action + " (" + "interval:" + str(interval) + "s, " + "stepsize:" + str(rate) + "s)") # 定义特征提取器 extractor = FeatureExtractor() for feature in feature_names: # 定义特征提取模块 module = eval(feature + "(" + str(interval) + "," + str(rate) + ")") # 注册特征提取模块 extractor.register(module) # 丢弃数据 discard = { "get_up": [], "go_to_bed": [], "turn_over": [2, 4, 5, 6, 9], "legs_stretch": [2, 5, 7, 8, 9, 11], "hands_stretch": [7, 8, 9], "legs_tremble": [3, 6, 9, 10, 11, 12], "hands_tremble": [2, 3, 7, 8, 9, 11], "body_tremble": [1, 2, 3, 6, 7, 8, 9], "head_move": [3, 9, 12], "legs_move": [1, 3, 4, 6, 9], "hands_move": [3, 6, 9], "hands_rising": [4, 5, 7, 8, 9], "kick": [2, 4, 5, 6, 7, 8, 9, 11, 12] } # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break # if(tag_acc in discard[action]): # if (tag_acc == 9 or tag_acc == 11): if (tag_acc == 9 or (tag_acc == 11 and action != "hands_move")): continue print("people_" + str(tag_acc)) # inittime, termtime inittime, termtime = tag['termtime'] - 30, tag['termtime'] # inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts, filter_volts = {}, {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] filter_volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) for i in range(1, ndevices + 1): filter_volts[i] = volts[i] # 小波变换滤波 filter_volts[i] = cwt_filter(volts[i], 0.08) # 低通滤波器滤波 # b, a = signal.butter(8, 4 / 7, 'lowpass') # 配置滤波器,8表示滤波器的阶数 # filter_volts[i] = signal.filtfilt(b, a, filter_volts[i]) # 傅里叶变换滤波,使用后动作识别准确率反而降低 # filter_volts[i] = fft_filter(filter_volts[i], 1 / 70, 25) #滤波后准确率下降 # 移动平均滤波,参数可选:full, valid, same # filter_volts[i] = np_move_avg(filter_volts[i], 5, mode="same") # 除以体重,归一化数据 # filter_volts[i] = list(map(lambda x: x / weights[tag_acc - 1], filter_volts[i])) filter_volts[i] = getNormalization(filter_volts[i]) # 定义存储时间、特征列表 feature_times, feature_values, feature_matrixs = {}, {}, {} for i in range(1, ndevices + 1): feature_times[i] = [] feature_matrixs[i] = [] from collections import defaultdict feature_values[i] = defaultdict(list) for feature in feature_names: feature_values[i][feature[:-6]] = [] # 提取第几个设备的特征 start = 1 end = ndevices # 对每个采集设备进行特征提取 for i in range(start, end + 1): for j in range(len(filter_volts[i])): value = {"time": times[i][j], "volt": filter_volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_times[i].append(features['feature_time']) for feature_type in feature_values[i].keys(): feature_values[i][feature_type].append( features['feature_value'][feature_type]) # 清理所有模块,防止过期数据 extractor.clear() # 定义特征数量 nfeatures = len(feature_values[1]) # 定义特征类型 feature_type = list( feature_values[1].keys()) # keys()方法虽然返回的是列表,但是不可以索引 for i in range(start, end + 1): # 如果文件存在,则以添加的方式打开 if (os.path.exists("feature_matrixs/feature_matrix" + str(i) + ".npy")): feature_matrix = np.load("feature_matrixs/feature_matrix" + str(i) + ".npy") label_matrix = np.load("feature_matrixs/label_matrix" + str(i) + ".npy") temp_matrix = np.zeros((len(feature_times[i]), nfeatures), dtype=float) # 可以删除这部分,np.save直接覆盖原文件 # os.remove("feature_matrixs/feature_matrix" + str(i) + ".npy") # os.remove("feature_matrixs/label_matrix" + str(i) + ".npy") for j in range(len(feature_times[i])): for k in range(nfeatures): temp_matrix[j][k] = feature_values[i][ feature_type[k]][j] label_matrix = np.append(label_matrix, [action_num]) # np.append(feature_matrixs, [temp_matrix], axis=0) feature_matrix = np.insert(feature_matrix, feature_matrix.shape[0], values=temp_matrix, axis=0) np.save('feature_matrixs/feature_matrix' + str(i), feature_matrix) np.save('feature_matrixs/label_matrix' + str(i), label_matrix) print("feature_matrix" + str(i) + ":" + str(feature_matrix.shape)) print("label_matrix" + str(i) + ":" + str(label_matrix.shape)) feature_matrixs[i] = feature_matrix # 如果文件不存在,则定义特征矩阵和标签矩阵 else: feature_matrix = np.zeros((len(feature_times[i]), nfeatures), dtype=float) label_matrix = np.zeros((len(feature_times[i]), 1), dtype=int) for j in range(len(feature_times[i])): for k in range(nfeatures): feature_matrix[j][k] = feature_values[i][ feature_type[k]][j] label_matrix[j] = action_num # np.save保存时自动为8位小数 np.save('feature_matrixs/feature_matrix' + str(i), feature_matrix) np.save('feature_matrixs/label_matrix' + str(i), label_matrix) print("feature_matrix" + str(i) + ":" + str(feature_matrix.shape)) print("label_matrix" + str(i) + ":" + str(label_matrix.shape)) feature_matrixs[i] = feature_matrix
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents({}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) ntags = 1 tag_acc = 0 title = config['volt_collection'][6:] + "" + action + "_filter" # fig = plt.figure(title, figsize=(6, 8)) fig = plt.figure(title) fig.suptitle(action + "_filter") # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break # inittime # inittime, termtime = tag['inittime'] - offset, tag['termtime'] - offset inittime, termtime = tag['termtime'] - offset - 31, tag['termtime'] - offset # get the arrays according to which we will plot later times, volts, volts_filter = {}, {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] volts_filter[i] = [] for volt in volt_collection.find({'time': {'$gt': inittime, '$lt': termtime}}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c subtitle = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] ax = fig.add_subplot(ntags, 1, tag_acc) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) ax.set_xlim(inittime, termtime) # 自定义y轴的区间范围,可以使图放大或者缩小 # ax.set_ylim([0.8,1.8]) # ax.set_ylim([0.75, 0.90]) ax.set_ylim([0, 1]) # ax.set_ylim([0.60, 0.75]) # ax.set_ylim([0.85, 1.0]) ax.set_ylabel('Voltage(mv)') # 查看第几号设备 start = 1 end = 1 # filter_thread = [0.2, 0.06, 0.08] for i in range(start, end + 1): volts_filter[i] = volts[i] # 小波变换滤波 volts_filter[i] = cwt_filter(volts_filter[i], 0.1) # 傅里叶变换滤波 volts_filter[i] = fft_filter(volts_filter[i], 1 / 70, 20) # 低通滤波器滤波 # b, a = signal.butter(8, 3 / 7, 'lowpass') # 配置滤波器,8表示滤波器的阶数 # volts_filter[i] = signal.filtfilt(b, a, volts_filter[i]) # 移动平均滤波,参数可选:full, valid, same # volts_filter[i] = np_move_avg(volts_filter[i], 5, mode="same") # 归一化数据 # volts_filter[i] = getNormalization(volts_filter[i]) ax.plot(times[i], volts_filter[i], label='device_' + str(i), color=colors[i - 1], alpha=0.9) ax.grid(linestyle=':') if tag_acc == 1: ax.legend(loc='upper right') if tag_acc == ntags: ax.set_xlabel('Time(s)') # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(times[1]) interval = length // 15 - 1 for i in range(0, length, interval): xticks.append(times[1][i]) # xticklabels.append(timeToSecond(times[1][i] + offset)) xticklabels.append(int(times[1][i] - inittime)) # 图中的开始时间表示时间间隔interval ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 ax.set_xticklabels(xticklabels, rotation=15) # 最大化显示图像窗口 # plt.get_current_fig_manager().window.state('zoomed') plt.show()
def draw_features_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0, action_num=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) title = config['volt_collection'][6:] + "" + action + "_features" fig = plt.figure(title, figsize=(6, 8)) # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 interval = 1 rate = 1 fig.suptitle(action + " (" + "interval:" + str(interval) + "s, " + "stepsize:" + str(rate) + "s)") # 定义特征提取器 extractor = FeatureExtractor() for feature in feature_names: # 定义特征提取模块 module = eval(feature + "(" + str(interval) + "," + str(rate) + ")") # 注册特征提取模块 extractor.register(module) # 定义画布左右位置的计数:标签累加,即人数累加 tag_acc = 1 # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] sampling_counter = 0 sampling_factor = 3 #表示sampling_factor个数据只下采样一个数据 for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): if (sampling_counter % sampling_factor == 0): device_no = int(volt['device_no']) v = volt['voltage'] time = volt['time'] times[device_no].append(time) volts[device_no].append(v) sampling_counter = 1 sampling_counter += 1 # 定义存储时间、特征列表 feature_times, feature_values = {}, {} for i in range(1, ndevices + 1): feature_times[i] = [] from collections import defaultdict feature_values[i] = defaultdict(list) for feature in feature_names: feature_values[i][feature[:-6]] = [] # 提取第几个设备的特征 start = 1 end = ndevices # 对每个采集设备进行特征提取 ndevices for i in range(start, end + 1): for j in range(len(volts[i])): value = {"time": times[i][j], "volt": volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_times[i].append(features['feature_time']) for feature_type in feature_values[i].keys(): feature_values[i][feature_type].append( features['feature_value'][feature_type]) # 清理所有模块,防止过期数据 extractor.clear() # 定义特征数量 nfeatures = len(feature_values[1]) # 定义特征类型 feature_type = list( feature_values[1].keys()) # keys()方法虽然返回的是列表,但是不可以索引 for i in range(start, end + 1): # 如果文件存在,则以添加的方式打开 if (os.path.exists("feature_matrixs/feature_matrix" + str(i) + ".npy")): feature_matrix = np.load("feature_matrixs/feature_matrix" + str(i) + ".npy") label_matrix = np.load("feature_matrixs/label_matrix" + str(i) + ".npy") temp_matrix = np.zeros((len(feature_times[i]), nfeatures), dtype=float) os.remove("feature_matrixs/feature_matrix" + str(i) + ".npy") os.remove("feature_matrixs/label_matrix" + str(i) + ".npy") for j in range(len(feature_times[i])): for k in range(nfeatures): temp_matrix[j][k] = feature_values[i][ feature_type[k]][j] label_matrix = np.append(label_matrix, [action_num]) # np.append(feature_matrixs, [temp_matrix], axis=0) feature_matrix = np.insert(feature_matrix, feature_matrix.shape[0], values=temp_matrix, axis=0) np.save('feature_matrixs/feature_matrix' + str(i), feature_matrix) np.save('feature_matrixs/label_matrix' + str(i), label_matrix) print("feature_matrix" + str(i) + ":" + str(feature_matrix.shape)) # 如果文件不存在,则定义特征矩阵和标签矩阵 else: feature_matrix = np.zeros((len(feature_times[i]), nfeatures), dtype=float) label_matrix = np.zeros((len(feature_times[i]), 1), dtype=int) for j in range(len(feature_times[i])): for k in range(nfeatures): feature_matrix[j][k] = feature_values[i][ feature_type[k]][j] label_matrix[j] = action_num # np.save保存时自动为8位小数 np.save('feature_matrixs/feature_matrix' + str(i), feature_matrix) np.save('feature_matrixs/label_matrix' + str(i), label_matrix) print("feature_matrix" + str(i) + ":" + str(feature_matrix.shape)) tag_acc += 1
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=5, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) ntags = 1 tag_acc = 0 # 用于查看几号设备的图 start = 1 end = 1 title = config['volt_collection'][6:] + "" + action + "_filter_" + str( start) # fig = plt.figure(title, figsize=(6, 8)) fig = plt.figure(title) fig.suptitle(action + "_filter_" + str(start)) # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break # inittime inittime, termtime = tag['termtime'] - offset - 31, tag[ 'termtime'] - offset # get the arrays according to which we will plot later times, volts, volts_filter = {}, {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] volts_filter[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] subtitle = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'L', 'M', 'N' ] ax = fig.add_subplot(ntags, 1, tag_acc) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) # 自定义y轴的区间范围,可以使图放大或者缩小 # ax.set_ylim(0, 0.002) ax.set_ylim(0, 0.005) # ax.set_ylim(0, 0.0003) # ax.set_ylim(0, 1) ax.set_ylabel('Amplitude') # filter_thread = [0.2, 0.06, 0.08] for i in range(start, start + 1): volts_filter[i] = volts[i] # 小波变换滤波 volts_filter[i] = cwt_filter(volts_filter[i], 0.08) # 傅里叶变换滤波 # volts_filter[i] = fft_filter(volts_filter[i], 1 / 70, 25) # fft返回值实部表示 result = np.fft.fft(volts_filter[i]) # 实数fft后会使原信号幅值翻N/2倍,直流分量即第一点翻N倍 amplitudes = abs(result) / (len(result) / 2) # 复数的绝对值其实就是它的模长 amplitudes[0] /= 2 # fftfreq第一个参数n是FFT的点数,一般取FFT之后的数据的长度(size) # 第二个参数d是采样周期,其倒数就是采样频率Fs,即d=1/Fs freqs = np.fft.fftfreq(len(result), d=1 / 70) ax.plot(abs(freqs), amplitudes, label='device_' + str(i), color=colors[i - 1], alpha=0.9) ax.grid(linestyle=':') if tag_acc == 1: ax.legend(loc='upper right') if tag_acc == ntags: ax.set_xlabel('Frequency') # 最大化显示图像窗口 # plt.get_current_fig_manager().window.state('zoomed') plt.show()
def feature_to_matrix_file(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0, action_num=0, interval=2, rate=1): # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 # print(interval,rate) # 针对不同动作,设置不同时间窗口 # if(action == "turn_over"): # interval = 2 # rate = 1 client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) # 提取第几个设备的特征 start = 1 end = ndevices # 定义特征提取器 extractor = FeatureExtractor() for feature in feature_names: # 定义特征提取模块 module = eval(feature + "(" + str(interval) + "," + str(rate) + ")") # 注册特征提取模块 extractor.register(module) # 定义画布左右位置的计数:标签累加,即人数累加 tag_acc = 0 # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > 8): break print("people_" + str(tag_acc)) inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts, filter_volts, normalize_volts = {}, {}, {}, {} for i in range(start, ndevices + 1): times[i] = [] volts[i] = [] filter_volts[i] = [] normalize_volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) filter_thread = [0.2, 0.06, 0.08] for i in range(start, end + 1): filter_volts[i] = volts[i] # 小波变换滤波 filter_volts[i] = cwt_filter(volts[i], 0.08) # 傅里叶变换滤波 # filter_volts[i] = fft_filter(filter_volts[i], 1 / 70, 15) # 低通滤波器滤波 # b, a = signal.butter(8, 3 / 7, 'lowpass') # 配置滤波器,8表示滤波器的阶数 # filter_volts[i] = signal.filtfilt(b, a, filter_volts[i]) # 移动平均滤波,参数可选:full, valid, same # filter_volts[i] = np_move_avg(filter_volts[i], 5, mode="same") # 归一化数据 normalize_volts[i] = getNormalization(filter_volts[i]) # 定义存储时间、特征列表 feature_times, feature_values = {}, {} for i in range(start, end + 1): feature_times[i] = [] from collections import defaultdict feature_values[i] = defaultdict(list) for feature in feature_names: feature_values[i][feature[:-6]] = [] # 对每个采集设备进行特征提取 for i in range(start, end + 1): for j in range(len(normalize_volts[i])): value = {"time": times[i][j], "volt": normalize_volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_times[i].append(features['feature_time']) for feature_type in feature_values[i].keys(): feature_values[i][feature_type].append( features['feature_value'][feature_type]) # 清理所有模块,防止过期数据 extractor.clear() extractor.clear() # 定义特征数量 nfeatures = len(feature_values[1]) # 定义特征类型 feature_type = list( feature_values[1].keys()) # keys()方法虽然返回的是列表,但是不可以索引 for i in range(start, end + 1): # 如果文件存在,则以添加的方式打开 if (os.path.exists("feature_matrixs/feature_matrix" + str(i) + ".npy")): feature_matrix = np.load("feature_matrixs/feature_matrix" + str(i) + ".npy") label_matrix = np.load("feature_matrixs/label_matrix" + str(i) + ".npy") temp_matrix = np.zeros((len(feature_times[i]), nfeatures), dtype=float) os.remove("feature_matrixs/feature_matrix" + str(i) + ".npy") os.remove("feature_matrixs/label_matrix" + str(i) + ".npy") for j in range(len(feature_times[i])): for k in range(nfeatures): temp_matrix[j][k] = feature_values[i][ feature_type[k]][j] label_matrix = np.append(label_matrix, [action_num]) # np.append(feature_matrixs, [temp_matrix], axis=0) feature_matrix = np.insert(feature_matrix, feature_matrix.shape[0], values=temp_matrix, axis=0) np.save('feature_matrixs/feature_matrix' + str(i), feature_matrix) np.save('feature_matrixs/label_matrix' + str(i), label_matrix) np.set_printoptions(suppress=True) np.savetxt('feature_matrixs/feature_matrix' + str(device_no) + '.txt', feature_matrix, fmt="%.18f,%.18f") print("feature_matrix" + str(i) + ":" + str(feature_matrix.shape)) # 如果文件不存在,则定义特征矩阵和标签矩阵 else: feature_matrix = np.zeros((len(feature_times[i]), nfeatures), dtype=float) label_matrix = np.zeros((len(feature_times[i]), 1), dtype=int) for j in range(len(feature_times[i])): for k in range(nfeatures): feature_matrix[j][k] = feature_values[i][ feature_type[k]][j] label_matrix[j] = action_num # np.save保存时自动为8位小数 np.save('feature_matrixs/feature_matrix' + str(i), feature_matrix) np.save('feature_matrixs/label_matrix' + str(i), label_matrix) np.set_printoptions(suppress=True) np.savetxt('feature_matrixs/feature_matrix' + str(device_no) + '.txt', feature_matrix, fmt="%.18f,%.18f") print("feature_matrix" + str(i) + ":" + str(feature_matrix.shape))
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents({}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found, please check names of the collection and database') except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) ntags = 1 tag_acc = 0 # 查看第几号设备 start = 1 end = 1 title = config['volt_collection'][6:] + "" + action + "_cwt_filter" fig = plt.figure(title) fig.suptitle(action + "_cwt_filter") # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break # inittime,termtime inittime, termtime = tag['termtime'] - offset - 31, tag['termtime'] - offset # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find({'time': {'$gt': inittime, '$lt': termtime}}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c subtitle = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'L', 'M', 'N'] ax = fig.add_subplot(ntags, 1, tag_acc) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat( termtime + offset)) ax.set_xlim(inittime, termtime) # 自定义y轴的区间范围,可以使图放大或者缩小 # ax.set_ylim([0.8,1.8]) ax.set_ylim([0.75, 0.90]) ax.set_ylim([0, 1]) # ax.set_ylim([0.82, 0.83]) ax.set_ylabel('Voltage(mv)') for i in range(start, end + 1): volts_filter = volts[i] # filter_thread = [0.2, 0.06, 0.08] # volts_filter = cwt_filter(volts[i], filter_thread[i-1]) #411数据专用 volts_filter = cwt_filter(volts[i], 0.1) print("device"+str(i)+"'RMSE:",get_rmse(volts[i], volts_filter)) ax.plot(times[i], volts_filter, label='device_' + str(i), color=colors[i - 1], alpha=0.9) ax.grid(linestyle=':') if tag_acc == 1: # ax.legend(loc='upper right') pass if tag_acc == ntags: ax.set_xlabel('Time(s)') # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(times[1]) interval = length // 15 - 1 for i in range(0, length, interval): xticks.append(times[1][i]) # xticklabels.append(timeToSecond(times[1][i] + offset)) xticklabels.append(int(times[1][i] - inittime)) # 图中的开始时间表示时间间隔interval ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticklabels(xticklabels, rotation=15) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 # 最大化显示图像窗口 # plt.get_current_fig_manager().window.state('zoomed') plt.show()
def draw_features_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError( 'Collection not found, please check names of the collection and database' ) except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) title = config['volt_collection'][6:] + "" + action + "_features" fig = plt.figure(title, figsize=(6, 8)) fig.suptitle(action) # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 interval = 1 rate = 1 # 定义特征提取器 extractor = FeatureExtractor() # 定义特征提取模块 rangemodule = RangeModule(interval, rate) vibrationfreq = VibrationFreqModule(interval, rate) thresholdcounter = ThresholdCounterModule(interval, rate) # 注册特征提取模块 extractor.register(rangemodule) extractor.register(vibrationfreq) extractor.register(thresholdcounter) # 定义画布左右位置的计数:标签累加,即人数累加 tag_acc = 1 # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] time = volt['time'] times[device_no].append(time) volts[device_no].append(v) # 定义存储时间、特征列表 feature_times, feature_values = {}, {} for i in range(1, ndevices + 1): feature_times[i] = [] feature_values[i] = { 'Range': [], 'VibrationFreq': [], 'ThresholdCounter': [] } # 对每个采集设备进行特征提取 for i in range(1, ndevices + 1): for j in range(len(volts[i])): value = {"time": times[i][j], "volt": volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_times[i].append(features['feature_time']) for feature_type in feature_values[i].keys(): feature_values[i][feature_type].append( features['feature_value'][feature_type]) # 清理所有模块,防止过期数据 extractor.clear() # 定义特征数量 nfeatures = 3 # 定义画布上下位置的计数,即特征累加 fea_acc = 1 base = nfeatures * 100 + ntags * 10 style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c # subtitle = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] # fig.suptitle("Person" + subtitle[tag_acc - 1] + ": " + timeToFormat(inittime + offset) # + " ~ " + timeToFormat(termtime + offset)) for feature_type in feature_values[1].keys(): # plot, add_subplot(311)将画布分割成3行1列,图像画在从左到右从上到下的第1块 ax = fig.add_subplot(base + tag_acc + (fea_acc - 1) * ntags) plt.subplots_adjust( hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title(feature_type) for i in range(1, ndevices + 1): ax.set_xlim(feature_times[i][0], feature_times[i][-1]) ax.plot(feature_times[i], feature_values[i][feature_type], label='device_' + str(i), color=colors[i - 1], alpha=0.9) # 设置每个数据对应的图像名称 if fea_acc == 1 and tag_acc == 2: ax.legend(loc='best') if fea_acc == nfeatures: ax.set_xlabel('Time') fea_acc += 1 # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(feature_times[1]) interval = length // 10 - 1 for i in range(0, length, interval): xticks.append(feature_times[1][i]) xticklabels.append(timeToSecond(feature_times[1][i] + offset)) ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticklabels( xticklabels, rotation=15) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 tag_acc += 1 plt.show()
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError( 'Collection not found, please check names of the collection and database' ) except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) # ntags = 1 tag_acc = 0 # 查看第几号设备 start = 1 end = ndevices # 存储一个动作所有人数的列表 one_action_all_people = {} for i in range(1, ndevices + 1): one_action_all_people[i] = [] # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break # if (tag_acc == 9 or tag_acc == 11): # don't discard data if (tag_acc == 9 or (tag_acc == 11 and action != "hands_move")): continue inittime, termtime = tag['inittime'], tag['termtime'] times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) for i in range(1, ndevices + 1): one_action_all_people[i].append(len(volts[i])) # print(len(volts[i])) for i in range(1, ndevices + 1): origin_num[i].append(one_action_all_people[i])
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents({}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found, please check names of the collection and database') except CollectionError as e: print(e.message) # ntags = tag_collection.count_documents({'tag': action}) ntags = 1 #为了观察图的情况,只显示1个人的图 tag_acc = 0 n = 1 title = config['volt_collection'][6:] + "" + action fig = plt.figure(title) fig.suptitle(action) # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if(tag_acc > ntags): break # inittime # inittime, termtime = tag['inittime'] - offset, tag['inittime'] - offset + 30 inittime, termtime = tag['inittime'] - offset - 30, tag['inittime'] - offset # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find({'time': {'$gt': inittime, '$lt': termtime}}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c subtitle = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] ax = fig.add_subplot(ntags, 1, tag_acc) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat( termtime + offset)) ax.set_xlim(inittime, termtime) # 自定义y轴的区间范围,可以使图放大或者缩小 # ax.set_ylim([0.8,1.8]) # ax.set_ylim([0.75, 0.90]) # ax.set_ylim([0.82, 0.92]) #三个设备时 ax.set_ylim([0.845, 1.025]) #五个设备时 ax.set_ylabel('Voltage(mV)') # 查看第几号设备 start = 1 # end = ndevices end = 1 for i in range(start, end + 1): # 三个设备时 # ax.plot(times[i], [v + i*0.025 for v in volts[i]], # label='device_' + str(i), color=colors[i - 1], alpha=0.9) # 五个设备 ax.plot(times[i], [v + i * 0.04 for v in volts[i]], label='device_' + str(i), color=colors[i - 1], alpha=0.9) for i in range(2, 6): ax.plot(0,0,label='device_' + str(i), color=colors[i - 1],) if tag_acc == 1: ax.legend(loc='upper right') if tag_acc == ntags: ax.set_xlabel('Time(mm:ss)') # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(times[1]) # interval = length // 15 interval = length // 7 for i in range(0, length, interval): xticks.append(times[1][i]) xticklabels.append(timeToSecond(times[1][i] + offset)) # xticklabels.append(int(times[1][i] - inittime)) # 图中的开始时间表示时间间隔interval ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticklabels(xticklabels, rotation=60) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 # 最大化显示图像窗口 # plt.get_current_fig_manager().window.state('zoomed') plt.show()
def draw_features_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=5, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) title = config['volt_collection'][6:] + "" + action + "_features" fig = plt.figure(title, figsize=(6, 8)) # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 interval = 1 rate = 1 fig.suptitle(action + " (" + "interval:" + str(interval) + "s, " + "stepsize:" + str(rate) + "s)") # 定义特征提取器 extractor = FeatureExtractor() # 定义特征提取模块 rangemodule = RangeModule(interval, rate) standarddeviation = StandardDeviationModule(interval, rate) energe = EnergyModule(interval, rate) # 注册特征提取模块 extractor.register(rangemodule) extractor.register(standarddeviation) extractor.register(energe) # 定义画布左右位置的计数:标签累加,即人数累加 tag_acc = 1 # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] time = volt['time'] times[device_no].append(time) volts[device_no].append(v) # 定义存储时间、特征列表 feature_times, feature_values = {}, {} for i in range(1, ndevices + 1): feature_times[i] = [] feature_values[i] = { 'Range': [], 'StandardDeviation': [], 'Energy': [] } # 提取第几个设备的特征 start = 1 # 对每个采集设备进行特征提取 ndevices for i in range(start, 5 + 1): for j in range(len(volts[i])): value = {"time": times[i][j], "volt": volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_times[i].append(features['feature_time']) for feature_type in feature_values[i].keys(): feature_values[i][feature_type].append( features['feature_value'][feature_type]) # 清理所有模块,防止过期数据 extractor.clear() # 定义特征数量 nfeatures = 3 # 定义画布上下位置的计数,即特征累加 fea_acc = 1 base = nfeatures * 100 + ntags * 10 style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c for feature_type in feature_values[1].keys(): # plot, add_subplot(311)将画布分割成3行1列,图像画在从左到右从上到下的第1块 ax = fig.add_subplot(base + tag_acc + (fea_acc - 1) * ntags) plt.subplots_adjust( hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title(feature_type) for i in range(start, 3 + 1): ax.set_xlim(feature_times[i][0], feature_times[i][-1]) ax.plot(feature_times[i], feature_values[i][feature_type], label='device_' + str(i), color=colors[i - 1], alpha=0.9) # # 获取最大最小值,并且打上标记 # max_index = np.argmax(feature_values[i][feature_type]) # min_index = np.argmin(feature_values[i][feature_type]) # ax.plot(feature_times[i][max_index],feature_values[i][feature_type][max_index],'rs') # show_max = str(i)+":"+str(round(feature_values[i][feature_type][max_index],6)) # # xy=(横坐标,纵坐标) 箭头尖端, xytext=(横坐标,纵坐标) 文字的坐标,指的是最左边的坐标 # # https://blog.csdn.net/qq_30638831/article/details/79938967 # plt.annotate(show_max, xy=(feature_times[i][max_index], # feature_values[i][feature_type][max_index]), # xytext=(feature_times[i][max_index], feature_values[i][feature_type][max_index])) # ax.plot(feature_times[i][min_index], feature_values[i][feature_type][min_index], 'gs') # show_min = str(i)+":"+str(round(feature_values[i][feature_type][min_index],6)) # plt.annotate(show_min, xy=(feature_times[i][min_index], # feature_values[i][feature_type][min_index]), # xytext=(feature_times[i][min_index], feature_values[i][feature_type][min_index])) # 设置每个数据对应的图像名称 if fea_acc == 1 and tag_acc == 1: ax.legend(loc='upper right') ax.set_xlabel('Time(s)') if fea_acc == nfeatures: # 设置人员 person = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] ax.set_xlabel("Person" + person[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) fea_acc += 1 # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(feature_times[i]) interval = length // 8 - 1 for k in range(0, length, interval): xticks.append(feature_times[i][k]) # xticklabels.append(timeToSecond(feature_times[i][k] + offset)) xticklabels.append(int(feature_times[i][k] - inittime)) # 图中的开始时间表示时间间隔interval # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticks(xticks) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 ax.set_xticklabels(xticklabels, rotation=15) tag_acc += 1 figure = plt.gcf() # get current figure figure.set_size_inches(20, 10) plt.savefig("feature_images/" + title + ".png", dpi=200)
def feature_to_matrix_file(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0, action_num=0, interval=2, rate=1): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) # 定义特征提取器 extractor = FeatureExtractor() for feature in feature_names: # 定义特征提取模块 module = eval(feature + "(" + str(interval) + "," + str(rate) + ")") # 注册特征提取模块 extractor.register(module) # 定义画布左右位置的计数:标签累加,即人数累加 tag_acc = 0 # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > 8): break print("people_" + str(tag_acc)) inittime, termtime = tag['inittime'], tag['termtime'] # 提取第几个设备的特征 start = 1 end = ndevices # get the arrays according to which we will plot later times, volts, filter_volts, normalize_volts = {}, {}, {}, {} for i in range(start, end + 1): times[i] = [] volts[i] = [] filter_volts[i] = [] normalize_volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) for i in range(1, ndevices + 1): filter_volts[i] = volts[i] # 小波变换滤波 filter_volts[i] = cwt_filter(volts[i], 0.08) # 傅里叶变换滤波 # filter_volts[i] = fft_filter(filter_volts[i], 1 / 70, 15) # 归一化数据 normalize_volts[i] = getNormalization(filter_volts[i]) # 定义存储时间、特征列表 feature_times, feature_values = {}, {} for i in range(start, end + 1): feature_times[i] = [] from collections import defaultdict feature_values[i] = defaultdict(list) for feature in feature_names: feature_values[i][feature[:-6]] = [] # 对每个采集设备进行特征提取 ndevices for i in range(start, end + 1): import pickle with open('models/' + 'device_' + str(i) + '_post_prune.pickle', 'rb') as f: model = pickle.load(f) result = [] for j in range(len(normalize_volts[i])): value = {"time": times[i][j], "volt": normalize_volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_temp = [] #存储实时计算的一条特征数据 for feature_type in feature_values[i].keys(): # print(feature_type, features['feature_value'][feature_type]) feature_temp.append( features['feature_value'][feature_type]) predict_result = model.predict([feature_temp]) predict_proba = max(model.predict_proba([feature_temp])[0]) print(timeToSecond(times[i][j]), action_names_copy[predict_result[0]], predict_proba) # print(action_names[predict_result], predict_proba) # 清理所有模块,防止过期数据 extractor.clear()
def draw_features_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0, action_num=0, feature_name="Mean"): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) ntags = 3 tag_acc = 0 # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 interval = 2 rate = 1 # 定义特征提取器 extractor = FeatureExtractor() for feature in feature_names: # 定义特征提取模块 module = eval(feature + "(" + str(interval) + "," + str(rate) + ")") # 注册特征提取模块 extractor.register(module) # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc < ntags): continue if (tag_acc > ntags): break # inittime # inittime, termtime = tag['inittime'], tag['termtime'] inittime, termtime = tag['termtime'] - 31, tag['termtime'] # get the arrays according to which we will plot later times, volts, filter_volts = {}, {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] filter_volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] time = volt['time'] times[device_no].append(time) volts[device_no].append(v) # 滤波 for i in range(start, end + 1): # 小波变换滤波 filter_volts[i] = cwt_filter(volts[i], 0.08) # 傅里叶变换滤波 # filter_volts[i] = fft_filter(filter_volts[i], 1 / 70, 25) # 归一化数据 filter_volts[i] = getNormalization(filter_volts[i]) # 定义存储时间、特征列表 # feature_times, feature_values = {}, {} # for i in range(start, end + 1): # feature_times[i] = [] # from collections import defaultdict # feature_values[i] = defaultdict(list) # for feature in feature_names: # feature_values[i][feature[:-6]] = [] # 对每个采集设备进行特征提取 ndevices for i in range(start, end + 1): for j in range(len(filter_volts[i])): value = {"time": times[i][j], "volt": filter_volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } fea_diff_action[action_num].append( features['feature_value'][feature_name]) # 清理所有模块,防止过期数据 extractor.clear()
def draw_features_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=5, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError('Collection not found!') except CollectionError as e: print(e.message) # ntags表示总标签数,即人数;tag_acc表示累加计数 ntags = tag_collection.count_documents({'tag': action}) ntags = 8 tag_acc = 0 title = config['volt_collection'][6:] + "" + action + "_features" fig = plt.figure(title, figsize=(6, 8)) # 根据时间采集数据,基本单位为s,比如1s、10s、30s、60s # interval表示每次分析的时间跨度,rate表示间隔多长时间进行一次分析 interval = 2 rate = 1 fig.suptitle(action + " (" + "interval:" + str(interval) + "s, " + "stepsize:" + str(rate) + "s)") # 定义特征提取器 extractor = FeatureExtractor() for feature in feature_names: # 定义特征提取模块 module = eval(feature + "(" + str(interval) + "," + str(rate) + ")") # 注册特征提取模块 extractor.register(module) # read the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break if (tag_acc == 9 or (tag_acc == 11 and action != "hands_move")): continue inittime, termtime = tag['inittime'], tag['termtime'] # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] time = volt['time'] times[device_no].append(time) volts[device_no].append(v) # 定义存储时间、特征列表 feature_times, feature_values = {}, {} for i in range(1, ndevices + 1): feature_times[i] = [] from collections import defaultdict feature_values[i] = defaultdict(list) for feature in feature_names: feature_values[i][feature[:-6]] = [] # 提取第几个设备的特征 start = 1 end = ndevices # 对每个采集设备进行特征提取 ndevices for i in range(start, end + 1): for j in range(len(volts[i])): value = {"time": times[i][j], "volt": volts[i][j]} output = extractor.process(value) if (output): features = { "device_no": i, "feature_time": times[i][j], "feature_value": output, "interval": interval, "rate": rate } feature_times[i].append(features['feature_time']) for feature_type in feature_values[i].keys(): feature_values[i][feature_type].append( features['feature_value'][feature_type]) # 清理所有模块,防止过期数据 extractor.clear() # 定义特征数量 nfeatures = len(feature_values[1]) # 定义画布上下位置的计数,即特征累加 fea_acc = 0 style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c for feature_type in feature_values[1].keys(): fea_acc += 1 ax = fig.add_subplot(nfeatures, ntags, (fea_acc - 1) * ntags + tag_acc) plt.subplots_adjust( hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title(feature_type) for i in range(start, end + 1): ax.set_xlim(feature_times[i][0], feature_times[i][-1]) ax.plot(feature_times[i], feature_values[i][feature_type], label='device_' + str(i), color=colors[i - 1], alpha=0.9) # # 获取最大最小值,并且打上标记 # max_index = np.argmax(feature_values[i][feature_type]) # min_index = np.argmin(feature_values[i][feature_type]) # ax.plot(feature_times[i][max_index],feature_values[i][feature_type][max_index],'rs') # show_max = str(i)+":"+str(round(feature_values[i][feature_type][max_index],6)) # # xy=(横坐标,纵坐标) 箭头尖端, xytext=(横坐标,纵坐标) 文字的坐标,指的是最左边的坐标 # # https://blog.csdn.net/qq_30638831/article/details/79938967 # plt.annotate(show_max, xy=(feature_times[i][max_index], # feature_values[i][feature_type][max_index]), # xytext=(feature_times[i][max_index], feature_values[i][feature_type][max_index])) # ax.plot(feature_times[i][min_index], feature_values[i][feature_type][min_index], 'gs') # show_min = str(i)+":"+str(round(feature_values[i][feature_type][min_index],6)) # plt.annotate(show_min, xy=(feature_times[i][min_index], # feature_values[i][feature_type][min_index]), # xytext=(feature_times[i][min_index], feature_values[i][feature_type][min_index])) # 设置每个数据对应的图像名称 if fea_acc == 1 and tag_acc == 1: ax.legend(loc='upper right') ax.set_xlabel('Time(s)') if fea_acc == nfeatures: # 设置人员 person = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' ] ax.set_xlabel("Person" + person[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(feature_times[i]) step = length // 8 - 1 for k in range(0, length, step): xticks.append(feature_times[i][k]) # xticklabels.append(timeToSecond(feature_times[i][k] + offset)) xticklabels.append(int(feature_times[i][k] - inittime)) # 图中的开始时间表示时间间隔interval # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticks(xticks) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 ax.set_xticklabels(xticklabels, rotation=15) # 显示网格 ax.grid(linestyle=':') # ax.grid(True, which='both') # 最大化显示图像窗口 plt.get_current_fig_manager().window.state('zoomed') plt.show()
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=5, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError( 'Collection not found, please check names of the collection and database' ) except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) n = 1 title = config['volt_collection'][6:] + "" + action fig = plt.figure(title, figsize=(6, 8)) fig.suptitle(action) # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): # inittime inittime, termtime = tag['inittime'] - offset, tag['termtime'] - offset # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] #m c subtitle = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] base = ntags * 100 + 10 # plot, add_subplot(211)将画布分割成2行1列,图像画在从左到右从上到下的第1块 ax = fig.add_subplot(base + n) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[n - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) ax.set_xlim(inittime, termtime) # 自定义y轴的区间范围,可以使图放大或者缩小 # ax.set_ylim([0.8,1.8]) ax.set_ylim([0.8, 1.1]) # ax.set_ylim([0.75, 0.90]) # ax.set_ylim([0.82, 0.83]) ax.set_ylabel('voltage') for i in range(1, ndevices + 1): # [v + i*0.2 for v in volts[i]]为了把多个设备的数据隔离开 ax.plot(times[i], [v + i * 0.05 for v in volts[i]], label='device_' + str(i), color=colors[i - 1], alpha=0.9) if n == 1: ax.legend(loc='upper right') if n == ntags: ax.set_xlabel('time') n += 1 # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(times[1]) interval = length // 10 - 1 for i in range(0, length, interval): xticks.append(times[1][i]) xticklabels.append(timeToSecond(times[1][i] + offset)) ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticklabels( xticklabels, rotation=15) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 plt.show()
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError( 'Collection not found, please check names of the collection and database' ) except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) # ntags = 12 tag_acc = 0 title = config['volt_collection'][6:] + "" + action fig = plt.figure(title, figsize=(6, 8)) fig.suptitle(action) # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): tag_acc += 1 if (tag_acc > ntags): break # if (tag_acc == 9 or tag_acc == 11): # don't discard data if (tag_acc == 9 or (tag_acc == 11 and action != "hands_move")): continue # inittime inittime, termtime = tag['inittime'] - offset, tag['termtime'] - offset # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] # m c subtitle = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N' ] subtitle = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14' ] ax = fig.add_subplot(ntags, 1, tag_acc) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[tag_acc - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) ax.set_xlim(inittime, termtime) # 自定义y轴的区间范围,可以使图放大或者缩小 # ax.set_ylim([0.8,1.8]) ax.set_ylim([0.75, 0.90]) # ax.set_ylim([0.82, 0.83]) ax.set_ylabel('Voltage(mv)') for i in range(start, end + 1): # [v + i*0.2 for v in volts[i]]为了把多个设备的数据隔离开 ax.plot(times[i], volts[i], label='device_' + str(i), color=colors[i - 1], alpha=0.9) if tag_acc == 1: ax.legend(loc='upper right') if tag_acc == ntags: ax.set_xlabel('Time(mm:ss)') # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(times[1]) interval = length // 15 - 1 for i in range(0, length, interval): xticks.append(times[1][i]) # xticklabels.append(timeToSecond(times[1][i] + offset)) xticklabels.append(int(times[1][i] - inittime)) # 图中的开始时间表示时间间隔interval ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticklabels( xticklabels, rotation=15) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 figure = plt.gcf() # get current figure figure.set_size_inches(20, 10) plt.savefig("action_images/" + title + str(start) + ".png", dpi=200) plt.close()
def plot_from_db(action, db, volt_collection, tag_collection, port=27017, host='localhost', ndevices=3, offset=0): client = MongoClient(port=port, host=host) database = client[db] tag_collection = database[tag_collection] volt_collection = database[volt_collection] try: if volt_collection.count_documents( {}) + tag_collection.count_documents({}) < 2: raise CollectionError( 'Collection not found, please check names of the collection and database' ) except CollectionError as e: print(e.message) ntags = tag_collection.count_documents({'tag': action}) n = 1 # 用于查看几号设备的图 start = 3 title = config['volt_collection'][6:] + "" + action + "_cwt" fig = plt.figure(title, figsize=(6, 8)) fig.suptitle(action + "_cwt") # plot the data that is of a certain action one by one for tag in tag_collection.find({'tag': action}): # inittime termtime inittime, termtime = tag['inittime'] - offset, tag[ 'inittime'] - offset + 30 # get the arrays according to which we will plot later times, volts = {}, {} for i in range(1, ndevices + 1): times[i] = [] volts[i] = [] for volt in volt_collection.find( {'time': { '$gt': inittime, '$lt': termtime }}): device_no = int(volt['device_no']) v = volt['voltage'] t = volt['time'] times[device_no].append(t) volts[device_no].append(v) style.use('default') colors = ['r', 'b', 'g', 'c', 'm'] subtitle = ['A', 'B', 'C', 'D', 'E', 'F', 'G'] base = ntags * 100 + 10 # plot, add_subplot(211)将画布分割成2行1列,图像画在从左到右从上到下的第1块 ax = fig.add_subplot(base + n) plt.subplots_adjust(hspace=0.5) # 函数中的wspace是子图之间的垂直间距,hspace是子图的上下间距 ax.set_title("Person" + subtitle[n - 1] + ": " + timeToFormat(inittime + offset) + " ~ " + timeToFormat(termtime + offset)) # 自定义y轴的区间范围,可以使图放大或者缩小 ax.set_ylim(0, 0.9) ax.set_ylabel('Frequency') for i in range(start, start + 1): ax.plot(times[i], volts[i], label='device_' + str(i), color=colors[i - 1], alpha=0.9) # gaus1、cgau8 #gaus1、dmey、mexh、cgau1、fbsp、cmor cgau8、morl、shan wavename = "cgau8" totalscal = len(times[i]) fc = pywt.central_frequency(wavename) # 中心频率 cparam = 2 * fc * totalscal scales = cparam / np.arange(totalscal, 1, -1) volts[i] = [x * 100 for x in volts[i]] print(volts[i]) cwtmatr, freqs = pywt.cwt(volts[i], scales, wavename, 1 / 70) # 最后参数用于计算将尺度转换为实际频率 # cwtmatr, freqs = pywt.cwt(volts[i], np.arange(70, 100), 'cgau8', 1 / 70) ax.contourf(times[i], freqs, abs(cwtmatr)) #绘制等高线,得到的图两边高,中间一条线,表示频率一样? # ax.contourf(times[i], freqs, cwtmatr.real, cmap=plt.cm.hot) # 绘制热力图 # if n == 1: # ax.legend(loc='upper right') # if n == ntags: # ax.set_xlabel('Time') n += 1 # 以第一个设备的时间数据为准,数据的每1/10添加一个x轴标签 xticks = [] xticklabels = [] length = len(times[1]) interval = length // 10 - 1 for i in range(0, length, interval): xticks.append(times[1][i]) xticklabels.append(timeToSecond(times[1][i] + offset)) ax.set_xticks(xticks) # 设定标签的实际数字,数据类型必须和原数据一致 ax.set_xticklabels( xticklabels, rotation=15) # 设定我们希望它显示的结果,xticks和xticklabels的元素一一对应 plt.show()