How do I use reticulate within a package I am building?

Issue

I am trying to write a function for a package I am working to build an I have had t resort to using using some Python through reticulate becuase of the differences between {HDF5r} and {h5py}. Unfortunately, the way I would use

reticulate::repl_python()

python code here

exit

does not appear to be accepted by the package builder. Is there a correct way I should be doing this? Thanks for all of your help!

Bonus Questions

I am pretty new to Python and trying to cross some of my learning over as best I can.

I am also curious if there is a way I can seamlessly continue the {progressr} steps within the repl_python() section. Any Ideas?

Lastly, I have used R's {future} and {furrr} in the past to parallelize actions when dealing with data frame operations. I would like to do something similar for the for loops in my python segment but I have no idea where to begin with that. If anyone has experience to share, I would appreciate it very much!

Actual Function

#' reticulate_trainer_hdf5
#'
#' @param h5_file_path path, with new file name included, where h5 file to be created
#' @param meta_data output from form_STEAD
#' @param signal sac_EV column values from sac2STEAD
#'
#' @importFrom dplyr %>%
#'
#' @return
#' @export
#'
#' @examples

reticulate_trainer_hdf5 <- function(h5_file_path, meta_data, signal){

  progressr::with_progress({
    p <- progressr::progressor(steps = 5)
    
    h5_file_path = sprintf("%s/NMTSO_test.h5", here::here())
    p()
    Sys.sleep(.2)

    x = list()
    for (i in 1:length(signal$sac_EV)){
      x[[i]] <- signal$sac_EV[i] %>% unlist() %>% matrix(nrow = 1)
      x[[i]] <- rep(0,(2 * length(signal$sac_EV[[1]]))) %>% matrix(nrow = 2) %>% rbind(x[[i]])
    }
    p()
    Sys.sleep(.2)
    rm(i)

    col_names <- colnames(meta_data)

    info <- tidyr::expand_grid(trace_name = meta_data$trace_name, col_names) %>%
      dplyr::mutate(value = furrr::future_map2(trace_name, col_names, function(x, y){
        dplyr::pull(meta_data[meta_data$trace_name == x, y])
      }))
    p()
    Sys.sleep(.2)

    reticulate::repl_python()

    import h5py
    import pandas as pd
    import numpy as np

    e = h5py.File(''+r.h5_file_path, 'w')

    for i in np.r_[0:len(r.meta_data.trace_name)]:
      e.create_dataset("data/"+ r.meta_data.trace_name[i], r.x[i].shape, data=r.x[i], dtype=np.float32)

    for i in np.r_[0:len(r.meta_data.trace_name)]:
      for j in np.r_[0:34]:
      e['data/'+r.meta_data.trace_name[i]].attrs[''+r.info.col_names[j]] = r.info.value[j]

    e.close()
    del(e)
    exit

    p()
    Sys.sleep(.2)
    rm(col_names)
    rm(x)
    rm(info)
    p()
    Sys.sleep(.2)
  })
}

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.