1

I'm trying to create a script that reruns maxent for different inputs. I have around 1500 species that need to be processed separately. My idea is to use a python loop for this program. But I can't seem to find the right information to start. Right now I have 3 simple lines which tells python to open the program.

import subprocess
subprocess.call(['java', '-jar', r'C:\Program Files (x86)\Maxent\maxent.jar'])
subprocess.call([r'C:\Program Files (x86)\Maxent\maxent.bat'])

Now I want to tell python which input to use. However, I can't seem to find any documentation on a function which specifies the input for a program.

Does anyone have any ideas on how to approach the next step?

-------------------Edit------------------------------------

Right now I have the following code:

import glob
import subprocess

insect = glob.glob('D:\Maxent\samples\*.csv')
for species in insect:
    subprocess.call(['java', '-jar', r'D:\Maxent\maxent.jar', 'environmentallayers=D:\Maxent\layers',
                     species, 'outputdirectory= D:\Maxent\outputs', 'redoifexists', 'autorun'])

This gives me the following error in maxent:

Initialization flags not understood: D:\Maxent\samples\Aeshna_juncea.csv

and the folowing error in pyhton

C:\Users\merel\PycharmProjects\untitled\venv\Scripts\python.exe "C:/Users/merel/PycharmProjects/untitled/maxent python.py"
Error: Initialization flags not understood: species
Error: No species selected

I also tried it with the ' around species. This gave me the following error:

C:\Users\merel\PycharmProjects\untitled\venv\Scripts\python.exe "C:/Users/merel/PycharmProjects/untitled/maxent python.py"
Error: Initialization flags not understood: species
Error: No species selected

I don't know why the program doesn't understand the argument. I also tried it with x instead of species to make sure that the word species didn't already exist in the library.

M. Heynen
  • 11
  • 2
  • I'm not sure to understand of what type of input you're speaking of. Is it the path to the program to call ? Or something else ? – Romain Jan 17 '20 at 10:23

1 Answers1

1

You need to pass arguments/flags to Maxent's jar file in order to achieve your goals, if I understood it correctly.

I've downloaded the Maxen and found the necessary arguments/flags. When you start Maxent, click help and scroll down to Batch mode, you can find all the arguments/flags there also an example usage as well; java -mx512m -jar maxent.jar environmentallayers=layers samplesfile=samples\bradypus.csv outputdirectory=outputs togglelayertype=ecoreg redoifexists autorun

You can add those arguments/flags after your path such like this:

subprocess.call(['java', '-jar', r'C:\Program Files (x86)\Maxent\maxent.jar', 'environmentallayers=layers', 'samplesfile=samples\bradypus.csv', 'outputdirectory=outputs', 'togglelayertype=ecoreg', 'redoifexists', 'autorun'])

I hope this helps you on your project. I have not tried any of this since I do not know anything about your field.

Edit: You don't have to call the .bat file since it also executes the maxent.jar wtih the given arguments/flags.

  • Thank you! This was exactly what I was looking for! – M. Heynen Jan 20 '20 at 10:22
  • What would you recommend for the repeating of the subprocess.call? Right now I used a for loop but it only repeats the process for the same species (defined by samplesfile=sample\bradypus.csv). So I'm thinking about creating a list and giving an index number to each file and then in the loop function link the index number to the 'samplesfile ='. Or should I use an iterate function? – M. Heynen Jan 20 '20 at 13:24
  • You can use the `glob` module to get all the files in a directory to a list, then you can iterate over that list to spawn the processes. You can use `glob` like this: ```import glob monkeys = glob.glob('./species/monkeys/*.csv') ``` this will give you a list that holds all the species inside that folder: `['monkey1.csv', 'monkey2.csv', 'monkey3.csv', ...]` – Ata Berk YILMAZ Jan 21 '20 at 12:25