From 905605a7ed11a00c08f7c880b087e8da77549d36 Mon Sep 17 00:00:00 2001 From: Koushik Naskar Date: Mon, 21 Nov 2022 23:49:21 +0530 Subject: [PATCH] update with interpolation --- README.md | 24 ++++++++++++++++++++++-- kfutils/cli.py | 10 ++++++++++ kfutils/funcs.py | 36 +++++++++++++++++++++++++++--------- setup.py | 3 ++- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 73afa51..6cf117a 100644 --- a/README.md +++ b/README.md @@ -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/) + + + ### 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
If no operations are given it will show the stats about the file. | @@ -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. diff --git a/kfutils/cli.py b/kfutils/cli.py index 867dc56..ca6343f 100644 --- a/kfutils/cli.py +++ b/kfutils/cli.py @@ -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: diff --git a/kfutils/funcs.py b/kfutils/funcs.py index fb4d933..0a36fd8 100644 --- a/kfutils/funcs.py +++ b/kfutils/funcs.py @@ -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): @@ -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 = [ @@ -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: @@ -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: @@ -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]) @@ -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): @@ -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 diff --git a/setup.py b/setup.py index d17ce85..3a839e5 100644 --- a/setup.py +++ b/setup.py @@ -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', @@ -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',