ci_mapping/analysis/descriptive_analysis.py
def annual_fields_of_study_usage(
    data,
    pfos,
    fos_metadata,
    fos_levels,
    preselected_fos=[],
    excluded_fos=[],
    top_n=None,
    filename="annual_fields_of_study_usage",
):
    """Field of study comparison for CI, AI+CI. 

    Args:
        data (`pd.DataFrame`): MAG paper data.
        pfos (`pd.DataFrame`): Paper-level fields of study.
        fos_metadata (`pd.DataFrame`): Level of the FoS in MAG hierarchy.
        fos_level (int): Level in the MAG hierarchy to plot for.
        top_n (int): Number of most used FoS to plot.
        filename (str): Name of the HTML file to store the plot.

    """
    df = data.merge(pfos, left_on="id", right_on="paper_id").merge(
        fos_metadata, left_on="field_of_study_id", right_on="id"
    )
    df = df[["paper_id", "type", "year", "name", "level"]]
    df = df[df.type.isin(["CI", "AI_CI"])]
    df["year"] = df.year.astype(int)

    most_used_fos_by_level_and_type = flatten_lists(
        flatten_lists(
            [
                [
                    list(
                        df[(df.level == fos_level) & (df.type == type_)]
                        .name.value_counts()
                        .index[:top_n]
                    )
                    for fos_level in fos_levels
                ]
                for type_ in df["type"].unique()
            ]
        )
    )
    df = df[df.name.isin(set(most_used_fos_by_level_and_type))]
    df = df[~df.name.isin(set(excluded_fos))]
    df = pd.DataFrame(
        df.groupby(["type", "year", "name", "level"])["paper_id"].count()
    ).reset_index()

    lst = []
    for year in df.year.unique():
        for name in df.name.unique():
            if len(df[(df.name == name) & (df.year == year)]["type"].values) == 2:
                continue
            elif len(df[(df.name == name) & (df.year == year)]["type"].values) == 1:
                if df[(df.name == name) & (df.year == year)]["type"].values[0] == "CI":
                    lst.append(
                        {
                            "type": "AI_CI",
                            "year": year,
                            "name": name,
                            "paper_id": 0,
                            "level": df[df.name == name]["level"].values[0],
                        }
                    )
                else:
                    lst.append(
                        {
                            "type": "CI",
                            "year": year,
                            "name": name,
                            "paper_id": 0,
                            "level": df[df.name == name]["level"].values[0],
                        }
                    )
            else:
                lst.append(
                    {
                        "type": "AI_CI",
                        "year": year,
                        "name": name,
                        "paper_id": 0,
                        "level": df[df.name == name]["level"].values[0],
                    }
                )
                lst.append(
                    {
                        "type": "CI",
                        "year": year,
                        "name": name,
                        "paper_id": 0,
                        "level": df[df.name == name]["level"].values[0],
                    }
                )

    df = pd.concat([df, pd.DataFrame(lst)])

    fraq = []
    for _, row in df.iterrows():
        fraq.append(
            (
                row["paper_id"]
                / data[
                    (data.type == row["type"]) & (data.year == str(row["year"]))
                ].shape[0]
            )
            * 100
        )

    df["fraq"] = fraq
    df = df.fillna(0)

    if not preselected_fos:
        for fos_level in fos_levels:
            _fos_plot(df[df.level == fos_level], filename, fos_level=fos_level)
    else:
        _fos_plot(
            df[df.name.isin(preselected_fos)], filename, fos_level="preselected_fos"
        )
Field of study comparison for CI, AI+CI. 

    Args:
        data (`pd.DataFrame`): MAG paper data.
        pfos (`pd.DataFrame`): Paper-level fields of study.
        fos_metadata (`pd.DataFrame`): Level of the FoS in MAG hierarchy.
        fos_level (int): Level in the MAG hierarchy to plot for.
        top_n (int): Number of most used FoS to plot.
        filename (str): Name of the HTML file to store the plot.

    
Field of study comparison for CI, AI+CI.
Field of study comparison for CI AI + CI .
def annual_fields_of_study_usage(
    data,
    pfos,
    fos_metadata,
    fos_levels,
    preselected_fos=[],
    excluded_fos=[],
    top_n=None,
    filename="annual_fields_of_study_usage",
):
    df = data.merge(pfos, left_on="id", right_on="paper_id").merge(
        fos_metadata, left_on="field_of_study_id", right_on="id"
    )
    df = df[["paper_id", "type", "year", "name", "level"]]
    df = df[df.type.isin(["CI", "AI_CI"])]
    df["year"] = df.year.astype(int)
    most_used_fos_by_level_and_type = flatten_lists(
        flatten_lists(
            [
                [
                    list(
                        df[(df.level == fos_level) & (df.type == type_)]
                        .name.value_counts()
                        .index[:top_n]
                    )
                    for fos_level in fos_levels
                ]
                for type_ in df["type"].unique()
            ]
        )
    )
    df = df[df.name.isin(set(most_used_fos_by_level_and_type))]
    df = df[~df.name.isin(set(excluded_fos))]
    df = pd.DataFrame(
        df.groupby(["type", "year", "name", "level"])["paper_id"].count()
    ).reset_index()
    lst = []
    for year in df.year.unique():
        for name in df.name.unique():
            if len(df[(df.name == name) & (df.year == year)]["type"].values) == 2:
                continue
            elif len(df[(df.name == name) & (df.year == year)]["type"].values) == 1:
                if df[(df.name == name) & (df.year == year)]["type"].values[0] == "CI":
                    lst.append(
                        {
                            "type": "AI_CI",
                            "year": year,
                            "name": name,
                            "paper_id": 0,
                            "level": df[df.name == name]["level"].values[0],
                        }
                    )
                else:
                    lst.append(
                        {
                            "type": "CI",
                            "year": year,
                            "name": name,
                            "paper_id": 0,
                            "level": df[df.name == name]["level"].values[0],
                        }
                    )
            else:
                lst.append(
                    {
                        "type": "AI_CI",
                        "year": year,
                        "name": name,
                        "paper_id": 0,
                        "level": df[df.name == name]["level"].values[0],
                    }
                )
                lst.append(
                    {
                        "type": "CI",
                        "year": year,
                        "name": name,
                        "paper_id": 0,
                        "level": df[df.name == name]["level"].values[0],
                    }
                )
    df = pd.concat([df, pd.DataFrame(lst)])
    fraq = []
    for _, row in df.iterrows():
        fraq.append(
            (
                row["paper_id"]
                / data[
                    (data.type == row["type"]) & (data.year == str(row["year"]))
                ].shape[0]
            )
            * 100
        )
    df["fraq"] = fraq
    df = df.fillna(0)
    if not preselected_fos:
        for fos_level in fos_levels:
            _fos_plot(df[df.level == fos_level], filename, fos_level=fos_level)
    else:
        _fos_plot(
            df[df.name.isin(preselected_fos)], filename, fos_level="preselected_fos"
        )
def annual_fields_of_study_usage ( data , pfos , fos_metadata , fos_levels , preselected_fos = [ ] , excluded_fos = [ ] , top_n = None , filename = "annual_fields_of_study_usage" , ) : df = data . merge ( pfos , left_on = "id" , right_on = "paper_id" ) . merge ( fos_metadata , left_on = "field_of_study_id" , right_on = "id" ) df = df [ [ "paper_id" , "type" , "year" , "name" , "level" ] ] df = df [ df . type . isin ( [ "CI" , "AI_CI" ] ) ] df [ "year" ] = df . year . astype ( int ) most_used_fos_by_level_and_type = flatten_lists ( flatten_lists ( [ [ list ( df [ ( df . level == fos_level ) & ( df . type == type_ ) ] . name . value_counts ( ) . index [ : top_n ] ) for fos_level in fos_levels ] for type_ in df [ "type" ] . unique ( ) ] ) ) df = df [ df . name . isin ( set ( most_used_fos_by_level_and_type ) ) ] df = df [ ~ df . name . isin ( set ( excluded_fos ) ) ] df = pd . DataFrame ( df . groupby ( [ "type" , "year" , "name" , "level" ] ) [ "paper_id" ] . count ( ) ) . reset_index ( ) lst = [ ] for year in df . year . unique ( ) : for name in df . name . unique ( ) : if len ( df [ ( df . name == name ) & ( df . year == year ) ] [ "type" ] . values ) == 2 : continue elif len ( df [ ( df . name == name ) & ( df . year == year ) ] [ "type" ] . values ) == 1 : if df [ ( df . name == name ) & ( df . year == year ) ] [ "type" ] . values [ 0 ] == "CI" : lst . append ( { "type" : "AI_CI" , "year" : year , "name" : name , "paper_id" : 0 , "level" : df [ df . name == name ] [ "level" ] . values [ 0 ] , } ) else : lst . append ( { "type" : "CI" , "year" : year , "name" : name , "paper_id" : 0 , "level" : df [ df . name == name ] [ "level" ] . values [ 0 ] , } ) else : lst . append ( { "type" : "AI_CI" , "year" : year , "name" : name , "paper_id" : 0 , "level" : df [ df . name == name ] [ "level" ] . values [ 0 ] , } ) lst . append ( { "type" : "CI" , "year" : year , "name" : name , "paper_id" : 0 , "level" : df [ df . name == name ] [ "level" ] . values [ 0 ] , } ) df = pd . concat ( [ df , pd . DataFrame ( lst ) ] ) fraq = [ ] for _ , row in df . iterrows ( ) : fraq . append ( ( row [ "paper_id" ] / data [ ( data . type == row [ "type" ] ) & ( data . year == str ( row [ "year" ] ) ) ] . shape [ 0 ] ) * 100 ) df [ "fraq" ] = fraq df = df . fillna ( 0 ) if not preselected_fos : for fos_level in fos_levels : _fos_plot ( df [ df . level == fos_level ] , filename , fos_level = fos_level ) else : _fos_plot ( df [ df . name . isin ( preselected_fos ) ] , filename , fos_level = "preselected_fos" )
Field of study comparison for CI, AI+CI.
Field of study comparison for CI AI + CI .
"""Field of study comparison for CI, AI+CI. 

    Args:
        data (`pd.DataFrame`): MAG paper data.
        pfos (`pd.DataFrame`): Paper-level fields of study.
        fos_metadata (`pd.DataFrame`): Level of the FoS in MAG hierarchy.
        fos_level (int): Level in the MAG hierarchy to plot for.
        top_n (int): Number of most used FoS to plot.
        filename (str): Name of the HTML file to store the plot.

    """
data
MAG paper data.
MAG paper data .
pfos
Paper-level fields of study.
Paper - level fields of study .
fos_metadata
Level of the FoS in MAG hierarchy.
Level of the FoS in MAG hierarchy .
fos_levels
preselected_fos
excluded_fos
top_n
Number of most used FoS to plot.
Number of most used FoS to plot .
filename
Name of the HTML file to store the plot.
Name of the HTML file to store the plot .
fos_level
Level in the MAG hierarchy to plot for.
Level in the MAG hierarchy to plot for .