from __future__ import division
from collections import Counter
import numpy as np
import pandas as pd


class DataFrameSummary(object):

    # define global variables
    TYPE_BOOL = 'bool'
    TYPE_NUMERIC = 'numeric'
    TYPE_DATE = 'date'
    TYPE_CATEGORICAL = 'categorical'
    TYPE_CONSTANT = 'constant'
    TYPE_UNIQUE = 'unique'


    def __init__(self, df):
        self.df = df
        self.columns = df.columns
        self.shape = df.shape
        self.row_count = df.shape[0]
        self.count = df.count()


    def in_ipynb():
        '''
        check if code is running in ipython notebook or jupyter
        taken from SO thread:
        http://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook
        '''
        try:
            cfg = get_ipython().config 
            if cfg['IPKernelApp']['parent_appname'] == 'ipython-notebook':
                return True
            else:
                return False
        except NameError:
            return False


    def get_column_types(self):
        '''get column type of each column in dataframe'''
        types = [[col, self.df[col].dtype, 'dtypes'] for col in self.columns]
        col_types_df = pd.DataFrame(types,
                                    columns=['column_name',
                                             'value',
                                             'variable'])
        return col_types_df


    def get_unique_count(self):
        '''get number of unique values in each column'''
        unique_cnt = [[col,
                       self.df[col].nunique(),
                       'unique_count'] for col in self.columns]
        unique_cnt_df = pd.DataFrame(unique_cnt,
                                    columns=['column_name',
                                             'value',
                                             'variable'])
        return unique_cnt_df

    def get_missing_count(self):
        '''get raw count of missing values in each column'''
        missing_cnt = [[x,
                        self.row_count - self.count[i],
                        'missing_count'] for i, x in enumerate(self.columns)]
        missing_cnt_df = pd.DataFrame(missing_cnt,
                                      columns=['column_name',
                                               'value',
                                               'variable'])
        return missing_cnt_df


    def get_missing_percentage(self):
        '''get proportion of missing values in each column of dataframe'''
        missing_percent = [[x,
                           (self.count[i] / self.row_count)*100,
                           'missing_%'] for i, x in enumerate(self.columns)]
        missing_percent_df = pd.DataFrame(missing_percent,
                                          columns=['column_name',
                                                   'value',
                                                   'variable'])
        return missing_percent_df


    def get_summary(self):
        '''collect all column summaries and return as dataframe'''
        col_types_df = self.get_column_types()
        unique_cnt_df = self.get_unique_count()
        missing_cnt_df = self.get_missing_count()
        missing_percent_df = self.get_missing_percentage()
        df_summary = pd.concat([col_types_df,
                                unique_cnt_df,
                                missing_cnt_df,
                                missing_percent_df])
        df_summary = df_summary.pivot(index='variable',
                                      columns='column_name',
                                      values='value')
        df_summary.index.name = None
        return df_summary


    def get_distributions(self):
        '''generate distribution plots and summary statistics'''
        import matplotlib.pyplot as plt
        plt.style.use('fivethirtyeight')
        if in_ipynb:

        else:

        numeric_df = self.df._get_numeric_data()
        f = open('/home/jack/report.html','w')
        f.write(html_string)
        f.close()



@@ -3022,24 +3022,211 @@
 summary(self
+,%0A                    plot=True,%0A                    style='fivethirtyeight',%0A                    top=15,%0A                    save=False,%0A                    filename='pandas_summary.png'
 ):%0A        '
@@ -3889,16 +3889,231 @@
  = None%0A
+        if plot is True:%0A            self.get_summary_plot(df_summary,%0A                             style,%0A                             top,%0A                             save,%0A                             filename)%0A
         
@@ -4132,16 +4132,17 @@
 mmary%0A%0A%0A
+%0A
     def 
@@ -4149,26 +4149,165 @@
 get_
-distributions(self
+summary_plot(self, df_summary,%0A                         style,%0A                         top,%0A                         save,%0A                         filename
 ):%0A 
@@ -4329,38 +4329,31 @@
 ate 
-distribution plots 
+plot for p
 and
+as
  summary
  sta
@@ -4352,19 +4352,8 @@
 mary
- statistics
 '''%0A
@@ -4418,187 +4418,940 @@
 use(
-'fivethirtyeight')%0A        if in_ipynb:%0A%0A        else:%0A%0A        numeric_df = self.df._get_numeric_data()%0A        f = open('/home/jack/report.html','w')%0A        f.write(html_string
+style)%0A        # Set color transparency (0: transparent; 1: solid)%0A        a = 0.7%0A        # Create a colormap%0A        customcmap = %5B(x/24.0,  x/48.0, 0.05) for x in range(len(df_summary))%5D%0A        # Create a figure of given size%0A        fig = plt.figure(figsize=(16,8))%0A        # Add a subplot%0A        ax = fig.add_subplot(111)%0A        ax.set_ylabel('')%0A        ax.set_xlabel('%25 of mising values in dataframe columns')%0A        df_summary.T.sort_values('missing_%25')%5B'missing_%25'%5D.plot(kind='barh',%0A                                                            ax=ax,%0A                                                            alpha=a,%0A                                                            legend=False,%0A                                                            color=customcmap,%0A                                                            edgecolor='w', xlim=(0, 100))%0A        if save is True:%0A            plt.savefig(filename
 )%0A  
@@ -5360,15 +5360,16 @@
     
-f.close
+plt.show
 ()%0A%0A

add get_summary_plot function