src.fairreckitlib.data.ratings.count

This module contains counting functionality.

Both APC and ALC can be used in the Kullback-Leibler formula, for which the module is also in this package. As of now, they don't specifically count the 'artist' play and listen count, but just whatever is in the 'item' column. If you pass a dataframe for which the 'item' column contains artists, it should work correctly, otherwise it will require changes to work properly.

Functions:

get_item_dict: return dict with unique items.
calculate_apc: count the artist play count.
calculate_alc: count the artist listener count.

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

 1"""This module contains counting functionality.
 2
 3Both APC and ALC can be used in the Kullback-Leibler formula,
 4for which the module is also in this package.
 5As of now, they don't specifically count the 'artist'
 6play and listen count, but just whatever is in the 'item' column.
 7If you pass a dataframe for which the 'item' column contains artists,
 8it should work correctly, otherwise it will require changes to work properly.
 9
10Functions:
11
12    get_item_dict: return dict with unique items.
13    calculate_apc: count the artist play count.
14    calculate_alc: count the artist listener count.
15
16This program has been developed by students from the bachelor Computer Science at
17Utrecht University within the Software Project course.
18© Copyright Utrecht University (Department of Information and Computing Sciences)
19"""
20
21from typing import Dict
22import pandas as pd
23
24
25def get_item_dict(dataframe: pd.DataFrame) -> Dict[int, float]:
26    """Make a dict with all unique items from the dataframe and starting value 0.
27
28    Args:
29        dataframe: the dataframe from which the items are taken.
30
31    Returns:
32        a dictionary with unique items as keys and 0 as values.
33    """
34    result = {}
35    for item in dataframe['item'].unique():
36        result[item] = 0
37    return result
38
39
40def calculate_apc(dataframe: pd.DataFrame) -> Dict[int, float]:
41    """Sum up the total artist play count (apc).
42
43    Used in the Kullback-Leibler formula for converting ratings.
44
45    Args:
46        dataframe with an item and rating header.
47
48    Returns:
49        a dictionary with key:item, value:apc.
50    """
51    apc = get_item_dict(dataframe)
52
53    for _, row in dataframe.iterrows():
54        apc[row['item']] += row['rating']
55
56    return apc
57
58
59def calculate_alc(dataframe: pd.DataFrame) -> Dict[int, float]:
60    """Sum up the total artist listener count (alc).
61
62    Used in the Kullback-Leibler formula for converting ratings.
63
64    Args:
65        dataframe with an item and rating header.
66
67    Returns:
68        a dictionary with key:item, value:alc.
69    """
70    alc = get_item_dict(dataframe)
71
72    for _, row in dataframe.iterrows():
73        alc[row['item']] += 1
74
75    return alc
def get_item_dict(dataframe: pandas.core.frame.DataFrame) -> Dict[int, float]:
26def get_item_dict(dataframe: pd.DataFrame) -> Dict[int, float]:
27    """Make a dict with all unique items from the dataframe and starting value 0.
28
29    Args:
30        dataframe: the dataframe from which the items are taken.
31
32    Returns:
33        a dictionary with unique items as keys and 0 as values.
34    """
35    result = {}
36    for item in dataframe['item'].unique():
37        result[item] = 0
38    return result

Make a dict with all unique items from the dataframe and starting value 0.

Args: dataframe: the dataframe from which the items are taken.

Returns: a dictionary with unique items as keys and 0 as values.

def calculate_apc(dataframe: pandas.core.frame.DataFrame) -> Dict[int, float]:
41def calculate_apc(dataframe: pd.DataFrame) -> Dict[int, float]:
42    """Sum up the total artist play count (apc).
43
44    Used in the Kullback-Leibler formula for converting ratings.
45
46    Args:
47        dataframe with an item and rating header.
48
49    Returns:
50        a dictionary with key:item, value:apc.
51    """
52    apc = get_item_dict(dataframe)
53
54    for _, row in dataframe.iterrows():
55        apc[row['item']] += row['rating']
56
57    return apc

Sum up the total artist play count (apc).

Used in the Kullback-Leibler formula for converting ratings.

Args: dataframe with an item and rating header.

Returns: a dictionary with key:item, value:apc.

def calculate_alc(dataframe: pandas.core.frame.DataFrame) -> Dict[int, float]:
60def calculate_alc(dataframe: pd.DataFrame) -> Dict[int, float]:
61    """Sum up the total artist listener count (alc).
62
63    Used in the Kullback-Leibler formula for converting ratings.
64
65    Args:
66        dataframe with an item and rating header.
67
68    Returns:
69        a dictionary with key:item, value:alc.
70    """
71    alc = get_item_dict(dataframe)
72
73    for _, row in dataframe.iterrows():
74        alc[row['item']] += 1
75
76    return alc

Sum up the total artist listener count (alc).

Used in the Kullback-Leibler formula for converting ratings.

Args: dataframe with an item and rating header.

Returns: a dictionary with key:item, value:alc.