Skip to content

Commit

Permalink
add importable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Koushikphy committed Nov 21, 2022
1 parent 6e9aa2f commit 93dd95f
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 6 deletions.
2 changes: 1 addition & 1 deletion kfutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .funcs import *
from kfutils.funcs import getShape, showStats, writeShapedFile, writeFile, smoothen, repeat, mirror, PrepData
3 changes: 2 additions & 1 deletion kfutils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def createParser():
parser.add_argument('-dr', help="index of columns to convert to radian to degree",
nargs='+', metavar='COLS', type=listOfInts)
parser.add_argument('-dc', help="index of columns to drop", nargs='+', metavar='COLS', type=listOfInts)
parser.add_argument('-int', help="Interpolate to new number of grid. Can be 1D or 2D.", nargs='+', metavar='COLS', type=listOfInts)
return parser.parse_args()


Expand Down Expand Up @@ -93,7 +94,7 @@ def main():
if len(cols) == 1: #1D file
write1DFile(outFile, data)
else:
write2DFile(outFile, data, cols[0])
writeFile(outFile, data, cols[0])



Expand Down
143 changes: 139 additions & 4 deletions kfutils/funcs.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@

import numpy as np
from scipy.interpolate import RectBivariateSpline
from scipy.interpolate import RectBivariateSpline, InterpolatedUnivariateSpline
import os,shutil,subprocess,sys
from csaps import csaps
from tabulate import tabulate
import time


__all__ = ['showStats', 'smoothen', 'writeFile', 'write1DFile', 'repeat', 'mirror','PrepData']


def getSize(file):
size = os.path.getsize(file)
for unit in ['B', 'KB', 'MB', 'GB']:
Expand All @@ -17,6 +20,9 @@ def getSize(file):


def showStats(fileName):
'''
Show stats about the current file
'''
data = np.loadtxt(fileName)
table = [
["File Name",fileName],
Expand Down Expand Up @@ -71,14 +77,143 @@ def writeShapedFile(file,data,fmt='%.8f'):



def write2DFile(file,dat,tc=0,fmt='%.8f'):

def write1DFile(file,dat,fmt='%.8f'):
np.savetxt(file, dat, delimiter='\t', fmt=fmt)



def writeFile(file,dat,tc=0,fmt='%.8f'):
assert len(dat.shape)==2, "A 2D data is required for this function"

with open(file,'w') as f:
for i in np.unique(dat[:,tc]):
np.savetxt(f,dat[dat[:,tc]==i],delimiter='\t', fmt=fmt)
f.write('\n')


def write1DFile(file,dat,fmt='%.8f'):
np.savetxt(file, dat, delimiter='\t', fmt=fmt)

def rectGridInt(data, fc, sc, newGrid1, newGrid2):
# make a rectangular data dense or vice versa
g1 = np.unique(data[:,fc])
g2 = np.unique(data[:,sc])
c1 = g1.shape[0]
c2 = g2.shape[0]

ng1 = np.linspace(g1.min(),g1.max(),newGrid1)
ng2 = np.linspace(g2.min(),g2.max(),newGrid2)
ng1m, ng2m = np.meshgrid(ng1, ng2, indexing='ij')

result = []
for i in range(data.shape[1]):
if i==fc:
res = ng1m.reshape(-1)
elif i==sc:
res = ng2m.reshape(-1)
else:
res = RectBivariateSpline(g1,g2,data[:,i].reshape(c1,c2))(ng1, ng2).reshape(-1)
result.append(res)
return np.column_stack(result)





def mirror(data, fc=0, sc=1, pDiff=1):
assert len(data.shape)==2, "A 2D array is required"
# grid has to be in degree, for easy calculation
data = data[data[:,sc]<=180] # remove data after 180 phi
new_ph = np.arange(0,360+pDiff,pDiff)

res = []
for th in np.unique(data[:,fc]):
dat = data[data[:,fc]==th]
dat = np.vstack([dat, np.flipud(dat[:-1])]) # mirror two times
dat[:,sc] = new_ph
res.append(dat)
return np.vstack(res)



def repeat(data, fc=0, sc=1, pDiff=1):
assert len(data.shape)==2, "A 2D array is required"

# grid has to be in degree, for easy calculation
data = data[data[:,sc]<=120] # remove data after 120 phi
new_ph = np.arange(0,360+pDiff,pDiff)

res = []
for th in np.unique(data[:,fc]):
dat = data[data[:,fc]==th]
dat = np.vstack([dat, dat[1:], dat[1:]]) # repeat three times
dat[:,sc] = new_ph
res.append(dat)
return np.vstack(res)










class PrepData(object):
# Prepare your data with object chaining
def __init__(self,fileName):
try:
self.data = np.loadtxt(fileName)
except:
self.data = np.load(fileName)

def useCols(self,*cols):
self.data=self.data[:,cols]
return self

def toRad(self,*cols):
self.data[:,cols] = np.deg2rad(self.data[:,cols])
return self

def interpTo(self,cols,grid):
self.data = rectGridInt(self.data, cols[0],cols[1], grid[0], grid[1])
return self

def removeNegetive(self,cols=None):
if cols:
self.data[:,cols] = np.clip(self.data[:,cols], 0, np.inf)
else:
self.data = np.clip(self.data, 0, np.inf)
return self


def repeat(self,tC=0,pC=1,pDiff=1):
self.data = repeat(self.data,fc=tC,sc=pC,pDiff=pDiff)
return self


def mirror(self,tC=0,pC=1,pDiff=1):
self.data = mirror(self.data,fc=tC,sc=pC,pDiff=pDiff)
return self


def reshape(self,*shape):
self.data.shape = shape


def reshape3D(self,tc=0,pc=1):
tShape = np.unique(self.data[:,tc]).shape[0]
pShape = np.unique(self.data[:,pc]).shape[0]
shape = (tShape,pShape,-1)
self.data.shape = (tShape,pShape,-1)
print(f"Data Reshaped as {self.data.shape}")
return self


def getData(self):
return self.data


def writeFile(self,fileName,col=0,fmt='%.8f'):
writeFile(fileName,self.data,tc=col,fmt=fmt)
return self

0 comments on commit 93dd95f

Please sign in to comment.