def PowerFactor(filename, time, type): path = 'C:\\Users\\shijingliu\\workspace\\PowerFactor\\' file = filename filename = path + file + '.txt' LabelVector, DataMatrix = DataFileLoader.LoadADIData(filename) AORTScolumn = VectorOperations.FindColumn('Aorta', LabelVector) AOPcolumn = VectorOperations.FindColumn('AOP', LabelVector) RAPcolumn = VectorOperations.FindColumn('RAP', LabelVector) IVCTScolumn = VectorOperations.FindColumn('IVC', LabelVector) if AOPcolumn > 10: print "finding new AOP" RAPcolumn = VectorOperations.FindColumn('RAP 1', LabelVector) AOPcolumn = VectorOperations.FindColumn('AOP 1', LabelVector) if type == 'aorta': 'obtain data from the first t seconds' Aorts = DataMatrix[:, AORTScolumn][0:time * 100] 'process the flow one more time get all absolute values' AbsAorts = np.abs(Aorts) Aop = DataMatrix[:, AOPcolumn][0:time * 100] S = np.multiply(AbsAorts, Aop) elif type == 'vena': Ivcts = DataMatrix[:, IVCTScolumn][0:time * 100] AbsIvcts = np.abs(Ivcts) Rap = DataMatrix[:, RAPcolumn][0:time * 100] S = np.multiply(AbsIvcts, Rap) else: print "wrong input!" return 'smooth the value with 11 in window and hanning in type' sasmooth = VectorOperations.smooth(S, 11, 'hanning') 'obtain the RMS value of t minutes baseline' SValue = VectorOperations.RMS(sasmooth[0:12000], 0) 'obtain the min, max value of each phase' min, max = VectorOperations.FindExtrema(sasmooth, 0.6, 100) 'now we obtain the RMS value of each phase during t minutes' SEachPhase = [] for i in range(2, len(max)): SEachPhase.append( VectorOperations.RMS(sasmooth[max[i - 1][0]:max[i][0]], 0)) 'obtain the power factor vector' PF = [x / (SValue * 1.0) for x in SEachPhase] return PF
def AOPSinusoidModeling(filename, WindowSize=2): path = 'C:\\Users\\shijingliu\\workspace\\PowerFactor\\' file = filename finalname = path + file + '.txt' LabelVector, DataMatrix = DataFileLoader.load(finalname) AOPcolumn = VectorOperations.FindColumn('AOP', LabelVector) if AOPcolumn > 10: print "finding new AOP" AOPcolumn = VectorOperations.FindColumn('AOP 1', LabelVector) AOP = DataMatrix[:, AOPcolumn] 'declare a file' saveEVFile = open('SinusoidModeling CPR Flow 347.csv', 'wb') fileWriter = csv.writer(saveEVFile) fileWriter.writerows( [["index", "estimated A:", "estimated B", "estimated D"]]) for i in range((len(AOP) / 100) - WindowSize): 'define the start time and end time of each interval' StartTime = i EndTime = StartTime + WindowSize AopInterval = AOP[StartTime * 100:EndTime * 100] t = np.linspace(StartTime, EndTime - 0.01, WindowSize * 100) 'figure out the appropriate guess using FFT' amplitude = np.abs(np.fft.fft(AopInterval))[1:(len(t) / 2 + 1)] frequency = np.fft.fftfreq(len(t), 0.01)[1:(len(t) / 2 + 1)] 'find out the maximum amplitude and its corresponding frequency' maxFreq, maxAmp = MaximumRecorder(amplitude) 'now we guess the a, b, c, d for the sinusoid modeling' guess_a = VectorOperations.RMS( scipy.signal.detrend(AopInterval)) * np.sqrt(2) guess_b = (3.1415926 * 2.0) * frequency[maxFreq] guess_c = 0.0 guess_d = np.mean(AopInterval) 'optimize the guess values using least square algorithm' optimize_func = lambda x: x[0] * np.sin(x[1] * t + x[2]) + x[ 3] - AopInterval result = leastsq(optimize_func, [guess_a, guess_b, guess_c, guess_d]) 'we only record the sets when the least square can converge' if (result[1] == 1) or (result[1] == 2) or (result[1] == 3) or (result[1] == 4): est_a, est_b, est_c, est_d = result[0] fileWriter.writerows([[i, est_a, est_b, est_d]]) saveEVFile.close()