<commit_before>#!/usr/bin/env python

import glob
import shutil

# from repex import async_repex as async 
from repex import dummy_exchange as async 


# ------------------------------------------------------------------------------
#
def set_waitlist(exchange, rids):

    exchange._waitlist = list()
    for rid in rids:
        exchange._waitlist.append(exchange._replicas[rid])


# ------------------------------------------------------------------------------
#
def test_ReplicaExchange():

    # exchange_size:  how big the exchange list needs to be
    #                 to move to the exchange phase
    # window_size:    the width of the sliding window
    exchange = async.ReplicaExchange(ensemble_size = 16,
                                     exchange_size = 4,
                                     window_size   = 8,
                                     md_cycles     = 3,
                                   # min_temp      = 300,
                                   # max_temp      = 320,
                                   # timesteps     = 500,
                                   # basename      = 'ace-ala', 
                                   # executable    = 'SANDER', 
                                   # cores         = 1
                                    )
    assert len(exchange._replicas) == 16


    # assign seven replicas to waitlist
    set_waitlist(exchange, [0, 2, 4, 5, 12, 13, 15])

    old_len = len(exchange._waitlist)
    ex_list = exchange._find_exchange_list(4, 8, exchange._replicas[0])

    assert(isinstance(ex_list, list))
    assert(len(ex_list) == 4)
    assert(len(exchange._waitlist) == old_len - len(ex_list))
    assert(exchange._replicas[0] in ex_list)

    for r in exchange._waitlist:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in exchange._waitlist)


    # assign seven replicas to waitlist with ending replicas
    # fulfilling the criteria
    set_waitlist(exchange, [0, 4, 14, 12, 13, 15])

    old_len = len(exchange._waitlist)
    ex_list = exchange._find_exchange_list(4, 8, exchange._replicas[12])

    assert(isinstance(ex_list, list))
    assert(len(ex_list) == 4)
    assert(len(exchange._waitlist) == old_len - len(ex_list))  
    assert(exchange._replicas[12] in ex_list)

    for r in exchange._waitlist:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in exchange._waitlist)


    # assign eight replicas to waitlist with two sets of
    # replicas fulfilling the criteria
    set_waitlist(exchange, [0, 2, 3, 4, 14, 12, 13, 15])

    old_len = len(exchange._waitlist)
    ex_list = exchange._find_exchange_list(4, 8, exchange._replicas[2])

    assert(isinstance(ex_list, list))
    assert([r.rid for r in ex_list] == [0, 2, 3, 4])
    assert(len(ex_list) == 4)
    assert(len(exchange._waitlist) == old_len - len(ex_list))  

    for r in exchange._waitlist:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in exchange._waitlist)

    old_len = len(exchange._waitlist)
    ex_list = exchange._find_exchange_list(4, 8, exchange._replicas[12])

    assert(isinstance(ex_list, list))
    assert([r.rid for r in ex_list] == [12, 13, 14, 15]) 
    assert(len(ex_list) == 4)
    assert(len(exchange._waitlist) == old_len - len(ex_list)) 

    for r in exchange._waitlist:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in exchange._waitlist)


    # assign four replicas to waitlist
    set_waitlist(exchange, [0, 2, 4, 5])

    ex_list = exchange._find_exchange_list(4, 8, exchange._replicas[0])

    assert(isinstance(ex_list, list))
    assert(len(ex_list) == 4)
    assert(len(exchange._waitlist)) == 0

    for r in exchange._waitlist:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in exchange._waitlist)


  # assign three replicas to waitlist

    set_waitlist(exchange, [0, 2, 1])

    ex_list = exchange._find_exchange_list(4, 8, exchange._replicas[0])

    assert((ex_list) is None)


  # assign two replicas to waitlist

    set_waitlist(exchange, [0, 2])

    ex_list = exchange._find_exchange_list(4, 4, exchange._replicas[0])

    assert((ex_list) is None)

  # assign 8 replicas to waitlist

    set_waitlist(exchange, [0, 1, 2, 3, 4, 6, 7, 8, 9])

    ex_list = exchange._find_exchange_list(4, 4, exchange._replicas[4])
    ex_list_ids = [rep.rid for rep in ex_list]

    assert((ex_list_ids) == [1,2,3,4])


# ------------------------------------------------------------------------------
#
def cleanup():

    for filename in glob.glob("re.session*"):
        shutil.rmtree(filename) 


# ------------------------------------------------------------------------------
#
if __name__ == '__main__':

    test_ReplicaExchange()
    cleanup()


# ------------------------------------------------------------------------------

<commit_msg>testing for sel alg<commit_after>#!/usr/bin/env python

import glob
import shutil

# from repex import async_repex as async 
from radical.repex import algorithms #import select_replicas_1D as rsa 


# ------------------------------------------------------------------------------

class Replica():

    def __init__(self, rid):
        self.rid=rid



# ------------------------------------------------------------------------------
#

def create_replica_list(ensemble_size):
    replica_list = []
    for i in range(ensemble_size):
        replica = Replica(i)
        replica_list.append(replica)
    return replica_list

# ------------------------------------------------------------------------------
#
def test_select_replicas_1D():
    ensemble_size = 16
    rlist=create_replica_list(ensemble_size)
    criteria =   {"exchange_size" : 4,
                  "window_size"   : 4,
                  "select_alg"    : "1D",
                  "exchange_alg"  : "RANDOM"}


    assert len(rlist) == 16
    waitlist = [rlist[1], rlist[2], rlist[4], rlist[7]]
    replica = rlist[2]
    [ex_list,new_wl] = algorithms.select_replicas_1D(waitlist, criteria, replica)
    # assign four replicas to waitlist
    # returns new_waitlist, exchange_list
  

    old_len = len(waitlist)
    

    assert(isinstance(ex_list, list))
    assert(len(ex_list) == 4)
    assert(len(new_wl) == old_len - len(ex_list))
    assert(replica in ex_list)

    for r in new_wl:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in new_wl)


    # assign three replicas to waitlist 
    # this should fail, but it fails for the wron reason?
    waitlist = [rlist[1], rlist[2], rlist[4]]
    replica = rlist[2]
    [ex_list,new_wl] = algorithms.select_replicas_1D(waitlist, criteria, replica) ### This test fails because the algorithm returns 
                                                                                  ### NoneType instead of an empty ex_list.
                                                                                  ### So clearly new_wl is not being generated either  

    assert(isinstance(ex_list, list))
    assert(len(ex_list) == 0)
    assert(len(new_wl) == old_len - len(ex_list))
    assert(replica in ex_list)

    for r in new_wl:
        assert(r not in ex_list)

    for r in ex_list:
        assert(r not in new_wl)


    

# ------------------------------------------------------------------------------
#
def cleanup():

    for filename in glob.glob("re.session*"):
        shutil.rmtree(filename) 


# ------------------------------------------------------------------------------
#
if __name__ == '__main__':

    test_select_replicas_1D()
    cleanup()


# ------------------------------------------------------------------------------


{'fraction_non_alphanumeric': 0.20237473903966596, 'fraction_numerical': 0.018528183716075158, 'mean_word_length': 1.5939086294416243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4416202', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2219, 'n_words': 564}