包含导入另一个模块的模块时出现语法错误

时间:2014-08-26 23:09:48

标签: python import path

我正在尝试创建比自己独立运行更小的文件以便测试它们。我的一个文件从csv读取数据,测试是数据的绘图。我现在正在创建一个模块,它使数据的移动平均值,测试将是读取数据(使用其他模块),然后在数据上运行并绘制它。每个文件都存储在自己的目录中(读取文件模块存储在_ReadBand中)

问题似乎与我正在使用的%matplotlib inline命令(我在ipython中工作)只能在测试区域中找到

这是我的阅读CSV文件

##
# Read sensor band data
# Creates Timestamps from RTC ticks
##

import pandas as pd

from ticksConversion import ticks_to_time #converts difference in rtc to float of seconds

def read_sensor(folder_path, file_path):

    ##This function takes csv of raw data from sensor
    ##and returns the invidual signals as a well as a timestamp made from the real time clock

    #Create full path
    data_path = folder_path + file_path

    #Read CSV
    sensor_data = pd.read_csv(data_path, header=0, names=['time', 'band_rtc', 'ps1', 'ps2', 'ps3'])

    #Extract sensor signals and rtc into lists
    sensorData = [list(sensor_data.ps1), list(sensor_data.ps2), list(sensor_data.ps3)]

    #Create Timestamps based on RTC
    sensorTimestamp = [0] #first timestamp value
    secondsElapsed = 0 #running total of seconds for timestamps
    for indx, val in enumerate(sensor_data.band_rtc):
        if( indx == 0):
            continue #If first rtc value simply continue, this data already has timestamp zero

        secondsElapsed += ticks_to_time(sensor_data.band_rtc[indx-1], sensor_data.band_rtc[indx]) #convert rtc elapsed to seconds and add to total

        sensorTimestamp.append(secondsElapsed) #add timestamp for this data point

    return sensorTimestamp, sensorData

#Test code
if __name__ == "__main__":

    #matplotlib - 2D plotting library
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    %matplotlib inline

    #Test Data Path
    folder_path = './'
    file_path = 'testRead.csv'

    #Test Fuunction
    sensorTimestamp, sensorData = read_sensor(folder_path, file_path)

    #Plot Data
    for indx, data in enumerate(sensorData):
        plt.figure(figsize=(20,3))
        plt.plot(sensorTimestamp, sensorData[indx], label='data from LED %i' %indx)
        plt.title('Raw Reading from Band')
        plt.legend(loc='best')
        plt.show()

这是我的移动平均档:

##
# Moving average Filter
##

def loop_rolling_mean(numRolls, dataset, window):

    ## Moving average with specificed window sized moved over data specified number of times
    ## Input:
    # dataset - data to average
    # numRolls - number of times to do a moving average
    # window - window size for average, must be odd for unshifted data
    ##Output
    # rolledMean - averaged data

    rolledMean = Series(dataset) # Copy data over and cast to pandas series

    for x in range(0,numRolls): #iterate how many times that you want to do run the window
        rolledMean = pd.rolling_mean(rolledMean, window, min_periods=1, center=True)

    return rolledMean ## the dataset that had the rolling mean going forward x number of times

def loop_rolling_mean_with_reverse(numRolls, dataset, window):

    ##roll over data set forward and backward to get rid of offset
    ## Input:
    # dataset - data to average
    # numRolls - number of rolls (forward and backward), must be even for unshifted data
    # window - window size for average
    ##Output
    # rolledMean - averaged data

    #Error Checking
    if(numRolls%2 != 0):
        return "Number of rolls must be even for un-shifted data"

    ## Now going to do the alternating rolling

    rolledMean = Series(dataset) # Copy data over and cast to pandas series

    for x in range(0, int(numRolls/2)):
        forwardRoll = pd.rolling_mean(rolledMean, window, min_periods=1) #roll data in forward direction
        reversdData = forwardRoll.reindex(index=forwardRoll.index[::-1]) #reverse data
        reverseRoll = pd.rolling_mean(reversdData, window, min_periods=1) #roll over reversed data
        rolledMean = reverseRoll.reindex(index=reverseRoll.index[::-1]) #reverse data again so its in correct order

    return rolledMean

#Test code
if __name__ == "__main__":

    # import readBand
    import sys
    sys.path.insert(0, '../_ReadBand')
    import readBand

    matplotlib - 2D plotting library
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    %matplotlib inline

    #Set number of rolls and Window
    numRolls = 2
    windowSize = 3

    rolledSensorData = []
    for indx, val in enumerate(sensorData):
        rolledSensorData.append(loop_rolling_mean(numRolls, sensorData[indx], windowSize))

    #Plot Data
    for indx, val in enumerate(rolledSensorData):
        plt.figure(figsize=(20,3))
        plt.plot(sens_data.timestamp, sensorData[indx], label='raw data')
        plt.plot(sensor_data.timestamp, rolledSensorData[indx], label='Moving Avg forward')
        plt.title('LED %i' %indx)
        #plt.axis([8,22, 7000, 8600])
        plt.legend(loc='best')
        plt.show()

    for indx, val in enumerate(rolledSensorData):
        print len(rolledSensorData[indx])

这是我收到的错误,因为你可以看到它引用了readBand.py中的测试区域

  File "../_ReadBand\readBand.py", line 43
    %matplotlib inline
    ^
SyntaxError: invalid syntax

我甚至不认为这部分代码应该在导入时运行,而不是主要的

0 个答案:

没有答案