<commit_before># -*- coding: utf-8 -*-
from openerp import fields, models, api
import datetime
import math

def sigmoid(t, c, r, e, down):
    return 1*down + (1 - 2*down)/(1 + math.pow(e, -(t-c)/r))

def expdec(t, c, r, e):
    return 1 - math.pow(e, -(t+c)/r)

def powinc(t, c, r, e):
    if t > c:
        return math.pow((t-c), e) / math.pow(r, e)
    else:
        return 0

class Mqo_exArr():
    exercise_id = 0
    suitability = 0
    score = 0
    
    def __init__(self, exercise_id, suitability, score):
        self.exercise_id = exercise_id
        self.suitability = suitability
        self.score = score
        self.discount = score
        self.bst = 0
        self.bur = 0
        self.tper = 0
        self.pre = 0
 
    def mod_Score(self, assignment):
        # modify score based on an recent assignment
        elapsed_timedelta = datetime.datetime.now() - fields.Datetime.from_string(assignment.datetime_allocated)
        # elapsed_timedelta_datum = datetime.datetime.now() # - some date
        t = elapsed_timedelta.days
        a = assignment.exercise_id
        d_sig = 1 - a.sig_m*sigmoid(t, a.sig_c, a.sig_r, a.sig_e, True)
        d_exp = max(1 - a.exp_m*(1 - expdec(t, a.exp_c, a.exp_r, a.exp_e)), 0)
        self.discount = self.discount * d_sig * d_exp
        
        # need to do oldest to newest for this
        self.bur = a.bur_m*(sigmoid(t, a.bur_c, a.bur_r, a.bur_e, True) - sigmoid(t, a.bur_c2, a.bur_r, a.bur_e, True))

    def mod_bst(self, bstex, assignment):
        # modify score based on an recent assignment
        elapsed_timedelta = datetime.datetime.now() - fields.Datetime.from_string(assignment.datetime_allocated)
        # elapsed_timedelta_datum = datetime.datetime.now() # - some date
        t = elapsed_timedelta.days
        bstmag = bstex.sig_m*sigmoid(t, bstex.sig_c, bstex.sig_r, bstex.sig_e, True)
        self.bst = self.bst + bstmag


    def mod_tper(self, ex):
        # modify score based on datetime
        for per in ex.tper_ids:
            currenttime = datetime.datetime.now()
            tper_start =  fields.Datetime.from_string(per.start_date)
            tper_end =  fields.Datetime.from_string(per.end_date)
            if per.annual:
                tper_start.replace(year=currenttime.year)
                tper_end.replace(year=currenttime.year)
                if (tper_end - tper_start) > 0:
                    tper_end.replace(year=currenttime.year+1)
            if currenttime >= tper_start and currenttime < tper_end: 
                self.tper = self.tper + per.mag


        
    def mod_pre(self, pre, assignment):
        # modify score based on an recent assignment
        elapsed_timedelta = datetime.datetime.now() - fields.Datetime.from_string(assignment.datetime_allocated)
        # elapsed_timedelta_datum = datetime.datetime.now() # - some date
        t = elapsed_timedelta.days
        premag = pre.pow_m*powinc(t, pre.pow_c, pre.pow_r, pre.pow_e, True)
        self.pre = self.pre + premag



class Partner(models.Model):
    _inherit = 'res.partner'

    allocation_ids = fields.One2many('mqo.allocation', 'partner_id', string="Allocated Exercises")
    assignment_ids = fields.One2many('mqo.assignment', 'partner_id', string="Assigned Exercises")
    
    next_exercise_id = fields.Many2one(comodel_name='mqo.exercise', store=True, readonly=True, compute='_compute_next_exercise', string="Next Exercise")
    
    # @api.depends('allocation_ids', 'assignment_ids')
    def _compute_next_exercise(self):
        for r in self:
            # set no exercise by default in case no exercises are allocated
            next_exercise_collection = []
            r.next_exercise_id = next_exercise_collection 
            if r.allocation_ids:
                # set to first allocated exercise by default in case no exercise assigned 
                next_exercise_collection = r.allocation_ids[0].exercise_id
                # set up exArr, which contains a list of all the allocated exercises and their allocation scores.
                exArr = []
                suitabilities = []
                exDic = dict()
                for i, allocation in enumerate(r.allocation_ids):
                    mqo_exArr = Mqo_exArr(allocation.exercise_id.id, allocation.suitability, allocation.suitability)
                    exArr.append(mqo_exArr)
                    exDic[allocation.exercise_id.id] = i
                    if allocation.exercise_id.tper_ids:
                        exArr[exDic[allocation.exercise_id.id]].mod_tper(allocation.exercise_id)
                    # if allocation.exercise_id.pre:
                    # maybe build record of prerequisite exercises, and set initial pre values. Then if find a prerequisite ex, modify the pre values accordingly.
                # now adjust score if there are assignments
                if r.assignment_ids:
                    for assignment in r.assignment_ids.sorted(key=lambda rec: rec.create_date):
                        exArr[exDic[assignment.exercise_id.id]].mod_Score(assignment)
                        if assignment.exercise_id.bstex_ids:
                            for bstex in assignment.exercise_id.bstex_ids:<commit_msg>Minor refactoring of _compute_next_exercise.<commit_after># -*- coding: utf-8 -*-
from openerp import fields, models, api
import datetime
import math

def sigmoid(t, c, r, e, down):
    return 1*down + (1 - 2*down)/(1 + math.pow(e, -(t-c)/r))

def expdec(t, c, r, e):
    return 1 - math.pow(e, -(t+c)/r)

def powinc(t, c, r, e):
    if t > c:
        return math.pow((t-c), e) / math.pow(r, e)
    else:
        return 0

class Mqo_exArr():
    exercise_id = 0
    suitability = 0
    score = 0
    
    def __init__(self, exercise_id, suitability, score):
        self.exercise_id = exercise_id
        self.suitability = suitability
        self.score = score
        self.discount = score
        self.bst = 0
        self.bur = 0
        self.tper = 0
        self.pre = 0
 
    def mod_Score(self, assignment):
        # modify score based on an recent assignment
        elapsed_timedelta = datetime.datetime.now() - fields.Datetime.from_string(assignment.datetime_allocated)
        # elapsed_timedelta_datum = datetime.datetime.now() # - some date
        t = elapsed_timedelta.days
        a = assignment.exercise_id
        d_sig = 1 - a.sig_m*sigmoid(t, a.sig_c, a.sig_r, a.sig_e, True)
        d_exp = max(1 - a.exp_m*(1 - expdec(t, a.exp_c, a.exp_r, a.exp_e)), 0)
        self.discount = self.discount * d_sig * d_exp
        
        # need to do oldest to newest for this
        self.bur = a.bur_m*(sigmoid(t, a.bur_c, a.bur_r, a.bur_e, True) - sigmoid(t, a.bur_c2, a.bur_r, a.bur_e, True))

    def mod_bst(self, bstex, assignment):
        # modify score based on an recent assignment
        elapsed_timedelta = datetime.datetime.now() - fields.Datetime.from_string(assignment.datetime_allocated)
        # elapsed_timedelta_datum = datetime.datetime.now() # - some date
        t = elapsed_timedelta.days
        bstmag = bstex.sig_m*sigmoid(t, bstex.sig_c, bstex.sig_r, bstex.sig_e, True)
        self.bst = self.bst + bstmag


    def mod_tper(self, ex):
        # modify score based on datetime
        for per in ex.tper_ids:
            currenttime = datetime.datetime.now()
            tper_start =  fields.Datetime.from_string(per.start_date)
            tper_end =  fields.Datetime.from_string(per.end_date)
            if per.annual:
                tper_start.replace(year=currenttime.year)
                tper_end.replace(year=currenttime.year)
                if (tper_end - tper_start) > 0:
                    tper_end.replace(year=currenttime.year+1)
            if currenttime >= tper_start and currenttime < tper_end: 
                self.tper = self.tper + per.mag


        
    def mod_pre(self, pre, assignment):
        # modify score based on an recent assignment
        elapsed_timedelta = datetime.datetime.now() - fields.Datetime.from_string(assignment.datetime_allocated)
        # elapsed_timedelta_datum = datetime.datetime.now() # - some date
        t = elapsed_timedelta.days
        premag = pre.pow_m*powinc(t, pre.pow_c, pre.pow_r, pre.pow_e, True)
        self.pre = self.pre + premag



class Partner(models.Model):
    _inherit = 'res.partner'

    allocation_ids = fields.One2many('mqo.allocation', 'partner_id', string="Allocated Exercises")
    assignment_ids = fields.One2many('mqo.assignment', 'partner_id', string="Assigned Exercises")
    
    next_exercise_id = fields.Many2one(comodel_name='mqo.exercise', store=True, readonly=True, compute='_compute_next_exercise', string="Next Exercise")
    
    # @api.depends('allocation_ids', 'assignment_ids')
    def _compute_next_exercise(self):
        for r in self:
            # set no exercise by default in case no exercises are allocated
            next_exercise_collection = []
            r.next_exercise_id = next_exercise_collection 
            if r.allocation_ids:
                # set to first allocated exercise by default in case no exercise assigned 
                next_exercise_collection = r.allocation_ids[0].exercise_id
                # set up exArr, which contains a list of all the allocated exercises and their allocation scores.
                exArr = []
                suitabilities = []
                exDic = dict()
                for i, allocation in enumerate(r.allocation_ids):
                    ex = allocation.exercise_id
                    mqo_exArr = Mqo_exArr(ex.id, allocation.suitability, allocation.suitability)
                    exArr.append(mqo_exArr)
                    exDic[ex.id] = i
                    if ex.tper_ids:
                        exArr[exDic[ex.id]].mod_tper(ex)
                    if allocation.exercise_id.pre_ids:
                        exArr[exDic[ex.id]].pre = -10
                    # maybe build record of prerequisite exercises, and set initial pre values. Then if find a prerequisite ex, modify the pre values accordingly.
                # now adjust score if there are assignments
                if r.assignment_ids:
                    for assignment in r.assignment_ids.sorted(key=lambda rec: rec.create_date):
                        ex = assignment.exercise_id
                        exArr[exDic[ex.id]].mod_Score(assignment)
                        if ex.bstex_ids:
                            for bstex in ex.bstex_ids:
{'fraction_non_alphanumeric': 0.0968267663694756, 'fraction_numerical': 0.004985140446745279, 'mean_word_length': 2.1824283099450885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3374288', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3219, 'n_words': 752}