Skip to content

Commit

Permalink
add datamodule for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mvinyard committed Jun 22, 2023
1 parent c9ef930 commit 38eebaa
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 9 deletions.
14 changes: 9 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__module_name__ = "setup.py"
__doc__ = """PYPI package distribution setup."""
__author__ = ", ".join(["Michael E. Vinyard"])
__email__ = ", ".join(["[email protected]"])
__email__ = ", ".join(["[email protected]"])


# -- import packages: ----------------------------------------------------------
Expand All @@ -15,10 +15,10 @@
# -- run setup: ----------------------------------------------------------------
setuptools.setup(
name="torch-adata",
version="0.0.23",
version="0.0.24rc0",
python_requires=">3.9.0",
author="Michael E. Vinyard",
author_email="mvinyard@g.harvard.edu",
author_email="mvinyard[email protected]",
url=None,
long_description=open("README.md", encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand All @@ -28,11 +28,15 @@
"anndata>=0.9.1",
"licorice_font>=0.0.3",
"lightning>=2.0.1",
"torch>=2.0",
"torch>=2.0",
"numpy==1.24",
"scanpy==1.9.3",
"scikit-learn==1.2.2",
"webfiles",
],
classifiers=[
"Development Status :: 2 - Pre-Alpha",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.9",
"Intended Audience :: Science/Research",
"Topic :: Scientific/Engineering :: Bio-Informatics",
],
Expand Down
5 changes: 4 additions & 1 deletion torch_adata/_demo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@

from ._plot_umap import PlotUMAP
from ._plot_umap import PlotUMAP
from ._pbmc_3k import PBMC3k
from ._larry import LARRY
from ._demo_data import DemoData
26 changes: 26 additions & 0 deletions torch_adata/_demo/_demo_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


from .. import _utils as utils
from ._pbmc_3k import PBMC3k
from ._larry import LARRY


class DemoData(utils.ABCParse):
def __init__(self):
""""""

@property
def PBMC3k(self):
if not hasattr(self, "_pbmcs"):
self._pbmcs = PBMC3k()
else:
self._pbmcs.plot()
return self._pbmcs.adata

@property
def LARRY(self):
if not hasattr(self, "_larry"):
self._larry = LARRY()
else:
self._larry.plot()
return self._larry.adata
56 changes: 56 additions & 0 deletions torch_adata/_demo/_larry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

import vinplots
import anndata
import os
import web_files

from .. import _utils as utils
from ._plot_umap import PlotUMAP

class LARRY(utils.ABCParse):
_HTTP_ADDRESS = "https://figshare.com/ndownloader/files/38171943"

def __init__(self, filename="adata.LARRY.h5ad", data_dir="data", silent=False):
self._config(locals())

def _config(self, kwargs):
""""""

self.__parse__(kwargs, public=[None])

self._INFO = utils.InfoMessage()

if not os.path.exists(self._data_dir):
os.mkdir(self._data_dir)

def _download(self):
msg = "Downloading {:.<10}...".format(self._filename)
self._INFO(msg, end=" ")
f = web_files.WebFile(
http_address=self._HTTP_ADDRESS,
local_path=self.local_path,
)
f.download()
print("Done.")

@property
def local_path(self):
return os.path.join(self._data_dir, self._filename)

@property
def adata(self):
if not hasattr(self, "_adata"):
if not os.path.exists(self.local_path):
self._download()
self._adata = anndata.read_h5ad(self.local_path)
self._adata.uns["cmap"] = vinplots.colors.LARRY_in_vitro
if not self._silent:
print(self._adata)
self.plot()
return self._adata

def plot(self):
umap_plot = PlotUMAP(
self._adata, title="LARRY hematopoiesis"
)
umap_plot(groupby="Cell type annotation")
39 changes: 39 additions & 0 deletions torch_adata/_demo/_pbmc_3k.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

# -- import packages: --------------------------------------------------------
import scanpy as sc
import pandas as pd
import vinplots


from .. import _utils as utils
from ._plot_umap import PlotUMAP

class PBMC3k(utils.ABCParse):
def __init__(self, silent=False):
self.__parse__(locals(), public=[None])

def _configure(self):
_adata = sc.datasets.pbmc3k_processed()
cell_type_df = pd.DataFrame(_adata.obs["louvain"].unique()).reset_index()
cell_type_df.columns = ["cell_type_idx", "louvain"]
df_obs = _adata.obs.merge(cell_type_df, on="louvain", how="left")
df_obs.index = df_obs.index.astype(str)
_adata.obs = df_obs
_adata.uns["cmap"] = vinplots.colors.pbmc3k
return _adata

@property
def adata(self):
if not hasattr(self, "_adata"):
self._adata = self._configure()
if not self._silent:
print(self._adata)
self.plot()
return self._adata

def plot(self):
umap_plot = PlotUMAP(
self._adata, title="10x PBMCs (~3k cells)"
)
umap_plot(groupby="louvain")

8 changes: 7 additions & 1 deletion torch_adata/_demo/_plot_umap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ def CMAP(self):
return self.adata.uns[self._cmap_key]

def _plot_grouped(self):
for group, group_df in self.adata.obs.groupby(self.groupby):
for en, (group, group_df) in enumerate(self.adata.obs.groupby(self.groupby)):
if group in self.plot_behind:
z = 0
else:
z = en + 1
xu = self.adata[group_df.index].obsm[self._use_key]
self.axes[0].scatter(
xu[:, 0],
xu[:, 1],
label=group,
color=self.CMAP[group],
alpha=self._alpha,
zorder = z,
)

def _format(self):
Expand All @@ -51,6 +56,7 @@ def _format(self):
def __call__(
self,
groupby=None,
plot_behind = ["Undifferentiated"],
):
self.__update__(locals())

Expand Down
4 changes: 2 additions & 2 deletions torch_adata/_utils/_info_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self, INFO="INFO", color="BLUE"):
def _format_msg_title(self, INFO):
return f" - [{INFO}] | "

def __call__(self, msg):
def __call__(self, msg, end = "\n"):

self.BASE = self._format_msg_title(self.INFO)
print(self.BASE + msg)
print(self.BASE + msg, end = end)

0 comments on commit 38eebaa

Please sign in to comment.