Skip to content

Commit

Permalink
Fixed warnings, added read_csv, fix installation issue with windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Artuur Couckuyt committed Dec 9, 2023
1 parent 581f5a3 commit dd7f8c2
Show file tree
Hide file tree
Showing 11 changed files with 19,488 additions and 173 deletions.
381 changes: 218 additions & 163 deletions docs/notebooks/example.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/FlowSOM/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

__all__ = ["FlowSOM", "flowsom_clustering", "io", "pl", "pp", "tl"]

__version__ = version("FlowSOM")
__version__ = version("flowsom")
4 changes: 2 additions & 2 deletions src/FlowSOM/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .read_fcs import read_FCS
from .read_fcs import read_FCS, read_csv

__all__ = ["read_FCS"]
__all__ = ["read_FCS", "read_csv"]
12 changes: 12 additions & 0 deletions src/FlowSOM/io/read_fcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re

import pytometry as pm
import anndata as ad
import pandas as pd


def read_FCS(filepath):
Expand Down Expand Up @@ -32,3 +34,13 @@ def read_FCS(filepath):
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


def read_csv(filepath, spillover=None, **kwargs):
ff = ad.read_csv(filepath, **kwargs)
ff.var = pd.DataFrame(
{"n": range(ff.shape[1]), "channel": ff.var_names, "marker": ff.var_names}, index=ff.var.index
)
if spillover is not None:
ff.uns["meta"]["SPILL"] = pd.read_csv(spillover)
return ff
7 changes: 5 additions & 2 deletions src/FlowSOM/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from scipy.stats import median_abs_deviation
from sklearn.cluster import AgglomerativeClustering

from .io import read_FCS
from .io import read_FCS, read_csv
from .tl import SOM, ConsensusCluster, get_channels, get_markers, map_data_to_codes


Expand Down Expand Up @@ -48,7 +48,10 @@ def read_input(self, inp):
if isinstance(inp, ad.AnnData):
data = inp.X
elif isinstance(inp, str):
inp = read_FCS(inp)
if inp.endswith(".csv"):
inp = read_csv(inp)
elif inp.endswith(".fcs"):
inp = read_FCS(inp)
data = inp.X
channels = np.asarray(inp.var["channel"])
markers = np.asarray(inp.var["marker"])
Expand Down
20 changes: 16 additions & 4 deletions src/FlowSOM/pl/_plot_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def plot_FlowSOM(
# Add background
if background_values is not None:
if equal_background_size:
background_size = np.repeat(max_node_size * background_size, len(background_values))
background_size = np.repeat(np.max(node_sizes) * background_size, len(background_values))
else:
background_size = (
parse_node_sizes(
Expand Down Expand Up @@ -217,8 +217,10 @@ def plot_star_legend(fig, ax, markers, coords=(0, 0), cmap=FlowSOM_colors(), max
n_left_right = pd.crosstab(n_left_right, columns="x")
if n_left_right.shape[0] != 1:
by = 1 if len(markers) <= 8 else 0.65
left = np.linspace(start=0, stop=(n_left_right.x[0] - 1) * by, num=n_left_right.x[0])
right = np.multiply(-1, np.linspace(start=0, stop=(n_left_right.x[1] - 1) * by, num=n_left_right.x[1]))
left = np.linspace(start=0, stop=(n_left_right.x.iloc[0] - 1) * by, num=n_left_right.x.iloc[0])
right = np.multiply(
-1, np.linspace(start=0, stop=(n_left_right.x.iloc[1] - 1) * by, num=n_left_right.x.iloc[1])
)
segments_left = segments[segments[:, 1] < 0, :]
segments_left = segments_left[segments_left[:, 2].argsort()]
segments_right = segments[segments[:, 1] >= 0]
Expand Down Expand Up @@ -266,6 +268,8 @@ def plot_star_legend(fig, ax, markers, coords=(0, 0), cmap=FlowSOM_colors(), max


def scale_star_heights(median_values, node_sizes):
if isinstance(node_sizes, pd.Series):
node_sizes = node_sizes.to_numpy()
max_all_nodes = median_values[~np.isnan(median_values)].max()
min_all_nodes = median_values[~np.isnan(median_values)].min()
scaled_row = [
Expand Down Expand Up @@ -297,7 +301,9 @@ def auto_max_node_size(layout, overlap):
return min_distance / 2 * overlap


def add_text(ax, layout, text, text_size=20, text_color="black", ha=None, va=None):
def add_text(ax, layout, text, text_size=25, text_color="black", ha=None, va=None):
if isinstance(text, pd.Series):
text = text.to_numpy()
if va is None:
va = ["center"]
if ha is None:
Expand Down Expand Up @@ -327,16 +333,22 @@ def parse_edges(fsom):


def add_nodes(layout, heights):
if isinstance(heights, pd.Series):
heights = heights.to_numpy()
patches = [Circle((row[0], row[1]), heights[i]) for i, row in enumerate(layout)]
return patches


def add_stars(layout, heights):
if isinstance(heights, pd.Series):
heights = heights.to_numpy()
patches = np.hstack([add_wedges((row[0], row[1]), heights[i, :]) for i, row in enumerate(layout)])
return patches


def add_wedges(coord, heights, angles=None):
if isinstance(heights, pd.Series):
heights = heights.to_numpy()
if angles is None:
part = 360 / len(heights)
angles = np.arange(0, 360.01, part)
Expand Down
2 changes: 1 addition & 1 deletion src/FlowSOM/pl/plot_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def plot_pies(
if 0 not in angles:
angles = np.insert(angles, 0, 0)
row = layout[cl, :]
patches = add_wedges(tuple(row), heights=np.repeat(scaled_node_size[cl], len(angles)), angles=angles)
patches = add_wedges(tuple(row), heights=np.repeat(scaled_node_size.iloc[cl], len(angles)), angles=angles)
p = mc.PatchCollection(patches)
p.set_facecolor([color_dict.get(key) for key in table.index.values])
p.set_edgecolor("black")
Expand Down
Loading

0 comments on commit dd7f8c2

Please sign in to comment.