Skip to content

Instantly share code, notes, and snippets.

@kitsamho
Created June 20, 2023 09:30
Show Gist options
  • Select an option

  • Save kitsamho/e3483bd146a0f78aa47f00bb0b7a023f to your computer and use it in GitHub Desktop.

Select an option

Save kitsamho/e3483bd146a0f78aa47f00bb0b7a023f to your computer and use it in GitHub Desktop.
class D3Transformer:
"""
Class to handle the transformation of processed data into a shape that can be used by the d3blocks network graph
Parameters:
df_transformed (pandas.DataFrame): The processed dataframe.
"""
def __init__(self, df_transformed):
self.df_transformed = df_transformed
def __get_nested_cast(self):
"""
Get the nested cast data.
Returns:
pandas.DataFrame: The dataframe with nested cast data.
"""
df_nested = _get_nested_cast(self.df_transformed)
return df_nested
def __generate_nested_cast_combinations(self, df_nested):
"""
Generate nested cast combinations.
Parameters:
df_nested (pandas.DataFrame): The dataframe with nested cast data.
Returns:
pandas.DataFrame: The dataframe with generated nested cast combinations.
"""
df_nested['all_combinations'] = df_nested.c_name.apply(_get_nested_cast_combinations)
return df_nested
def __flatten_nested_cast_combinations(self, df_nested):
"""
Flatten nested cast combinations.
Parameters:
df_nested (pandas.DataFrame): The dataframe with nested cast combinations.
Returns:
List[Tuple]: The flattened list of combination tuples.
"""
flattened_combination_tuples = _flatten_nested_cast_combinations(df_nested)
return flattened_combination_tuples
def __get_combinations_dict(self, flattened_combination_tuples):
"""
Generate combinations dictionary.
Parameters:
flattened_combination_tuples (List[Tuple]): The flattened list of combination tuples.
Returns:
Dict: The combinations dictionary.
"""
combinations_dict = _get_combinations_dict(flattened_combination_tuples)
return combinations_dict
def __get_d3_dataframe(self, combinations_dict):
"""
Get the D3 dataframe.
Parameters:
combinations_dict (Dict): The combinations dictionary.
Returns:
pandas.DataFrame: The D3 dataframe.
"""
df_d3 = _get_d3_dataframe(combinations_dict)
return df_d3
def transform_data(self):
"""
Perform the data transformation.
Returns:
pandas.DataFrame: The transformed D3 dataframe.
"""
df_nested = self.__get_nested_cast()
df_nested = self.__generate_nested_cast_combinations(df_nested)
flattened_combination_tuples = self.__flatten_nested_cast_combinations(df_nested)
combinations_dict = self.__get_combinations_dict(flattened_combination_tuples)
df_d3 = self.__get_d3_dataframe(combinations_dict)
return df_d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment