dsilvestro / PyRate

PyRate is a program to estimate speciation, extinction, and preservation rates from fossil occurrence data using a Bayesian framework.
GNU Affero General Public License v3.0
76 stars 34 forks source link

Plotting creates wrong filepath in R for Windows #8

Closed bmchorse closed 7 years ago

bmchorse commented 7 years ago

When I run plotting commands for PyRate, the R script saved to generate the PDF contains path formatting that does not work because of backslashes.

Platform: Windows 10 Python v2.7 R v3.4.0 Rstudio v1.0.143

Example: after running a trait covariance analysis, I call: > python "./PyRate/PyRate.py" -plot2 "./Results/pyrate_mcmc_logs/occ_1_G_COV_marginal_rates.log" This successfully creates a _RTT.R script. The call to pdf is written as follows (with the '...' representing a longer path of folders):

pdf(file='C:\...\Results\pyrate_mcmc_logs\occ_1_G_COV_marginal_rates_RTT.pdf',width=10.8, height=8.4)

In both base R and RStudio, this line gives the error: Error: '\U' used without hex digits in character string starting "'C:\U"

Escaping by using \\ instead of \ did not seem to work, but replacing with / does work:

pdf(file='C:/.../Results/pyrate_mcmc_logs/occ_1_G_COV_marginal_rates_RTT.pdf',width=10.8, height=8.4)

The PyRate plot methods seem to intentionally use the backslash in this pdf() call when the system platform is Windows (or Microsoft), but it needs a little more of a workaround as the working directory will also use \ when called.

I solved the problem locally by modifying the code in PyRate.py at line 427 with the following:

    if platform.system() == "Windows" or platform.system() == "Microsoft":
        modpath = wd.replace('\\', '/')
        Rfile+= "\n\npdf(file='%s/%s_RTT.pdf',width=10.8, height=8.4)\npar(mfrow=c(2,2))" % (modpath,name_file)
    else: 
        Rfile+= "\n\npdf(file='%s/%s_RTT.pdf',width=10.8, height=8.4)\npar(mfrow=c(2,2))" % (wd,name_file)

But I am not sure if this is broadly applicable. Similar fixes are necessary for, e.g., PyRateContinuous.py.

Thanks!

dsilvestro commented 7 years ago

Hi thanks a lot for reporting the issue! I don't often have the chance to test things on Windows and find the question of slash, backslash, double-backslash quite confusing. I will look into into it and try to fix it following your suggestions. In the meantime, you can always open the PyRate-generated R scripts and execute them line-by-line in R to get the plots. Cheers - Daniele