ratpmetro/main.py
def plot_incident_prob(self, by="hour", year=None, loc=None, **kwargs):
        """
        Plot (marginal) probability of operational incidents

        Args:
            by (str): Can be "year", "month", "day", "weekday", "hour", or any two of them connected by a "-", like "hour-weekday"
            year (int): If ``year`` is given then only tweets within this specific year are used, else then all downloaded tweets are used
            loc (list of str): Time period from ``loc[0]`` to ``loc[1]``
        """
        if "year" in by:
            year = None
        df = self._df_processed_loc(year=year, loc=loc)

        # Extract "by" for 2d plots
        if "-" in by:
            by_x, by_y = by.split("-")
            assert by_x in df
            assert by_y in df
        else:
            assert by in df

        weekday_label = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

        # 2d plot
        if "-" in by:
            df = df.groupby([by_x, by_y]).mean()["is_incident"]
            x, y = df.index.levels  # already sorted
            x1 = list(x) + [2 * x[-1] - x[-2]]
            y1 = list(y) + [2 * y[-1] - y[-2]]
            xx, yy = np.meshgrid(x1, y1, indexing="ij")
            c = np.zeros((len(x), len(y)))
            for i in range(len(x)):
                for j in range(len(y)):
                    index = xx[i, j], yy[i, j]
                    if index in df:
                        c[i, j] = df[index]
                    else:  # data not available
                        c[i, j] = np.nan
            plt.pcolormesh(xx, yy, c, cmap="Reds", **kwargs)

            # Center year, month, day, weekday to the ticks center
            for by, label, set_ticks in zip(
                [by_x, by_y], [x, y], [plt.xticks, plt.yticks]
            ):
                ticks = np.linspace(label[0] + 0.5, label[-1] + 0.5, len(label))
                if by == "year":
                    pass
                elif by == "month":
                    ticks = ticks[::2]
                    label = label[::2]
                elif by == "day":
                    ticks = ticks[::5]
                    label = label[::5]
                elif by == "weekday":
                    label = weekday_label
                else:  # "hour":
                    continue
                set_ticks(ticks, label)

            plt.xlabel(by_x)
            plt.ylabel(by_y)
            plt.colorbar(format=FuncFormatter(lambda y, _: "{:.1%}".format(y)))
            plt.title("probability of incidents")
            return xx, yy, c

        # 1d plot
        else:
            df = df.groupby(by).mean()["is_incident"]
            y = df.values
            if by == "weekday":
                x = weekday_label
            else:
                x = df.index

            plt.plot(x, y, "-o", color=self.color, **kwargs)
            plt.gca().yaxis.set_major_formatter(
                FuncFormatter(lambda x, _: "{:.1%}".format(x))
            )
            plt.xlabel(by)
            plt.ylabel("probability of incidents")
            plt.grid()
            return x, y

        Plot (marginal) probability of operational incidents

        Args:
            by (str): Can be "year", "month", "day", "weekday", "hour", or any two of them connected by a "-", like "hour-weekday"
            year (int): If ``year`` is given then only tweets within this specific year are used, else then all downloaded tweets are used
            loc (list of str): Time period from ``loc[0]`` to ``loc[1]``
        
Plot (marginal) probability of operational incidents
Plot ( marginal ) probability of operational incidents
def plot_incident_prob(self, by="hour", year=None, loc=None, **kwargs):
        if "year" in by:
            year = None
        df = self._df_processed_loc(year=year, loc=loc)
        if "-" in by:
            by_x, by_y = by.split("-")
            assert by_x in df
            assert by_y in df
        else:
            assert by in df
        weekday_label = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
        if "-" in by:
            df = df.groupby([by_x, by_y]).mean()["is_incident"]
            x, y = df.index.levels  
            x1 = list(x) + [2 * x[-1] - x[-2]]
            y1 = list(y) + [2 * y[-1] - y[-2]]
            xx, yy = np.meshgrid(x1, y1, indexing="ij")
            c = np.zeros((len(x), len(y)))
            for i in range(len(x)):
                for j in range(len(y)):
                    index = xx[i, j], yy[i, j]
                    if index in df:
                        c[i, j] = df[index]
                    else:  
                        c[i, j] = np.nan
            plt.pcolormesh(xx, yy, c, cmap="Reds", **kwargs)
            for by, label, set_ticks in zip(
                [by_x, by_y], [x, y], [plt.xticks, plt.yticks]
            ):
                ticks = np.linspace(label[0] + 0.5, label[-1] + 0.5, len(label))
                if by == "year":
                    pass
                elif by == "month":
                    ticks = ticks[::2]
                    label = label[::2]
                elif by == "day":
                    ticks = ticks[::5]
                    label = label[::5]
                elif by == "weekday":
                    label = weekday_label
                else:  
                    continue
                set_ticks(ticks, label)
            plt.xlabel(by_x)
            plt.ylabel(by_y)
            plt.colorbar(format=FuncFormatter(lambda y, _: "{:.1%}".format(y)))
            plt.title("probability of incidents")
            return xx, yy, c
        else:
            df = df.groupby(by).mean()["is_incident"]
            y = df.values
            if by == "weekday":
                x = weekday_label
            else:
                x = df.index
            plt.plot(x, y, "-o", color=self.color, **kwargs)
            plt.gca().yaxis.set_major_formatter(
                FuncFormatter(lambda x, _: "{:.1%}".format(x))
            )
            plt.xlabel(by)
            plt.ylabel("probability of incidents")
            plt.grid()
            return x, y
def plot_incident_prob ( self , by = "hour" , year = None , loc = None , ** kwargs ) : if "year" in by : year = None df = self . _df_processed_loc ( year = year , loc = loc ) if "-" in by : by_x , by_y = by . split ( "-" ) assert by_x in df assert by_y in df else : assert by in df weekday_label = [ "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" , "Sun" ] if "-" in by : df = df . groupby ( [ by_x , by_y ] ) . mean ( ) [ "is_incident" ] x , y = df . index . levels x1 = list ( x ) + [ 2 * x [ - 1 ] - x [ - 2 ] ] y1 = list ( y ) + [ 2 * y [ - 1 ] - y [ - 2 ] ] xx , yy = np . meshgrid ( x1 , y1 , indexing = "ij" ) c = np . zeros ( ( len ( x ) , len ( y ) ) ) for i in range ( len ( x ) ) : for j in range ( len ( y ) ) : index = xx [ i , j ] , yy [ i , j ] if index in df : c [ i , j ] = df [ index ] else : c [ i , j ] = np . nan plt . pcolormesh ( xx , yy , c , cmap = "Reds" , ** kwargs ) for by , label , set_ticks in zip ( [ by_x , by_y ] , [ x , y ] , [ plt . xticks , plt . yticks ] ) : ticks = np . linspace ( label [ 0 ] + 0.5 , label [ - 1 ] + 0.5 , len ( label ) ) if by == "year" : pass elif by == "month" : ticks = ticks [ : : 2 ] label = label [ : : 2 ] elif by == "day" : ticks = ticks [ : : 5 ] label = label [ : : 5 ] elif by == "weekday" : label = weekday_label else : continue set_ticks ( ticks , label ) plt . xlabel ( by_x ) plt . ylabel ( by_y ) plt . colorbar ( format = FuncFormatter ( lambda y , _ : "{:.1%}" . format ( y ) ) ) plt . title ( "probability of incidents" ) return xx , yy , c else : df = df . groupby ( by ) . mean ( ) [ "is_incident" ] y = df . values if by == "weekday" : x = weekday_label else : x = df . index plt . plot ( x , y , "-o" , color = self . color , ** kwargs ) plt . gca ( ) . yaxis . set_major_formatter ( FuncFormatter ( lambda x , _ : "{:.1%}" . format ( x ) ) ) plt . xlabel ( by ) plt . ylabel ( "probability of incidents" ) plt . grid ( ) return x , y
Plot (marginal) probability of operational incidents
Plot ( marginal ) probability of operational incidents
"""
        Plot (marginal) probability of operational incidents

        Args:
            by (str): Can be "year", "month", "day", "weekday", "hour", or any two of them connected by a "-", like "hour-weekday"
            year (int): If ``year`` is given then only tweets within this specific year are used, else then all downloaded tweets are used
            loc (list of str): Time period from ``loc[0]`` to ``loc[1]``
        """ # Extract "by" for 2d plots # 2d plot # already sorted # data not available # Center year, month, day, weekday to the ticks center # "hour": # 1d plot
self
by
Can be "year", "month", "day", "weekday", "hour", or any two of them connected by a "-", like "hour-weekday"
Can be " year " " month " " day " " weekday " " hour " or any two of them connected by a " - " like " hour - weekday "
year
If ``year`` is given then only tweets within this specific year are used, else then all downloaded tweets are used
If ` ` year ` ` is given then only tweets within this specific year are used else then all downloaded tweets are used
loc
None