示例#1
0
    def train(self, datasetdscr, data, acts):
        self.gacts = self.groupize(datasetdscr, acts)
        self.acts = acts
        self.strategies = {}
        self.acts_name = {}
        train_results = {}
        self.train_quality = {}

        intree = IntervalTree()

        # intree = IntervalTree()
        for indx, tacts in enumerate(self.gacts):
            logger.info("=======================working on activties " +
                        tacts.__str__() + "=========")
            Tdata = self.justifySet(tacts, data, False)

            self.acts_name[indx] = datasetdscr.activities[tacts]
            self.strategies[indx] = ml_strategy.Simple.SimpleStrategy()
            self.strategies[indx].train(datasetdscr, Tdata,
                                        list(range(len(tacts))))
            if ('result' in self.strategies[indx].bestOpt.result):
                result = self.strategies[indx].bestOpt.result['result']
            else:
                result = self.strategies[indx].test(Tdata)

            utils.saveState(self.strategies[indx].get_info(), 'groupact',
                            str(indx))

            train_results[indx] = result
        utils.saveState(
            [self.strategies[indx].get_info() for indx in self.strategies],
            'groupact', 'all')
        return self.fusion(train_results, data.a_events, True)
示例#2
0
    def train(self, datasetdscr, data, acts):
        self.gacts = self.groupize(datasetdscr, acts)
        self.acts = acts
        self.strategies = {}
        self.acts_name = {}
        train_results = {}
        self.train_quality = {}

        intree = IntervalTree()

        # intree = IntervalTree()
        for indx, tacts in enumerate(self.gacts):
            logger.info("=======================working on activties " +
                        tacts.__str__() + "=========")
            weight = np.ones(len(acts))
            for a in tacts:
                weight[a] = self.alpha
            datasetdscr.indx = indx
            self.strategies[indx] = ml_strategy.Simple.SimpleStrategy()
            self.strategies[indx].train(datasetdscr, data, acts, weight)
            if ('result' in self.strategies[indx].bestOpt.result):
                result = self.strategies[indx].bestOpt.result['result']
            else:
                result = self.strategies[indx].test(data)

            utils.saveState(self.strategies[indx].get_info(), 'wgroupact',
                            'n-%d' % (indx))

            train_results[indx] = result
        utils.saveState(
            [self.strategies[indx].get_info() for indx in self.strategies],
            'wgroupact', 'n-all')
        return self.fusion(train_results, data.a_events, True)
示例#3
0
def run(args):
    logger = logging.getLogger(__file__)
    logger.debug(f'args={args}')
    
    if(args.dataset<0):
        logger.error('Invalid dataset argument')
        return
    
    if(args.mlstrategy<0):
        logger.error('Invalid mlstrategy argument')
        return
        
    if(args.evaluation<0):
        logger.error('Invalid evaluation argument')
        return
    datasetdscr = methods.dataset[args.dataset]['method']().load()
    strategy = methods.mlstrategy[args.mlstrategy]['method']()
    evaluation=methods.evaluation[args.evaluation]['method']()
    
    if(args.feature_extraction>=0): methods.feature_extraction=[methods.feature_extraction[args.feature_extraction]]
    if(args.segmentation>=0): methods.segmentation=[methods.segmentation[args.segmentation]]
    if(args.classifier>=0): methods.classifier=[methods.classifier[args.classifier]]
    
    evalres = evaluation.evaluate(datasetdscr, strategy)

    run_date=datetime.now().strftime('%y%m%d_%H-%M-%S')
    run_info={'dataset':datasetdscr.shortname(),'run_date':run_date,'dataset_path':datasetdscr.data_path, 'strategy':strategy.shortname(),'evalution':evaluation.shortname()}
    compressdata={'run_info':run_info, 'folds':{k:{'quality':evalres[k]['test'].quality,'runname':evalres[k]['test'].shortrunname} for k in evalres}}
    
    utils.saveState([compressdata],'%s-%s/' % (run_date,datasetdscr.shortname()),'info')
    utils.saveState([run_info,datasetdscr, evalres],'%s-%s/' % (run_date,datasetdscr.shortname()))
    for i in range(len(evalres)):
        logger.debug(f'Evalution quality fold={i} is {evalres[i]["test"].quality}')

    logger.debug(f'run finished args={args}')
示例#4
0
def load_run_table(file):
    runtable=utils.loadState(file,'runtable1',raiseException=False)
    if(runtable is None):
        res=utils.loadState(file)
        if(len(res)!=3):
            #raise Error
            logger.warn('File %s can not import'%file)
            return
        [run_info,datasetdscr,evalres]=res
        runtable=getRunTable(run_info,datasetdscr,evalres)
        utils.saveState(runtable,file,'runtable1')
    return runtable
示例#5
0
def load_run_info(file):
    runinfo=utils.loadState(file,'info',raiseException=False)
    if(runinfo is None):
        try:
            res=utils.loadState(file)
            if(len(res)!=3):
                #raise Error
                logger.warn('File %s can not import'%file)
                return
            [run_info,datasetdscr,evalres]=res
            runinfo=getRunInfo(run_info,datasetdscr,evalres)
            utils.saveState([runinfo],file,'info')
            return runinfo
        except:
            logger.warn('File %s can not import'%file)
            return
    return runinfo[0]
示例#6
0
def get(key, valf):
    hkey = hashkey(key)
    try:
        val = utils.loadState(cachefolder, hkey)
        logger.debug(f'cached file found {key} {hkey}')
    except Exception as e:

        if not (os.path.exists(f'save_data/{hkey}')):
            logger.debug(f'cached file not found {key} {hkey}')
        else:
            logger.error(f'error in cached {e}', exc_info=True)

        val = valf()
        utils.saveState(val, cachefolder, hkey)
        with open(f'save_data/{cachefolder}/{hkey}.txt', 'w') as f:
            print(key, file=f)
            f.close()

    return val
示例#7
0
def create(real, pred, filename):

    evalres = [{}]
    evalres[0]['test'] = Data('test res')
    evalres[0]['test'].real_events = vs.convert2event(real)
    evalres[0]['test'].pred_events = vs.convert2event(pred)
    evalres[0]['test'].quality = {}

    dataset = Data('MyDataset')
    dataset.activities = ['None', 'Act']
    dataset.activity_events = evalres[0]['test'].real_events
    dataset.activities_map_inverse = {
        k: v
        for v, k in enumerate(dataset.activities)
    }
    dataset.activities_map = {v: k for v, k in enumerate(dataset.activities)}
    dataset.sensor_events = pd.DataFrame()
    runinfo = filename

    utils.saveState([runinfo, dataset, evalres], filename)