From 5f360fc0f4a213d6abec3495987ac4bcd4340f0e Mon Sep 17 00:00:00 2001 From: RobbeF Date: Wed, 18 Nov 2026 10:33:55 +0100 Subject: [PATCH] fix: silence anndata index-cast warning when reading FCS files --- pyproject.toml | 5 +-- src/flowsom/io/read_fcs.py | 56 ++++++++++++++++++++++----------- src/flowsom/pp/fcs_functions.py | 14 +++++---- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0018048..27b9cb4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,13 +4,14 @@ requires = [ "hatchling" ] [project] name = "flowsom" -version = "0.2.2" +version = "0.2.3" description = "The complete FlowSOM package known from R, now available in Python!" readme = "README.md" license = { file = "LICENSE" } maintainers = [ { name = "Artuur Couckuyt", email = "Artuur.Couckuyt@ugent.be" }, { name = "Benjamin Rombaut", email = "Benjamin.Rombaut@ugent.be" }, + { name = "Robbe Fonteyn", email = "Robbe.Fonteyn@ugent.be" }, { name = "Yvan Saeys", email = "Yvan.Saeys@UGent.be" }, { name = "Sofie Van Gassen", email = "Sofie.VanGassen@UGent.be" }, ] @@ -66,7 +67,7 @@ optional-dependencies.doc = [ "sphinxcontrib-bibtex>=1", "sphinxext-opengraph", ] -optional-dependencies.test = [ "coverage", "pyflowsom", "pytest" ] +optional-dependencies.test = [ "coverage", "pyflowsom>=0.1.5", "pytest" ] urls.Documentation = "https://flowsom.readthedocs.io/en/latest/" urls.Home-page = "https://github.com/saeyslab/FlowSOM_Python" urls.Source = "https://github.com/saeyslab/FlowSOM_Python" diff --git a/src/flowsom/io/read_fcs.py b/src/flowsom/io/read_fcs.py index 913d04a..c79f2ec 100644 --- a/src/flowsom/io/read_fcs.py +++ b/src/flowsom/io/read_fcs.py @@ -1,10 +1,29 @@ from __future__ import annotations import re +import warnings +from contextlib import contextmanager import anndata as ad import pandas as pd import readfcs +from anndata import ImplicitModificationWarning + + +@contextmanager +def _ignore_index_cast_warning(): + """Silence anndata's warning about casting integer indices to str. + + readfcs builds AnnData from frames with integer indices; anndata casts them + to str and warns. Harmless and not actionable by the user, so silence it. + """ + with warnings.catch_warnings(): + warnings.filterwarnings( + "ignore", + message="Transforming to str index", + category=ImplicitModificationWarning, + ) + yield def read_FCS(filepath): @@ -13,24 +32,25 @@ def read_FCS(filepath): :param filepath: An array containing a full path to the FCS file :type filepath: str """ - try: - f = readfcs.read(filepath, reindex=True) - f.var.n = f.var.n.astype(int) - f.var = f.var.sort_values(by="n") - # f.uns["meta"]["channels"].index = f.uns["meta"]["channels"].index.astype(int) - # f.uns["meta"]["channels"] = f.uns["meta"]["channels"].sort_index() - except ValueError: - f = readfcs.read(filepath, reindex=False) - markers = { - str(re.sub("S$", "", re.sub("^P", "", string))): f.uns["meta"][string] - for string in f.uns["meta"].keys() - if re.match("^p[0-9]+s$", string) - } - fluo_channels = list(markers.keys()) - non_fluo_channels = {i: f.var["channel"][i] for i in f.var.index if i not in fluo_channels} - index_markers = dict(markers, **non_fluo_channels) - f.var.rename(index=index_markers, inplace=True) - # f.uns["meta"]["channels"]["$PnS"] = [index_markers[key] for key in f.uns["meta"]["channels"].index] + with _ignore_index_cast_warning(): + try: + f = readfcs.read(filepath, reindex=True) + f.var.n = f.var.n.astype(int) + f.var = f.var.sort_values(by="n") + # f.uns["meta"]["channels"].index = f.uns["meta"]["channels"].index.astype(int) + # f.uns["meta"]["channels"] = f.uns["meta"]["channels"].sort_index() + except ValueError: + f = readfcs.read(filepath, reindex=False) + markers = { + str(re.sub("S$", "", re.sub("^P", "", string))): f.uns["meta"][string] + for string in f.uns["meta"].keys() + if re.match("^p[0-9]+s$", string) + } + fluo_channels = list(markers.keys()) + non_fluo_channels = {i: f.var["channel"][i] for i in f.var.index if i not in fluo_channels} + index_markers = dict(markers, **non_fluo_channels) + f.var.rename(index=index_markers, inplace=True) + # f.uns["meta"]["channels"]["$PnS"] = [index_markers[key] for key in f.uns["meta"]["channels"].index] return f diff --git a/src/flowsom/pp/fcs_functions.py b/src/flowsom/pp/fcs_functions.py index d594e12..d04f863 100644 --- a/src/flowsom/pp/fcs_functions.py +++ b/src/flowsom/pp/fcs_functions.py @@ -9,11 +9,11 @@ from flowsom.tl import get_markers -def aggregate_flowframes(filenames, c_total, channels=None, keep_order=False): +def aggregate_flowframes(files, c_total, channels=None, keep_order=False): """Aggregate multiple FCS files together. - :param filenames: An array containing full paths to the FCS files - :type filenames: np.array + :param files: An array/list containing full paths to the FCS files or fcs files as anndata objects + :type files: np.array :param c_total: Total number of cells to write to the output file :type c_total: int :param channels: Channels/markers to keep in the aggregate. Default None @@ -26,12 +26,14 @@ def aggregate_flowframes(filenames, c_total, channels=None, keep_order=False): new file. Default = False. :type silent: boolean. """ - nFiles = len(filenames) + nFiles = len(files) cFile = int(np.ceil(c_total / nFiles)) flow_frame = [] - for i, file_path in enumerate(filenames): - f = read_FCS(file_path) + for i, f in enumerate(files): + if not isinstance(f, ad.AnnData): + f = read_FCS(f) + if channels is not None: f = f[:, list(get_markers(f, channels).keys())] cPerFile = min([f.X.shape[0], cFile])