Skip to content

Commit

Permalink
automatically reindex row/col indices if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Apr 12, 2022
1 parent 6b65d79 commit 0debe3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions cactice/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def read_grids_csv(
# load the CSV into a data frame
# TODO: determine whether to read headers or not based on params
df = pd.read_csv(path, sep=',')
print(df)

# if there's only 1 grid and no specified name column, create it
if name_header not in df: df[name_header] = 1
Expand All @@ -86,9 +87,17 @@ def read_grids_csv(
# subset the data frame corresponding to the current grid
sdf = df.loc[df[name_header] == name]

# reindex rows and cols
rows = sorted(list(set(sdf[row_header])))
nums = sorted(list(set(sdf[col_header])))

# attach new row and column indices to dataframe
sdf.loc[df[name_header] == name, 'I'] = sdf.apply(lambda r: rows.index(r[row_header]), axis=1)
sdf.loc[df[name_header] == name, 'J'] = sdf.apply(lambda r: nums.index(r[col_header]), axis=1)

# find row and column counts (including missing values)
rows = max(sdf[row_header]) - min(sdf[row_header]) + 1
cols = max(sdf[col_header]) - min(sdf[col_header]) + 1
rows = max(sdf['I']) - min(sdf['I']) + 1
cols = max(sdf['J']) - min(sdf['J']) + 1

# initialize grid as empty 2D array
grid = np.zeros(shape=(rows, cols))
Expand All @@ -98,7 +107,7 @@ def read_grids_csv(
for j in range(0, cols):

# check entry at location (i, j)
matched = sdf.loc[(df[row_header] == i) & (df[col_header] == j)]
matched = sdf.loc[(sdf['I'] == i) & (sdf['J'] == j)]

# if missing, fill it in with class = 0 (unknown)
if len(matched) == 0:
Expand All @@ -111,12 +120,12 @@ def read_grids_csv(
}, ignore_index=True)
# otherwise use the given value
else:
cls = matched.to_dict('records')[0][class_header]
cls = int(matched.to_dict('records')[0][class_header])

# update the grid
grid[i, j] = cls

# cast to int and save by name
grids[str(name)] = grid.astype(int)

return grids
return grids
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setuptools.setup(
name='cactice',
version='0.0.4',
version='0.0.5',
description='computing agricultural crop lattices',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 0debe3c

Please sign in to comment.