|
@@ -2,7 +2,7 @@ import math
|
|
|
import pathlib as pl
|
|
import pathlib as pl
|
|
|
import random
|
|
import random
|
|
|
import re
|
|
import re
|
|
|
-from typing import Callable, Dict, Iterator, List, Tuple
|
|
|
|
|
|
|
+from typing import Callable, Dict, Iterator, List, Tuple, cast
|
|
|
|
|
|
|
|
import nibabel as nib
|
|
import nibabel as nib
|
|
|
import pandas as pd
|
|
import pandas as pd
|
|
@@ -47,9 +47,21 @@ def xls_pre(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
|
|
|
|
|
# Work on an explicit copy so column assignment writes back reliably instead
|
|
# Work on an explicit copy so column assignment writes back reliably instead
|
|
|
# of raising SettingWithCopyWarning against a view of ``df``.
|
|
# of raising SettingWithCopyWarning against a view of ``df``.
|
|
|
- data = df[["Image Data ID", "Sex", "Age (current)"]].copy()
|
|
|
|
|
- data["Sex"] = data["Sex"].str.strip()
|
|
|
|
|
- data = data.replace({"M": 0, "F": 1})
|
|
|
|
|
|
|
+ #
|
|
|
|
|
+ # The ``cast``s are needed because pandas ships no type stubs: basedpyright
|
|
|
|
|
+ # widens ``.copy()``/``.replace()``/``__getitem__`` to
|
|
|
|
|
+ # ``DataFrame | Series | Unknown``, which then has no ``.str`` accessor and
|
|
|
|
|
+ # isn't assignable to the declared return type. ``cast`` asserts the concrete
|
|
|
|
|
+ # pandas type at each step without any runtime effect.
|
|
|
|
|
+ data = cast(pd.DataFrame, df[["Image Data ID", "Sex", "Age (current)"]].copy())
|
|
|
|
|
+
|
|
|
|
|
+ # ``.str`` is pandas' vectorized string accessor: it applies a str method
|
|
|
|
|
+ # element-wise over every value in the Series. Here it strips surrounding
|
|
|
|
|
+ # whitespace from each Sex entry (e.g. " M " -> "M") before encoding.
|
|
|
|
|
+ sex_col = cast(pd.Series, data["Sex"])
|
|
|
|
|
+ data["Sex"] = sex_col.str.strip()
|
|
|
|
|
+
|
|
|
|
|
+ data = cast(pd.DataFrame, data.replace({"M": 0, "F": 1}))
|
|
|
|
|
|
|
|
return data
|
|
return data
|
|
|
|
|
|