PK$(N7vishwakarma/__init__.py# -*- coding: utf-8 -*- ''' Python visualization library for Probabilistic Graphical Models, Discrete & Continuous Distributions, and a lot more! :copyright: (c) 2018 by Diagram AI. :license: see LICENSE for more details. ''' __title__ = 'vishwakarma' __version__ = '0.0.4' __author__ = 'diagram-ai' from .pgmplot import pgmplot from .pdfplot import pdfplot from .pmfplot import pmfplot PK$(NEvishwakarma/pdfplot.py# -*- coding: utf-8 -*- ''' Build visualizations for continuous distributions [Uniform, Gaussian, Exponential, Gamma] Example: from vishwakarma import pdfplot pdfplot.gaussian(mu, sigma) Attributes: Todo: ''' import requests import tempfile import os import random import shutil import string import datetime from IPython.display import Image class pdfplot: ''' This class generates visualizations for continuous distributions ''' # setup the API endpoint to be called _url = 'http://www.diagram.ai/api/vishwakarma/' _endpoint = 'pdfplot' # default width of image to be displayed _width = 600 @classmethod def uniform(cls, a, b): ''' Visualization for a Uniform distribution Args: a, b (float): parameters to a Uniform distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' return cls._call_post(dist='uniform', a=a, b=b) @classmethod def gaussian(cls, mu, sigma): ''' Visualization for a Gaussian distribution Args: mu, sigma (float): parameters to a Gaussian distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' return cls._call_post(dist='gaussian', mu=mu, sigma=sigma) @classmethod def exponential(cls, lam): ''' Visualization for an Exponential distribution Args: lam (float): parameter to an Exponential distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' return cls._call_post(dist='exponential', lam=lam) @classmethod def gamma(cls, alpha, beta): ''' Visualization for a Gamma distribution Args: alpha, beta (float): parameters to a Gamma distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' return cls._call_post(dist='gamma', alpha=alpha, beta=beta) @classmethod def _call_post(cls, **kwargs): ''' Calls the API hosted on www.diagram.ai Args: kwargs: Name and parameters of the distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook Note: Internal function - not to be exposed ''' tmp_dir_name = '' try: # create a temp directory & temp file tmp_dir_name = tempfile.mkdtemp() # generate a tmp file name (excluding file extension) epoch = datetime.datetime.now().strftime('%s') tmp_file_name = ''.join( [random.choice(string.ascii_letters + string.digits) for n in range(8)]) tmp_file_name = os.path.join( tmp_dir_name, tmp_file_name + epoch + '.png') # make the call ... resp = requests.post(cls._url + cls._endpoint, data=kwargs) if(resp.ok): # get the image file and write it to temp dir if resp.headers.get('Content-Disposition'): open(tmp_file_name, 'wb').write(resp.content) # now return this image as an Image object displayable in a # Jupyter notebook return Image(filename=tmp_file_name, width=cls._width) else: raise Exception(resp.raise_for_status()) finally: # cleanup the temp directory shutil.rmtree(tmp_dir_name, ignore_errors=True) PKS)NVZ  vishwakarma/pgmplot.py# -*- coding: utf-8 -*- '''Build visualization for a pgmpy model Example: Attributes: Todo: ''' import pgmpy import jsonpickle import requests import tempfile import os import random import shutil import string import datetime from IPython.display import Image def pgmplot(obj, width=600): '''Build visualization for a pgmpy model Args: obj (pgmpy.models): The pgmpy model that needs to be visualized width (int): Width of the image in px (default 600) Returns: Image: The image that can be displayed inline in a Jupyter notebook ''' # list of supported classes supp_classes = (pgmpy.models.BayesianModel, pgmpy.models.NaiveBayes, pgmpy.models.MarkovModel, pgmpy.models.FactorGraph, pgmpy.models.DynamicBayesianNetwork) if isinstance(obj, supp_classes): tmp_dir_name = '' try: # get json representation of the object frozen_pgm = jsonpickle.encode(obj) # create a temp directory & temp file tmp_dir_name = tempfile.mkdtemp() # generate a tmp file name (excluding file extension) epoch = datetime.datetime.now().strftime('%s') tmp_file_name = ''.join( [random.choice(string.ascii_letters + string.digits) for n in range(8)]) tmp_file_name = os.path.join( tmp_dir_name, tmp_file_name + epoch + '.png') url = 'http://www.diagram.ai/api/vishwakarma/pgmplot' resp = requests.post(url, data=frozen_pgm) if(resp.ok): # get the image file and write it to temp dir if resp.headers.get('Content-Disposition'): open(tmp_file_name, 'wb').write(resp.content) # now return this image as an Image object displayable in # the jupyter notebook return Image(filename=tmp_file_name, width=width) else: raise Exception(resp.raise_for_status()) finally: # cleanup the temp directory shutil.rmtree(tmp_dir_name, ignore_errors=True) else: raise TypeError( 'Expected pgmpy modely; unsupported object type: ' + type(obj)) PK$(N5%\jJqF|CQ/FmKxKgMJAϕ]ZVXqA;{Xچp3'!85fCHU\ I.4[m={)sF eQ,PEzr ɄHy^=֯+5w_a_x4r_B #E쒫,%#EMw)b$ooa,%7s8펚^Ο~G#4-lAHsanr#Gq2ab0rM]+uzY84PK!H@~q"vishwakarma-0.0.4.dist-info/RECORD}Ir@}ff-DHDFifD8*xnYJ[F ʬ'dQO\RQQm:a;XuLn'kG X;p ݞE?iqƾs] M 1s˖Jh%'iKnʫn3HAԬi5CuYdDIs.3VC`4|+Ccx9C~Dm%3 a!/.YYWϴ~PgqAd6u4<4@>l{QiTfmLtX{Ptx}Z0:bn#lܛk-YV /79'N6OlqPK$(N7vishwakarma/__init__.pyPK$(NEvishwakarma/pdfplot.pyPKS)NVZ  vishwakarma/pgmplot.pyPK$(N