File size: 2,813 Bytes
18d335b
c05668f
372f231
 
c05668f
 
 
372f231
2cf1cf5
 
 
 
 
 
 
 
 
 
 
372f231
 
 
 
 
 
 
da21b0d
 
 
c05668f
 
372f231
 
 
 
9806a2a
 
 
372f231
9806a2a
372f231
9806a2a
 
 
 
372f231
c05668f
 
 
 
372f231
2cf1cf5
 
372f231
 
18d335b
372f231
9806a2a
372f231
c05668f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from arxiv_classifier.utils.maps import (CATEGORY_ALIASES, 
                                    GENERAL_CATEGORIES, 
                                    IGNORED_CATEGOREIES,
                                    SUBFIELD_MAP)

IGNORE = GENERAL_CATEGORIES | IGNORED_CATEGOREIES

def preprocess_primary_secondary(df):
    """
    Applies substitutions and filtering to primary and secondary subfields in the DataFrame.
    Adds a new column `field` to the DataFrame based on the primary subfield.
    (If there exists a `field` column in the original DataFrame, it will be overwritten.)

    Args:
        df (pd.DataFrame): DataFrame with columns `primary_subfield` and `secondary_subfield`

    Returns:
        pd.DataFrame: DataFrame with columns `primary_subfield`, `secondary_subfield`, and `field`
    """
    def substitute_aliases(s):
        if s in CATEGORY_ALIASES:
            return CATEGORY_ALIASES[s]
        return s
    # substitute aliases for primary subfield
    df.loc[:, 'primary_subfield'] = df['primary_subfield'].apply(substitute_aliases)
    # ignore row if primary subfield is one of the ignored categories
    ignore_rows = df['primary_subfield'].isin(IGNORE)
    df = df[~ignore_rows]
    # print(f"Ignored {ignore_rows.sum()} rows with primary subfield in IGNORE")

    def preprocess_secondary(s):
        subfields = []
        # split string to list by space        
        for subfield in s.split():
            # substitute aliases
            new_subfield = substitute_aliases(subfield)
            if new_subfield == 'cs.NA':
                print(f"Warning:25> Subfield {new_subfield} not in SUBFIELD_MAP")
            # ignore secondary subfield if one of the ignored categories
            if new_subfield in IGNORE:
                continue
            # assert new_subfield in SUBFIELD_MAP.keys(), f"Subfield {new_subfield} not in SUBFIELD_MAP"
            if new_subfield not in SUBFIELD_MAP.keys():
                print(f"Warning: Subfield {new_subfield} not in SUBFIELD_MAP")
            subfields.append(new_subfield)
        # return list of subfields
        return subfields
    
    # Preprocess secondary subfields
    df.loc[:, 'secondary_subfield'] = df['secondary_subfield'].apply(preprocess_secondary)

    # Generate new column `field` from primary subfield
    df.loc[:, 'field'] = df['primary_subfield'].apply(lambda x: x.split('.')[0])
    # tests
    # assert that each primary subfield is in SUBFIELD_MAP
    assert all(df['primary_subfield'].isin(SUBFIELD_MAP.keys())), df[~df['primary_subfield'].isin(SUBFIELD_MAP.keys())]
    # assert that each secondary subfield is in SUBFIELD_MAP
    # assert all(df['secondary_subfield'].apply(lambda x: all([subfield in SUBFIELD_MAP.keys() for subfield in x])))
    # return preprocessed dataframe
    return df