Skip to content

Example of processing

Cris Silva Jr edited this page Jan 10, 2017 · 4 revisions

Example of processing

This file will show a commented file containing a possible processing that can be done using this EDF lib. It will create a CSV table relating each EDF file path to the subject and the sample rate. This is a common operation when dealing with multiple EDF files, and some lot processing is intended.

Code

The following code contains an example processing procedure. It uses a WaitGroup to process many EDF files in parallel, and each analysis is done by loading the file onto memory and then using both the raw information on the EDF header (contained in a map) and a built-in function from the library, that extracts the sampling rate. These data is printed on screen separated by tabs.

package main

import "os"
import "io/ioutil"
import "sync"
import "fmt"
import "github.com/ishiikurisu/edf"

// Processes a folder containing many EDF files
func main() {
		var wg sync.WaitGroup
		var source string

		// Deciding source folder
		if len(os.Args) > 1 {
				source = os.Args[1]
		} else {
				source = "."
		}
		source += "/"

		// Processing each file on directory
		files, _ := ioutil.ReadDir(source)
		for _, file := range files {
				fileName := file.Name()
				if validFile(fileName) {
						wg.Add(1)
						go process(source + fileName, &wg)
				}
		}
		wg.Wait()
}

// Checks if the file name is validFile
func validFile(inlet string) bool {
		isEdf := true
		ext := ".edf"

		for i := 1; i <= 4 && isEdf; i++ {
				if inlet[len(inlet)-i] != ext[len(ext)-i] {
						 isEdf = false
				}
		}

		return isEdf
}

// Processes an EDF file, extracting the subject name and the sampling rate
func process(inlet string, wg *sync.WaitGroup) {
		edfContents := edf.ReadFile(inlet)
		fmt.Printf("%s\t%s\t%s\n", inlet, edfContents.Header["patient"], edfContents.GetSampling())
		wg.Done()
}
Clone this wiki locally