0

Hello I am very new to python language I am getting the following error , when running the test.py file

Traceback (most recent call last):

File "D:/Py Charm projects/Example/test.py", line 3, in <module>

repository = Repository(Config)

File "D:\Py Charm projects\Example\repository.py", line 29, in __init__

assert isinstance(config, Config)
AssertionError

test.py

from repository import Repository
from configuration import Config
repository = Repository(Config)
dataset, labels = repository.get_dataset_and_labels()

repository.py

import json
import re
import time
from random import random
from glob import glob
from os.path import basename, dirname
from os.path import sep as path_sep
from os.path import join as path_join
from datetime import datetime

from numpy import inf, array
from pandas import DataFrame
from configuration import Config

__author__ = 'luis'
methods = []


def method_list(method):
if method not in methods:
    methods.append(method)

return method


class Repository(object):
def __init__(self, config):
    self.expert_locations = {}
    assert isinstance(config, Config)
    self.config = config
    self.base_path = config.base_path
    self.config_path = config.config_path
    self.sample_path = config.path_user_sample
    self.expert_data_path = config.expert_data_path
    self.users = []
    self.access_points = {}
    self.locations = {}
    self.goal_types = {}
    self.goals = {}
    self.goals_timetable = {}
    self.samples = {}
    self.expert_samples = {}
    self.expert_validation_samples = {}
    self.fingerprints = {}
    for m in methods:
        m(self)

@method_list
def load_users(self):
    user_file = "%s/users.json" % self.config_path
    with open(user_file) as fd:
        jf = json.load(fd)

    for u in jf:
        self.users.append(u['user_id'])

@method_list
def load_locations(self):
    user_file = "%s/goallist.json" % self.config_path
    with open(user_file) as fd:
        jf = json.load(fd)

    for loc in jf:
        self.locations.setdefault((loc['latitude'], loc['longitude']), set())

@method_list
def load_goals(self):
    file_list = glob('%s/Goals/Goals*/*.json' % self.config_path)
    for f in file_list:
        goal_type = re.match(".*_([a-z]*)", dirname(f)).group(1)
        time_name = basename(f).split(".")[0]
        ts = time.mktime(time.strptime(time_name, "%Y-%m-%d_%H-%M-%S"))
        with open(f) as fd:
            jf = json.load(fd)

        for loc in jf:
            priority = self.goals.setdefault(goal_type, {}).setdefault(ts, {})
            priority[(loc['location']['latitude'], loc['location']['longitude'])] = loc['priority']

    self.goal_types = dict([x[::-1] for x in enumerate(self.goals)])

@method_list
def load_expert_data(self):
    file_list = glob("%s/%s" % (self.expert_data_path, self.config.expert_data_glob_pattern))
    for f in file_list:
        user = re.match(".*StampRally_([a-z]*)/", dirname(f)).group(1)
        time_name = basename(f).split(".")[0]
        ts = time.mktime(time.strptime(time_name, "%Y-%m-%d_%H-%M-%S"))
        with open(f) as fd:
            jf = json.load(fd)

        for fingerprint in jf:
            loc = (fingerprint['location']['latitude'], fingerprint['location']['longitude'])
            for mac, v in fingerprint['fingerprint']['wifi'].items():
                # self.access_points.setdefault(mac, set()).add(loc)
                self.expert_locations.setdefault(loc, set()).add(mac)
                self.expert_samples.setdefault(ts, {}).setdefault(user, {}).setdefault(loc, []).append(
                    {'level': v["level"], 'mac': mac})

@method_list
def load_expert_data_test(self):
    file_list = glob("%s/%s" % (self.expert_data_path, self.config.expert_data_test_glob_pattern))
    for f in file_list:
        user = re.match(".*StampRally_([a-z]*)/", dirname(f)).group(1)
        time_name = basename(f).split(".")[0]
        ts = time.mktime(time.strptime(time_name, "%Y-%m-%d_%H-%M-%S"))
        with open(f) as fd:
            jf = json.load(fd)

        for fingerprint in jf:
            loc = (fingerprint['location']['latitude'], fingerprint['location']['longitude'])
            for mac, v in fingerprint['fingerprint'].items():
                # self.access_points.setdefault(mac, set()).add(loc)
                self.expert_locations.setdefault(loc, set()).add(mac)
                self.expert_validation_samples.setdefault(ts, {}).setdefault(user, {}).setdefault(loc, []).append(
                    {'level': v["level"], 'mac': mac})

@method_list
def load_goals_timetable(self):
    file_list = glob('%s/Goals/Users/*.json' % self.config_path)
    for f in file_list:
        time_name = basename(f).split(".")[0]
        ts = time.mktime(time.strptime(time_name, "%Y-%m-%d_%H-%M-%S"))
        with open(f) as fd:
            jf = json.load(fd)

        try:
            goal_type = jf.values()[0]
        except IndexError:
            continue

        self.goals_timetable.setdefault(ts, []).append(self.goal_types[goal_type])

@method_list
def load_samples(self):
    file_list = glob('%s/StampRally_*/%s/*.json' % (self.config.data_set_path, self.sample_path))
    for f in file_list:
        user = re.match(".*StampRally_([em][0-9])/", dirname(f)).group(1)
        time_name = basename(f).split(".")[0]
        ts = time.mktime(time.strptime(time_name, "%Y-%m-%d_%H-%M-%S"))
        with open(f) as fd:
            jf = json.load(fd)

        for fingerprint in jf:
            loc = (fingerprint['location']['latitude'], fingerprint['location']['longitude'])
            for mac, v in fingerprint['fingerprint']['wifi'].items():
                self.access_points.setdefault(mac, set()).add(loc)
                self.locations[loc].add(mac)
                self.samples.setdefault(ts, {}).setdefault(user, {}).setdefault(loc, []).append(
                    {'level': v["level"], 'mac': mac})

def iter_samples(self, time_filter=None, user_filter=None, loc_filter=None):
    time_filter = time_filter and time_filter or (0., inf)
    user_filter = user_filter and re.compile(user_filter) or re.compile(".*")
    for ts, users in filter(lambda x: time_filter[0] <= x[0] <= time_filter[1], self.samples.items()):
        for u, locations in filter(lambda x: user_filter.search(x[0]), users.items()):
            for loc, fingerprints in filter(lambda x: not loc_filter and True or loc_filter == x[0],
                                            locations.items()):
                for fingerprint in fingerprints:
                    mac = fingerprint['mac']
                    value = fingerprint['level']
                    yield [ts, u, loc, mac, value]

def iter_fingerprints(self, time_filter=None, user_filter=None, loc_filter=None, group_filter=None, src='samples'):
    time_filter = time_filter and time_filter or (0., inf)
    user_filter = user_filter and re.compile(user_filter) or re.compile(".*")
    src = getattr(self, src)
    for ts, users in src.items():
        if not (time_filter[0] <= ts <= time_filter[1]):
            continue

        for u, locations in users.items():
            if not user_filter.search(u):
                continue

            for loc, fingerprints in locations.items():
                if loc_filter and loc_filter != loc:
                    continue

                if group_filter:
                    try:
                        fps = [fp for fp in fingerprints if fp['mac'] in group_filter[loc]]
                    except TypeError:
                        fps = [fp for fp in fingerprints if fp['mac'] in group_filter]
                    except KeyError:
                        fps = fingerprints

                    yield [ts, u, loc, fps]
                    continue

                yield [ts, u, loc, fingerprints]

def get_dataset_and_labels(self, columns=None, **kwargs):
    df = []
    labels = []
    for ts, usr, loc, fingerprint in self.iter_fingerprints(**kwargs):
        fp = dict([(m['mac'], m['level']) for m in fingerprint])
        df.append(fp)
        labels.append(loc)

    return DataFrame(df, columns=columns), array(labels)

def create_time_series(self, time_filter=None, user_filter=None, sample_time=70):
    time_filter = time_filter and time_filter or (0., inf)
    user_filter = user_filter and re.compile(user_filter) or re.compile(".*")
    time_series = []
    for ts, users in filter(lambda x: time_filter[0] <= x[0] <= time_filter[1], self.samples.items()):
        lts = ts
        for u, locations in filter(lambda x: user_filter.search(x[0]), users.items()):
            for loc, fingerprints in locations.items():
                for fingerprint in fingerprints:
                    mac = fingerprint['mac']
                    lts += random()
                    time_series.append([lts, mac])

            lts += sample_time

    return time_series

@method_list
def update_week_specs(self):
    week_stats = {}
    for ts in self.samples:
        date = datetime.fromtimestamp(ts)
        week = date.strftime("%W")
        week_stats.setdefault(week, []).append(ts)

    self.config.week_specs =  dict([("W%s" % w, (min(v), max(v))) for w, v in week_stats.items()])
    self.config.week_names = sorted(self.config.week_specs.keys(), key=lambda x: self.config.week_specs[x][0])

configuration.py

from os.path import sep as path_sep
from os.path import join as path_join


class Config(object):
base_path = "."
date_format = "%Y-%m-%d_%H-%M-%S"
recording_start = 1390202100.0
stages = []
var_fill = -200.
knn_var_fill = -90.
week_specs = {"W03": (1390202100.6, 1390589153.57),
              "W04": (1390806000.32, 1391193208.91),
              "W05": (1391410800.65, 1391797033.75),
              "W06": (1392015600.01, 1392398212.78)}
week_names = sorted(week_specs.keys())
week_for_evaluation = week_names
path_user_sample=path_join("Uploads", "TrainData")
expert_data_glob_pattern = path_join("StampRally_*", "Uploads", "TrainData",        "*.json")
expert_data_test_glob_pattern = path_join("StampRally_*", "ValidationData", "*.json")
k = 50

@property
def data_set_path(self):
    return path_join(self.base_path, "SCSUT2014v1", "EXP201312")

@property
def expert_data_path(self):
    return path_join(self.base_path, "SCSUT2014v1", "TESTDATA") 

@property
def config_path(self):
    return path_join(self.data_set_path, "StampRally")

@property
def results_path(self):
    return path_join(self.base_path, "results")

@property
def rules_path(self):
    return path_join(self.results_path, 'rules')

@property
def unified_time_series_path(self):
    return path_join(self.results_path, 'timeseries') 

@property
def titarl_configs_path(self):
    return path_join(self.base_path, 'titarl_cfg')

@property
def rule_learning_template(self):
    return path_join(self.base_path, "learning_config_e0.xml")

@property
def learn_all_file(self):
    return path_join(self.base_path, "learn_all.bat")

@property
def log_path(self):
    return path_join(self.base_path, "logs")

@property
def trees_path(self):
    return path_join(self.results_path, "trees")


config = Config()

paths = []
for attrib in dir(config):
  if "_path" in attrib:
    paths.append(attrib)

for path in [config.titarl_configs_path, config.unified_time_series_path,   config.rules_path, config.trees_path]:
paths.append("%s%squarterly" % (path, path_sep))

config.paths = paths

Please help me I don't know what the error is and I have searched it in Google as well but didn't get any solution to this error , and what kind of error is this I can't understand , so please help I am stuck in here, as I am very new to python language.

2 Answers 2

2

In:

repository = Repository(Config)

you're passing the Config class itself instead of an instance of that class.

Change that to:

config = Config()
# ... set up config here ...
repository = Repository(config)
Sign up to request clarification or add additional context in comments.

Comments

0

In your test.py file try to change these two line of code:

from configuration import Config
repository = Repository(Config)

like this:

from configuration import config
repository = Repository(config)

5 Comments

@AnasReza, I've recreated this part of your project on my local computer and after this fix the code doesn't throw an AssertionError. Maybe there is some other exception? Can you provide an output?
Traceback (most recent call last): File "D:/Py Charm projects/Example/test.py", line 3, in <module> repository = Repository(config) File "D:\Py Charm projects\Example\repository.py", line 46, in init m(self) File "D:\Py Charm projects\Example\repository.py", line 86, in load_expert_data user = re.match(".*StampRally_([a-z]*)/", dirname(f)).group(1) AttributeError: 'NoneType' object has no attribute 'group'
this is the error i got after implementing your solution
@AnasReza, as I thought, this is something completely different. The problem is that regex at repository.py:86 can't find any occurences of .*StampRally_([a-z]*)/ in dirname of some file f (dirname(f)). I don't think it's possible to help you without having a tree of all the project's directories and files. You can try to create another question with this new problem and all the necessary info, because this problem with AssertionError is actually solved now and you are one step closer to the solution.
@AnasReza, I think this problem is not related to the code. Probably it's related to your project's files or lack of them. Or maybe wrong files/directories naming. Can't say without any additional information about what this code should do and what input data (files) does it receive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.