<commit_before># /usr/bin/env python

import sys
import os
import datetime

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


def HTSOHM(children_per_generation,    # number of materials per generation
           number_of_atomtypes,        # number of atom-types per material
           strength_0,                 # intial strength parameter
           number_of_bins,             # number of bins for analysis
           max_generations=20):        # maximum number of generations

    # Start run (see DD.MM.YYYY_HH.MM.SS_CpG.NoA_S0_NoB.txt for parameters)
    sys.path.insert(0, os.environ['SRC_DIR'])

    start = datetime.datetime.now()
    run_ID = ( "%s.%s.%s_%s.%s.%s_%s.%s_%s_%s" %
               (start.day, start.month, start.year,
                start.hour, start.minute, start.second,
                children_per_generation, number_of_atomtypes,
                strength_0,
                number_of_bins))

    wd = os.environ['HTSOHM_DIR']      # specify working directory

    run_file = open( wd + '/' + run_ID + '.txt', "w")
    run_file.write( "Date:\t\t\t\t%s:%s:%s\n" % (start.day, start.month,
                                                 start.year) +
                    "Time:\t\t\t\t%s:%s:%s\n" % (start.hour, start.minute,
                                                 start.second) +
                    "Children per generation:\t%s\n" % (
                                                 children_per_generation) +
                    "Number of atom-types:\t\t%s\n" % (number_of_atomtypes) +
                    "Initial mutation strength:\t%s\n" % (strength_0) +
                    "Number of bins:\t\t\t%s\n" % (number_of_bins))
    run_file.close()


    # SEED (GENERATION = 0)
    generation = 0
    gen.generate(children_per_generation, number_of_atomtypes, run_ID)
    sim.AddRows( run_ID, GenIDs(generation, children_per_generation) )
    sim.simulate( run_ID, GenIDs(generation, children_per_generation) )


    # FIRST GENERATION
    generation = 1                     # `Generation` counter
    # Select parents, add IDs to database...
    bng.SelectParents(run_ID, children_per_generation, generation)
    sim.DummyTest(run_ID, generation)
    
    mut.FirstS(run_ID, strength_0)     # Create strength-parameter array `run_ID`.npy
    mut.mutate(run_ID, generation)     # Create first generation of child-materials

    sim.simulate( run_ID, GenIDs(generation, children_per_generation) )


    # SECOND GENERATION, AND ON...
    NextGens = np.arange(2, max_generations)
    for i in NextGens:
        generation = i

        bng.SelectParents(run_ID, children_per_generation, generation)
        sim.DummyTest(run_ID, generation)
        mut.CalculateS(run_ID, generation)
        mut.mutate(run_ID, generation)
        sim.simulate( run_ID, GenIDs(generation, children_per_generation) )

def GenIDs(generation, children_per_generation):
    first = generation * children_per_generation
    last = (generation + 1) * children_per_generation
    GenIDs = np.arange(first, last)

    return GenIDs
<commit_msg>separated FIRST, SECOND, and NEXT generation(s); added while-loop for dummy test(s)<commit_after># /usr/bin/env python

import sys
import os
import datetime

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


def write_run_file(children_per_generation,
                   number_of_atomtypes,
                   strength_0,
                   number_of_bins,
                   max_generations):

    sys.path.insert(0, os.environ['HTSOHM_DIR'] + '/htsohm')

    start = datetime.datetime.now()
    run_id = ( "%s.%s.%s_%s.%s.%s" %
               (start.day, start.month, start.year,
                start.hour, start.minute, start.second) )

    wd = os.environ['HTSOHM_DIR']      # specify working directory

    run_file = open( wd + '/' + run_id + '.txt', "w")
    run_file.write( "Date:\t\t\t\t%s:%s:%s\n" % (start.day, start.month,
                                                 start.year) +
                    "Time:\t\t\t\t%s:%s:%s\n" % (start.hour, start.minute,
                                                 start.second) +
                    "Children per generation:\t%s\n" % (
                                                 children_per_generation) +
                    "Number of atom-types:\t\t%s\n" % (number_of_atomtypes) +
                    "Initial mutation strength:\t%s\n" % (strength_0) +
                    "Number of bins:\t\t\t%s\n" % (number_of_bins))
    run_file.close()

    return run_id


def grep_run_file(run_id):

    wd = os.environ['HTSOHM_DIR']
    with open( wd + '/' + run_id + '.txt' ) as origin:
        for line in origin:
            if "Children per generation:" in line:
                children_per_generation = int( line.split()[3] )
            elif "Number of atom-types:" in line:
                number_of_atomtypes = int( line.split()[3] )

    return children_per_generation, number_of_atomtypes


def seed_generation(run_id):

    children_per_generation, number_of_atomtypes = grep_run_file(run_id)
    generation = 0
    ids = gen_ids(generation, children_per_generation)

    gen.generate(children_per_generation, number_of_atomtypes, run_id)
    sim.add_rows(run_id, generation, ids)
    sim.simulate(run_id, ids)


def first_generation(run_id, strength_0):

    children_per_generation, number_of_atomtypes = grep_run_file(run_id)
    generation = 1
    ids = gen_ids(generation, children_per_generation)

    status = "Dummy test:   RUNNING"
    while status == "Dummy test:   RUNNING":
        # Select parents, add IDs to database...
        next_materials_list = bng.select_parents(run_id,
                                                 children_per_generation,
                                                 generation)
        sim.add_rows(run_id, generation, ids)
        bng.add_parent_ids(run_id, next_materials_list)
        status = sim.dummy_test(run_id,
                                next_materials_list,
                                status,
                                generation)
    mut.first_s(run_id, strength_0)    # Create strength-parameter array `run_id`.npy
    mut.mutate(run_id, generation)     # Create first generation of child-materials
    sim.simulate( run_id, gen_ids(generation, children_per_generation) )


def next_generation(run_id, generation):

    children_per_generation, number_of_atomtypes = grep_run_file(run_id)
    ids = gen_ids(generation, children_per_generation)

    status = "Dummy test:   RUNNING"
    while status == "Dummy test:   RUNNING":
        # Select parents, add IDs to database...
        next_materials_list = bng.select_parents(run_id,
                                                 children_per_generation,
                                                 generation)
        sim.add_rows(run_id, generation, ids)
        bng.add_parent_ids(run_id, next_materials_list)
        status = sim.dummy_test(run_id,
                                next_materials_list,
                                status,
                                generation)

    mut.calculate_s(run_id, generation)
    mut.mutate(run_id, generation)
    sim.simulate( run_id, ids )


def htsohm(children_per_generation,    # number of materials per generation
           number_of_atomtypes,        # number of atom-types per material
           strength_0,                 # intial strength parameter
           number_of_bins,             # number of bins for analysis
           max_generations=20):        # maximum number of generations

    run_id = write_run_file(children_per_generation,
                            number_of_atomtypes,
                            strength_0,
                            number_of_bins,
                            max_generations)

    for i in range(max_generations):
        if i == 0:                     # SEED GENERATION
            seed_generation(run_id)
        elif i == 1:                   # FIRST GENERATION
            first_generation(run_id, strength_0)
        elif i >= 2:                   # SECOND GENERATION(S), and on...
            next_generation(run_id, i)


def gen_ids(generation, children_per_generation):

    first = generation * children_per_generation
    last = (generation + 1) * children_per_generation
    gen_ids = np.arange(first, last)

    return gen_ids

{'fraction_non_alphanumeric': 0.09049402950128775, 'fraction_numerical': 0.0036291266682275812, 'mean_word_length': 1.952989975803664, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1406948', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2458, 'n_words': 551}