<commit_before>from __future__ import division; __metaclass__ = type
import logging
log = logging.getLogger(__name__)

import numpy as np
import types

import westpa, west
from westpa import extloader
from westpa.yamlcfg import check_bool, ConfigItemMissing
from westext.stringmethod import WESTStringMethod, DefaultStringMethod
from west.binning import VoronoiBinMapper


class StringDriver(object):
    def __init__(self, sim_manager, plugin_config):
        super(StringDriver, self).__init__()

        if not sim_manager.work_manager.is_master:
                return

        self.sim_manager = sim_manager
        self.data_manager = sim_manager.data_manager
        self.system = sim_manager.system

        # Parameters from config file
        self.windowsize = plugin_config.get('windowsize', 10)
        self.update_interval = plugin_config.get('update_interval', 10)
        self.initial_update = plugin_config.get('initial_update', 20)
        self.priority = plugin_config.get('priority', 0)

        self.write_avg_pos = check_bool(plugin_config.get('write_avgpos', True))
        self.do_update = check_bool(plugin_config.get('do_update', True))
        self.init_from_data = check_bool(plugin_config.get('init_from_data', True))

        self.dfunc = self.get_dfunc_method(plugin_config)

        # Load method to calculate average position in a bin
        # If the method is defined in an external module, correctly bind it
        ap = self.get_avgpos_method(plugin_config)
        if hasattr(ap, 'im_class'):
            self.get_avgpos = ap
        else:
            self.get_avgpos = types.MethodType(ap, self)

        # Get initial set of string centers
        centers = self.get_initial_centers()

        try:
            sm_params = self.system.sm_params
        except AttributeError as e:
            log.error('String Driver Error: system does not define sm_params. \
                        This is required and should be added to the system definition; {}'.format(e))
            raise

        # Initialize the string
        str_method = self.get_string_method(plugin_config)

        try:
            self.strings = str_method(centers, **sm_params)
        except (TypeError, AssertionError) as e:
            log.error('String Driver Error: Failed during initialization of string method: {}'.format(e))
            raise

        # Update the Region Set
        self.update_bin_mapper()

        # Register callback
        sim_manager.register_callback(sim_manager.prepare_new_iteration, self.prepare_new_iteration, self.priority)

        west.rc.pstatus('-westext.stringmethod -----------------\n')
        west.rc.pstatus('windowsize: {}\n'.format(self.windowsize))
        west.rc.pstatus('update interval: {}\n'.format(self.update_interval))
        west.rc.pstatus('initial update: {}\n'.format(self.initial_update))
        west.rc.pstatus('priority: {}\n'.format(self.priority))
        west.rc.pstatus('write average positions: {}\n'.format(self.write_avg_pos))
        west.rc.pstatus('do update: {}\n'.format(self.do_update))
        west.rc.pstatus('initialize from WE data: {}\n'.format(self.init_from_data))
        west.rc.pstatus('----------------------------------------\n')
        west.rc.pflush()

    def dfunc(self):
        raise NotImplementedError

    def get_avgpos(self, n_iter):
        raise NotImplementedError

    def get_dfunc_method(self, plugin_config):
        try:
            methodname = plugin_config['dfunc_method']
        except KeyError:
            raise ConfigItemMissing('dfunc_method')

        dfunc_method = extloader.get_object(methodname)

        log.debug('loaded stringmethod dfunc method {!r}'.format(dfunc_method))

        return dfunc_method

    def get_avgpos_method(self, plugin_config):
        try:
            methodname = plugin_config['avgpos_method']
        except KeyError:
            raise ConfigItemMissing('avgpos_method')

        if methodname.lower() == 'cartesian':
            avgpos_method = self.avgpos_cartesian
        else:
            avgpos_method = extloader.get_object(methodname)

        log.debug('loaded stringmethod avgpos method {!r}'.format(avgpos_method))

        return avgpos_method

    def get_string_method(self, plugin_config):
        try:
            methodname = plugin_config['string_method']
        except KeyError:
            raise ConfigItemMissing('string_method')

        if methodname.lower() == 'default':
            str_method = DefaultStringMethod
        else:
            str_method = extloader.get_object(methodname)

        assert issubclass(str_method, WESTStringMethod)
        log.debug('loaded stringmethod string method {!r}'.format(str_method))

        return str_method

    def get_initial_centers(self):
        self.data_manager.open_backing()

        with self.data_manager.lock:
            n_iter = max(self.data_manager.current_iteration - 1, 1)
            iter_group = self.data_manager.get_iter_group(n_iter)

            # First attempt to initialize string from data rather than system
            centers = None
            if self.init_from_data:
                log.info('Attempting to initialize stringmethod from data')

                try:
                    binhash = iter_group.attrs['binhash']
                    bin_mapper = self.data_manager.get_bin_mapper(binhash)

                    centers = bin_mapper.centers

                except:
                    log.warning('Initializing string centers from data failed; Using definition in system instead.')
                    centers = self.system.bin_mapper.centers
            else:
                log.info('Initializing string centers from system definition')
                centers = self.system.bin_mapper.centers

        self.data_manager.close_backing()

        return centers

    def update_bin_mapper(self):
        '''Update the bin_mapper using the current string'''

        west.rc.pstatus('westext.stringmethod: Updating bin mapper\n')
        west.rc.pflush()

        try:
            self.system.bin_mapper = VoronoiBinMapper(self.dfunc, self.strings.centers)
        except (ValueError, TypeError) as e:
            log.error('StringDriver Error: Failed updating region set: {}'.format(e))
            raise

    def avgpos_cartesian(self, n_iter):<commit_msg>Merging b90397d3082c and 908e2f9b2629<commit_after>from __future__ import division; __metaclass__ = type
import logging
log = logging.getLogger(__name__)

import numpy as np
import types

import westpa, west
from westpa import extloader
from westpa.yamlcfg import check_bool, ConfigItemMissing
from westext.stringmethod import WESTStringMethod, DefaultStringMethod
from west.binning import VoronoiBinMapper


class StringDriver(object):
    def __init__(self, sim_manager, plugin_config):
        super(StringDriver, self).__init__()

        if not sim_manager.work_manager.is_master:
                return

        self.sim_manager = sim_manager
        self.data_manager = sim_manager.data_manager
        self.system = sim_manager.system

        # Parameters from config file
        self.windowsize = plugin_config.get('windowsize', 10)
        self.update_interval = plugin_config.get('update_interval', 10)
        self.initial_update = plugin_config.get('initial_update', 20)
        self.priority = plugin_config.get('priority', 0)

        self.write_avg_pos = check_bool(plugin_config.get('write_avgpos', True))
        self.do_update = check_bool(plugin_config.get('do_update', True))
        self.init_from_data = check_bool(plugin_config.get('init_from_data', True))

        self.dfunc = self.get_dfunc_method(plugin_config)

        # Load method to calculate average position in a bin
        # If the method is defined in an external module, correctly bind it
        ap = self.get_avgpos_method(plugin_config)
        if hasattr(ap, 'im_class'):
            self.get_avgpos = ap
        else:
            self.get_avgpos = types.MethodType(ap, self)

        # Get initial set of string centers
        centers = self.get_initial_centers()

        try:
            sm_params = self.system.sm_params
        except AttributeError as e:
            log.error('String Driver Error: system does not define sm_params. \
                        This is required and should be added to the system definition; {}'.format(e))
            raise

        # Initialize the string
        str_method = self.get_string_method(plugin_config)

        try:
            self.strings = str_method(centers, **sm_params)
        except (TypeError, AssertionError) as e:
            log.error('String Driver Error: Failed during initialization of string method: {}'.format(e))
            raise

        # Update the BinMapper
        self.update_bin_mapper()

        # Register callback
        sim_manager.register_callback(sim_manager.prepare_new_iteration, self.prepare_new_iteration, self.priority)

        west.rc.pstatus('-westext.stringmethod -----------------\n')
        west.rc.pstatus('windowsize: {}\n'.format(self.windowsize))
        west.rc.pstatus('update interval: {}\n'.format(self.update_interval))
        west.rc.pstatus('initial update: {}\n'.format(self.initial_update))
        west.rc.pstatus('priority: {}\n'.format(self.priority))
        west.rc.pstatus('write average positions: {}\n'.format(self.write_avg_pos))
        west.rc.pstatus('do update: {}\n'.format(self.do_update))
        west.rc.pstatus('initialize from WE data: {}\n'.format(self.init_from_data))
        west.rc.pstatus('----------------------------------------\n')
        west.rc.pflush()

    def dfunc(self):
        raise NotImplementedError

    def get_avgpos(self, n_iter):
        raise NotImplementedError

    def get_dfunc_method(self, plugin_config):
        try:
            methodname = plugin_config['dfunc_method']
        except KeyError:
            raise ConfigItemMissing('dfunc_method')

        dfunc_method = extloader.get_object(methodname)

        log.info('loaded stringmethod dfunc method {!r}'.format(dfunc_method))

        return dfunc_method

    def get_avgpos_method(self, plugin_config):
        try:
            methodname = plugin_config['avgpos_method']
        except KeyError:
            raise ConfigItemMissing('avgpos_method')

        if methodname.lower() == 'cartesian':
            avgpos_method = self.avgpos_cartesian
        else:
            avgpos_method = extloader.get_object(methodname)

        log.info('loaded stringmethod avgpos method {!r}'.format(avgpos_method))

        return avgpos_method

    def get_string_method(self, plugin_config):
        try:
            methodname = plugin_config['string_method']
        except KeyError:
            raise ConfigItemMissing('string_method')

        if methodname.lower() == 'default':
            str_method = DefaultStringMethod
        else:
            str_method = extloader.get_object(methodname)

        assert issubclass(str_method, WESTStringMethod)
        log.debug('loaded stringmethod string method {!r}'.format(str_method))

        return str_method

    def get_initial_centers(self):
        self.data_manager.open_backing()

        with self.data_manager.lock:
            n_iter = max(self.data_manager.current_iteration - 1, 1)
            iter_group = self.data_manager.get_iter_group(n_iter)

            # First attempt to initialize string from data rather than system
            centers = None
            if self.init_from_data:
                log.info('Attempting to initialize stringmethod from data')

                try:
                    binhash = iter_group.attrs['binhash']
                    bin_mapper = self.data_manager.get_bin_mapper(binhash)

                    centers = bin_mapper.centers

                except:
                    log.warning('Initializing string centers from data failed; Using definition in system instead.')
                    centers = self.system.bin_mapper.centers
            else:
                log.info('Initializing string centers from system definition')
                centers = self.system.bin_mapper.centers

        self.data_manager.close_backing()

        return centers

    def update_bin_mapper(self):
        '''Update the bin_mapper using the current string'''

        west.rc.pstatus('westext.stringmethod: Updating bin mapper\n')
        west.rc.pflush()

        try:
            dfargs = getattr(self.system, 'dfargs', None)
            dfkwargs = getattr(self.system, 'dfkwargs', None)
            self.system.bin_mapper = VoronoiBinMapper(self.dfunc, self.strings.centers, 
                                                      dfargs=dfargs, 
                                                      dfkwargs=dfkwargs)
        except (ValueError, TypeError) as e:
            log.error('StringDriver Error: Failed updating the bin mapper: {}'.format(e))
            raise

    def avgpos_cartesian(self, n_iter):
{'fraction_non_alphanumeric': 0.09644317568088882, 'fraction_numerical': 0.002777563459609598, 'mean_word_length': 2.4146469968387776, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1776503', 'n_tokens_mistral': 3654, 'n_tokens_neox': 3521, 'n_words': 792}