<commit_before>"""
Top-level run script for IPCluster parallel Queue implementation
Run as:
    python runQueue.py --outpath '/dirToCreateAllOutputFolders/'

This script is designed to load the queue.json file located in the "run" folder.
queue.json contains the names of .json files located in ../Scripts/. to be run by this script
This script then runs .json file named in queue.json for the number of runs specified in queue.json
This script should be executed from the "run" folder
Written by <NAME> 4/27/2018
"""

#

import json
from subprocess import Popen
import subprocess
import os
import numpy as np
import EXOSIMS
import EXOSIMS.MissionSim
import os
import os.path
import cPickle
import time
import random
import argparse
import traceback

def run_one(genNewPlanets=True, rewindPlanets=True, outpath='.'):
    # reset simulation at the end of each simulation
    SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)  
    # wrap the run_sim in a try/except loop
    nbmax = 10
    for attempt in range(nbmax):
        try:
            # run one survey simulation
            SS.run_sim()
            DRM = SS.DRM[:]
            systems = SS.SimulatedUniverse.dump_systems()
            seed = SS.seed
        except Exception as e:
            # if anything goes wrong, log the error and reset simulation
            with open(os.path.join(outpath,'log.err'), 'ab') as f:
                f.write(repr(e))
                f.write('\n')
                f.write(traceback.format_exc())
                f.write('\n\n')
            
            SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)
        else:
            break
    else:
        raise ValueError("Unsuccessful run_sim after %s reset_sim attempts"%nbmax)
    
    pklname = 'run'+str(int(time.clock()*100))+''.join(["%s" % random.randint(0, 9) for num in range(5)]) + '.pkl'
    pklpath = os.path.join(outpath, pklname)
    with open(pklpath, 'wb') as f:
        cPickle.dump({'DRM':DRM,'systems':systems,'seed':seed}, f)
        
    return 0

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run an ipcluster parallel ensemble job queue.")
    #parser.add_argument('scriptfile', nargs=1, type=str, help='Full path to scriptfile (string).')
    # parser.add_argument('numruns', nargs=1, type=int, help='Number of runs (int).')
    parser.add_argument('--outpath',nargs=1,type=str, help='Full path to output directory where each job directory will be saved (string).')<commit_msg>moving reset sim to match run_one in run_ipcluster_ensemble<commit_after>"""
Top-level run script for IPCluster parallel Queue implementation
Run as:
    python runQueue.py --outpath '/dirToCreateAllOutputFolders/'

This script is designed to load the queue.json file located in the "run" folder.
queue.json contains the names of .json files located in ../Scripts/. to be run by this script
This script then runs .json file named in queue.json for the number of runs specified in queue.json
This script should be executed from the "run" folder
Written by <NAME> 4/27/2018
"""

#

import json
from subprocess import Popen
import subprocess
import os
import numpy as np
import EXOSIMS
import EXOSIMS.MissionSim
import os
import os.path
import cPickle
import time
import random
import argparse
import traceback

def run_one(genNewPlanets=True, rewindPlanets=True, outpath='.'):
    # wrap the run_sim in a try/except loop
    nbmax = 10
    for attempt in range(nbmax):
        try:
            # run one survey simulation
            SS.run_sim()
            DRM = SS.DRM[:]
            systems = SS.SimulatedUniverse.dump_systems()
            seed = SS.seed
        except Exception as e:
            # if anything goes wrong, log the error and reset simulation
            with open(os.path.join(outpath,'log.err'), 'ab') as f:
                f.write(repr(e))
                f.write('\n')
                f.write(traceback.format_exc())
                f.write('\n\n')
            
            SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)
        else:
            break
    else:
        raise ValueError("Unsuccessful run_sim after %s reset_sim attempts"%nbmax)
    
    # reset simulation at the end of each simulation
    SS.reset_sim(genNewPlanets=genNewPlanets, rewindPlanets=rewindPlanets)  

    pklname = 'run'+str(int(time.clock()*100))+''.join(["%s" % random.randint(0, 9) for num in range(5)]) + '.pkl'
    pklpath = os.path.join(outpath, pklname)
    with open(pklpath, 'wb') as f:
        cPickle.dump({'DRM':DRM,'systems':systems,'seed':seed}, f)
        
    return 0

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Run an ipcluster parallel ensemble job queue.")
    #parser.add_argument('scriptfile', nargs=1, type=str, help='Full path to scriptfile (string).')
    # parser.add_argument('numruns', nargs=1, type=int, help='Number of runs (int).')
    parser.add_argument('--outpath',nargs=1,type=str, help='Full path to output directory where each job directory will be saved (string).')
{'fraction_non_alphanumeric': 0.10426913240212472, 'fraction_numerical': 0.007475900059020263, 'mean_word_length': 3.050996015936255, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1168301', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1544, 'n_words': 490}