<commit_before># encoding: latin2

"""
Objective functions
"""
__author__ = "<NAME>"
__credits__ = "Copyright (c) 2009-11 <NAME>"
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "RiSE Group"
__email__ = "<EMAIL>"

from distanceFunctions import distMethods

def getObjectiveFunctionSumSquares(regionMaker,
                                   region2AreaDict,
                                   indexData=[]):
    """
    Sum of squares from each area to the region's centroid
    """
    objDict = {}
    for region in region2AreaDict.keys():
        objDict[region] = 0.0
        areasIdsIn = region2AreaDict[region]
        areasInNow = [regionMaker.areas[aID] for aID in areasIdsIn]
        dataAvg = regionMaker.am.getDataAverage(areasIdsIn, indexData)
        c = 1
        for area in areasInNow:
            areaData = []
            for index in indexData:
                areaData += [area.data[index]]
            data = [areaData] + [dataAvg]
            areaDistance = distMethods[regionMaker.distanceType](data)
            objDict[region] += areaDistance[0][0]
    return sum(objDict.values())

cachedObj = {}
def getObjectiveFunctionSumSquaresFast(regionMaker,
                                       region2AreaDict,
                                       modifiedRegions,
                                       indexData=[]):
    """
    Sum of squares from each area to the region's centroid
    """
    obj = 0.0
    r2aDictKeys = region2AreaDict.keys()
    for region in r2aDictKeys:
        if region in modifiedRegions:
            valRegion = 0.0
            areasIdsIn = region2AreaDict[region]
            key = areasIdsIn
            key.sort()
            key = tuple(key)
            if cachedObj.has_key(key):
                valRegion = cachedObj[key]
            else:
                areasInNow = [regionMaker.areas[_aid] for _aid in areasIdsIn]
                dataAvg = regionMaker.am.getDataAverage(areasIdsIn, indexData)

                for area in areasInNow:
                    areaData = []
                    areaDataList = area.data
                    for index in indexData:
                        areaData.append(areaDataList[index])
                    areaData = [areaData] + [dataAvg]
                    # Taking the first element from the dataDistance
                    dist = distMethods[regionMaker.distanceType](areaData)[0][0]
                    valRegion += dist

                cachedObj[key] = valRegion
            obj += valRegion
        else:
            obj += regionMaker.objDict[region]
    return obj

objectiveFunctionTypeDispatcher = {}
objectiveFunctionTypeDispatcher["SS"] = getObjectiveFunctionSumSquares
objectiveFunctionTypeDispatcher["SSf"] = getObjectiveFunctionSumSquaresFast

def makeObjDict(regionMaker, indexData=[]):
    """
    constructs a dictionary with the objective function per region
    """
    objDict = {}<commit_msg>Add new method for objective function: Clique<commit_after># encoding: latin2

"""
Objective functions
"""
__author__ = "<NAME>"
__credits__ = "Copyright (c) 2009-11 <NAME>"
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "RiSE Group"
__email__ = "<EMAIL>"

from distanceFunctions import distMethods
import numpy as np

def getObjectiveFunctionSumSquares(regionMaker,
                                   region2AreaDict,
                                   indexData=[]):
    """
    Sum of squares from each area to the region's centroid
    """
    objDict = {}
    for region in region2AreaDict.keys():
        objDict[region] = 0.0
        areasIdsIn = region2AreaDict[region]
        areasInNow = [regionMaker.areas[aID] for aID in areasIdsIn]
        dataAvg = regionMaker.am.getDataAverage(areasIdsIn, indexData)
        c = 1
        for area in areasInNow:
            areaData = []
            for index in indexData:
                areaData += [area.data[index]]
            data = [areaData] + [dataAvg]
            areaDistance = distMethods[regionMaker.distanceType](data)
            objDict[region] += areaDistance[0][0]
    return sum(objDict.values())

cachedObj = {}
def getObjectiveFunctionSumSquaresFast(regionMaker,
                                       region2AreaDict,
                                       modifiedRegions,
                                       indexData=[]):
    """
    Sum of squares from each area to the region's centroid
    """
    obj = 0.0
    r2aDictKeys = region2AreaDict.keys()
    for region in r2aDictKeys:
        if region in modifiedRegions:
            valRegion = 0.0
            areasIdsIn = region2AreaDict[region]
            key = areasIdsIn
            key.sort()
            key = tuple(key)
            if cachedObj.has_key(key):
                valRegion = cachedObj[key]
            else:
                areasInNow = [regionMaker.areas[_aid] for _aid in areasIdsIn]
                dataAvg = regionMaker.am.getDataAverage(areasIdsIn, indexData)

                for area in areasInNow:
                    areaData = []
                    areaDataList = area.data
                    for index in indexData:
                        areaData.append(areaDataList[index])
                    areaData = [areaData] + [dataAvg]
                    # Taking the first element from the dataDistance
                    dist = distMethods[regionMaker.distanceType](areaData)[0][0]
                    valRegion += dist

                cachedObj[key] = valRegion
            obj += valRegion
        else:
            obj += regionMaker.objDict[region]
    return obj

def getObjectiveFunctionClique(regionMaker, *args):
    """
    Objective function computed with the distance between all the areas
    in a region (Clique).
    """
    ofuncval = 0.0
    for region in regionMaker.region2Area.values():
        size = len(region)
        distmatrix = np.zeros((size, size))

        for iti in xrange(size):
            areaid = region[iti]
            areai = np.array(regionMaker.areas[areaid].data)
            for itj in xrange(size):
                if iti < itj:
                    areaid = region[itj]
                    areaj = np.array(regionMaker.areas[areaid].data)
                    distmatrix[iti][itj] = np.linalg.norm(areai - areaj) ** 2

        ofuncval += distmatrix.sum()
    return ofuncval

objectiveFunctionTypeDispatcher = {}
objectiveFunctionTypeDispatcher["SS"] = getObjectiveFunctionSumSquares
objectiveFunctionTypeDispatcher["SSf"] = getObjectiveFunctionSumSquaresFast
objectiveFunctionTypeDispatcher["complete"] = getObjectiveFunctionClique

def makeObjDict(regionMaker, indexData=[]):
    """
    constructs a dictionary with the objective function per region
    """
    objDict = {}
{'fraction_non_alphanumeric': 0.08663292646621018, 'fraction_numerical': 0.00922893718368562, 'mean_word_length': 1.9149674620390456, 'pattern_counts': {'":': 0, '<': 10, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4891685', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1818, 'n_words': 433}