Skip to content

Commit

Permalink
Merge pull request #1162 from DimitriPapadopoulos/refurb
Browse files Browse the repository at this point in the history
STY: Apply a few refurb suggestions
  • Loading branch information
oesteban committed Nov 27, 2023
2 parents 85565e4 + d41e41f commit 89fedbe
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 25 deletions.
9 changes: 4 additions & 5 deletions mriqc/bin/dfcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ def read_iqms(feat_file):
feat_file = Path(feat_file)

if feat_file.suffix == ".csv":
bids_comps = list(BIDS_COMP.keys())
x_df = pd.read_csv(
feat_file, index_col=False, dtype={col: str for col in bids_comps}
feat_file, index_col=False, dtype={col: str for col in BIDS_COMP}
)
# Find present bids bits and sort by them
bids_comps_present = list(set(x_df.columns.ravel().tolist()) & set(bids_comps))
bids_comps_present = [bit for bit in bids_comps if bit in bids_comps_present]
bids_comps_present = list(set(x_df.columns.ravel().tolist()) & set(BIDS_COMP))
bids_comps_present = [bit for bit in BIDS_COMP if bit in bids_comps_present]
x_df = x_df.sort_values(by=bids_comps_present)
# Remove sub- prefix in subject_id
x_df.subject_id = x_df.subject_id.str.lstrip("sub-")

# Remove columns that are not IQMs
feat_names = list(x_df._get_numeric_data().columns.ravel())
for col in bids_comps:
for col in BIDS_COMP:
try:
feat_names.remove(col)
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion mriqc/interfaces/anatomical.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def _run_interface(self, runtime): # pylint: disable=R0914,E1101
# SNR
snrvals = []
self._results["snr"] = {}
for tlabel in ["csf", "wm", "gm"]:
for tlabel in ("csf", "wm", "gm"):
snrvals.append(
snr(
stats[tlabel]["median"],
Expand Down
2 changes: 1 addition & 1 deletion mriqc/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _run_interface(self, runtime):

# Fill in the "bids_meta" key
id_dict = {}
for comp in list(BIDS_COMP.keys()):
for comp in BIDS_COMP:
comp_val = getattr(self.inputs, comp, None)
if isdefined(comp_val) and comp_val is not None:
id_dict[comp] = comp_val
Expand Down
4 changes: 2 additions & 2 deletions mriqc/qc/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ def gsr(epi_data, mask, direction="y", ref_file=None, out_file=None):
"""
direction = direction.lower()
if direction[-1] not in ["x", "y", "all"]:
if direction[-1] not in ("x", "y", "all"):
raise Exception(
f"Unknown direction {direction}, should be one of x, -x, y, -y, all"
)

if direction == "all":
result = []
for newdir in ["x", "y"]:
for newdir in ("x", "y"):
ofile = None
if out_file is not None:
fname, ext = op.splitext(ofile)
Expand Down
17 changes: 8 additions & 9 deletions mriqc/reports/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def gen_html(csv_file, mod, csv_failed=None, out_file=None):
(["efc"], None),
(["fber"], None),
(["fwhm", "fwhm_x", "fwhm_y", "fwhm_z"], "mm"),
(["gsr_%s" % a for a in ["x", "y"]], None),
(["gsr_%s" % a for a in ("x", "y")], None),
(["snr"], None),
(["dvars_std", "dvars_vstd"], None),
(["dvars_nstd"], None),
Expand Down Expand Up @@ -203,12 +203,11 @@ def gen_html(csv_file, mod, csv_failed=None, out_file=None):
}

if csv_file.suffix == ".csv":
def_comps = list(BIDS_COMP.keys())
dataframe = pd.read_csv(
csv_file, index_col=False, dtype={comp: object for comp in def_comps}
csv_file, index_col=False, dtype={comp: object for comp in BIDS_COMP}
)

id_labels = list(set(def_comps) & set(dataframe.columns.ravel().tolist()))
id_labels = list(set(BIDS_COMP) & set(dataframe.columns.ravel().tolist()))
dataframe["label"] = dataframe[id_labels].apply(
_format_labels, args=(id_labels,), axis=1
)
Expand Down Expand Up @@ -291,9 +290,9 @@ def gen_html(csv_file, mod, csv_failed=None, out_file=None):

def _format_labels(row, id_labels):
"""format participant labels"""
crow = []

for col_id, prefix in list(BIDS_COMP.items()):
if col_id in id_labels:
crow.append(f"{prefix}-{row[[col_id]].values[0]}")
crow = (
f"{prefix}-{row[[col_id]].values[0]}"
for col_id, prefix in list(BIDS_COMP.items())
if col_id in id_labels
)
return "_".join(crow)
11 changes: 5 additions & 6 deletions mriqc/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def reorder_csv(csv_file, out_file=None):
# The column does not exist
pass

for col in ["scan", "session", "subject"]:
for col in ("scan", "session", "subject"):
cols.remove(col)
cols.insert(0, col)

Expand Down Expand Up @@ -144,7 +144,7 @@ def generate_pred(derivatives_dir, output_dir, mod):
if not jsonfiles:
return None

headers = list(BIDS_COMP.keys()) + ["mriqc_pred"]
headers = list(BIDS_COMP) + ["mriqc_pred"]
predictions = {k: [] for k in headers}

for jsonfile in jsonfiles:
Expand All @@ -157,12 +157,12 @@ def generate_pred(derivatives_dir, output_dir, mod):
for k in headers:
predictions[k].append(data.pop(k, None))

dataframe = pd.DataFrame(predictions).sort_values(by=list(BIDS_COMP.keys()))
dataframe = pd.DataFrame(predictions).sort_values(by=list(BIDS_COMP))

# Drop empty columns
dataframe.dropna(axis="columns", how="all", inplace=True)

bdits_cols = list(set(BIDS_COMP.keys()) & set(dataframe.columns.ravel()))
bdits_cols = list(set(BIDS_COMP) & set(dataframe.columns.ravel()))

# Drop duplicates
dataframe.drop_duplicates(bdits_cols, keep="last", inplace=True)
Expand Down Expand Up @@ -208,8 +208,7 @@ def generate_tsv(output_dir, mod):


def _read_and_save(in_file):
data = json.loads(Path(in_file).read_text())
return data if data else None
return json.loads(Path(in_file).read_text()) or None


def _flatten(in_dict, parent_key="", sep="_"):
Expand Down
2 changes: 1 addition & 1 deletion mriqc/workflows/functional/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def spikes_mask(in_file, in_mask=None, out_file=None):
else:
new_mask_3d = np.zeros(in_4d_nii.shape[:3]) == 1

if orientation[0] in ["L", "R"]:
if orientation[0] in ("L", "R"):
new_mask_3d[0:2, :, :] = True
new_mask_3d[-3:-1, :, :] = True
else:
Expand Down

0 comments on commit 89fedbe

Please sign in to comment.