Skip to content

Commit 20a5d89

Browse files
committed
reduce the mem of latent_to_gene.py
1 parent c371023 commit 20a5d89

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/GPS/find_latent_representation.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,30 @@ def run_find_latent_representation(args:FindLatentRepresentationsConfig):
177177

178178
else:
179179
args = parser.parse_args()
180-
config=FindLatentRepresentationsConfig(**vars(args))
180+
config=FindLatentRepresentationsConfig(**{'annotation': 'SubClass',
181+
'convergence_threshold': 0.0001,
182+
'epochs': 300,
183+
'feat_cell': 3000,
184+
'feat_hidden1': 256,
185+
'feat_hidden2': 128,
186+
'gcn_decay': 0.01,
187+
'gcn_hidden1': 64,
188+
'gcn_hidden2': 30,
189+
'gcn_lr': 0.001,
190+
'hierarchically': False,
191+
'input_hdf5_path': '/storage/yangjianLab/songliyang/SpatialData/Data/Brain/macaque/Cell/processed/h5ad/T862_macaque3.h5ad',
192+
'label_w': 1.0,
193+
'n_comps': 300,
194+
'n_neighbors': 11,
195+
'nheads': 3,
196+
'output_hdf5_path': 'T862_macaque3/find_latent_representations/T862_macaque3_add_latent.h5ad',
197+
'p_drop': 0.1,
198+
'rec_w': 1.0,
199+
'sample_name': 'T862_macaque3',
200+
'type': 'SCT',
201+
'var': False,
202+
'weighted_adj': False})
203+
# config=FindLatentRepresentationsConfig(**vars(args))
181204
start_time = time.time()
182205
logger.info(f'Find latent representations for {config.sample_name}...')
183206
pprint.pprint(config)

src/GPS/latent_to_gene.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ def _compute_regional_mkscore(cell_tg, ):
115115
gene_ranks_region[gene_ranks_region <= 1] = 0
116116

117117
# Simultaneously consider the ratio of expression fractions and ranks
118-
gene_ranks_region = pd.DataFrame(gene_ranks_region * frac_region)
119-
gene_ranks_region.columns = [cell_tg]
118+
gene_ranks_region = (gene_ranks_region * frac_region).values
120119

121120
mkscore = np.exp(gene_ranks_region ** 2) - 1
122-
return mkscore
121+
return mkscore.astype(np.float16, copy=False)
123122

124123

125124
def run_latent_to_gene(config: LatentToGeneConfig):
@@ -193,8 +192,7 @@ def run_latent_to_gene(config: LatentToGeneConfig):
193192
desc="Finding markers (Rank-based approach) | cells")
194193
]
195194
# Normalize the marker scores
196-
mk_score = pd.concat(mk_score, axis=1)
197-
mk_score.index = adata.var_names
195+
mk_score=pd.DataFrame(np.vstack(mk_score).T, index=adata.var.index,columns=cell_list)
198196
# mk_score_normalized = mk_score.div(mk_score.sum())*1e+2
199197
# Remove the mitochondrial genes
200198
mt_genes = [gene for gene in mk_score.index if gene.startswith('MT-') or gene.startswith('mt-')]
@@ -248,8 +246,8 @@ def run_latent_to_gene(config: LatentToGeneConfig):
248246
)
249247
else:
250248
args = parser.parse_args()
249+
config = LatentToGeneConfig(**vars(args))
251250
logger.info(f'Latent to gene for {args.sample_name}...')
252-
config = LatentToGeneConfig(**vars(args))
253251
pprint.pprint(config)
254252
start_time = time.time()
255253
run_latent_to_gene(config)

test/GPS-snakemake-workflow-macaque.smk

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ for file in Path(root).glob('*.h5ad'):
2323

2424
annotation = "SubClass"
2525
data_type = "SCT"
26-
# sample_names = ['T135_macaque1']
26+
# sample_names = ['T584_macaque2']
2727
num_processes = 20
2828

2929
rule all:
@@ -38,7 +38,7 @@ rule test_run:
3838
[f'{sample_name}/generate_ldscore/{sample_name}_generate_ldscore_chr{chrom}.done' for sample_name in
3939
sample_names]
4040

41-
localrules: find_latent_representations,latent_to_gene
41+
# localrules: find_latent_representations,latent_to_gene
4242

4343
rule find_latent_representations:
4444
input:
@@ -67,8 +67,11 @@ rule find_latent_representations:
6767
convergence_threshold=1e-4,
6868
hierarchically=False
6969
threads:
70-
1
70+
2
7171
benchmark: '{sample_name}/find_latent_representations/{sample_name}_add_latent.h5ad.benchmark'
72+
resources:
73+
mem_mb_per_cpu=lambda wildcards, threads, attempt: 20_000 * np.log2(attempt + 1),
74+
qos='huge'
7275
run:
7376
command = f"""
7477
GPS run_find_latent_representations \
@@ -119,10 +122,9 @@ rule latent_to_gene:
119122
annotation=annotation,
120123
type=data_type
121124
threads:
122-
3
125+
1
123126
resources:
124-
mem_mb=80_000,
125-
mem_mb_per_cpu=lambda wildcards, threads, attempt: 80_000 * np.log2(attempt + 1),
127+
mem_mb_per_cpu=lambda wildcards, threads, attempt: 70_000 * np.log2(attempt + 1),
126128
qos='huge'
127129
benchmark: '{sample_name}/latent_to_gene/{sample_name}_gene_marker_score.feather.benchmark'
128130
run:
@@ -166,7 +168,7 @@ rule generate_ldscore:
166168
threads:
167169
3
168170
resources:
169-
mem_mb_per_cpu=lambda wildcards, threads, attempt: 30_000 / threads * np.log2(attempt + 1),
171+
mem_mb_per_cpu=lambda wildcards, threads, attempt: 45_000 / threads * np.log2(attempt + 1),
170172
qos='huge'
171173
shell:
172174
"""
@@ -202,7 +204,7 @@ rule spatial_ldsc:
202204
sumstats_config_file='/storage/yangjianLab/chenwenhao/projects/202312_GPS/src/GPS/example/sumstats_config.yaml',
203205
all_chunk = 20
204206
threads:
205-
3
207+
2
206208
benchmark:
207209
'{sample_name}/spatial_ldsc/{sample_name}.spatial_ldsc.done.benchmark'
208210
resources:

0 commit comments

Comments
 (0)