Skip to content

Instantly share code, notes, and snippets.

@Vindaar
Created July 10, 2018 18:03
Show Gist options
  • Save Vindaar/4b3c00bbe7508dbd006cd9d8758d07bb to your computer and use it in GitHub Desktop.
Save Vindaar/4b3c00bbe7508dbd006cd9d8758d07bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import numpy as np
import re
def parseFile(filename):
lines = open(filename).readlines()
x = []
y = []
for l in lines:
linesplit = l.split()
x.append(float(linesplit[0]))
y.append(float(linesplit[1]))
x = np.asarray(x)
y = np.asarray(y)
return x, y
def main(args):
if len(args) == 0:
quit("Please hand a folder with files")
path = args[0]
reg = re.compile(r"(\d+)_1\.norm")
filenameTemplate = "{}_{}.norm"
xMeans = []
yMeans = []
for folder, _, files in os.walk(path):
for name in files:
if reg.match(name):
# match all 3 files
m = reg.match(name)
# create all filenames using template
f2 = filenameTemplate.format(m.group(1), 2)
f3 = filenameTemplate.format(m.group(1), 3)
# parse all files
x1, y1 = parseFile(os.path.join(folder, name))
x2, y2 = parseFile(os.path.join(folder, f2))
x3, y3 = parseFile(os.path.join(folder, f3))
# zip together, and put into array
x = np.asarray(zip(x1, x2, x3))
# mean along "x" axis of array
xmean = np.mean(x, axis = 1)
y = np.asarray(zip(y1, y2, y3))
ymean = np.mean(y, axis = 1)
xMeans.append(xmean)
yMeans.append(ymean)
print xMeans, yMeans
if __name__=="__main__":
import sys
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment