Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update classify.py #103

Merged
merged 5 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 0 additions & 186 deletions scimap/helpers/_classify.py

This file was deleted.

43 changes: 27 additions & 16 deletions scimap/helpers/classify.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def classify (adata,
Key under which classification results are stored in `adata.obs`.

showPhenotypeLabel (bool, optional):
If True, appends classification status to existing phenotype labels in the results.
If True, appends classification status to existing phenotype labels in the results. If True, classification
results will instead be stored under "[phenotype]_[label]" key in `adata.obs`

verbose (bool, optional):
If True, prints progress and informational messages during the classification process.
Expand All @@ -90,16 +91,19 @@ def classify (adata,

```
"""

# clean the input
if isinstance(pos, str):
pos = [pos]
if isinstance(neg, str):
neg = [neg]
if isinstance(subclassify_phenotype, str):
subclassify_phenotype = [subclassify_phenotype]
if (showPhenotypeLabel):
phenotype_label=phenotype+"_"+label
if phenotype is not None:
if isinstance(subclassify_phenotype, str):
subclassify_phenotype = [subclassify_phenotype]
if (showPhenotypeLabel):
phenotype_label=phenotype+"_"+label
elif phenotype is None:
if isinstance(subclassify_phenotype, str) or (showPhenotypeLabel):
raise TypeError("You must pass a column name to the PHENOTYPE argument in order to use `subclassify_phenotype` or to set `showPhenotypeLabel = True`")


# Create a dataFrame with the necessary inforamtion
Expand All @@ -126,21 +130,28 @@ def classify (adata,
raise TypeError("No cells were found to satisfy your `classify` criteria")
else:
# create new naming scheme for label and phenotype_label cols in classified
non_summary = pd.DataFrame({phenotype: adata.obs[phenotype]}) # gets the index and phenotype
non_summary[phenotype] = non_summary[phenotype].astype(str)

classify_idx=data.index
classified = pd.DataFrame(non_summary.loc[data.index]) #subsets phenotype rows to only classified cells
if showPhenotypeLabel:
if showPhenotypeLabel is True:
non_summary = pd.DataFrame({phenotype: adata.obs[phenotype]}) # gets the index and phenotype
non_summary[phenotype] = non_summary[phenotype].astype(str)

classified = pd.DataFrame(non_summary.loc[data.index]) #subsets phenotype rows to only classified cells

classified[phenotype_label] = classified[phenotype]+"_"+classify_label # add phenotype_label col
classified[label]=pd.DataFrame(np.repeat(classify_label, len(classify_idx)), index = classify_idx) # add label col
classified.drop([phenotype], axis='columns', inplace=True) # drop phenotype col, for merge
classified.drop([phenotype], axis='columns', inplace=True) # drop phenotype col, for merge
else:
classified=pd.DataFrame(np.repeat(classify_label, len(classify_idx)),index= classify_idx, columns=[label]) # add label col



if collapse_failed is True:
meta = non_summary # has index and phenotype col
if showPhenotypeLabel is True:
meta = non_summary # has index and phenotype col
else:
meta = pd.DataFrame(index= adata.obs.index)

meta = meta.merge(classified, how='outer', left_index=True, right_index=True) # gain classified col(s) and NaNs for non-matches

if showPhenotypeLabel is True:
meta[phenotype_label]= meta[phenotype_label].fillna(meta[phenotype].astype(str)+"_"+failed_label)
meta=meta[phenotype_label]
Expand All @@ -159,7 +170,9 @@ def classify (adata,
meta.update(classified) # updates with phenotype_label for only the classified cells
else:
meta= pd.DataFrame(adata.obs[phenotype])
meta = meta[phenotype].astype("object")
classified = pd.DataFrame(np.repeat(classify_label, len(classify_idx)), index = classify_idx, columns = [phenotype])
classified = classified[phenotype].astype("object")
meta.update(classified) # updates with label for only the classified cells


Expand All @@ -169,7 +182,5 @@ def classify (adata,
adata.obs[phenotype_label]=meta
else:
adata.obs[label]=meta

# return
return adata

Loading