Beispiel #1
0
def convert_points_to_target_vector(points, vector):
    '''
    This function converts an array of points into a target vector.
    [1,4,6] --> [0 1 0 0 1 0 1]
    
    Parameters
    ----------
    points : list of np.array
        Points to be remapped into a vector.
    vector : np.array
        vector for remapping of the points.

    Returns
    -------
    vector_target : np.array
        vector with pointes remapped.

    '''
    from utils import find_first

    if type(points) is np.ndarray:
        points = [points]

    if type(points) is list and type(points[0]) is not np.ndarray:
        raise Exception('ERROR: points input must be a list of np.array!')

    if type(vector) is list:
        vector = np.array(vector)
    vector_target = np.zeros(len(vector)).astype('int')

    for iPnt, points_list in enumerate(points):
        for point in points_list:
            vector_target[find_first(point, vector)] = iPnt + 1

    return vector_target
    def create_user(self,
                    email,
                    customer_id,
                    tenant_id,
                    password,
                    enabled=True):
        name = self.make_id(customer_id, email, "user")

        user = find_first(
            self.get_users(),
            lambda x: getattr(x, "email", None) == email and getattr(
                x, "name", None) == name)
        if user is None:
            user = self.client_keystone.users.create(name=name,
                                                     password=password,
                                                     email=email,
                                                     tenant_id=tenant_id,
                                                     enabled=enabled)
            log.info(
                'OpenStack create user. email: {}, user_id: {}, tenant_id: {}',
                email, user.id, tenant_id)
        else:
            log.info('User for email {} already exists: {}', email, user.id)
            self.client_keystone.users.update_password(user, password)
            user = self.client_keystone.users.update_tenant(user, tenant_id)
        return user
Beispiel #3
0
    def create_tariff(self, params=None):
        messages = []
        can_modify = self.update

        immutable = params.pop('immutable', False)
        default = params.pop('default', False)
        parsed_services = []
        for service_id, price in params.pop('services', {}).items():
            flavor = self.get_flavor(service_id)
            if flavor:
                service_id = flavor['service_id']
            parsed_services.append({'service_id': service_id, 'price': price})
        params['services'] = parsed_services

        tariff_list = self.client.tariff.list(
            name=params['localized_name']['en'])
        if tariff_list['total'] == 0:
            tariff_info = self.client.tariff.create(**params)
            logbook.info('Tariff "{}" created with id={}'.format(
                tariff_info['localized_name']['en'], tariff_info['tariff_id']))
            can_modify = True
        else:
            tariff_info = tariff_list['items'][0]
            diff = self.compare_fields(
                tariff_info, params,
                ['localized_name', 'description', 'currency'])
            for service_config in params['services']:
                service_id, price = service_config['service_id'], str(
                    service_config['price'])
                service = find_first(
                    tariff_info['services'],
                    lambda srv: service_id == srv['service']['service_id'])
                if not service:
                    messages.append(
                        'Service "{}" (id:{}) not in tariff service list'.
                        format(service['service']['localized_name']['en'],
                               service_id))
                elif Decimal(price) != Decimal(service['price']):
                    diff.append(('service price ({})'.format(
                        service['service']['localized_name']['en']),
                                 service['price'], price))

            if diff or messages:
                diff_str = self.diff_to_str(
                    'Tariff "{}" (id:{})'.format(
                        tariff_info['localized_name']['en'],
                        tariff_info['tariff_id']), diff)
                logbook.info(diff_str)
                for message in messages:
                    logbook.info(message)
                if not tariff_info['mutable']:
                    logbook.warning('Tariff is immutable')
                if tariff_info['mutable'] and self.update:
                    self.client.tariff.update(tariff_info['tariff_id'],
                                              **params)

        if immutable and can_modify:
            self.client.tariff.immutable(tariff_info['tariff_id'])
        if default and can_modify:
            self.client.tariff.set_default(tariff_info['tariff_id'])
Beispiel #4
0
    def make_action(cls, state, now=None):
        from model import Customer
        logbook.info("Try apply action: {}", state)

        now = now or utcnow().datetime

        machine = cls.machines[state.name]
        customer = Customer.get_by_id(state.customer_id)
        try:
            new_state_name = getattr(machine, state.action)(customer)
        except Exception as e:
            logbook.error("action {} failed: {}", state, e)
            state.remove()
            db.session.commit()
            raise

        state.step += 1
        if not new_state_name:
            if state.step >= len(machine.schedule):
                logbook.info("Actions {} are completed for {}", cls.name, customer)
                state.remove()
                db.session.commit()
                return

            new_state = machine.schedule[state.step]
        else:
            new_state = find_first(machine.schedule, lambda x: x[0] == new_state_name)
            if not new_state:
                state.remove()
                db.session.commit()
                raise Exception("Can't find new state %s for machine %s" % (new_state_name, cls.name))
        state.action = new_state[0]
        state.scheduled_at = now + timedelta(seconds=new_state[1])
        logbook.info("New action {} is scheduled", state)
Beispiel #5
0
    def attached_flavors(self, tenant_id):
        os_flavors = self.get_nova_flavors(is_public=None)
        attached = {}
        for flavor in os_flavors:
            if not flavor.is_public:
                attached_tenants = self.client_nova.flavor_access.list(flavor=flavor.id)
                tenant_attached = find_first(attached_tenants, lambda x: x.tenant_id == tenant_id)
                if tenant_attached:
                    attached[flavor.name] = flavor

        return attached
Beispiel #6
0
 def create_tenant(self, email, customer_id):
     """Create new tenant with default limits"""
     name = self.make_id(customer_id, email, "tenant")
     tenant = find_first(self.get_tenants(), lambda x: x.name == name)
     if tenant is None:
         tenant = self.client_keystone.tenants.create(tenant_name=name,
                                                      description=self.make_description(name, email),
                                                      enabled=True)
         log.info('OpenStack create tenant. email: {}, tenant_id: {}', email, tenant.id)
     else:
         log.info('Tenant for customer {} already exists. Tenant id: {}', email, tenant.id)
     return tenant
    def attached_flavors(self, tenant_id):
        os_flavors = self.get_nova_flavors(is_public=None)
        attached = {}
        for flavor in os_flavors:
            if not flavor.is_public:
                attached_tenants = self.client_nova.flavor_access.list(
                    flavor=flavor.id)
                tenant_attached = find_first(
                    attached_tenants, lambda x: x.tenant_id == tenant_id)
                if tenant_attached:
                    attached[flavor.name] = flavor

        return attached
Beispiel #8
0
def parse_interface(item, logical_system=None):
    iface = Interface()
    iface.name = item['name']
    iface.description = item.get('description')
    iface.vlantagging = 'vlan-tagging' in item or 'flexible-vlan-tagging' in item
    iface.unitdict = [parse_unit(u, logical_system) for u in item.get('unit', [])]
    iface.bundle = find_first('bundle', item) or None
    iface.tunneldict = [
        {
            'source': find('tunnel.source', u),
            'destination': find('tunnel.destination', u)
        } for u in item.get('unit', []) if 'tunnel' in u
    ]
    return iface
 def create_tenant(self, email, customer_id):
     """Create new tenant with default limits"""
     name = self.make_id(customer_id, email, "tenant")
     tenant = find_first(self.get_tenants(), lambda x: x.name == name)
     if tenant is None:
         tenant = self.client_keystone.tenants.create(
             tenant_name=name,
             description=self.make_description(name, email),
             enabled=True)
         log.info('OpenStack create tenant. email: {}, tenant_id: {}',
                  email, tenant.id)
     else:
         log.info('Tenant for customer {} already exists. Tenant id: {}',
                  email, tenant.id)
     return tenant
Beispiel #10
0
def get_tenants_and_users(keyword):
    id_pairs = []
    all_tenants = openstack.get_tenants()
    tenants = [tenant.id for tenant in all_tenants if keyword in tenant.description]
    for tenant in tenants:
        users = openstack.client_keystone.tenants.list_users(tenant)
        user = users[0].id if users else None
        id_pairs.append((user, tenant))

    all_users = openstack.client_keystone.users.list()
    users = [(user.id, None) for user in all_users
             if keyword in user.name and find_first(id_pairs, lambda x: x[0] == user.id) is None]
    id_pairs.extend(users)

    return id_pairs
Beispiel #11
0
    def create_tariff(self, params=None):
        messages = []
        can_modify = self.update

        immutable = params.pop('immutable', False)
        default = params.pop('default', False)
        parsed_services = []
        for service_id, price in params.pop('services', {}).items():
            flavor = self.get_flavor(service_id)
            if flavor:
                service_id = flavor['service_id']
            parsed_services.append({'service_id': service_id, 'price': price})
        params['services'] = parsed_services

        tariff_list = self.client.tariff.list(name=params['localized_name']['en'])
        if tariff_list['total'] == 0:
            tariff_info = self.client.tariff.create(**params)
            logbook.info('Tariff "{}" created with id={}'.format(tariff_info['localized_name']['en'],
                                                                 tariff_info['tariff_id']))
            can_modify = True
        else:
            tariff_info = tariff_list['items'][0]
            diff = self.compare_fields(tariff_info, params, ['localized_name', 'description', 'currency'])
            for service_config in params['services']:
                service_id, price = service_config['service_id'], str(service_config['price'])
                service = find_first(tariff_info['services'], lambda srv: service_id == srv['service']['service_id'])
                if not service:
                    messages.append('Service "{}" (id:{}) not in tariff service list'.format(
                        service['service']['localized_name']['en'], service_id))
                elif Decimal(price) != Decimal(service['price']):
                    diff.append(('service price ({})'.format(service['service']['localized_name']['en']),
                                 service['price'], price))

            if diff or messages:
                diff_str = self.diff_to_str('Tariff "{}" (id:{})'.format(tariff_info['localized_name']['en'],
                                                                         tariff_info['tariff_id']), diff)
                logbook.info(diff_str)
                for message in messages:
                    logbook.info(message)
                if not tariff_info['mutable']:
                    logbook.warning('Tariff is immutable')
                if tariff_info['mutable'] and self.update:
                    self.client.tariff.update(tariff_info['tariff_id'], **params)

        if immutable and can_modify:
            self.client.tariff.immutable(tariff_info['tariff_id'])
        if default and can_modify:
            self.client.tariff.set_default(tariff_info['tariff_id'])
Beispiel #12
0
    def create_user(self, email, customer_id, tenant_id, password, enabled=True):
        name = self.make_id(customer_id, email, "user")

        user = find_first(self.get_users(), lambda x: getattr(x, "email", None) == email and
                          getattr(x, "name", None) == name)
        if user is None:
            user = self.client_keystone.users.create(name=name,
                                                     password=password,
                                                     email=email,
                                                     tenant_id=tenant_id,
                                                     enabled=enabled)
            log.info('OpenStack create user. email: {}, user_id: {}, tenant_id: {}', email, user.id, tenant_id)
        else:
            log.info('User for email {} already exists: {}', email, user.id)
            self.client_keystone.users.update_password(user, password)
            user = self.client_keystone.users.update_tenant(user, tenant_id)
        return user
Beispiel #13
0
def get_tenants_and_users(keyword):
    id_pairs = []
    all_tenants = openstack.get_tenants()
    tenants = [
        tenant.id for tenant in all_tenants if keyword in tenant.description
    ]
    for tenant in tenants:
        users = openstack.client_keystone.tenants.list_users(tenant)
        user = users[0].id if users else None
        id_pairs.append((user, tenant))

    all_users = openstack.client_keystone.users.list()
    users = [(user.id, None) for user in all_users if keyword in user.name
             and find_first(id_pairs, lambda x: x[0] == user.id) is None]
    id_pairs.extend(users)

    return id_pairs
Beispiel #14
0
remove_method = 'amplitude'
remove_threshold = np.mean(thresholds)
remove_signal_n = 2

# Collect additional epochs to remove
EPOCHS_FILE = 'epochs_decoder'
td_epochs = load_data_from_folder(folders=DATA_PATH, files_name=EPOCHS_FILE)[0]

epochs_on = np.round(FS * (td_epochs['epochs_on'])).astype('int')
epochs_off = np.round(FS * (td_epochs['epochs_off'])).astype('int')
epochs = np.zeros(td[DATA_NAME[0]].shape)

if epochs_off[0] < epochs_on[0]:
    epochs[:epochs_off[0]] = 1
for epoch_on in epochs_on:
    if find_first(epoch_on, epochs_off) != None:
        epoch_off_next = epochs_off[find_first(epoch_on, epochs_off)]
        epochs[epoch_on:epoch_off_next] = 1
if epochs_on[-1] > epochs_off[-1]:
    epochs[epochs_on[-1]:] = 1

# Plot epochs
fig, axs = plt.subplots(len(td['params']['data']['data']['signals']),
                        1,
                        sharex=True)
for field, th, ax in zip(td['params']['data']['data']['signals'], thresholds,
                         axs):
    ax.plot(td[field], '-b')
    ax.axhline(th, color='r')
    ax.axhline(-th, color='r')
    ax.plot((epochs - 0.5) * 0.1, '-c', linewidth=0.5)