Skip to content

Commit

Permalink
fixed the bug in load_openpiv_txt
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlib committed Oct 11, 2022
1 parent 177babe commit c4a282d
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 82 deletions.
104 changes: 42 additions & 62 deletions examples/notebooks/test_reading_different_files.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -11,101 +11,82 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import xarray as xr\n",
"from typing import List \n",
"import numpy as np\n",
"import pandas as pd\n",
"import pkg_resources as pkg\n"
"import pkg_resources as pkg\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[PosixPath('/home/user/Documents/repos/pivpy/pivpy/data/Insight'), PosixPath('/home/user/Documents/repos/pivpy/pivpy/data/PIV_Challenge'), PosixPath('/home/user/Documents/repos/pivpy/pivpy/data/day2'), PosixPath('/home/user/Documents/repos/pivpy/pivpy/data/openpiv'), PosixPath('/home/user/Documents/repos/pivpy/pivpy/data/urban_canopy')]\n",
"/home/user/Documents/repos/pivpy/pivpy/data/Insight/Run000001.T000.D000.P000.H001.L.vec\n",
"/home/user/Documents/repos/pivpy/pivpy/data/PIV_Challenge/B00001.txt\n",
"/home/user/Documents/repos/pivpy/pivpy/data/day2/day2a005000.T000.D000.P003.H001.L.vec\n",
"/home/user/Documents/repos/pivpy/pivpy/data/openpiv/exp1_001_b.txt\n",
"/home/user/Documents/repos/pivpy/pivpy/data/urban_canopy/B00001.vc7\n"
]
}
],
"outputs": [],
"source": [
"import pathlib\n",
"\n",
"path = pathlib.Path(pkg.resource_filename(\"pivpy\", \"data\"))\n",
"subdirs = [x for x in sorted(path.glob('**/*')) if x.is_dir()]\n",
"# subdirs = [s for s in subdirs if s.stem != '.ipynb_checkpoints']\n",
"print(subdirs)\n",
"subdirs = [s for s in subdirs if s.stem != '.ipynb_checkpoints']\n",
"\n",
"# sorted(dirs[0].rglob('[!.]*'))\n",
"test_files = []\n",
"for d in subdirs:\n",
" files = [x for x in sorted(d.rglob('[!.]*')) if not x.is_dir() ]\n",
" print(files[0])\n",
" test_files.append(files[0])"
" files = [x for x in sorted(d.glob('[!.]*')) if not x.is_dir() ]\n",
" test_files.append(files[0])\n",
" \n",
"print(test_files)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/user/Documents/repos/pivpy/pivpy/data/Insight/Run000001.T000.D000.P000.H001.L.vec\n",
"Run000001.T000.D000.P000.H001.L\n",
"1\n",
"/home/user/Documents/repos/pivpy/pivpy/data/PIV_Challenge/B00001.txt\n",
"B00001\n",
"1\n",
"/home/user/Documents/repos/pivpy/pivpy/data/day2/day2a005000.T000.D000.P003.H001.L.vec\n",
"day2a005000.T000.D000.P003.H001.L\n",
"5000\n",
"/home/user/Documents/repos/pivpy/pivpy/data/openpiv/exp1_001_b.txt\n",
"exp1_001_b\n",
"1\n",
"/home/user/Documents/repos/pivpy/pivpy/data/urban_canopy/B00001.vc7\n",
"B00001\n",
"1\n"
]
}
],
"outputs": [],
"source": [
"from pivpy.io import *\n",
"\n",
"for file in test_files:\n",
" print(file)\n",
" print(file.stem)\n",
" #print(file)\n",
" # print(file.stem)\n",
" variables, units, rows, cols, dt, frame, method = parse_header(file)\n",
" print(frame)\n",
" # print(f'frame = {frame}')\n",
" print(file.stem, method)\n",
" ds = method(file)\n",
" # print(ds.head())\n"
" plt.figure()\n",
" ds.isel(t=0).piv.quiver(arrScale=5)\n",
" plt.title(file.stem)\n",
" \n",
" \n",
" if file.suffix == '.vec':\n",
" try:\n",
" # try another method\n",
" ds = load_insight_vec_as_csv(file)\n",
" plt.figure()\n",
" ds.isel(t=0).piv.quiver(arrScale=5)\n",
" plt.title(file.stem)\n",
" except:\n",
" pass\n",
" elif file.suffix == '.txt':\n",
" try:\n",
" ds = load_openpiv_txt_as_csv(file)\n",
" plt.figure()\n",
" ds.isel(t=0).piv.quiver(arrScale=5)\n",
" plt.title(file.stem)\n",
" except:\n",
" pass\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.12 64-bit",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -121,13 +102,12 @@
"pygments_lexer": "ipython3",
"version": "3.8.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "cc9f4a0bb10ffa66460a048df631f1f9f016c2b1c927bb3119f96382f3a023b9"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
File renamed without changes.
File renamed without changes.
103 changes: 88 additions & 15 deletions pivpy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ def from_df(

x, ix = unsorted_unique(d[:, 0])
y, iy = unsorted_unique(d[:, 1])
if d.shape[1] < 5: # not always there's a mask
tmp = np.ones((d.shape[0], 5))
tmp[:, :-1] = d
d = tmp

if d.shape[1] < 5: # davis8 does not have mask or chc
d = np.column_stack((d,np.zeros_like(d[:,-1])))

if ix[1] == 1: #x grows first
d = d.reshape(len(y), len(x), 5).transpose(1, 0, 2)
elif iy[1] == 1: # y grows first
d = d.reshape(len(y), len(x), 5) # .transpose(1,0,2)
d = d.reshape(len(x), len(y), 5) # .transpose(1,0,2)
else:
raise ValueError('not sorted x or y')

Expand Down Expand Up @@ -269,26 +269,33 @@ def load_vec(
"""
if rows is None or cols is None:
_, _, rows, cols, dt, frame, _ = parse_header(filename)
# print(f'rows = {rows}, cols = {cols}')

if rows is None: # means no headers
d = np.genfromtxt(filename, usecols=(0, 1, 2, 3, 4))
if rows is None: # means no headers, openpiv vec file
# d = np.genfromtxt(filename, usecols=(0, 1, 2, 3, 4))
d = np.genfromtxt(
filename,
usecols=(0, 1, 2, 3, 4)
)
x, ix = unsorted_unique(d[:, 0])
y, iy = unsorted_unique(d[:, 1])

# print(f'rows = {len(y)}, cols = {len(x)}')

if ix[1] == 1: #x grows first
d = d.reshape(len(y), len(x), 5).transpose(1, 0, 2)
elif iy[1] == 1: # y grows first
d = d.reshape(len(y), len(x), 5) # .transpose(1,0,2)
else:
raise ValueError('not sorted x or y')
else:
# d = np.genfromtxt(
# filename, skiprows=1, delimiter=",", usecols=(0, 1, 2, 3, 4)
# ).reshape(rows, cols, 5)
else: # Insight VEC file
d = np.genfromtxt(
filename, skip_header=1, delimiter=",", usecols=(0, 1, 2, 3, 4)
).reshape(cols, rows, 5)
x = d[:, :, 0][0, :]
y = d[:, :, 1][:, 0]
).reshape(cols, rows, 5).transpose(1, 0, 2)

x = d[:, :, 0][:, 0]
y = d[:, :, 1][0, :]


u = d[:, :, 2]
v = d[:, :, 3]
Expand All @@ -313,6 +320,33 @@ def load_vec(

return dataset

def load_insight_vec_as_csv(
filename: pathlib.Path,
rows: int = None,
cols: int = None,
delta_t: float = None,
frame: int = 0,
) -> xr.Dataset:
"""
load_insight_vec_as_csv(filename,rows=rows,cols=cols)
Loads the VEC file (TECPLOT format by TSI Inc.),
Arguments:
filename : file name, expected to have a header and 5 columns
rows, cols : number of rows and columns of a vector field,
if None, None, then parse_header is called to infer the number
written in the header
DELTA_T : time interval (default is None)
frame : frame or time marker (default is None)
Output:
dataset is a xAarray Dataset, see xarray for help
"""
df = pd.read_csv(filename,header=None,skiprows=1,names=["x","y","u","v","chc"])
dataset = from_df(df,frame=frame,filename=filename)

return dataset




def load_vc7(
filename: pathlib.Path,
Expand Down Expand Up @@ -596,6 +630,45 @@ def load_openpiv_txt(
return dataset


def load_openpiv_txt_as_csv(
filename: str,
rows: int = None,
cols: int = None,
delta_t: float = None,
frame: int = 0,
) -> xr.Dataset:
""" loads OpenPIV txt file
Args:
filename (str): _description_
rows (int, optional): _description_. Defaults to None.
cols (int, optional): _description_. Defaults to None.
delta_t (float, optional): _description_. Defaults to None.
frame (int, optional): _description_. Defaults to 0.
Returns:
xr.Dataset: _description_
"""
df = pd.read_csv(
filename,
header=None,
names=['x','y','u','v','chc'],
delim_whitespace=True
)

dataset = from_df(
df,
frame=frame,
filename=filename
)

return dataset






def load_davis8_txt(
filename: pathlib.Path,
rows: int = None, # pylint: disable=W0613
Expand All @@ -618,7 +691,7 @@ def load_davis8_txt(
dataframe = pd.read_csv(
filename, delimiter="\t", skiprows=1, names=["x", "y", "u", "v"], decimal=","
)
dataset = from_df(dataframe, frame=frame)
dataset = from_df(dataframe, frame=frame,filename=filename)
# print(f'{rows},{cols},{delta_t}')
return dataset

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "pivpy"
version = "0.0.17"
version = "0.0.18"
authors = [
{ name="Alex Liberzon and Ron Shnapp", email="[email protected]" },
]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def test_histogram():
def test_quiver_openpiv_vec():
""" tests quiver of openpiv vec file
"""
filename = pathlib.Path(pkg.resource_filename("pivpy", "data")) / "openpiv" / "exp1_001_b.vec"
filename = pathlib.Path(
pkg.resource_filename("pivpy", "data")) / "openpiv_vec" / "exp1_001_b.vec"
print(filename, filename.exists())
_d = io.load_vec(filename)
_d.piv.quiver() # notice the warning
Expand Down
6 changes: 3 additions & 3 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
path = pathlib.Path(pkg.resource_filename("pivpy", "data"))

vec_file = path / "Insight" / "Run000002.T000.D000.P000.H001.L.vec"
openpiv_txt_file = path / "openpiv" / "exp1_001_b.txt"
openpiv_txt_file = path / "openpiv_txt" / "exp1_001_b.txt"


def test_get_dt():
Expand All @@ -27,11 +27,11 @@ def test_get_frame():
)
assert frame == 2
_, _, _, _, _, frame,_ = io.parse_header(
path / "openpiv" / "exp1_001_b.vec"
path / "openpiv_vec" / "exp1_001_b.vec"
)
assert frame == 1
_, _, _, _, _, frame,_ = io.parse_header(
path / "openpiv" / "exp1_001_b.txt"
path / "openpiv_txt" / "exp1_001_b.txt"
)
assert frame == 1

Expand Down

0 comments on commit c4a282d

Please sign in to comment.