/stata_paralleliser.py Secret
Created
November 8, 2019 00:41
Star
You must be signed in to star a gist
parallelise stata with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, itertools | |
from subprocess import run | |
from multiprocessing.dummy import Pool | |
from functools import partial | |
def set_globs(stexec,scname): | |
global script_name, stata_exec | |
stata_exec = stexec | |
script_name = scname | |
def create_command_list(*args): | |
commands = [] | |
combinations = list(itertools.product(*args)) | |
for combination in combinations: | |
arguments = " ".join(combination) | |
command = stata_exec + ' /e /q do ' + script_name + ' ' + arguments | |
commands.append(command) | |
return commands | |
def run_do_file( | |
script_location, | |
commands, | |
script_name = 'caller.do', | |
nprocesses=2, | |
keeplog = False): | |
os.chdir(script_location) | |
pool = Pool(nprocesses) | |
for i, returncode in enumerate(pool.imap(partial(run,shell=True),commands)): | |
print(i, 'command executed, Returned', returncode) | |
if __name__ == '__main__': | |
stexec = 'StataMP-64' # short exec because Stata is already in my Path, else wrap in quotes on windows | |
scname = 'caller.do' | |
set_globs(stexec,scname) | |
script_location = r'C:\Users\alal\Desktop\Research\temp' | |
files = ['all','sub1','sub2'] | |
yvars = ['life_expectancy','income'] | |
xvars = ['1','2','3'] | |
commands = create_command_list(files,yvars,xvars) | |
print(commands) | |
run_do_file(script_location, commands, script_name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment