|
21 | 21 | from tqdm import tqdm
|
22 | 22 |
|
23 | 23 |
|
24 |
| -def get_data_custom(interpreter, n_eval_nodes_per_graph: int=10): |
| 24 | +def get_data_custom(interpreter, deconvolution: bool = False, n_eval_nodes_per_graph: int=10): |
25 | 25 | interpreter.undefined_node_types = None
|
26 | 26 | interpreter.img_to_patient_dict = interpreter.data.celldata.uns["img_to_patient_dict"]
|
27 | 27 | interpreter.complete_img_keys = list(interpreter.data.img_celldata.keys())
|
@@ -64,8 +64,10 @@ def get_data_custom(interpreter, n_eval_nodes_per_graph: int=10):
|
64 | 64 | interpreter.split_data_node(0.1, 0.1)
|
65 | 65 | interpreter.n_eval_nodes_per_graph = n_eval_nodes_per_graph
|
66 | 66 | interpreter.cell_names = list(interpreter.data.celldata.uns['node_type_names'].values())
|
| 67 | + if deconvolution: |
| 68 | + interpreter.proportions = {k: adata.obsm["proportions"] for k, adata in interpreter.data.img_celldata.items()} |
| 69 | + |
67 | 70 |
|
68 |
| - |
69 | 71 | class GraphTools:
|
70 | 72 | """GraphTools class."""
|
71 | 73 |
|
@@ -1921,6 +1923,109 @@ def _register_graph_features(self, label_selection):
|
1921 | 1923 | }
|
1922 | 1924 | self.celldata.uns["graph_covariates"] = graph_covariates
|
1923 | 1925 |
|
| 1926 | + |
| 1927 | +class customLoaderDeconvolution(DataLoader): |
| 1928 | + |
| 1929 | + def __init__( |
| 1930 | + self, |
| 1931 | + adata, |
| 1932 | + patient, |
| 1933 | + library_id, |
| 1934 | + radius, |
| 1935 | + coord_type='generic', |
| 1936 | + n_rings=1, |
| 1937 | + n_top_genes=None, |
| 1938 | + label_selection=None |
| 1939 | + ): |
| 1940 | + self.adata = adata.copy() |
| 1941 | + self.patient = patient |
| 1942 | + self.library_id = library_id |
| 1943 | + |
| 1944 | + print("Loading data from raw files") |
| 1945 | + self.register_celldata(n_top_genes=n_top_genes) |
| 1946 | + self.register_img_celldata() |
| 1947 | + self.register_graph_features(label_selection=label_selection) |
| 1948 | + self.compute_adjacency_matrices(radius=radius, coord_type=coord_type, n_rings=n_rings) |
| 1949 | + self.radius = radius |
| 1950 | + |
| 1951 | + print( |
| 1952 | + "Loaded %i images with complete data from %i patients " |
| 1953 | + "over %i cells with %i cell features and %i distinct celltypes." |
| 1954 | + % ( |
| 1955 | + len(self.img_celldata), |
| 1956 | + len(self.patients), |
| 1957 | + self.celldata.shape[0], |
| 1958 | + self.celldata.shape[1], |
| 1959 | + len(self.celldata.uns["node_type_names"]), |
| 1960 | + ) |
| 1961 | + ) |
| 1962 | + |
| 1963 | + def _register_celldata(self, n_top_genes): |
| 1964 | + |
| 1965 | + metadata = { |
| 1966 | + #"cluster_col_preprocessed": self.cluster, |
| 1967 | + "image_col": self.library_id |
| 1968 | + } |
| 1969 | + |
| 1970 | + celldata = self.adata.copy() |
| 1971 | + celldata.uns["metadata"] = metadata |
| 1972 | + |
| 1973 | + if self.patient: |
| 1974 | + img_to_patient_dict = {} |
| 1975 | + for p in np.unique(celldata.obs[self.patient]): |
| 1976 | + for i in np.unique(celldata.obs[celldata.obs[self.patient] == p][self.library_id]): |
| 1977 | + img_to_patient_dict[i] = p |
| 1978 | + else: |
| 1979 | + if self.library_id: |
| 1980 | + img_to_patient_dict = {} |
| 1981 | + for img in np.unique(celldata.obs[self.library_id]): |
| 1982 | + img_to_patient_dict[str(img)] = "patient" |
| 1983 | + else: |
| 1984 | + img_to_patient_dict = {"image": "patient"} |
| 1985 | + celldata.uns["img_to_patient_dict"] = img_to_patient_dict |
| 1986 | + self.img_to_patient_dict = img_to_patient_dict |
| 1987 | + |
| 1988 | + self.celldata = celldata |
| 1989 | + |
| 1990 | + def _register_img_celldata(self): |
| 1991 | + """Load dictionary of of image-wise celldata objects with {imgage key : anndata object of image}.""" |
| 1992 | + img_celldata = {} |
| 1993 | + if self.library_id: |
| 1994 | + for k in np.unique(self.celldata.obs[self.library_id]): |
| 1995 | + img_celldata[str(k)] = self.celldata[self.celldata.obs[self.library_id] == k].copy() |
| 1996 | + self.img_celldata = img_celldata |
| 1997 | + else: |
| 1998 | + self.img_celldata = {"image": self.celldata} |
| 1999 | + |
| 2000 | + def _register_graph_features(self, label_selection): |
| 2001 | + """Load graph level covariates. |
| 2002 | +
|
| 2003 | + Parameters |
| 2004 | + ---------- |
| 2005 | + label_selection |
| 2006 | + Label selection. |
| 2007 | + """ |
| 2008 | + # Save processed data to attributes. |
| 2009 | + for adata in self.img_celldata.values(): |
| 2010 | + graph_covariates = { |
| 2011 | + "label_names": {}, |
| 2012 | + "label_tensors": {}, |
| 2013 | + "label_selection": [], |
| 2014 | + "continuous_mean": {}, |
| 2015 | + "continuous_std": {}, |
| 2016 | + "label_data_types": {}, |
| 2017 | + } |
| 2018 | + adata.uns["graph_covariates"] = graph_covariates |
| 2019 | + |
| 2020 | + graph_covariates = { |
| 2021 | + "label_names": {}, |
| 2022 | + "label_selection": [], |
| 2023 | + "continuous_mean": {}, |
| 2024 | + "continuous_std": {}, |
| 2025 | + "label_data_types": {}, |
| 2026 | + } |
| 2027 | + self.celldata.uns["graph_covariates"] = graph_covariates |
| 2028 | + |
1924 | 2029 |
|
1925 | 2030 | class DataLoaderZhang(DataLoader):
|
1926 | 2031 | """DataLoaderZhang class. Inherits all functions from DataLoader."""
|
|
0 commit comments