R – translating Python class to R’s R6 class – error

I saw a similar post on here about this error but my code is a bit different. I’m converting a python class from a tool called ‘graphST’ (https://github.com/JinmiaoChenLab/GraphST/blob/d62b0b7b6cd38ee285f3ac8cd67b7341a10bcc74/GraphST/GraphST.py).

Now mine (https://github.com/myanna1416/GraphSTR_WORK/blob/main/ZorFirstRproject.R)

The error I am getting is:

Error in R6::R6Class("GraphST", public = list(adata = NULL, features = NULL,  : 
  All items in public, private, and active must have unique names. 

This is the R6 class I created that is throwing the error

GraphST <- R6::R6Class(
  "GraphST",
  public = list(
    adata = NULL,
    features = NULL,
    features_a = NULL,
    label_CSL = NULL,
    adj = NULL,
    graph_neigh = NULL,
    dim_input = NULL,
    dim_output = NULL,
    feat_sc = NULL,
    feat_sp = NULL,
    n_cell = NULL,
    n_spot = NULL,
    dim_input_sc = NULL,
    dim_output_sc = NULL,
    emb_rec = NULL,
    adata_sc = NULL,
    # Adding adata_sc for scRNA-seq data
    device = "cpu",
    learning_rate = 0.001,
    learning_rate_sc = 0.01,
    weight_decay = 0.00,
    epochs = 600,
    dim_input = 3000,
    dim_output = 64,
    random_seed = 41,
    alpha = 10,
    beta = 1,
    theta = 0.1,
    lamda1 = 10,
    lamda2 = 1,
    deconvolution = FALSE,
    datatype = "10X",
    
    #constructor function
    initialize = function(adata,
                          adata_sc = NULL,
                          device = "cpu",
                          learning_rate = 0.001,
                          learning_rate_sc = 0.01,
                          weight_decay = 0.00,
                          epochs = 600,
                          random_seed = 41,
                          alpha = 10,
                          beta = 1,
                          theta = 0.1,
                          lamda1 = 10,
                          lamda2 = 1,
                          deconvolution = FALSE,
                          datatype = "10X") {
      self$adata <- adata
      self$adata_sc <- adata_sc  # Initialize adata_sc
      self$device <- device
      self$learning_rate <- learning_rate
      self$learning_rate_sc <- learning_rate_sc
      self$weight_decay <- weight_decay
      self$epochs <- epochs
      self$random_seed <- random_seed
      self$alpha <- alpha
      self$beta <- beta
      self$theta <- theta
      self$lamda1 <- lamda1
      self$lamda2 <- lamda2
      self$deconvolution <- deconvolution
      self$datatype <- datatype
      self$features_a <- NULL
      self$label_CSL <- NULL
      self$adj <- NULL
      self$graph_neigh <- NULL
      self$dim_input <- NULL
      self$dim_output <- NULL
      self$feat_sc <- NULL
      self$feat_sp <- NULL
      self$n_cell <- NULL
      self$n_spot <- NULL
      self$dim_input_sc <- NULL
      self$dim_output_sc <- NULL
      self$emb_rec <- NULL 
      
      
      
      
      set.seed(self$random_seed)  # Fix the seed for reproducibility
      
      
      
      self$adata <- preprocess(self$adata)
      
      self$adata <- construct_interaction(self$adata)
      
      
      self$adata <- add_contrastive_label(self$adata)
      
      self$adata <- get_feature(self$adata)
      
      # Convert 'feat' to a torch tensor and move to the specified device
      self$features <-
        torch_tensor(as.array(self$adata@misc$feat),
                     dtype = torch_float32())$to(device = self$device)
      # Convert 'feat_a' to a torch tensor and move to the specified device
      self$features_a <-
        torch_tensor(as.array(self$adata@misc$feat_a),
                     dtype = torch_float32())$to(device = self$device)
      # Convert 'label_CSL' to a torch tensor and move to the specified device
      self$label_CSL <-
        torch_tensor(as.array(self$adata@misc$label_CSL),
                     dtype = torch_float32())$to(device = self$device)
      # For 'adj', we simply reference it as it does not necessarily need conversion for this context
      self$adj <- self$adata@misc$adj
      n <- nrow(self$adj)
      self$graph_neigh <-
        torch_tensor(as.array(self$adata@misc$graph_neigh) + diag(rep(1, n)),
                     dtype = torch_float32())$to(device = self$device)
      self$dim_input <- dim(self$features)[2]
      self$dim_output <- dim_output
      
      self$adj <- preprocess_adj(self$adj)
      self$adj <- torch_tensor(as.array(self$adj), dtype = torch_float32())$to(device = self$device)
      
      if (self$deconvolution) {
        self$adata_sc <-
          self$adata_sc$clone(deep = TRUE)
        
        # Replace NA (equivalent to NaN in Python) with 0 for self$feat_sc
        self$feat_sc[is.na(self$feat_sc)] <- 0
        
        # Convert to a torch tensor and move to the specified device
        self$feat_sc <-
          torch_tensor(self$feat_sc, dtype = torch_float32)$to(device = self$device)
        
        # Repeat the process for self$feat_sp
        self$feat_sp[is.na(self$feat_sp)] <- 0
        self$feat_sp <-
          torch_tensor(self$feat_sp, dtype = torch_float32)$to(device = self$device)
        
        # Check if self$adata_sc exists
        if (!is.null(self$adata_sc)) {
          # Assuming self$feat_sc is a torch tensor, use dim() to get dimensions
          self$dim_input <- dim(self$feat_sc)[2]
        }
        
        # Set the number of cells and spots based on observations in adata_sc and adata
        # Assuming adata_sc and adata are lists or similar objects with n_obs (number of observations) property
        self$n_cell <- self$adata_sc$n_obs
        self$n_spot <- self$adata$n_obs
      }
    },
    
    train = function() {
      self$model <-
        Encoder$new(self$dim_input, self$dim_output, self$graph_neigh)$to(device = self$device)
      
      self$loss_CSL <- nn_bce_with_logits_loss()
      self$optimizer <-
        optim_adam(
          self$model$parameters(),
          lr = self$learning_rate,
          weight_decay = self$weight_decay
        )
      
      cat("Begin to train ST data...n")
      pb <-
        progress_bar$new(total = self$epochs, format = "  [:bar] :percent :elapsed/:est")
      
      for (epoch in 1:self$epochs) {
        self$model$train()
        
        self$features_a2 <-
          permute_features(self$features)  # Ensure this function is defined to shuffle features
        list(hidden_feat, emb, ret, ret_a) <-
          self$model(self$features, self$features_a2, self$adj)
        
        loss_sl_1 <- self$loss_CSL(ret, self$label_CSL)
        loss_sl_2 <- self$loss_CSL(ret_a, self$label_CSL)
        loss_feat <- mse_loss(self$features, emb)
        
        loss <- self$alpha * loss_feat + self$beta * (loss_sl_1 + loss_sl_2)
        
        self$optimizer$zero_grad()
        loss$backward()
        self$optimizer$step()
        
        pb$tick()
      }
      
      cat("Optimization finished for ST data!n")
      
      no_grad({
        self$model$eval()
        if (self$deconvolution) {
          self$emb_rec <-
            self$model(self$features, self$features_a, self$adj)[[2]]
          return(self$emb_rec)
        } else {
          self$emb_rec <-
            self$model(self$features, self$features_a, self$adj)[[2]]$detach()$cpu()$numpy()
          self$adata@misc[['emb']] <- self$emb_rec
          return(self$adata)
        }
      })
    }
  )
)

New contributor

Zoranay Coursey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật