Esempio n. 1
0
    def sectorwise(self, column=None, sectors=12, plot='matplotlib', **kwargs):
        '''Bin and plot the data sectorwise
        
        Parameters:
        ___________
        column: tuple, default None
            Column to perform sectorwise analysis on
        sectors: int, default 12
            Number of sectors to bin
        plot: string, default 'matplotlib'
            Choose whether or not to plot your data, and what method.
            Currently only supporting matplotlib, but hoping to add
            Bokeh as that library evolves.

        Returns:
        ________
        DataFrame with sectorwise distribution
        
        '''
        cuts = 360 / sectors
        bins = [0, cuts / 2]
        bins.extend(np.arange(cuts * 1.5, 360 - cuts, cuts))
        bins.extend([360 - cuts / 2, 360])
        zeroed = lambda x: 0 if x == 360 else x
        self.data[column] = self.data[column].apply(zeroed)
        cats = pd.cut(self.data[column], bins, right=False)
        array = pd.value_counts(cats).reindex(cats.levels).fillna(0)
        wind_rose = pd.Series({
            '[{0}, {1})'.format(360 - cuts / 2, 0 + cuts / 2):
            array.ix[-1] + array.ix[0]
        })
        array = array.drop([array.index[0], array.index[-1]], axis=0)
        wind_rose = wind_rose.append(array)
        new_index = {
            x: y
            for x, y in zip(wind_rose.index, np.arange(0, 360, cuts))
        }
        wind_rose = wind_rose.rename(new_index)
        freq_frame = pd.DataFrame(
            {
                'Counts': wind_rose,
                'Frequencies': wind_rose / wind_rose.sum()
            },
            index=wind_rose.index)

        if plot == 'matplotlib':
            plottools.wind_rose(freq_frame['Frequencies'].values,
                                sectors=sectors,
                                **kwargs)
        return freq_frame
Esempio n. 2
0
    def sectorwise(self, column=None, sectors=12, plot='matplotlib', **kwargs):
        '''Bin and plot the data sectorwise
        
        Parameters:
        ___________
        column: tuple, default None
            Column to perform sectorwise analysis on
        sectors: int, default 12
            Number of sectors to bin
        plot: string, default 'matplotlib'
            Choose whether or not to plot your data, and what method.
            Currently only supporting matplotlib, but hoping to add
            Bokeh as that library evolves.

        Returns:
        ________
        DataFrame with sectorwise distribution
        
        '''
        cuts = 360/sectors
        bins = [0, cuts/2]
        bins.extend(np.arange(cuts*1.5, 360-cuts, cuts))
        bins.extend([360-cuts/2, 360])
        zeroed = lambda x: 0 if x == 360 else x
        self.data[column] = self.data[column].apply(zeroed)
        cats = pd.cut(self.data[column], bins, right=False)
        array = pd.value_counts(cats).reindex(cats.levels).fillna(0)
        wind_rose = pd.Series({'[{0}, {1})'.format(360-cuts/2, 0+cuts/2):
                               array.ix[-1] + array.ix[0]})
        array = array.drop([array.index[0], array.index[-1]], axis=0)
        wind_rose = wind_rose.append(array)
        new_index = {x: y for x, y in zip(wind_rose.index,
                                          np.arange(0, 360, cuts))}
        wind_rose = wind_rose.rename(new_index)
        freq_frame = pd.DataFrame({'Counts': wind_rose,
                                   'Frequencies': wind_rose/wind_rose.sum()},
                                  index=wind_rose.index)

        if plot == 'matplotlib':
            plottools.wind_rose(freq_frame['Frequencies'].values,
                                sectors=sectors, **kwargs)
        return freq_frame
Esempio n. 3
0
    def binned(self,
               column=None,
               bins=None,
               stat='mean',
               name=None,
               plot=None):
        '''Bin all data based on a single column. 
        
        Parameters: 
        ___________
        column: tuple, default None
            Column on which to bin data
        bins: array, default None
            List or np.array with bins
        stat: string, default 'mean'
            Statistic you want to perform on binned data (mean, max, etc)
        name: string, default None
            Attribute name for binned data. Will create a new MetMast 
            attribute with binned data. 
        plot: tuple, default None
            If you are binning by wind direction, plot=column_name will pass the 
            data to plottools.wind_rose
            
        Returns: 
        ________
        self.data_binned_name, DataFrame with data summed by bins/stat
        
        Examples:
        _________
        >>> mast.binned(column=('WS Mean 1', 56), bins=np.arange(0, 41, 1))
        >>> mast.data_binned

        >>> mast.binned(column=('WD Mean 1', 56), bins=np.arange(0, 375, 15), 
                        stat='max', name='WD1_Max', plot=('WS Mean 1', 56))
        >>> mast.data_binned_WD1_Max
        
        
        '''
        print('Mapping bins to data...')

        def map_bin(x, bins):
            kwargs = {}
            if x == max(bins):
                kwargs['right'] = True
            bin = bins[np.digitize([x], bins, **kwargs)[0]]
            bin_lower = bins[np.digitize([x], bins, **kwargs)[0] - 1]
            return '[{0}-{1}]'.format(bin_lower, bin)

        step = bins[1] - bins[0]
        new_index = ['[{0}-{1}]'.format(x, x + step) for x in bins]
        new_index.pop(-1)

        temp_df = self.data.dropna()
        temp_df['Binned'] = temp_df[column].apply(map_bin, bins=bins)
        grouped = temp_df.groupby('Binned')
        grouped_stat = getattr(grouped, stat)()
        grouped_stat = grouped_stat.reindex(new_index)
        if name is not None:
            attr_name = 'data_binned_{0}'.format(name)
        else:
            attr_name = 'data_binned'
        setattr(self, attr_name, grouped_stat)

        if plot:
            sect = len(new_index)
            plottools.wind_rose(grouped_stat[plot].tolist(), sectors=sect)
Esempio n. 4
0
    def binned(self, column=None, bins=None, stat='mean', name=None, 
               plot=None):
        '''Bin all data based on a single column. 
        
        Parameters: 
        ___________
        column: tuple, default None
            Column on which to bin data
        bins: array, default None
            List or np.array with bins
        stat: string, default 'mean'
            Statistic you want to perform on binned data (mean, max, etc)
        name: string, default None
            Attribute name for binned data. Will create a new MetMast 
            attribute with binned data. 
        plot: tuple, default None
            If you are binning by wind direction, plot=column_name will pass the 
            data to plottools.wind_rose
            
        Returns: 
        ________
        self.data_binned_name, DataFrame with data summed by bins/stat
        
        Examples:
        _________
        >>> mast.binned(column=('WS Mean 1', 56), bins=np.arange(0, 41, 1))
        >>> mast.data_binned

        >>> mast.binned(column=('WD Mean 1', 56), bins=np.arange(0, 375, 15), 
                        stat='max', name='WD1_Max', plot=('WS Mean 1', 56))
        >>> mast.data_binned_WD1_Max
        
        
        '''
        print('Mapping bins to data...')
        def map_bin(x, bins):
            kwargs = {}
            if x == max(bins):
                kwargs['right'] = True
            bin = bins[np.digitize([x], bins, **kwargs)[0]]
            bin_lower = bins[np.digitize([x], bins, **kwargs)[0]-1]
            return '[{0}-{1}]'.format(bin_lower, bin)
    
        step = bins[1]-bins[0]
        new_index = ['[{0}-{1}]'.format(x, x+step) for x in bins]
        new_index.pop(-1)
    
        temp_df = self.data.dropna()
        temp_df['Binned'] = temp_df[column].apply(map_bin, bins=bins)
        grouped = temp_df.groupby('Binned')
        grouped_stat = getattr(grouped, stat)()
        grouped_stat = grouped_stat.reindex(new_index)
        if name is not None: 
            attr_name = 'data_binned_{0}'.format(name)
        else: 
            attr_name = 'data_binned'
        setattr(self, attr_name, grouped_stat)
        
        if plot: 
            sect = len(new_index)
            plottools.wind_rose(grouped_stat[plot].tolist(), sectors=sect)