Skip to content

Commit

Permalink
Ensure that previous redcap csvs still work with prepare-bids
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan8456 committed Feb 7, 2025
1 parent c1196ba commit f614554
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/b2aiprep/prepare/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,25 @@ def load_redcap_csv(file_path):
return None


def insert_missing_columns(df: DataFrame) -> DataFrame:
"""
Adds any missing columns to the dataframe.
Parameters
----------
df: DataFrame
The DataFrame to update.
"""
all_columns_path = files("b2aiprep").joinpath("prepare", "resources", "all_columns.json")
all_columns = json.load(all_columns_path.open())
columns_to_add = [col for col in all_columns if col not in df.columns]
for column in columns_to_add:
df[column] = np.nan

return df


def validate_redcap_df_column_names(df: DataFrame) -> DataFrame:
"""RedCap allows two distinct export formats: raw data or with labels.
The raw data format exports column names as coded entries, e.g. "record_id".
Expand Down Expand Up @@ -662,6 +681,9 @@ def redcap_to_bids(
# manual step has been done here.
validate_redcap_df_column_names(df)

# ensures that no columns are missing in the dataframe
df = insert_missing_columns(df)

construct_tsv_from_json( # construct participants.tsv
df=df,
json_file_path=os.path.join(outdir, "participants.json"),
Expand Down

0 comments on commit f614554

Please sign in to comment.