Ejemplo n.º 1
0
def save_subopt_model(path: Path,
                      score: object,
                      it: int,
                      bn: dict,
                      save_subopt=False):

    bn.update({'stats': dict(score=score, it=it)})

    model_dir = get_dir(path)

    if save_subopt:
        # Save only if <sd_save_suboptimal_models> >= score
        write_json(
            bn, model_dir / GLOBALS.app['subopt_model_name'].format(
                date=FROZEN_DATE, it=GLOBALS.app['it_suffix'].format(it=it)))

    # Always save the last suboptimal model (overwrite)
    write_json(
        bn,
        model_dir / GLOBALS.app['last_model_name'].format(date=FROZEN_DATE))
Ejemplo n.º 2
0
def save_plots(path: Path, figs: list):

    plotter.ioff()

    mkdir = lambda x: get_dir(x, True)

    for fig in figs:
        fig.savefig(mkdir(path / 'pdf') /
                    f'{fig.canvas.get_window_title()}.pdf',
                    dpi=138,
                    bbox_inches='tight')
        fig.savefig(mkdir(path / 'svg') /
                    f'{fig.canvas.get_window_title()}.svg',
                    dpi=138,
                    bbox_inches='tight')
        fig.savefig(mkdir(path / 'png') /
                    f'{fig.canvas.get_window_title()}.png',
                    dpi=138,
                    bbox_inches='tight')

        print('Saved {}/<format>/{}.<format>'.format(
            str(path).replace('\\', '/'), fig.canvas.get_window_title()))

    plotter.close('all')
Ejemplo n.º 3
0
from pathlib import Path
from pandas import DataFrame
from bncontroller.sim.utils import GLOBALS, load_global_config
from bncontroller.filelib.utils import get_dir, FROZEN_DATE
from bncontroller.stubs.selector.utils import template_selector_generator
from bncontroller.stubs.selector.generation import generate_consistent_bnselector as generate_consistent_bnselector

if __name__ == "__main__":

    load_global_config()

    N, K, P, I, O = GLOBALS.bn_n, GLOBALS.bn_k, GLOBALS.bn_p, GLOBALS.bn_n_inputs, GLOBALS.bn_n_outputs

    path = get_dir(Path(GLOBALS.bn_model_path), create_if_dir=True)

    for l in range(GLOBALS.sd_max_iters):

        bn = GLOBALS.app_core_function()

        Path(path /
             f'{l}_bn_n{N}_k{K}_p{int(P*100)}_{FROZEN_DATE}.ebnf').write_text(
                 bn.to_ebnf())

        Path(path /
             f'{l}_bn_n{N}_k{K}_p{int(P*100)}_{FROZEN_DATE}.json').write_text(
                 bn.to_json())

        df = DataFrame(bn.atm.dtableau)

        df.to_csv(str(path / f'{l}_atm_{FROZEN_DATE}.csv'))
Ejemplo n.º 4
0
from pathlib import Path
from pandas import DataFrame
from pprint import pprint
from bncontroller.sim.utils import GLOBALS, load_global_config
from bncontroller.boolnet.selector import SelectiveBooleanNetwork
from bncontroller.boolnet.structures import BooleanNetwork
from bncontroller.jsonlib.utils import read_json
from bncontroller.filelib.utils import get_dir

if __name__ == "__main__":

    load_global_config()

    for path in get_dir(GLOBALS.bn_model_path).iterdir():

        if path.is_file():
            bn: BooleanNetwork = BooleanNetwork.from_json(read_json(path))

            print(str(path))
            print(DataFrame(bn.atm.dtableau).T)
            print(bn.atm.tableau)
            for k, a in bn.atm.dattractors.items():
                print(k, a)
            print(bn.atm.attractors)
Ejemplo n.º 5
0
                bis = {k: bninfos[k]}

                for l in q:
                    if k != l and k.split('_')[:2] == l.split('_')[:2]:
                        ks.update({l: data[l]})
                        bis.update({l: bninfos[l]})

                q.difference_update(set(ks.keys()))

                model, figs = plot_data(
                    ks, bis, args.config.plot_positives_threshold,
                    args.config.sim_light_intensity,
                    args.config.sim_run_time_s * 1000 /
                    args.config.sim_timestep_ms / 4)

                pu.save_plots(
                    fu.get_dir(args.config.plot_image_path / model,
                               create_if_dir=True),
                    transpose(figs)[0])

    else:
        model, figs = plot_data(
            data, bninfos, args.config.plot_positives_threshold,
            args.config.sim_light_intensity, args.config.sim_run_time_s *
            1000 / args.config.sim_timestep_ms / 4)

        pu.save_plots(
            fu.get_dir(args.config.plot_image_path / model,
                       create_if_dir=True),
            transpose(figs)[0])
Ejemplo n.º 6
0
if __name__ == "__main__":

    ### Load Configuration #########################################################

    load_global_config()

    check_config(GLOBALS)

    GLOBALS.app['mode'] = ('generate' if check_path(
        GLOBALS.bn_model_path, create_if_dir=True) else 'enhance')

    ### Init logger ################################################################

    logger.instance = LoggerFactory.filelogger(
        get_dir(GLOBALS.app_output_path, create_if_dir=True) /
        '{key}_{date}.log'.format(
            key=GLOBALS.app['mode'],
            date=FROZEN_DATE,
        ))

    ### BN Generation / Loading ####################################################

    bn = generate_or_load_bn(params=GLOBALS.bn_params,
                             path=GLOBALS.bn_model_path,
                             save_virgin=True)

    ### Launch search algorithm ##############################################

    if not GLOBALS.train_generate_only:
Ejemplo n.º 7
0
from bncontroller.sim.logging.logger import staticlogger as logger, LoggerFactory

from multiprocessing import Pool, cpu_count

########################################################################### 

if __name__ == "__main__":
    
    load_global_config()

    GLOBALS.app['mode'] = 'generate'

    if not GLOBALS.sim_suppress_logging:
        logger.instance = LoggerFactory.filelogger(
            get_dir(
                GLOBALS.app_output_path, create_if_dir=True
            ) / '{key}_{date}.log'.format(
                key=GLOBALS.app['mode'],
                date=FROZEN_DATE,
            )
        )

    ##############################################################

    NP = cpu_count()

    pool = Pool(processes=NP)
    
    mapper = lambda f, p: pool.imap_unordered(f, p, chunksize=2*NP)

    ##############################################################
Ejemplo n.º 8
0
    def generate_sim_config(
            self,
            world_fname='{name}_sim_world_{uniqueness}.{ext}',
            config_fname='{name}_sim_config_{uniqueness}.{ext}',
            data_fname='{name}_sim_data_{uniqueness}.{ext}',
            log_fname='{name}_sim_log_{uniqueness}.{ext}',
            ctrl_fname='{name}_sim_bn_{uniqueness}.{ext}'):
        '''
        Generate a config ready-to-use for running a simulation.
        This method completes or changes the paths of various 
        configuration options involving a path.
        '''

        uniqueness = lambda: FROZEN_DATE

        world_fname = gen_fname(self.app['mode'],
                                template=world_fname,
                                uniqueness=uniqueness,
                                ftype='wbt')

        ctrl_fname = gen_fname(self.app['mode'],
                               template=ctrl_fname,
                               uniqueness=uniqueness,
                               ftype='json')
        config_fname = gen_fname(self.app['mode'],
                                 template=config_fname,
                                 uniqueness=uniqueness,
                                 ftype='json')

        data_fname = gen_fname(
            self.app['mode'],
            template=data_fname,
            uniqueness=uniqueness if self.sim_unique_data_file else iso8106,
            ftype='json')

        log_fname = gen_fname(self.app['mode'],
                              template=log_fname,
                              uniqueness=iso8106,
                              ftype='log')

        # Create Sim Config based on the Experiment Config
        sim_config = Config.from_json(self.to_json())

        sim_config.webots_world_path = get_dir(
            sim_config.webots_world_path, create_if_dir=True) / world_fname

        # get_dir(sim_config.bn_model_path, create_if_dir=True)
        sim_config.bn_model_path = get_dir(Path('./tmp/model').absolute(),
                                           create_if_dir=True) / ctrl_fname

        # get_dir(sim_config.sim_config_path, create_if_dir=True)
        sim_config.sim_config_path = get_dir(Path('./tmp/config').absolute(),
                                             create_if_dir=True) / config_fname

        if check_path(sim_config.sim_data_path) in (1, -2):

            sim_config.sim_data_path = get_dir(
                sim_config.sim_data_path / FROZEN_DATE) / data_fname

        if check_path(sim_config.sim_log_path) in (1, -2):
            sim_config.sim_log_path = get_dir(
                sim_config.sim_log_path / FROZEN_DATE) / log_fname

        return sim_config
Ejemplo n.º 9
0
    load_global_config()

    toiter = lambda x: x if isinstance(x, (list, tuple)) else list([x])

    Ns, Ks, Ps, Qs, Is, Os = tuple(map(toiter, GLOBALS.bn_params))

    aNs = flat(toiter(GLOBALS.slct_target_n_attractors))
    tTaus = flat(toiter(GLOBALS.slct_target_transition_tau))
    nRhos = flat(toiter(GLOBALS.slct_noise_rho))
    iPhis = flat(toiter(GLOBALS.slct_input_steps_phi))

    instances = GLOBALS.sd_max_iters

    prod = itertools.product(aNs, Ns, Ks, Ps, Qs, Is, Os, nRhos, tTaus, iPhis)

    mpath = get_dir(GLOBALS.sim_data_path / 'stats/models', create_if_dir=True)
    dpath = get_dir(GLOBALS.sim_data_path / 'stats/data', create_if_dir=True)

    for nA, N, K, P, Q, I, O, nRho, tTau, isPhi in prod:

        bns = []
        files = []
        c1s = []
        c2s = []
        c2no1s = []
        c3s = []
        c4s = []

        generator = template_selector_generator(N, K, P, Q, I, O)

        for i in range(instances):