How to save peak detection outputs (Z-score algorithm) to a single file?

Asked

Viewed 74 times

0

I would like the output of my program to be saved in a txt file with two columns, no parentheses.

I tried to use a append (in the for, I believe on line 98 of the code) to join the outputs in a single file, but it did not work.

The code is:

#!/usr/bin/env python
#-*- coding: utf8 -*-

import pandas as pd
import numpy as np
import pylab
import matplotlib.pyplot as plt

# Implementacao do algoritmo de deteccao de pico (z_score)

# y e´ o conjunto de valores do traco
# lag e´ o numero de valores utilizados para calcular a media movel
# threshold e´ o numero de desvios padrao para um ponto ser considerado um pico
# influence e´ a influencia (entre 0 e 1) que um novo ponto tem para a media e desvio padrao
# influence = 0 e´ o mais robusto

def z_score(y, lag, threshold, influence):

    # inicializando os vetores que serao utilizados
    signals = np.zeros(len(y))
    filteredY = np.array(y)
    avgFilter = [0]*len(y)
    stdFilter = [0]*len(y)

    # calculo da media
    avgFilter[lag - 1] = np.mean(y[0:lag])
    # calculo do desvio padrao
    stdFilter[lag - 1] = np.std(y[0:lag])

    # se o ponto i esta a um determinado numero de desvios padrao da media, sera considerado um pico
    for i in range(lag, len(y)):
        if abs(y[i] - avgFilter[i-1]) > threshold * stdFilter [i-1]:

            # o pico pode ser positivo ou negativo
            if y[i] > avgFilter[i-1]:
                signals[i] = 1
            else:
                signals[i] = -1

            filteredY[i] = influence * y[i] + (1 - influence) * filteredY[i-1]
            avgFilter[i] = np.mean(filteredY[(i-lag+1):i+1])
            stdFilter[i] = np.std(filteredY[(i-lag+1):i+1])

        else:
            signals[i] = 0
            filteredY[i] = y[i]
            avgFilter[i] = np.mean(filteredY[(i-lag+1):i+1])
            stdFilter[i] = np.std(filteredY[(i-lag+1):i+1])

    return dict(signals = np.asarray(signals),
                avgFilter = np.asarray(avgFilter),
                stdFilter = np.asarray(stdFilter))

# Settings: lag = 30, threshold = 5, influence = 0
lag = 30
threshold = 5
influence = 0

# Dado
    # o dado e´ transformado em txt
data = np.loadtxt('data.txt')

# vetor das primeiras chegadas em tempo
picks = []
tracos = []

# numtrac e´ o numero de tracos do dado e dt o intervalo de amostragem
numtrac = 48
dt = 0.0005

# para percorrermos todos os tracos de forma coerente e´ necessario saber quantos valores ha em cada traco
# denominado tamtrac
tamtrac = 3000

for i in range(numtrac):

    # para encontrar a primeira chegada serao levados em conta os 200 primeiros valores do traco 
    y = data[(i*tamtrac):(i*tamtrac+200)]

    #executa o algoritmo para cada janela de 200 valores de todos os tracos
    result = z_score(y, lag=lag, threshold=threshold, influence=influence)
    for c in range(200-lag):
        #o primeiro sinal diferente de zero sera a primeira chegada
        if result['signals'][c] != 0:
            #print (i,c)
            # multiplicado por dt nos da o valor em tempo
            print (i,c*dt)
            picks.append(c*dt)
            tracos.append(i)
            break


plt.plot(tracos, picks, ".")
plt.show()

The exit is:

(2, 0.056)
(4, 0.0555)
(5, 0.056)
(6, 0.019)
(7, 0.0545)
(10, 0.055)
(11, 0.0545)
(12, 0.053)
(13, 0.052000000000000005)
(14, 0.052000000000000005)
(15, 0.0505)
(17, 0.0505)
(18, 0.0505)
(19, 0.0495)
(20, 0.0475)
(21, 0.0475)
(22, 0.0465)
(23, 0.046)
(24, 0.0455)
(25, 0.043500000000000004)
(26, 0.043000000000000003)
(27, 0.043000000000000003)
(28, 0.0425)
(29, 0.041)
(30, 0.04)
(31, 0.0385)
(32, 0.0375)
(33, 0.036000000000000004)
(34, 0.0345)
(35, 0.033)
(36, 0.0315)
(37, 0.029)
(38, 0.0275)
(39, 0.0265)
(40, 0.0235)
(41, 0.0195)
(42, 0.017)
(43, 0.015)
(44, 0.0165)

1 answer

1

Using Numpy:

datafile_path = "seu_diretorio/datafile.txt"
data = (np.array([tracos, picks])).T
with open(datafile_path, 'w+') as d:
   np.savetxt(d, data)

Or a more direct solution:

datafile_path = "seu_diretorio/datafile.txt"
with open(datafile_path, 'w+') as d:
    for t, p in zip(tracos, picks):
        d.write("{}, {}\n".format(t, p))

Please consider starting with the latest version of Python. You have less than 6 months!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.