Skip to content

Commit

Permalink
new repeat mirror option
Browse files Browse the repository at this point in the history
  • Loading branch information
Koushikphy committed Nov 23, 2022
1 parent b5c8f28 commit 98a231f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ This installs the python module and a command line tool named `kfutils`.

__✈ 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 ...]]
kutils [-h] -i FILE [-o FILE] [-c COLS [COLS ...]] [-rd COLS [COLS ...]] [-dr COLS [COLS ...]] [-dc COLS [COLS ...]] [-int N [N ...]] [-mir N] [-rep N]
```
| Argument | Description|
| ----------- | -----------
| `-i` | Input file name <br>If no operations are given it will show the stats about the file. |
| `-o` | Output file name |
| `-c` | Index(s) of grid columns. <br> 2 columns for 2D file. |
| `-s` | Input file name to get overall stats |
| `-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 |
| `-o` | OOutput file name. (default: '_out' prefix to input file name) |
| `-c` | Index(s) of grid columns. 2 columns for 2D file. |
| `-rd` | Index(s) of columns to convert to degree from radian |
| `-dr` | Index(s) of columns to convert to radian from degree |
| `-dc` | Index(s) of columns to delete |
| `-int` | Number of grid to interpolate to. Can be 1D or 2D |
| `-mir` | Number of times to mirror |
| `-rep` | Number of times to repeat |
Expand Down
37 changes: 25 additions & 12 deletions kfutils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,29 @@ def createParser():
#main parser
parser = CustomParser(prog="kutils",
formatter_class=argparse.RawTextHelpFormatter,
description="A tool for common data file operation.",
epilog="Version: {}\nCreated by Koushik Naskar (koushik.naskar9@gmail.com)".format(version)
description="A tool for common data file operations.",
epilog="Version: {}\nhttps://github.com/Koushikphy/kfutils\nCreated by Koushik Naskar (koushik.naskar9@gmail.com)".format(version)
)

#adding options for numerical jobs
parser.add_argument('-i', type=str, help="Input file name. \nIf no operations are given it will show the stats about the file.", metavar="FILE", required=True)
parser.add_argument('-o', type=str, help="Output file name", metavar="FILE")
parser.add_argument('-c', help="index(s) of grid columns. 2 columns for 2D file.", nargs='+', metavar='COLS', type=listOfInts)
parser.add_argument('-rd', help="index of columns to convert to degree from radian",
parser.add_argument('-o', type=str, help="Output file name. (default: '_out' prefix to input file name)", metavar="FILE")
parser.add_argument('-c', help="Index(s) of grid columns. 2 columns for 2D file.", nargs='+', metavar='COLS', type=listOfInts)
parser.add_argument('-rd', help="Index(s) of columns to convert to degree from radian",
nargs='+', metavar='COLS',type=listOfInts)
parser.add_argument('-dr', help="index of columns to convert to radian to degree",
parser.add_argument('-dr', help="Index(s) of columns to convert to radian from 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)
parser.add_argument('-dc', help="Index(s) of columns to delete", nargs='+', metavar='COLS', type=listOfInts)
parser.add_argument('-int', help="Number of grid to interpolate to. Can be 1D or 2D", nargs='+', metavar='N', type=listOfInts)
parser.add_argument('-mir', help="Number of times to mirror", metavar='N', type=int)
parser.add_argument('-rep', help="Number of times to repeat", metavar='N', type=int)

return parser.parse_args()


def commandGiven(args):
# check any of these command is given or not
for elem in ['c','rd','dr','dc']:
for elem in ['c','rd','dr','dc','int','mir','rep']:
if getattr(args,elem):
return True
return False
Expand Down Expand Up @@ -94,15 +97,25 @@ def main():


if (args.int):
if not cols:
raise ValueError("Index(s) of columns needed for interpolation")
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 = rectGridInt(data, *cols, *args.int)

if (args.mr): # do mirror


if args.mir or args.rep: # do mirror
cl = len(cols)
if cl==0:
raise ValueError("Column index(s) required")
elif cl>2:
raise ValueError("Column index(s) required")
else:
if args.mir:
data = repMirror(data, cols,args.mir,'mir')
else:
data = repMirror(data, cols,args.rep,'rep')

# now write file
outFile = args.o
Expand Down
34 changes: 27 additions & 7 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','rectGridInt','lineGridInt']
__all__ = ['showStats', 'smoothen', 'writeFile', 'write1DFile', 'repeat', 'mirror','PrepData','rectGridInt','lineGridInt','repMirror']


def getSize(file):
Expand Down Expand Up @@ -142,28 +142,48 @@ def rectGridInt(data, fc, sc, newGrid1, newGrid2):



def repMirror(data, cols, times):
def repMirror(data, cols, times, tp):
# 1D or 2D
# for 1D
# check values are full
col = cols[0] # just one column
if(len(cols)==1):
return repMir1D(data, cols[0], times, tp)
elif len(cols)==2:
fc,sc = cols
uniqueVals = np.unique(data[:,fc])
res = []
for th in uniqueVals:
dat = data[data[:,fc]==th]
res.append(repMir1D(dat, sc, times,tp))
return np.vstack(res)
else:
raise ValueError("Invalid number of columns.")


def repMir1D(data, col, times, tp):
grid = data[:,col]
grids = [grid]
for i in range(1,times):
grids.append(grid[1:]+grid[-1]*i)
newGrid = np.append(*grids)
newGrid = np.concatenate(grids)


dats = [data]
for i in range(1,times):
dats.append(np.flipud(data[:-1]))

if tp=='mir':
for i in range(1,times):
dats.append(np.flipud(data[:-1]))
elif tp=='rep':
for i in range(1,times):
dats.append(data[:-1])
else:
raise ValueError("Invalid type.")
res = np.vstack(dats)
res[:,col]= newGrid
return res




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
Expand Down
2 changes: 1 addition & 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.5',
version='0.1.6',
description='A tool for common data file operation.',
long_description=txt,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 98a231f

Please sign in to comment.