简体   繁体   English

使用并行计算在 R 中保存地块

[英]Saving plots in R using parallel computing

rm(list=ls())
    analysis <- function(time_series,sublist){
    ROIs <- c("RW","RFHM","RFD2","RFD3","RFD4")
    data <- time_series[[sublist]]
    for (subject in 1:length(data$successful_x_direction)){
        subject_name <- paste0("subject_",subject)
        success_x <- data$successful_x_direction[[subject]]
        success_y <- data$successful_y_direction[[subject]]
        fail_x <- data$failure_x_direction[[subject]]
        fail_y <- data$failure_y_direction[[subject]]
        for (success in 1:length(success_x)){
            matrix_x <- success_x[[success]]
            matrix_y <- success_y[[success]]
            matrix_x <- as.data.frame(matrix_x)
            matrix_x$Frame <- seq(1,dim(matrix_x)[1])
            matrix_x <- matrix_x %>% pivot_longer(!Frame,names_to = "ROI",values_to = "Value")
            x_cor <- ggplot(matrix_x,aes(x = Frame, y = Value, colour = ROI)) + geom_line() + ylab("X Coordinates")
            matrix_y <- as.data.frame(matrix_y)
            matrix_y$Frame <- seq(1,dim(matrix_y)[1])
            matrix_y <- matrix_y %>% pivot_longer(!Frame,names_to = "ROI",values_to = "Value")
            y_cor <- ggplot(matrix_y,aes(x = Frame, y = Value, colour = ROI)) + geom_line() +  ylab("Y Coordinates")
            g <- ggarrange(x_cor,y_cor,ncol = 1, nrow = 2)
            g <- annotate_figure(g, top = text_grob("Success", color = "red", face = "bold", size = 14))
           filename = paste0("Success_",success,".png")
           filename = paste("Time Series Plots",sublist,subject_name,filename,sep = "\\")
           ggsave(plot = g, filename = filename)
    }
}

Hello, I have this code which basically saves some plots.你好,我有这段代码,基本上可以保存一些情节。 Using a traditional for loop works fine:使用传统的 for 循环效果很好:

load("time_series.Rdata")
for (i in seq(1,length(time_series))){
    sublist <- names(time_series)[i]
    analysis(time_series,sublist)
}

but when I try to run it in parallel it doesn't work.但是当我尝试并行运行它时它不起作用。 I wait for several minutes and nothing is being saved and my CPU is not being used.我等了几分钟,没有任何东西被保存,我的 CPU 也没有被使用。 What am I doing wrong?我究竟做错了什么?

library(doParallel)
library(foreach)
numCores <- detectCores()-1
cl <- makeCluster(numCores)
registerDoParallel(cl)
random_name <- foreach(i=seq(1,length(time_series))) %dopar% {
    sublist <- names(time_series)[i]
    analysis(time_series,sublist)
}
stopCluster(cl)

you should provide a reprex if there is something peculiar about your data;如果您的数据有一些特殊之处,您应该提供一个代表; without a reprex I can only try to make something simple with similar structure to show in principle if it can work.没有代表我只能尝试用类似的结构做一些简单的事情来原则上展示它是否可行。 I use tictoc for timing;我使用 tictoc 进行计时; on my machine normal runtime is 2seconds;在我的机器上,正常运行时间是 2 秒; parallel is slower 2.7seconds. parallel 慢 2.7 秒。

library(tidyverse)
library(tictoc)

data_to_use <- list(a1=iris,
                    b2=mutate(iris,across(where(is.numeric),~.x*.x)))

analysis <- function(df,nm){
   require(ggplot2)
   require(dplyr)
  for(i in unique(df$Species)){
    g <- ggplot(data=df |> filter(Species==i),
                aes(x=Petal.Length,
                    y=Petal.Width))+geom_point()
    ggsave(plot = g, 
           filename = paste0(nm,"_",i,".png"))
  }
}

tic()
analysis(df=data_to_use[[1]],names(data_to_use)[[1]])
analysis(df=data_to_use[[2]],names(data_to_use)[[2]])
toc()


library(doParallel)
library(foreach)
numCores <- detectCores()-1
cl <- makeCluster(numCores)
registerDoParallel(cl)

tic()
foreach(i=1:2,.packages = "tictoc",) %dopar% {

  analysis(data_to_use[[i]],
           names(data_to_use)[[i]])

}
toc()
stopCluster(cl)

It seems that my script was working fine.看来我的脚本运行良好。 I just had to wait more than expected for the plots to be saved.我只需要等待比预期更长的时间才能保存地块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM