Skip to content

Commit

Permalink
Add notation of the output
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiyu committed Apr 22, 2024
1 parent 53ab8a1 commit ad4145e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
15 changes: 14 additions & 1 deletion R/Gibbs_sampler.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ SpaTopic_inference<-function(tissue, ntopics, sigma = 50, region_radius = 400, k
}

### release memory for large items
celltype<-levels(itr_df$type)
cellname<-rownames(itr_df)
rm(list= c("itr_df"))
gc()

Expand All @@ -371,7 +373,8 @@ SpaTopic_inference<-function(tissue, ntopics, sigma = 50, region_radius = 400, k
if(ninit > 1){ ## need update only when several initialization

## number of regions
M<-max(D)+1 ## M might be changed since D changed
M<-max(D)+1 ## M might be changed since D changed
# A potential bug (what if the max(D) has been changed after gibbs sampling)

Ndk <- table_2d_fast(D, Z, M, K) ## number of cells per topic per region (image specific)
Nwk <- table_2d_fast(C, Z, V, K) ## number of cells per topic per celltype
Expand Down Expand Up @@ -408,6 +411,16 @@ SpaTopic_inference<-function(tissue, ntopics, sigma = 50, region_radius = 400, k
message("Output model perplexity:")
message(gibbs.res$Perplexity)

## Beta: Topic Content
gibbs.res$Beta<-as.data.frame(gibbs.res$Beta)
colnames(gibbs.res$Beta)<-paste0("topic",1:K)
rownames(gibbs.res$Beta)<-celltype

## Z.trace: posterior prob for every single cell
gibbs.res$Z.trace<-as.data.frame(gibbs.res$Z.trace/niter)
colnames(gibbs.res$Z.trace)<-paste0("topic",1:K)
rownames(gibbs.res$Z.trace)<-cellname

return(gibbs.res)
}

32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,29 @@ It is stored in a Seurat v5 object.

``` r
str(gibbs.res)
#List of 8
# $ Perplexity : num 11.3
# $ Deviance : num 485960
# $ loglikelihood: num -242980
# $ Beta : num [1:38, 1:7] 0.03587 0.02539 0.00755 0.01858 0.02585 ...
# $ Theta : num [1:971, 1:7] 0.855601 0.000232 0.999269 0.99889 0.998725 ...
# $ Ndk : int [1:971, 1:7] 107 0 82 54 47 72 100 0 0 0 ...
# $ Nwk : int [1:38, 1:7] 390 276 82 202 281 505 697 522 29 58 ...
# $ Z.trace : int [1:100149, 1:7] 13 173 27 164 157 4 20 21 15 19 ...
# List of 8
# $ Perplexity : num 11.3
# $ Deviance : num 485960
# $ loglikelihood: num -242980
# $ Beta :'data.frame': 38 obs. of 7 variables:
# ..$ topic1: num [1:38] 0.03587 0.02539 0.00755 0.01858 0.02585 ...
# ..$ topic2: num [1:38] 6.51e-03 3.55e-02 2.62e-06 5.80e-04 7.75e-01 ...
# ..$ topic3: num [1:38] 4.54e-06 4.54e-06 9.13e-04 3.45e-01 1.73e-03 ...
# ..$ topic4: num [1:38] 0.02664 0.01743 0.00186 0.0152 0.08919 ...
# ..$ topic5: num [1:38] 2.99e-06 2.99e-06 5.32e-03 1.91e-02 4.90e-03 ...
# ..$ topic6: num [1:38] 6.35e-06 6.35e-06 2.04e-02 3.43e-03 6.35e-06 ...
# ..$ topic7: num [1:38] 0.00534 0.00699 0.00604 0.01843 0.00655 ...
# $ Theta : num [1:971, 1:7] 0.855601 0.000232 0.999269 0.99889 0.998725 ...
# $ Ndk : int [1:971, 1:7] 107 0 82 54 47 72 100 0 0 0 ...
# $ Nwk : int [1:38, 1:7] 390 276 82 202 281 505 697 522 29 58 ...
# $ Z.trace :'data.frame': 100149 obs. of 7 variables:
# ..$ topic1: num [1:100149] 0.065 0.865 0.135 0.82 0.785 0.02 0.1 0.105 0.075 0.095 ...
# ..$ topic2: num [1:100149] 0 0 0 0 0 0 0 0 0 0 ...
# ..$ topic3: num [1:100149] 0.275 0.005 0.21 0.005 0.005 0.77 0.02 0.015 0.085 0.075 ...
# ..$ topic4: num [1:100149] 0.415 0 0 0.01 0.005 0.1 0.665 0.62 0.015 0.025 ...
# ..$ topic5: num [1:100149] 0.005 0.01 0 0 0 0 0.005 0.005 0.005 0 ...
# ..$ topic6: num [1:100149] 0 0 0.655 0.165 0.205 0.005 0 0 0 0 ...
# ..$ topic7: num [1:100149] 0.24 0.12 0 0 0 0.105 0.21 0.255 0.82 0.805 ...
```

## Citation
Expand Down

0 comments on commit ad4145e

Please sign in to comment.