-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
94 lines (86 loc) · 2.69 KB
/
tools.py
File metadata and controls
94 lines (86 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from __future__ import print_function
import numpy as np
import sys
import copy
from scipy import interpolate
import os
import errno
# tools module
def takeRatio(d0,d1,logit=False):
print ('d1'," ",len(d1),'\n')
print ('d0',' ',len(d0),'\n')
dif = len(d0)-len(d1)
d0x=d0[:,0]
d0y=d0[:,1]
d1x=d1[:,0]
d1y=d1[:,1]
if logit==True:
d0x=np.log10(d0x)
d0y=np.log10(d0y)
d1x=np.log10(d1x)
d1y=np.log10(d1x)
if dif > 0:
ratioDat=np.zeros((len(d1),2))
fInterp = interpolate.pchip(d0x,d0y) #!!!Important pchip = monotonic interpolation
condition=d1x<=max(d0x)
ratio=d1y[condition]/fInterp(d1x[condition])
ratioDat=ratioDat[:len(ratio),:]
ratioDat=np.transpose([d1x[condition],ratio])
print (str(dif)+' elements deleted')
elif dif < 0:
ratioDat=np.zeros((len(d0),2))
fInterp = interpolate.pchip(d1x,d1y)
condition=d0x<=max(d1x)
print (d0x[condition],'\n',d1x)
ratio=fInterp(d0x[condition])/d0y[condition]
ratioDat=ratioDat[:len(ratio),:]
ratioDat=np.transpose([d0x[condition],ratio])
print (str(dif)+' elements deleted')
else:
ratioDat=np.zeros((len(d0),2))
fInterp = interpolate.pchip(d1x,d1y)
condition=d0x<=max(d1x)
ratio=fInterp(d0x[condition])/d0y[condition] #in all cases data d1 is divided through data d0
ratioDat=ratioDat[:len(ratio),:]
ratioDat=np.transpose([d0x[condition],ratio])
print ('equal size')
#print ratioDat, '\n'
#print darray1, '\n'
return ratioDat
def derivatives(x,y,order=1):
yderi=np.diff(y)/np.diff(x)
xderi=(x[1:]+x[:-1])/2
if order==2:
xderi2, yderi2 = derivatives(xderi,yderi,1)
return xderi,yderi,xderi2,yderi2
elif order==1:
return xderi,yderi
else:
print ("error, function does not return "+str(order)+"order derivatives")
return None
def calcMaxLim(x,y,xmin,maxArra,minArra):
arra = np.transpose([x,y])
arra = arra[arra[:,0]>=xmin]
maxY = max(arra[:,1])
minY = min(arra[:,1])
maxArra.append(maxY)
minArra.append(minY)
return maxArra, minArra
def mkdirp(dirname):
try:
os.mkdir(dirname)
except OSError as exc:
if exc.errno != errno.EEXIST:
raise
pass
def fileexists(filename):
flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
try:
file_handle = os.open(filename, flags)
except OSError as e:
if e.errno == errno.EEXIST: # Failed as the file already exists.
return True
else: # Something unexpected went wrong so reraise the exception.
raise
else:
return False