<commit_before>import sys
import os
from datetime import datetime
import yaml

import numpy as np

from htsohm import generate as gen
from htsohm import simulate as sim
from htsohm import binning as bng
from htsohm import mutate as mut
from htsohm.runDB_declarative import RunData, session

def write_config_file(children_per_generation, number_of_atomtypes, strength_0,
    number_of_bins, max_generations):
    """Writes run-specific parameters to /config/<run_id>.yaml.

    This function writes run-specific parameters to a configuration file that is loaded/written
    at different stages throughout the overall HTSOHM routine.
    """
    run_id = datetime.now().isoformat()
    wd = os.environ['HTSOHM_DIR']      # specify working directory
    config_file = os.path.join(wd, 'config', run_id + '.yaml')

    run_config = {
        "run-id" : run_id,
        "children-per-generation" : children_per_generation,
        "number-of-atom-types" : number_of_atomtypes,
        "initial-mutation-strength" : strength_0,
        "number-of-bins" : number_of_bins,
        "max-number-of-generations" : max_generations
    }

    with open(config_file, "w") as file:
        yaml.dump(run_config, file, default_flow_style=False)

    return run_config["run-id"]



def init_materials_in_database(run_id, children_per_generation, generation):
    for material in range(children_per_generation):
        new_material = RunData(run_id, generation, 'none')
        session.add(new_material)
    session.commit()

def simulate_all_materials(run_id, generation):
    """simulate methane loading, helium void fraction, and surface area for seed population"""
    materials = session.query(RunData).filter(RunData.run_id == run_id, RunData.generation == generation).all()
    for material in materials:
        sim.run_all_simulations(material.id)
    session.commit()

def seed_generation(run_id, children_per_generation, number_of_atomtypes):
    generation = 0

    init_materials_in_database(run_id, children_per_generation, generation)
    gen.write_seed_definition_files(run_id, children_per_generation, number_of_atomtypes)
    simulate_all_materials(run_id, generation)


def next_generation(run_id, children_per_generation, generation):
    s = session    # rename database objects to shorted queries
    db = RunData

    init_materials_in_database(run_id, children_per_generation, generation)

    ############################################################################
    # select potential parent-materials and run them in dummy-test
    test_complete = False
    while not test_complete:
        next_generation_list = bng.select_parents(run_id, children_per_generation, generation)
        s.commit()             # parent_ids added to database
        test_complete = sim.dummy_test(run_id, next_generation_list, generation)
        s.commit()             # dummy_test_results added to database

    ############################################################################
    # selected parent-materials are mutated to create next generation
    if generation == 1:
        mut.create_strength_array(run_id)                  # create strength-parameter array `run_id`.npy
    elif generation >= 2:
        mut.recalculate_strength_array(run_id, generation) # recalculate strength-parameters, as needed
    mut.write_children_definition_files(run_id, generation)  # create child-materials

    simulate_all_materials(run_id, generation)

def htsohm(children_per_generation,    # number of materials per generation
           number_of_atomtypes,        # number of atom-types per material
           strength_0,                 # intial strength parameter<commit_msg>factored out screen_parents() and create_next_generation() in htsohm.htsohm<commit_after>import sys
import os
from datetime import datetime
import yaml

import numpy as np

from htsohm import generate as gen
from htsohm import simulate as sim
from htsohm import binning as bng
from htsohm import mutate as mut
from htsohm.runDB_declarative import RunData, session

def write_config_file(children_per_generation, number_of_atomtypes, strength_0,
    number_of_bins, max_generations):
    """Writes run-specific parameters to /config/<run_id>.yaml.

    This function writes run-specific parameters to a configuration file that is loaded/written
    at different stages throughout the overall HTSOHM routine.
    """
    run_id = datetime.now().isoformat()
    wd = os.environ['HTSOHM_DIR']      # specify working directory
    config_file = os.path.join(wd, 'config', run_id + '.yaml')

    run_config = {
        "run-id" : run_id,
        "children-per-generation" : children_per_generation,
        "number-of-atom-types" : number_of_atomtypes,
        "initial-mutation-strength" : strength_0,
        "number-of-bins" : number_of_bins,
        "max-number-of-generations" : max_generations
    }

    with open(config_file, "w") as file:
        yaml.dump(run_config, file, default_flow_style=False)

    return run_config["run-id"]

def init_materials_in_database(run_id, children_per_generation, generation):
    """initialize materials in database with run_id and generation"""
    for material in range(children_per_generation):
        new_material = RunData(run_id, generation, 'none')
        session.add(new_material)
    session.commit()

def simulate_all_materials(run_id, generation):
    """simulate methane loading, helium void fraction, and surface area for seed population"""
    materials = session.query(RunData).filter(RunData.run_id == run_id, RunData.generation == generation).all()
    for material in materials:
        sim.run_all_simulations(material.id)
    session.commit()

def screen_parents(run_id, children_per_generation, generation):
    """select potential parent-materials and run them in dummy-test"""
    test_complete = False
    while not test_complete:
        next_generation_list = bng.select_parents(run_id, children_per_generation, generation)
        session.commit()             # parent_ids added to database
        test_complete = sim.dummy_test(run_id, next_generation_list, generation)
        session.commit()             # dummy_test_results added to database

def create_next_generation(run_id, generation):
    """once screened, parent-materials are mutated to create next generation"""
    if generation == 1:
        mut.create_strength_array(run_id)                  # create strength-parameter array `run_id`.npy
    elif generation >= 2:
        mut.recalculate_strength_array(run_id, generation) # recalculate strength-parameters, as needed
    mut.write_children_definition_files(run_id, generation)      # create child-materials


def seed_generation(run_id, children_per_generation, number_of_atomtypes):
    generation = 0

    init_materials_in_database(run_id, children_per_generation, generation)
    gen.write_seed_definition_files(run_id, children_per_generation, number_of_atomtypes)
    simulate_all_materials(run_id, generation)


def next_generation(run_id, children_per_generation, generation):
    init_materials_in_database(run_id, children_per_generation, generation)
    screen_parents(run_id, children_per_generation, generation)
    create_next_generation(run_id, generation)
    simulate_all_materials(run_id, generation)

def htsohm(children_per_generation,    # number of materials per generation
           number_of_atomtypes,        # number of atom-types per material
           strength_0,                 # intial strength parameter
{'fraction_non_alphanumeric': 0.0952063914780293, 'fraction_numerical': 0.0015978695073235686, 'mean_word_length': 4.054508748317631, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5127150', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2090, 'n_words': 534}