Skip to content

Commit

Permalink
update with interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Koushikphy committed Nov 21, 2022
1 parent 93dd95f commit 905605a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
## `kfutils`: A tool for common data file operations.
[![PyPI version](https://badge.fury.io/py/kfutils.svg)](https://badge.fury.io/py/kfutils)
[![Alt text](https://img.shields.io/pypi/v/kfutils.svg)](https://pypi.org/project/kfutils/)
[![Alt text](https://img.shields.io/pypi/pyversions/kfutils.svg)](https://pypi.org/project/kfutils/)
[![Alt text](https://img.shields.io/pypi/dm/kfutils.svg)](https://pypi.org/project/kfutils/)
[![Alt text](https://img.shields.io/pypi/l/kfutils.svg)](https://pypi.org/project/kfutils/)
[![Alt text](https://img.shields.io/pypi/status/kfutils.svg)](https://pypi.org/project/kfutils/)


<!--[![PyPI version](https://badge.fury.io/py/kfutils.svg)](https://badge.fury.io/py/kfutils)
![Alt text](https://img.shields.io/pypi/v/kfutils.svg) ![Alt text](https://img.shields.io/pypi/format/kfutils.svg) -->

### Instalation
Download and install the latest package from the [release section](https://github.com/Koushikphy/kfutils/releases/latest) or directly by pip
```bash
pip install kfutils
```
This installs the python module and a command line tool named `kfutils`.

### Usage

- __Using as a command line tool.__
```bash
kutils [-h] -i FILE [-o FILE] [-c COLS [COLS ...]] [-rd COLS [COLS ...]] [-dr COLS [COLS ...]] [-dc COLS [COLS ...]] [-int COLS [COLS ...]]
```
### Usage
| Argument | Description|
| ----------- | -----------
| `-i` | Input file name <br>If no operations are given it will show the stats about the file. |
Expand All @@ -17,3 +32,8 @@ pip install kfutils
| `-rd` | List of columns to convert to degree from radian |
| `-dr` | List of columns to convert to radian from degree |
| `-dc` | List of columns to delete |
| `-int` | Grid for the columns to interpolate |
- __Using as a python module__
The toplevel python module `kfutils` exposes several functions/class.
10 changes: 10 additions & 0 deletions kfutils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ def main():
if (args.dc):
data = np.delete(data, args.dc, axis=1)


if (args.int):
assert len(cols)==len(args.int), "Invalid number of columns for interpolation"
if len(cols)==1:
data = lineGridInt(data, cols[0], args.int[0])
elif len(cols)==2:
data = lineGridInt(data, *cols, *args.int)



# now write file
outFile = args.o
if not outFile:
Expand Down
36 changes: 27 additions & 9 deletions kfutils/funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time


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


def getSize(file):
Expand All @@ -21,7 +21,7 @@ def getSize(file):

def showStats(fileName):
'''
Show stats about the current file
Show stats about the file.
'''
data = np.loadtxt(fileName)
table = [
Expand Down Expand Up @@ -63,12 +63,16 @@ def smoothen(data, shape, tc, pc, cols, sm=0.95):


def getShape(data):
'''
Get details about the shape of the data
'''
for i in range(data.shape[1]):
print(np.unique(data[:,i]).shape)



def writeShapedFile(file,data,fmt='%.8f'):
''' Write a Shaped file '''
assert len(data.shape)==3, "A 3D data is required for this function"
with open(file, 'w') as f:
for i in data:
Expand All @@ -79,11 +83,13 @@ def writeShapedFile(file,data,fmt='%.8f'):


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



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

with open(file,'w') as f:
Expand All @@ -93,7 +99,26 @@ def writeFile(file,dat,tc=0,fmt='%.8f'):



def lineGridInt(data,fc,newGrid):
'''Interpolate a 1D data'''
# make a rectangular data dense or vice versa
g = data[:,fc]

ng = np.linspace(g.min(),g.max(),newGrid)

result = []
for i in range(data.shape[1]):
if i==fc:
res = ng
else:
res = InterpolatedUnivariateSpline(g,data[:,i])(ng)
result.append(res)
return np.column_stack(result)



def rectGridInt(data, fc, sc, newGrid1, newGrid2):
'''Interpolate a 2D data'''
# make a rectangular data dense or vice versa
g1 = np.unique(data[:,fc])
g2 = np.unique(data[:,sc])
Expand Down Expand Up @@ -153,12 +178,6 @@ def repeat(data, fc=0, sc=1, pDiff=1):









class PrepData(object):
# Prepare your data with object chaining
def __init__(self,fileName):
Expand Down Expand Up @@ -204,7 +223,6 @@ def reshape(self,*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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
txt = f.read()

setup(name='kfutils',
version='0.1.2',
version='0.1.3',
description='A tool for common data file operation.',
long_description=txt,
long_description_content_type='text/markdown',
Expand All @@ -18,6 +18,7 @@
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: 3.6',
'Topic :: System :: Shells'
],
keywords='File Operations',
Expand Down

0 comments on commit 905605a

Please sign in to comment.