Ejemplo n.º 1
0
def dict_to_json_file(dict_obj,
                      directory_path,
                      filename,
                      remove_file_extension=True):
    """

        Writes a dict to a json file.

    Args:
        dict_obj: dict
            Dictionary object.

        directory_path: string
            Given path that already exists.

        filename: string
            Json file's name.
    """
    directory_path = correct_directory_path(directory_path)
    check_if_directory_exists(directory_path)

    filename = convert_to_filename(filename,
                                   remove_file_extension=remove_file_extension)

    with open(f'{directory_path}{filename}.json', 'w',
              encoding='utf-8') as outfile:
        json.dump(dict_obj, outfile, ensure_ascii=False, indent=2)
Ejemplo n.º 2
0
def pickle_object_to_file(obj,
                          directory_path,
                          filename,
                          remove_file_extension=True):
    """

        Writes the object to a pickle file.

    Args:
        obj: any object
            Any python object that can be pickled.

        directory_path: string
            Given path that already exists.

        filename: string
             Pickle file's name.
    """
    try:
        directory_path = correct_directory_path(directory_path)
        check_if_directory_exists(directory_path)

        # Ensures no file extensions in filename
        filename = convert_to_filename(
            filename, remove_file_extension=remove_file_extension)
        file_dir = f'{directory_path}{filename}.pkl'
        list_pickle = open(file_dir, 'wb')
        pickle.dump(obj, list_pickle)
    finally:
        list_pickle.close()

    return file_dir
Ejemplo n.º 3
0
def write_object_text_to_file(obj,
                              directory_path,
                              filename,
                              remove_file_extension=True):
    """

        Writes the object's string representation to a text file.

    Args:
        obj: any
            Any object that has a string 'repr'.

        directory_path: string
            Given path that already exists.

        filename: string
            Text file's name.
    """
    directory_path = correct_directory_path(directory_path)
    check_if_directory_exists(directory_path)

    filename = convert_to_filename(filename,
                                   remove_file_extension=remove_file_extension)
    file_dir = f'{directory_path}{filename}.txt'

    f = open(file_dir, 'w')
    f.write('obj = ' + repr(obj) + '\n')
    f.close()
Ejemplo n.º 4
0
    def save_plot(self, filename="Unknown filename", sub_dir=""):
        """

            Checks the passed data to see if a plot can be saved; raises
            an error if it can't.

        Args:

            filename: string
                If set to 'None' will default to a pre-defined string;
                unless it is set to an actual filename.

            sub_dir: string
                Specify the sub directory to append to the pre-defined folder path.

        """

        if not isinstance(sub_dir, str):
            raise TypeError(
                f"Expected param 'sub_dir' to be type string! Was found to be {type(sub_dir)}"
            )

        # Create the png to save
        create_plt_png(self.folder_path, sub_dir,
                       convert_to_filename(filename))
Ejemplo n.º 5
0
    def save_plot(self,
                  sub_dir="",
                  filename="Unknown filename",
                  suppress_runtime_errors=True):
        """

            Checks the passed data to see if a plot can be saved; raises
            an error if it can't.

        Args:
            df: pd.Dataframe
                Pandas DataFrame object

            df_features: DataFrameTypes from eflow
                DataFrameTypes object.

            filename: string
                If set to 'None' will default to a pre-defined string;
                unless it is set to an actual filename.

            sub_dir: string
                Specify the sub directory to append to the pre-defined folder path.

            suppress_runtime_errors: bool
                If set to true; when generating any graphs will suppress any runtime
                errors so the program can keep running.
        """
        try:

            if not isinstance(sub_dir, str):
                raise TypeError(
                    f"Expected param 'sub_dir' to be type string! Was found to be {type(sub_dir)}"
                )

            # Create the png to save
            create_plt_png(self.folder_path, sub_dir,
                           convert_to_filename(filename))

        # Always raise snapshot error
        except SnapshotMismatchError as e:
            raise e

        except Exception as e:
            plt.close("all")
            if suppress_runtime_errors:
                warnings.warn(
                    f"An error occured when trying to save the plot:\n{str(e)}",
                    RuntimeWarning)
            else:
                raise e
Ejemplo n.º 6
0
    def save_table_as_plot(self,
                           table,
                           filename="Unknown filename",
                           sub_dir="",
                           show_index=False):
        """

            Checks the passed data to see if a table can be saved as a plot;
            raises an error if it can't.

        Args:

            table: pd.Dataframe
                Dataframe object to convert to plot.

            filename: string
                If set to 'None' will default to a pre-defined string;
                unless it is set to an actual filename.

            sub_dir: string
                Specify the sub directory to append to the pre-defined folder path.

            show_index: bool
                Show index of the saved dataframe.
        """

        if not isinstance(sub_dir, str):
            raise TypeError(
                f"Expected param 'sub_dir' to be type string! Was found to be {type(sub_dir)}"
            )

        # Convert dataframe to plot
        df_to_image(table,
                    self.folder_path,
                    sub_dir,
                    convert_to_filename(filename),
                    show_index=show_index,
                    format_float_pos=2)
Ejemplo n.º 7
0
    def save_plot(self,
                  df,
                  df_features,
                  filename="Unknown filename",
                  sub_dir="",
                  dataframe_snapshot=True,
                  suppress_runtime_errors=True,
                  compare_shape=True,
                  compare_feature_names=True,
                  compare_random_values=True,
                  meta_data=True):
        """

            Checks the passed data to see if a plot can be saved; raises
            an error if it can't.

        Args:
            df: pd.Dataframe
                Pandas DataFrame object

            df_features: DataFrameTypes from eflow
                DataFrameTypes object.

            filename: string
                If set to 'None' will default to a pre-defined string;
                unless it is set to an actual filename.

            sub_dir: string
                Specify the sub directory to append to the pre-defined folder path.

            dataframe_snapshot: bool
                Boolean value to determine whether or not generate and compare a
                snapshot of the dataframe in the dataset's directory structure.
                Helps ensure that data generated in that directory is correctly
                associated to a dataframe.

            compare_shape: bool
                When comparing and creating the dataframe snapshot of the data's
                shape.

            compare_feature_names: bool
                When comparing and creating the dataframe snapshot of the data's
                column names.

            compare_random_values: bool
                When comparing and creating the dataframe snapshot of the data
                sudo random values.

            meta_data:bool
                If set to true then it will generate meta data on the dataframe.

            suppress_runtime_errors: bool
                If set to true; when generating any graphs will suppress any runtime
                errors so the program can keep running.
        """
        try:

            if not isinstance(sub_dir, str):
                raise TypeError(
                    f"Expected param 'sub_dir' to be type string! Was found to be {type(sub_dir)}"
                )

            # Check if dataframe matches saved snapshot; Creates file if needed
            if dataframe_snapshot:
                df_snapshot = DataFrameSnapshot(
                    compare_shape=compare_shape,
                    compare_feature_names=compare_feature_names,
                    compare_random_values=compare_random_values)
                df_snapshot.check_create_snapshot(
                    df,
                    df_features,
                    directory_path=self.folder_path,
                    sub_dir=f"{sub_dir}/_Extras")

            # Create the png to save
            create_plt_png(self.folder_path, sub_dir,
                           convert_to_filename(filename))

            if meta_data:
                generate_meta_data(df, self.folder_path,
                                   f"{sub_dir}" + "/_Extras")

        # Always raise snapshot error
        except SnapshotMismatchError as e:
            raise e

        except Exception as e:
            plt.close()
            if suppress_runtime_errors:
                warnings.warn(
                    f"An error occured when trying to save the plot:\n{str(e)}",
                    RuntimeWarning)
            else:
                raise e
Ejemplo n.º 8
0
    def save_table_as_plot(self,
                           df,
                           sub_dir="",
                           filename="Unknown filename",
                           suppress_runtime_errors=True,
                           show_index=False,
                           format_float_pos=2):
        """

            Checks the passed data to see if a table can be saved as a plot;
            raises an error if it can't.

        Args:
            df: pd.Dataframe
                Pandas DataFrame object of all data.

            sub_dir:
                Directory to generate on top of the already made directory (self.folder_path).

            overwrite_full_path:
                A pre defined directory that already exists.

            df_features: DataFrameTypes from eflow
                DataFrameTypes object.

            table: pd.Dataframe
                Dataframe object to convert to plot.

            filename: string
                If set to 'None' will default to a pre-defined string;
                unless it is set to an actual filename.

            sub_dir: string
                Specify the sub directory to append to the pre-defined folder path.

            suppress_runtime_errors: bool
                If set to true; when generating any graphs will suppress any runtime
                errors so the program can keep running.

            meta_data:bool
                If set to true then it will generate meta data on the dataframe.

            show_index: bool
                Show index of the saved dataframe.
        """

        try:
            if not isinstance(sub_dir, str):
                raise TypeError(
                    f"Expected param 'sub_dir' to be type string! Was found to be {type(sub_dir)}"
                )

            # Convert dataframe to plot
            df_to_image(df,
                        self.folder_path,
                        sub_dir,
                        convert_to_filename(filename),
                        show_index=show_index,
                        format_float_pos=format_float_pos)

        # Always raise snapshot error
        except SnapshotMismatchError as e:
            raise e

        except Exception as e:
            plt.close("all")
            if suppress_runtime_errors:
                warnings.warn(
                    f"An error occured when trying to save the table as a plot:\n{str(e)}",
                    RuntimeWarning)
            else:
                raise e