PK܌9NF}9vishwakarma/__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.9' __author__ = 'diagram-ai' from .pgmplot import pgmplot from .pdfplot import pdfplot from .pmfplot import pmfplot PK 9NZw  vishwakarma/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://api.diagram.ai/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 (int): parameters to a Uniform distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' if not isinstance(a, int): raise ValueError('For a Uniform distribution, a should always be an integer.') if not isinstance(b, int): raise ValueError('For a Uniform distribution, b should always be an integer.') if b <= a: raise ValueError('For a Uniform distribution, b should always be greater than a.') 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 ''' if sigma <= 0: raise ValueError('For a Gaussian distribution, sigma should be greater than zero.') 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 ''' if lam <= 0: raise ValueError('For an Exponential distribution, lambda should be greater than zero.') 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 ''' if alpha <= 0: raise ValueError('For a Gamma distribution, alpha should be greater than zero.') if beta <= 0: raise ValueError('For a Gamma distribution, beta should be greater than zero.') 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, json=kwargs) if(resp.ok): # get the image file and write it to temp dir if resp.headers.get('Content-Type') == 'image/png': 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) PK+N 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://api.diagram.ai/vishwakarma/pgmplot/' resp = requests.post(url, json=frozen_pgm) if(resp.ok): # get the image file and write it to temp dir if resp.headers.get('Content-Type') == 'image/png': 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)) PK9NWvishwakarma/pmfplot.py# -*- coding: utf-8 -*- ''' Build visualizations for discrete distributions [Bernoulli, Binomial, Geometric, Poisson] Example: from vishwakarma import pmfplot pmfplot.bernoulli(k, p) Attributes: Todo: ''' import requests import tempfile import os import random import shutil import string import datetime from IPython.display import Image class pmfplot: ''' This class generates visualizations for discrete distributions ''' # setup the API endpoint to be called _url = 'http://api.diagram.ai/vishwakarma/' _endpoint = 'pmfplot/' # default width of image to be displayed _width = 600 @classmethod def bernoulli(cls, k, p): ''' Visualization for a Bernoulli distribution Args: k, p (float): parameters to a Bernoulli distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' if k not in (0,1): raise ValueError('For a Bernoulli distribution, k should always be either 0 or 1.') if not 0 <= p <= 1: raise ValueError('For a Bernoulli distribution, p should always be between 0 and 1.') return cls._call_post(dist='bernoulli', k=k, p=p) @classmethod def binomial(cls, k, n, p): ''' Visualization for a Binomial distribution Args: k, n, p (float): parameters to a Binomial distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' if not isinstance(k, int): raise ValueError('For a Binomial distribution, k should always be an integer.') if not isinstance(n, int): raise ValueError('For a Binomial distribution, n should always be an integer.') if n < 0: raise ValueError('For a Binomial distribution, n should be greater than or equal to zero.') if not 0 <= k <= n: raise ValueError('For a Binomial distribution, k should always be between 0 and n.') if not 0 <= p <= 1: raise ValueError('For a Binomial distribution, p should always be between 0 and 1.') return cls._call_post(dist='binomial', k=k, n=n, p=p) @classmethod def geometric(cls, n, p): ''' Visualization for a Geometric distribution Args: n, p (float): parameters to a Geometric distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' if not isinstance(n, int): raise ValueError('For a Geometric distribution, n should always be an integer.') if n < 0: raise ValueError('For a Geometric distribution, n should be greater than or equal to zero.') if not 0 <= p <= 1: raise ValueError('For a Geometric distribution, p should always be between 0 and 1.') return cls._call_post(dist='geometric', n=n, p=p) @classmethod def poisson(cls, mu, x): ''' Visualization for a Poisson distribution Args: mu, x (float): parameters to a Poisson distribution Returns: image (IPython.display.Image): The image that can be displayed inline in a Jupyter notebook ''' if not isinstance(x, int): raise ValueError('For a Poisson distribution, x should always be an integer.') if x < 0: raise ValueError('For a Poisson distribution, x should be greater than or equal to zero.') if mu <= 0: raise ValueError('For a Poisson distribution, mu should be greater than zero.') return cls._call_post(dist='poisson', mu=mu, x=x) @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, json=kwargs) if(resp.ok): # get the image file and write it to temp dir if resp.headers.get('Content-Type') == 'image/png': 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) PKf7M|X++#vishwakarma-0.0.9.dist-info/LICENSEMIT License Copyright (c) 2018 Diagram AI Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!Hd BUc!vishwakarma-0.0.9.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UD"PK!H+O~$vishwakarma-0.0.9.dist-info/METADATAQN0+!q" 9QTTMMVٛP.;3;]` ֔p_j*aǡ F۾/F/q/5 :`(=W>vak=ڞ:ފPK܌9NF}9vishwakarma/__init__.pyPK 9NZw  vishwakarma/pdfplot.pyPK+N vishwakarma/pgmplot.pyPK9NWZvishwakarma/pmfplot.pyPKf7M|X++#v2vishwakarma-0.0.9.dist-info/LICENSEPK!Hd BUc!6vishwakarma-0.0.9.dist-info/WHEELPK!H+O~$v7vishwakarma-0.0.9.dist-info/METADATAPK!HBq"9vishwakarma-0.0.9.dist-info/RECORDPKS: