Beispiel #1
0
    def show_image_grid(self, image_array, name=None, nrow=8, padding=2,
                        normalize=False, range=None, scale_each=False, pad_value=0, *args, **kwargs):
        """
        Sends an array of images to a chat using  an existing Telegram bot. (Requires torch and torchvision)


        Args:
            image_array (np.narray / torch.tensor): Image array/ tensor which will be sent as an image grid
            make_grid_kargs: Key word arguments for the torchvision make grid method
        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if isinstance(image_array, np.ndarray):
            image_array = torch.from_numpy(image_array)

        image_array = image_array.cpu()
        grid = torchvision.utils.make_grid(image_array, nrow=nrow, padding=padding, pad_value=pad_value,
                                           normalize=normalize, range=range, scale_each=scale_each)
        ndarr = grid.mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()
        buf = get_image_as_buffered_file(ndarr)

        try:
            self.bot.send_photo(chat_id=self.chat_id, photo=buf, caption=caption)
        except Exception as e:
            print("Could not send image_grid to telegram")
Beispiel #2
0
    def show_lineplot(self, y_vals, x_vals=None, name=None, *args, **kwargs):
        """
        Sends a lineplot to a chat using an existing Telegram bot.

        Args:
            y_vals: Array of shape MxN , where M is the number of points and N is the number of different line
            x_vals: Has to have the same shape as Y: MxN. For each point in Y it gives the corresponding X value (if
            not set the points are assumed to be equally distributed in the interval [0, 1] )
            name: The name of the figure

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        figure = super().show_lineplot(y_vals, x_vals, name, show=False, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)

        try:
            self.bot.send_photo(chat_id=self.chat_id, photo=buf, caption=caption)
        except Exception as e:
            print("Could not send plot to telegram")
    def show_scatterplot(self,
                         array,
                         name="scatterplot",
                         delete_last=True,
                         *args,
                         **kwargs):
        """
        Sends a scatterplot to a chat using an existing slack bot.

        Args:
            array: An array with size N x dim, where each element i \in N at X[i] results in a 2D
                (if dim = 2) or 3D (if dim = 3) point.
            name: The name of the figure
            delete_last: If a message with the same name was sent, delete it beforehand

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if delete_last and name in self.ts_dict:
            self.delete_message(self.ts_dict[name])

        figure = super().show_scatterplot(array, name, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)

        try:
            ts = self.send_message(message=caption, file=buf)
            self.ts_dict[name] = ts
        except Exception as e:
            print("Could not send plot to slack")
    def show_piechart(self,
                      array,
                      name="piechart",
                      delete_last=True,
                      *args,
                      **kwargs):
        """
        Sends a piechart to a chat using an existing slack bot.

        Args:
            array: Array of positive integers. Each integer will be presented as a part of the pie (with the total
                as the sum of all integers)
            name: The name of the figure
            delete_last: If a message with the same name was sent, delete it beforehand

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if delete_last and name in self.ts_dict:
            self.delete_message(self.ts_dict[name])

        figure = super().show_piechart(array, name, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)

        try:
            ts = self.send_message(message=caption, file=buf)
            self.ts_dict[name] = ts
        except Exception as e:
            print("Could not send plot to slack")
    def show_lineplot(self,
                      y_vals,
                      x_vals=None,
                      name="lineplot",
                      delete_last=True,
                      *args,
                      **kwargs):
        """
        Sends a lineplot to a chat using an existing slack bot.

        Args:
            y_vals: Array of shape MxN , where M is the number of points and N is the number of different line
            x_vals: Has to have the same shape as Y: MxN. For each point in Y it gives the corresponding X value (if
                not set the points are assumed to be equally distributed in the interval [0, 1])
            name: The name of the figure
            delete_last: If a message with the same name was sent, delete it beforehand

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if delete_last and name in self.ts_dict:
            self.delete_message(self.ts_dict[name])

        figure = super().show_lineplot(y_vals, x_vals, name, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)
        try:
            ts = self.send_message(message=caption, file=buf)
            self.ts_dict[name] = ts
        except Exception as e:
            print("Could not send plot to slack")
    def show_barplot(self,
                     array,
                     name="barplot",
                     delete_last=True,
                     *args,
                     **kwargs):
        """
        Sends a barplot to a chat using an existing slack bot.

        Args:
            array: array of shape NxM where N is the number of rows and M is the number of elements in the row.
            name: The name of the figure
            delete_last: If a message with the same name was sent, delete it beforehand

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if delete_last and name in self.ts_dict:
            self.delete_message(self.ts_dict[name])

        figure = super().show_barplot(array, name, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)
        try:
            ts = self.send_message(message=caption, file=buf)
            self.ts_dict[name] = ts
        except Exception as e:
            print("Could not send plot to slack")
    def show_image_grid(self,
                        image_array,
                        name=None,
                        nrow=8,
                        padding=2,
                        normalize=False,
                        range=None,
                        scale_each=False,
                        pad_value=0,
                        delete_last=True,
                        *args,
                        **kwargs):
        """
        Sends an array of images to a chat using  an existing Slack bot. (Requires torch and torchvision)


        Args:
            image_array (np.narray / torch.tensor): Image array/tensor which will be sent as an image grid
            make_grid_kargs: Key word arguments for the torchvision make grid method
            delete_last: If a message with the same name was sent, delete it beforehand
        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if delete_last and name in self.ts_dict:
            self.delete_message(self.ts_dict[name])

        if isinstance(image_array, np.ndarray):
            image_array = torch.from_numpy(image_array)

        image_array = image_array.cpu()
        grid = torchvision.utils.make_grid(image_array,
                                           nrow=nrow,
                                           padding=padding,
                                           pad_value=pad_value,
                                           normalize=normalize,
                                           range=range,
                                           scale_each=scale_each)
        ndarr = grid.mul(255).clamp(0, 255).byte().numpy()
        buf = get_image_as_buffered_file(ndarr)
        try:
            ts = self.send_message(message=caption, file=buf)
            self.ts_dict[name] = ts
        except Exception as e:
            print("Could not send image_grid to slack")
    def show_image(self, image, *args, **kwargs):
        """
        Sends an image file to a chat using an existing slack bot.

        Args:
            image (str or np array): Path to the image file to be sent to the chat.
        """
        try:
            if isinstance(image, str):
                with open(image, 'rb') as img_file:
                    self.send_message(message=self.exp_name, file=img_file)
            elif isinstance(image, np.ndarray):
                buf = get_image_as_buffered_file(image)
                self.send_message(message=self.exp_name, file=buf)

        except Exception as e:
            print("Could not send image to slack")
Beispiel #9
0
    def show_image(self, image, *args, **kwargs):
        """
        Sends an image file to a chat using an existing Telegram bot.

        Args:
            image (str or np array): Path to the image file to be sent to the chat.
        """
        try:
            if isinstance(image, str):
                with open(image, 'rb') as img_file:
                    self.bot.send_photo(chat_id=self.chat_id, photo=img_file)
            elif isinstance(image, np.ndarray):
                buf = get_image_as_buffered_file(image)

                self.bot.send_photo(chat_id=self.chat_id, photo=buf)

        except Exception as e:
            print("Could not send image to telegram")
    def show_value(self,
                   value,
                   name,
                   counter=None,
                   tag=None,
                   delete_last=True,
                   *args,
                   **kwargs):
        """
        Sends a value to a chat using an existing slack bot.

        Args:
            value: Value to be plotted sent to the chat.
            name: Name for the plot.
            counter: Optional counter to be sent in conjunction with the value.
            tag: Tag to be used as a label for the plot.
            delete_last: If a message with the same name was sent, delete it beforehand
        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        if delete_last and name in self.ts_dict:
            self.delete_message(self.ts_dict[name])

        figure = super().show_value(value=value,
                                    name=name,
                                    counter=counter,
                                    tag=tag)
        buf = get_image_as_buffered_file(figure)
        try:
            ts = self.send_message(message=caption, file=buf)
            self.ts_dict[name] = ts
        except Exception as e:
            print("Could not send plot to slack")
Beispiel #11
0
    def show_barplot(self, array, name=None, *args, **kwargs):
        """
        Sends a barplot to a chat using an existing Telegram bot.

        Args:
            array: array of shape NxM where N is the number of rows and M is the number of elements in the row.
            name: The name of the figure

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        figure = super().show_barplot(array, name, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)

        try:
            self.bot.send_photo(chat_id=self.chat_id, photo=buf, caption=caption)
        except Exception as e:
            print("Could not send plot to telegram")
Beispiel #12
0
    def show_piechart(self, array, name=None, *args, **kwargs):
        """
        Sends a piechart to a chat using an existing Telegram bot.

        Args:
            array: Array of positive integers. Each integer will be presented as a part of the pie (with the total
            as the sum of all integers)
            name: The name of the figure

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        figure = super().show_piechart(array, name,  *args, **kwargs)
        buf = get_image_as_buffered_file(figure)

        try:
            self.bot.send_photo(chat_id=self.chat_id, photo=buf, caption=caption)
        except Exception as e:
            print("Could not send plot to telegram")
Beispiel #13
0
    def show_scatterplot(self, array, name=None, *args, **kwargs):
        """
        Sends a scatterplot to a chat using an existing Telegram bot.

        Args:
            array: A 2d array with size N x dim, where each element i \in N at X[i] results in a a 2d (if dim = 2)/
            3d (if dim = 3) point.
            name: The name of the figure

        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        figure = super().show_scatterplot(array, name, *args, **kwargs)
        buf = get_image_as_buffered_file(figure)

        try:
            self.bot.send_photo(chat_id=self.chat_id, photo=buf, caption=caption)
        except Exception as e:
            print("Could not send plot to telegram")
Beispiel #14
0
    def show_value(self, value, name, counter=None, tag=None, *args, **kwargs):
        """
        Sends a value to a chat using an existing Telegram bot.

        Args:
            value: Value to be plotted sent to the chat.
            name: Name for the plot.
            counter: Optional counter to be sent in conjunction with the value.
            tag: Tag to be used as a label for the plot.
        """

        caption = ""
        if self.exp_name is not None:
            caption += self.exp_name + "  "
        if name is not None:
            caption += name + "  "

        figure = super().show_value(value, name, counter, tag, show=False)
        buf = get_image_as_buffered_file(figure)

        try:
            self.bot.send_photo(chat_id=self.chat_id, photo=buf, caption=caption)
        except Exception as e:
            print("Could not send plot to telegram")