code
stringlengths
501
5.19M
package
stringlengths
2
81
path
stringlengths
9
304
filename
stringlengths
4
145
# Zennit ![Zennit-Logo](share/img/zennit.png) [![Documentation Status](https://readthedocs.org/projects/zennit/badge/?version=latest)](https://zennit.readthedocs.io/en/latest/?badge=latest) [![tests](https://github.com/chr5tphr/zennit/actions/workflows/tests.yml/badge.svg)](https://github.com/chr5tphr/zennit/actions/workflows/tests.yml) [![PyPI Version](https://img.shields.io/pypi/v/zennit)](https://pypi.org/project/zennit/) [![License](https://img.shields.io/pypi/l/zennit)](https://github.com/chr5tphr/zennit/blob/master/COPYING.LESSER) Zennit (**Z**ennit **e**xplains **n**eural **n**etworks **i**n **t**orch) is a high-level framework in Python using Pytorch for explaining/exploring neural networks. Its design philosophy is intended to provide high customizability and integration as a standardized solution for applying rule-based attribution methods in research, with a strong focus on Layerwise Relevance Propagation (LRP). Zennit strictly requires models to use Pytorch's `torch.nn.Module` structure (including activation functions). Zennit is currently under active development, but should be mostly stable. If you find Zennit useful for your research, please consider citing our related [paper](https://arxiv.org/abs/2106.13200): ``` @article{anders2021software, author = {Anders, Christopher J. and Neumann, David and Samek, Wojciech and Müller, Klaus-Robert and Lapuschkin, Sebastian}, title = {Software for Dataset-wide XAI: From Local Explanations to Global Insights with {Zennit}, {CoRelAy}, and {ViRelAy}}, journal = {CoRR}, volume = {abs/2106.13200}, year = {2021}, } ``` ## Documentation The latest documentation is hosted at [zennit.readthedocs.io](https://zennit.readthedocs.io/en/latest/). ## Install To install directly from PyPI using pip, use: ```shell $ pip install zennit ``` Alternatively, install from a manually cloned repository to try out the examples: ```shell $ git clone https://github.com/chr5tphr/zennit.git $ pip install ./zennit ``` ## Usage At its heart, Zennit registers hooks at Pytorch's Module level, to modify the backward pass to produce rule-based attributions like LRP (instead of the usual gradient). All rules are implemented as hooks ([`zennit/rules.py`](src/zennit/rules.py)) and most use the LRP basis `BasicHook` ([`zennit/core.py`](src/zennit/core.py)). **Composites** ([`zennit/composites.py`](src/zennit/composites.py)) are a way of choosing the right hook for the right layer. In addition to the abstract **NameMapComposite**, which assigns hooks to layers by name, and **LayerMapComposite**, which assigns hooks to layers based on their Type, there exist explicit **Composites**, some of which are `EpsilonGammaBox` (`ZBox` in input, `Epsilon` in dense, `Gamma` in convolutions) or `EpsilonPlus` (`Epsilon` in dense, `ZPlus` in convolutions). All composites may be used by directly importing from `zennit.composites`, or by using their snake-case name as key for `zennit.composites.COMPOSITES`. **Canonizers** ([`zennit/canonizers.py`](src/zennit/canonizers.py)) temporarily transform models into a canonical form, if required, like `SequentialMergeBatchNorm`, which automatically detects and merges BatchNorm layers followed by linear layers in sequential networks, or `AttributeCanonizer`, which temporarily overwrites attributes of applicable modules, e.g. to handle the residual connection in ResNet-Bottleneck modules. **Attributors** ([`zennit/attribution.py`](src/zennit/attribution.py)) directly execute the necessary steps to apply certain attribution methods, like the simple `Gradient`, `SmoothGrad` or `Occlusion`. An optional **Composite** may be passed, which will be applied during the **Attributor**'s execution to compute the modified gradient, or hybrid methods. Using all of these components, an LRP-type attribution for VGG16 with batch-norm layers with respect to label 0 may be computed using: ```python import torch from torchvision.models import vgg16_bn from zennit.composites import EpsilonGammaBox from zennit.canonizers import SequentialMergeBatchNorm from zennit.attribution import Gradient data = torch.randn(1, 3, 224, 224) model = vgg16_bn() canonizers = [SequentialMergeBatchNorm()] composite = EpsilonGammaBox(low=-3., high=3., canonizers=canonizers) with Gradient(model=model, composite=composite) as attributor: out, relevance = attributor(data, torch.eye(1000)[[0]]) ``` A similar setup using [the example script](share/example/feed_forward.py) produces the following attribution heatmaps: ![beacon heatmaps](share/img/beacon_vgg16_epsilon_gamma_box.png) For more details and examples, have a look at our [**documentation**](https://zennit.readthedocs.io/en/latest/). ### More Example Heatmaps More heatmaps of various attribution methods for VGG16 and ResNet50, all generated using [`share/example/feed_forward.py`](share/example/feed_forward.py), can be found below. <details> <summary>Heatmaps for VGG16</summary> ![vgg16 heatmaps](share/img/beacon_vgg16_various.webp) </details> <details> <summary>Heatmaps for ResNet50</summary> ![resnet50 heatmaps](share/img/beacon_resnet50_various.webp) </details> ## Contributing See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed instructions on how to contribute. ## License Zennit is licensed under the GNU LESSER GENERAL PUBLIC LICENSE VERSION 3 OR LATER -- see the [LICENSE](LICENSE), [COPYING](COPYING) and [COPYING.LESSER](COPYING.LESSER) files for details.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/README.md
README.md
# Contributing Guide for Zennit Thank you for your interest in contributing to Zennit! If you would like to fix a bug or add a feature, please write an issue before submitting a pull request. ## Git We use a linear git-history, where each commit contains a full feature/bug fix, such that each commit represents an executable version. The commit message contains a subject followed by an empty line, followed by a detailed description, similar to the following: ``` Category: Short subject describing changes (50 characters or less) - detailed description, wrapped at 72 characters - bullet points or sentences are okay - all changes should be documented and explained - valid categories are, for example: - `Docs` for documentation - `Tests` for tests - `Composites` for changes in composites - `Core` for core changes - `Package` for package-related changes, e.g. in setup.py ``` We recommend to not use `-m` for committing, as this often results in very short commit messages. ## Code Style We use [PEP8](https://www.python.org/dev/peps/pep-0008) with a line-width of 120 characters. For docstrings we use [numpydoc](https://numpydoc.readthedocs.io/en/latest/format.html). We use [`flake8`](https://pypi.org/project/flake8/) for quick style checks and [`pylint`](https://pypi.org/project/pylint/) for thorough style checks. ## Testing Tests are written using [Pytest](https://docs.pytest.org) and executed in a separate environment using [Tox](https://tox.readthedocs.io/en/latest/). A full style check and all tests can be run by simply calling `tox` in the repository root. If you add a new feature, please also include appropriate tests to verify its intended functionality. We try to keep the code coverage close to 100%. ## Documentation The documentation uses [Sphinx](https://www.sphinx-doc.org). It can be built at `docs/build` using the respective Tox environment with `tox -e docs`. To rebuild the full documentation, `tox -e docs -- -aE` can be used. The API-documentation is generated from the numpycodestyle docstring of respective modules/classes/functions. ### Tutorials Tutorials are written as Jupyter notebooks in order to execute them using [Binder](https://mybinder.org/) or [Google Colab](https://colab.research.google.com/). They are found at [`docs/source/tutorial`](docs/source/tutorial). Their output should be empty when committing, as they will be executed when building the documentation. To reduce the building time of the documentation, their execution time should be kept short, i.e. large files like model parameters should not be downloaded automatically. To include parameter files for users, include a comment which describes how to use the full model/data, and provide the necessary code in a comment or an if-condition which always evaluates to `False`. ## Continuous Integration Linting, tests and the documentation are all checked using a Github Actions workflow which executes the appropriate tox environments.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/CONTRIBUTING.md
CONTRIBUTING.md
================ Getting started ================ Install ------- Zennit can be installed directly from PyPI: .. code-block:: console $ pip install zennit For the current development version, or to try out examples, Zennit may be alternatively cloned and installed with .. code-block:: console $ git clone https://github.com/chr5tphr/zennit.git $ pip install ./zennit Basic Usage ----------- Zennit implements propagation-based attribution methods by overwriting the gradient of PyTorch modules in PyTorch's auto-differentiation engine. This means that Zennit will only work on models which are strictly implemented using PyTorch modules, including activation functions. The following demonstrates a setup to compute Layerwise Relevance Propagation (LRP) relevance for a simple model and random data. .. code-block:: python import torch from torch.nn import Sequential, Conv2d, ReLU, Linear, Flatten # setup the model and data model = Sequential( Conv2d(3, 10, 3, padding=1), ReLU(), Flatten(), Linear(10 * 32 * 32, 10), ) input = torch.randn(1, 3, 32, 32) The most important high-level structures in Zennit are ``Composites``, ``Attributors`` and ``Canonizers``. Composites ^^^^^^^^^^ Composites map ``Rules`` to modules based on their properties and context to modify their gradient. The most common composites for LRP are implemented in :py:mod:`zennit.composites`. The following computes LRP relevance using the ``EpsilonPlusFlat`` composite: .. code-block:: python from zennit.composites import EpsilonPlusFlat # create a composite instance composite = EpsilonPlusFlat() # make sure the input requires a gradient input.requires_grad = True # compute the output and gradient within the composite's context with composite.context(model) as modified_model: output = modified_model(input) # gradient/ relevance wrt. class/output 0 output.backward(gradient=torch.eye(10)[[0]]) # relevance is not accumulated in .grad if using torch.autograd.grad # relevance, = torch.autograd.grad(output, input, torch.eye(10)[[0]) # gradient is accumulated in input.grad print('Backward:', input.grad) The context created by :py:func:`zennit.core.Composite.context` registers the composite, which means that all rules are applied according to the composite's mapping. See :doc:`/how-to/use-rules-composites-and-canonizers` for information on using composites, :py:mod:`zennit.composites` for an API reference and :doc:`/how-to/write-custom-composites` for writing new compositors. Available ``Rules`` can be found in :py:mod:`zennit.rules`, their use is described in :doc:`/how-to/use-rules-composites-and-canonizers` and how to add new ones is described in :doc:`/how-to/write-custom-rules`. Attributors ^^^^^^^^^^^ Alternatively, *attributors* may be used instead of ``composite.context``. .. code-block:: python from zennit.attribution import Gradient attributor = Gradient(model, composite) with attributor: # gradient/ relevance wrt. output/class 1 output, relevance = attributor(input, torch.eye(10)[[1]]) print('EpsilonPlusFlat:', relevance) Attribution methods which are not propagation-based, like :py:class:`zennit.attribution.SmoothGrad` are implemented as attributors, and may be combined with propagation-based (composite) approaches. .. code-block:: python from zennit.attribution import SmoothGrad # we do not need a composite to compute vanilla SmoothGrad with SmoothGrad(model, noise_level=0.1, n_iter=10) as attributor: # gradient/ relevance wrt. output/class 7 output, relevance = attributor(input, torch.eye(10)[[7]]) print('SmoothGrad:', relevance) More information on attributors can be found in :doc:`/how-to/use-attributors` and :doc:`/how-to/write-custom-attributors`. Canonizers ^^^^^^^^^^ For some modules and operations, Layerwise Relevance Propagation (LRP) is not implementation-invariant, eg. ``BatchNorm -> Dense -> ReLU`` will be attributed differently than ``Dense -> BatchNorm -> ReLU``. Therefore, LRP needs a canonical form of the model, which is implemented in ``Canonizers``. These may be simply supplied when instantiating a composite: .. code-block:: python from torchvision.models import vgg16 from zennit.composites import EpsilonGammaBox from zennit.torchvision import VGGCanonizer # instatiate the model model = vgg16() # create the canonizers canonizers = [VGGCanonizer()] # EpsilonGammaBox needs keyword arguments 'low' and 'high' high = torch.full_like(input, 4) composite = EpsilonGammaBox(low=-high, high=high, canonizers=canonizers) with Gradient(model, composite) as attributor: # gradient/ relevance wrt. output/class 0 # torchvision.vgg16 has 1000 output classes by default output, relevance = attributor(input, torch.eye(1000)[[0]]) print('EpsilonGammaBox:', relevance) Some pre-defined canonizers for models from ``torchvision`` can be found in :py:mod:`zennit.torchvision`. The :py:class:`zennit.torchvision.VGGCanonizer` specifically is simply :py:class:`zennit.canonizers.SequentialMergeBatchNorm`, which may be used when ``BatchNorm`` is used in sequential models. Note that for ``SequentialMergeBatchNorm`` to work, all functions (linear layers, activations, ...) must be modules and assigned to their parent module in the order they are visited (see :py:class:`zennit.canonizers.SequentialMergeBatchNorm`). For more information on canonizers see :doc:`/how-to/use-rules-composites-and-canonizers` and :doc:`/how-to/write-custom-canonizers`. Visualizing Results ^^^^^^^^^^^^^^^^^^^ While attribution approaches are not limited to the domain of images, they are predominantly used on image models and produce heat maps of relevance. For this reason, Zennit implements methods to visualize relevance heat maps. .. code-block:: python from zennit.image import imsave # sum over the color channels heatmap = relevance.sum(1) # get the absolute maximum, to center the heat map around 0 amax = heatmap.abs().numpy().max((1, 2)) # save heat map with color map 'coldnhot' imsave( 'heatmap.png', heatmap[0], vmin=-amax, vmax=amax, cmap='coldnhot', level=1.0, grid=False ) Information on ``imsave`` can be found at :py:func:`zennit.image.imsave`. Saving an image with 3 color channels will result in the image being saved without a color map but with the channels assumed as RGB. The keyword argument ``grid`` will create a grid of multiple images over the batch dimension if ``True``. Custom color maps may be created with :py:class:`zennit.cmap.ColorMap`, eg. to save the previous image with a color map ranging from blue to yellow to red: .. code-block:: python from zennit.cmap import ColorMap # 00f is blue, ff0 is yellow, f00 is red, 0x80 is the center of the range cmap = ColorMap('00f,80:ff0,f00') imsave( 'heatmap.png', heatmap, vmin=-amax, vmax=amax, cmap=cmap, level=1.0, grid=True ) More details to visualize heat maps and color maps can be found in :doc:`/how-to/visualize-results`. The ColorMap specification language is described in :py:class:`zennit.cmap.ColorMap` and built-in color maps are implemented in :py:obj:`zennit.image.CMAPS`. Example Script -------------- A ready-to use example to analyze a few ImageNet models provided by torchvision can be found at :repo:`share/example/feed_forward.py`. The following setup requires bash, cURL and (magic-)file. Create a virtual environment, install Zennit and download the example scripts: .. code-block:: console $ mkdir zennit-example $ cd zennit-example $ python -m venv .venv $ .venv/bin/pip install zennit $ curl -o feed_forward.py \ 'https://raw.githubusercontent.com/chr5tphr/zennit/master/share/example/feed_forward.py' $ curl -o download-lighthouses.sh \ 'https://raw.githubusercontent.com/chr5tphr/zennit/master/share/scripts/download-lighthouses.sh' Prepare the data required for the example: .. code-block:: console $ mkdir params data results $ bash download-lighthouses.sh --output data/lighthouses $ curl -o params/vgg16-397923af.pth 'https://download.pytorch.org/models/vgg16-397923af.pth' This creates the needed directories and downloads the pre-trained vgg16 parameters and 8 images of light houses from wikimedia commons into the required label-directory structure for the imagenet dataset in PyTorch. The ``feed_forward.py`` example can then be run using: .. code-block:: console $ .venv/bin/python feed_forward.py \ data/lighthouses \ 'results/vgg16_epsilon_gamma_box_{sample:02d}.png' \ --inputs 'results/vgg16_input_{sample:02d}.png' \ --parameters params/vgg16-397923af.pth \ --model vgg16 \ --composite epsilon_gamma_box \ --relevance-norm symmetric \ --cmap coldnhot which computes the lrp heatmaps according to the ``epsilon_gamma_box`` rule and stores them in results, along with the respective input images. Other possible composites that can be passed to ``--composites`` are, e.g., ``epsilon_plus``, ``epsilon_alpha2_beta1_flat``, ``guided_backprop``, ``excitation_backprop``. .. The resulting heatmaps may look like the following: .. image:: /img/beacon_vgg16_epsilon_gamma_box.png :alt: Lighthouses with Attributions Alternatively, heatmaps for SmoothGrad with absolute relevances may be computed by omitting ``--composite`` and supplying ``--attributor``: .. code-block:: console $ .venv/bin/python feed_forward.py \ data/lighthouses \ 'results/vgg16_smoothgrad_{sample:02d}.png' \ --inputs 'results/vgg16_input_{sample:02d}.png' \ --parameters params/vgg16-397923af.pth \ --model vgg16 \ --attributor smoothgrad \ --relevance-norm absolute \ --cmap hot For Integrated Gradients, ``--attributor integrads`` may be provided. Heatmaps for Occlusion Analysis with unaligned relevances may be computed by executing: .. code-block:: console $ .venv/bin/python feed_forward.py \ data/lighthouses \ 'results/vgg16_occlusion_{sample:02d}.png' \ --inputs 'results/vgg16_input_{sample:02d}.png' \ --parameters params/vgg16-397923af.pth \ --model vgg16 \ --attributor occlusion \ --relevance-norm unaligned \ --cmap hot
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/getting-started.rst
getting-started.rst
==================== Zennit Documentation ==================== Zennit (Zennit Explains Neural Networks in Torch) is a python framework using PyTorch to compute local attributions in the sense of eXplainable AI (XAI) with a focus on Layerwise Relevance Progagation. It works by defining *rules* which are used to overwrite the gradient of PyTorch modules in PyTorch's auto-differentiation engine. Rules are mapped to layers with *composites*, which contain directions to compute the attributions of a full model, which maps rules to modules based on their properties and context. Zennit is available on PyPI and may be installed using: .. code-block:: console $ pip install zennit Contents -------- .. toctree:: :maxdepth: 2 getting-started how-to/index tutorial/index reference/index bibliography Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` Citing ------ If you find Zennit useful, why not cite our related paper :cite:p:`anders2021software`: .. code-block:: bibtex @article{anders2021software, author = {Anders, Christopher J. and Neumann, David and Samek, Wojciech and Müller, Klaus-Robert and Lapuschkin, Sebastian}, title = {Software for Dataset-wide XAI: From Local Explanations to Global Insights with {Zennit}, {CoRelAy}, and {ViRelAy}}, journal = {CoRR}, volume = {abs/2106.13200}, year = {2021}, }
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/index.rst
index.rst
# Image Classification with VGG This tutorial will introduce the attribution of image classifiers using VGG11 on ImageNet. Feel free to replace VGG11 with any other version of VGG. First, we install **Zennit**. This includes its dependencies `Pillow`, `torch` and `torchvision`: ``` %pip install zennit ``` Then, we import necessary modules, classes and functions: ``` import logging import torch from PIL import Image from torchvision.transforms import Compose, Resize, CenterCrop from torchvision.transforms import ToTensor, Normalize from torchvision.models import vgg11_bn from zennit.attribution import Gradient, SmoothGrad from zennit.composites import EpsilonPlusFlat, EpsilonGammaBox from zennit.image import imgify, imsave from zennit.torchvision import VGGCanonizer ``` We download an image of the [Dornbusch Lighthouse](https://en.wikipedia.org/wiki/Dornbusch_Lighthouse) from [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:2006_09_06_180_Leuchtturm.jpg): ``` torch.hub.download_url_to_file( 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/2006_09_06_180_Leuchtturm.jpg/640px-2006_09_06_181_Leuchtturm.jpg', 'dornbusch-lighthouse.jpg', ) ``` We load and prepare the data. The image is resized such that the shorter side is 256 pixels in size, then center-cropped to `(224, 224)`, converted to a `torch.Tensor`, and then normalized according the channel-wise mean and standard deviation of the ImageNet dataset: ``` # define the base image transform transform_img = Compose([ Resize(256), CenterCrop(224), ]) # define the full tensor transform transform = Compose([ transform_img, ToTensor(), Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), ]) # load the image image = Image.open('dornbusch-lighthouse.jpg') # transform the PIL image and insert a batch-dimension data = transform(image)[None] ``` We can look at the original image and the cropped image: ``` # display the original image display(image) # display the resized and cropped image display(transform_img(image)) ``` Then, we initialize the model and load the hyperparameters. Set `pretrained=True` to use the pre-trained model instead of the random one: ``` # load the model and set it to evaluation mode model = vgg11_bn(pretrained=False).eval() ``` Compute the attribution using the ``EpsilonPlusFlat`` **Composite**: ``` # use the VGG-specific canonizer (alias for SequentialMergeBatchNorm, only # needed with batch-norm) canonizer = VGGCanonizer() # create a composite, specifying the canonizers, if any composite = EpsilonPlusFlat(canonizers=[canonizer]) # choose a target class for the attribution (label 437 is lighthouse) target = torch.eye(1000)[[437]] # create the attributor, specifying model and composite with Gradient(model=model, composite=composite) as attributor: # compute the model output and attribution output, attribution = attributor(data, target) print(f'Prediction: {output.argmax(1)[0].item()}') ``` Visualize the attribution: ``` # sum over the channels relevance = attribution.sum(1) # create an image of the visualize attribution img = imgify(relevance, symmetric=True, cmap='coldnhot') # show the image display(img) ``` Here, `imgify` produces a PIL-image, which can be saved with `.save()`. To directly save the visualized attribution, we can use `imsave` instead: ``` # directly save the visualized attribution imsave('attrib-1.png', relevance, symmetric=True, cmap='bwr') ```
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/tutorial/image-classification-vgg.ipynb
image-classification-vgg.ipynb
{{ fullname | escape | underline}} .. automodule:: {{ fullname }} :members: :show-inheritance: {% block attributes %} {% if attributes %} .. rubric:: {{ _('Module Attributes') }} .. autosummary:: :nosignatures: {% for item in attributes %} {{ item }} {%- endfor %} {% endif %} {% endblock %} {% block functions %} {% if functions %} .. rubric:: {{ _('Functions') }} .. autosummary:: :nosignatures: {% for item in functions %} {{ item }} {%- endfor %} {% endif %} {% endblock %} {% block classes %} {% if classes %} .. rubric:: {{ _('Classes') }} .. autosummary:: :nosignatures: {% for item in classes %} {{ item }} {%- endfor %} {% endif %} {% endblock %} {% block exceptions %} {% if exceptions %} .. rubric:: {{ _('Exceptions') }} .. autosummary:: :nosignatures: {% for item in exceptions %} {{ item }} {%- endfor %} {% endif %} {% endblock %} {% block modules %} {% if modules %} .. rubric:: Modules .. autosummary:: :toctree: :recursive: {% for item in modules %} {{ item }} {%- endfor %} {% endif %} {% endblock %}
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/_templates/modules.rst
modules.rst
========================== Writing Custom Attributors ========================== **Attributors** provide an additional layer of abstraction over the context of **Composites**, and are used to directly produce *attributions*, which may or may not be computed with modified gradients, if they are used, from **Composites**. More information on **Attributors**, examples and their use can be found in :doc:`/how-to/use-attributors`. **Attributors** can be used to implement non-layer-wise or only partly layer-wise attribution methods. For this, it is enough to define a subclass of :py:class:`zennit.attribution.Attributor` and implement its :py:meth:`~zennit.attribution.Attributor.forward` and optionally its :py:meth:`~zennit.attribution.Attributor.__init__` methods. :py:meth:`~zennit.attribution.Attributor.forward` takes 2 arguments, the tensor with respect to which the attribution shall be computed ``input``, and ``attr_output_fn``, which is a function that, given the output of the attributed model, computes the *gradient output* for the gradient computation, which is, for example, a one-hot encoding of the target label of the attributed input. When calling an :py:class:`~zennit.attribution.Attributor`, the ``__call__`` function will ensure ``forward`` receives a valid function to transform the output of the analyzed model to a tensor which can be used for the ``grad_output`` argument of :py:func:`torch.autograd.grad`. A constant tensor or function is provided by the user either to ``__init__`` or to ``__call__``. It is expected that :py:meth:`~zennit.attribution.Attributor.forward` will return a tuple containing, in order, the model output and the attribution. As an example, we can implement *gradient times input* in the following way: .. code-block:: python import torch from torchvision.models import vgg11 from zennit.attribution import Attributor class GradientTimesInput(Attributor): '''Model-agnostic gradient times input.''' def forward(self, input, attr_output_fn): '''Compute gradient times input.''' input_detached = input.detach().requires_grad_(True) output = self.model(input_detached) gradient, = torch.autograd.grad( (output,), (input_detached,), (attr_output_fn(output.detach()),) ) relevance = gradient * input return output, relevance model = vgg11() data = torch.randn((1, 3, 224, 224)) with GradientTimesInput(model) as attributor: output, relevance = attributor(data) :py:class:`~zennit.attribution.Attributor` accepts an optional :py:class:`~zennit.core.Composite`, which, if supplied, will always be used to create a context in ``__call__`` around ``forward``. For the ``GradientTimesInput`` class above, using a **Composite** will probably not produce anything useful, although more involved combinations of custom **Rules** and a custom **Attributor** can be used to implement complex attribution methods with both model-agnostic and layer-wise parts. The following shows an example of *sensitivity analysis*, which is the absolute value, with a custom ``__init__()`` where we can pass the argument ``sum_channels`` to specify whether the **Attributor** should sum over the channel dimension: .. code-block:: python import torch from torchvision.models import vgg11 from zennit.attribution import Attributor class SensitivityAnalysis(Attributor): '''Model-agnostic sensitivity analysis which optionally sums over color channels. ''' def __init__( self, model, sum_channels=False, composite=None, attr_output=None ): super().__init__( model, composite=composite, attr_output=attr_output ) self.sum_channels = sum_channels def forward(self, input, attr_output_fn): '''Compute the absolute gradient (or the sensitivity) and optionally sum over the color channels. ''' input_detached = input.detach().requires_grad_(True) output = self.model(input_detached) gradient, = torch.autograd.grad( (output,), (input_detached,), (attr_output_fn(output.detach()),) ) relevance = gradient.abs() if self.sum_channels: relevance = relevance.sum(1) return output, relevance model = vgg11() data = torch.randn((1, 3, 224, 224)) with SensitivityAnalysis(model, sum_channels=True) as attributor: output, relevance = attributor(data)
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/write-custom-attributors.rst
write-custom-attributors.rst
========================= Writing Custom Composites ========================= Zennit provides a number of commonly used **Composites**. While these are often enough for feed-forward-type neural networks, one primary goal of Zennit is to provide the tools to easily customize the computation of rule-based attribution methods. This is especially useful to analyze novel architectures, for which no attribution-approach has been designed before. For most use-cases, using the abstract **Composites** :py:class:`~zennit.composites.LayerMapComposite`, :py:class:`~zennit.composites.SpecialFirstLayerMapComposite`, and :py:class:`~zennit.composites.NameMapComposite` already provides enough freedom to customize which Layer should receive which rule. See :ref:`use-composites` for an introduction. Depending on the setup, it may however be more convenient to either directly use or implement a new **Composite** by creating a Subclass from :py:class:`zennit.core.Composite`. In either case, the :py:class:`~zennit.core.Composite` requires an argument ``module_map``, which is a function with the signature ``(ctx: dict, name: str, module: torch.nn.Module) -> Hook or None``, which, given a context dict, the name of a single module and the module itself, either returns an instance of :py:class:`~zennit.core.Hook` which should be copied and registered to the module, or ``None`` if no ``Hook`` should be applied. The context dict ``ctx`` can be used to track subsequent calls to the ``module_map`` function, e.g. to count the number of processed modules, or to verify if some condition has been met before, e.g. a linear layer has been seen before. The ``module_map`` is used in :py:meth:`zennit.core.Composite.register`, where the context dict is initialized to an empty dict ``{}`` before iterating over all the sub-modules of the root-module to which the composite will be registered. The iteration is done using :py:meth:`torch.nn.Module.named_modules`, which will therefore dictate the order modules are visited, which is depth-first in the order sub-modules were assigned. A simple **Composite**, which only provides rules for linear layers that are leaves and bases the rule on how many leaf modules were visited before could be implemented like the following: .. code-block:: python import torch from torchvision.models import vgg16 from zennit.rules import Epsilon, AlphaBeta from zennit.types import Linear from zennit.core import Composite from zennit.attribution import Gradient def module_map(ctx, name, module): # check whether there is at least one child, i.e. the module is not a leaf try: next(module.children()) except StopIteration: # StopIteration is raised if the iterator has no more elements, # which means in this case there are no children and module is a leaf pass else: # if StopIteration is not raised on the first element, module is not a leaf return None # if the module is not Linear, we do not want to assign a hook if not isinstance(module, Linear): return None # count the number of the leaves processed yet in 'leafnum' if 'leafnum' not in ctx: ctx['leafnum'] = 0 else: ctx['leafnum'] += 1 # the first 10 leaf-modules which are of type Linear should be assigned # the Alpha2Beta1 rule if ctx['leafnum'] < 10: return AlphaBeta(alpha=2, beta=1) # all other rules should be assigned Epsilon return Epsilon(epsilon=1e-3) # we can then create a composite by passing the module_map function # canonizers may also be passed as with all composites composite = Composite(module_map=module_map) # try out the composite model = vgg16() with Gradient(model, composite) as attributor: out, grad = attributor(torch.randn(1, 3, 224, 224)) A more general **Composite**, where we can specify which layer number and which type should be assigned which rule, can be implemented by creating a class: .. code-block:: python from itertools import islice import torch from torchvision.models import vgg16 from zennit.rules import Epsilon, ZBox, Gamma, Pass, Norm from zennit.types import Linear, Convolution, Activation, AvgPool from zennit.core import Composite from zennit.attribution import Gradient class LeafNumberTypeComposite(Composite): def __init__(self, leafnum_map): # pass the class method self.mapping as the module_map super().__init__(module_map=self.mapping) # set the instance attribute so we can use it in self.mapping self.leafnum_map = leafnum_map def mapping(self, ctx, name, module): # check whether there is at least one child, i.e. the module is not a leaf # but this time shorter using itertools.islice to get at most one child if list(islice(module.children(), 1)): return None # count the number of the leaves processed yet in 'leafnum' # this time in a single line with get and all layers count, e.g. ReLU ctx['leafnum'] = ctx.get('leafnum', -1) + 1 # loop over the leafnum_map and use the first template for which # the module type matches and the current ctx['leafnum'] falls into # the bounds for (low, high), dtype, template in self.leafnum_map: if isinstance(module, dtype) and low <= ctx['leafnum'] < high: return template # if none of the leafnum_map apply this means there is no rule # matching the current layer return None # this can be compared with int and will always be larger inf = float('inf') # we create an example leafnum-map, note that Linear is here # zennit.types.Linear and not torch.nn.Linear # the first two entries are for demonstration only and would # in practice most likely be a single "Linear" with appropriate low/high leafnum_map = [ [(0, 1), Convolution, ZBox(low=-3.0, high=3.0)], [(0, 1), torch.nn.Linear, ZBox(low=0.0, high=1.0)], [(1, 17), Linear, Gamma(gamma=0.25)], [(17, 31), Linear, Epsilon(epsilon=0.5)], [(31, inf), Linear, Epsilon(epsilon=1e-9)], # catch all activations [(0, inf), Activation, Pass()], # explicit None is possible e.g. to (ab-)use precedence [(0, 17), torch.nn.MaxPool2d, None], # catch all AvgPool/MaxPool2d, isinstance also accepts tuples of types [(0, inf), (AvgPool, torch.nn.MaxPool2d), Norm()], ] # finally, create the composite using the leafnum_map composite = LeafNumberTypeComposite(leafnum_map) # try out the composite model = vgg16() with Gradient(model, composite) as attributor: out, grad = attributor(torch.randn(1, 3, 224, 224)) In practice, however, we do not recommend to use the index of the layer when designing **Composites**, because most of the time, when such a configuration is chosen, it is done to shape the **Composite** for an explicit model. For these kinds of **Composites**, a :py:class:`~zennit.composites.NameMapComposite` will directly map the name of a sub-module to a Hook, which is a more explicit and transparent way to create a special **Composite** for a single neural network.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/write-custom-composites.rst
write-custom-composites.rst
================= Using Attributors ================= **Attributors** are used to both shorten Zennit's common ``composite.context -> gradient`` approach, as well as provide model-agnostic attribution approaches. Available **Attributors** can be found in :py:mod:`zennit.attribution`, some of which are: * :py:class:`~zennit.attribution.Gradient`, which computes the gradient * :py:class:`~zennit.attribution.IntegratedGradients`, which computes the Integrated Gradients * :py:class:`~zennit.attribution.SmoothGrad`, which computes SmoothGrad * :py:class:`~zennit.attribution.Occlusion`, which computes the attribution based on the model output activation values when occluding parts of the input with a sliding window Using the basic :py:class:`~zennit.attribution.Gradient`, the unmodified gradient may be computed with: .. code-block:: python import torch from torch.nn import Sequential, Conv2d, ReLU, Linear, Flatten from zennit.attribution import Gradient # setup the model model = Sequential( Conv2d(3, 8, 3, padding=1), ReLU(), Conv2d(8, 16, 3, padding=1), ReLU(), Flatten(), Linear(16 * 32 * 32, 1024), ReLU(), Linear(1024, 10), ) # some random input data input = torch.randn(1, 3, 32, 32, requires_grad=True) # compute the gradient and output using the Gradient attributor with Gradient(model) as attributor: output, relevance = attributor(input) Computing attributions using a composite can be done with: .. code-block:: python from zennit.composites import EpsilonPlusFlat # prepare the composite composite = EpsilonPlusFlat() # compute the gradient within the composite's context, i.e. the # EpsilonPlusFlat LRP relevance with Gradient(model, composite) as attributor: # torch.eye is used here to get a one-hot encoding of the # first (index 0) label output, relevance = attributor(input, torch.eye(10)[[0]]) which uses the second argument ``attr_output_fn`` of the call to :py:class:`~zennit.attribution.Attributor` to specify a constant tensor used for the *output relevance* (i.e. ``grad_output``), but alternatively, a function of the output may also be used: .. code-block:: python def one_hot_max(output): '''Get the one-hot encoded max at the original indices in dim=1''' values, indices = output.max(1) return values[:, None] * torch.eye(output.shape[1])[indices] with Gradient(model) as attributor: output, relevance = attributor(input, one_hot_max) The constructor of :py:class:`~zennit.attribution.Attributor` also has a third argument ``attr_output``, which also can either be a constant :py:class:`~torch.Tensor`, or a function of the model's output and specifies which *output relevance* (i.e. ``grad_output``) should be used by default. When not supplying anything, the default will be the *identity*. If the default should be for example ones for all outputs, one could write: .. code-block:: python # compute the gradient and output using the Gradient attributor, and with # a vector of ones as grad_output with Gradient(model, attr_output=torch.ones_like) as attributor: output, relevance = attributor(input) Gradient-based **Attributors** like :py:class:`~zennit.attribution.IntegratedGradients` and :py:class:`~zennit.attribution.SmoothGrad` may also be used together with composites to produce *hybrid attributions*: .. code-block:: python from zennit.attribution import SmoothGrad # prepare the composite composite = EpsilonPlusFlat() # do a *smooth* version of EpsilonPlusFlat LRP by using the SmoothGrad # attributor in combination with the composite with SmoothGrad(model, composite, noise_level=0.1, n_iter=20) as attributor: output, relevance = attributor(input, torch.eye(10)[[0]]) which in this case will sample 20 samples in an epsilon-ball (size controlled with `noise_level`) around the input. Note that for Zennit's implementation of :py:class:`~zennit.attribution.SmoothGrad`, the first sample will always be the original input, i.e. ``SmoothGrad(model, n_iter=1)`` will produce the plain gradient as ``Gradient(model)`` would. :py:class:`~zennit.attribution.Occlusion` will move a sliding window with arbitrary size and strides over an input with any dimensionality. In addition to specifying window-size and strides, a function may be specified, which will be supplied with the input and a mask. When using the default, everything within the sliding window will be set to zero. A function :py:func:`zennit.attribution.occlude_independent` is available to simplify the process of specifying how to fill the window, and to invert the window if desired. The following adds some gaussian noise to the area within the sliding window: .. code-block:: python from functools import partial from zennit.attribution import Occlusion, occlude_independent input = torch.randn((16, 3, 32, 32)) attributor = Occlusion( model, window=8, # 8x8 overlapping windows stride=4, # with strides 4x4 occlusion_fn=partial( # occlusion_fn gets the full input and a mask occlude_independent, # applies fill_fn at provided mask fill_fn=lambda x: x * torch.randn_like(x) * 0.2, # add some noise invert=False # do not invert, i.e. occlude *within* mask ) ) with attributor: # for occlusion, the score for each window-pass is the sum of the # provided *grad_output*, which we choose as the model output at index 0 output, relevance = attributor(input, lambda out: torch.eye(10)[[0]] * out) Note that while the interface allows to pass a composite for any :py:class:`~zennit.attribution.Attributor`, using a composite with :py:class:`~zennit.attribution.Occlusion` does not change the outcome, as it does not utilize the gradient. An introduction on how to write custom **Attributors** can be found at :doc:`/how-to/write-custom-attributors`.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/use-attributors.rst
use-attributors.rst
=================== Visualizing Results =================== While not limited to any domain in particular, attribution methods are most commonly applied on 2-dimensional image data. For this reason, Zennit implements a few functions to aid in the visualization of attributions of image data as heatmaps. These methods may be found in :py:mod:`zennit.image`. To simply save tensors that can be represented as images (1 or 3 channels, 2 dimensions), with or without heatmap, :py:func:`zennit.image.imsave` may be used. Let us consider the following setting which simulates image data: .. code-block:: python import torch from torch.nn import Sequential, Conv2d, ReLU, Linear, Flatten from zennit.attribution import Gradient # setup the model model = Sequential( Conv2d(3, 8, 3, padding=1), ReLU(), Conv2d(8, 16, 3, padding=1), ReLU(), Flatten(), Linear(16 * 32 * 32, 1024), ReLU(), Linear(1024, 10), ) # some random input data input = torch.randn(8, 3, 32, 32, requires_grad=True) # compute the gradient and output using the Gradient attributor with Gradient(model) as attributor: output, relevance = attributor(input) The relevance has the same shape as the input, which here is ``(8, 3, 32, 32)``. We can save the output and relevance, with all color-information intact, by simply doing: .. code-block:: python from zennit.image import imsave for n, (inp, rel) in enumerate(zip(input, relevance)): imsave(f'input_{n:03d}.png', inp.detach()) imsave(f'relevance_{n:03d}.png', rel) Alternatively, the images may be composed as a grid, and saved as a single image: .. code-block:: python imsave('input_grid.png', input.detach(), grid=True) imsave('relevance_grid.png', relevance, grid=(2, 4)) The keyword argument ``grid`` may either be *boolean*, or the 2d shape of the image grid. While this works well for the input, it is hard to interpret the attribution from the resulting images. Be aware that commonly input images are pre-processed before they are fed into networks. While clipping and scaling the image pose no problem for its visibility, normalization will change the look of the image greatly. Therefore, when saving images during training or inference, it is recommended to visualize input images either **before** applying the normalization, or after applying the *inverse* of the normalization. :py:func:`~zennit.image.imsave` uses :py:func:`zennit.image.imgify`, which, given a :py:class:`numpy.ndarray` or a :py:class:`torch.Tensor`, will return a Pillow image, which can also be used to quickly look at the image without saving it: .. code-block:: python from zennit.image import imgify image = imgify(input.detach(), grid=True) image.show() Heatmap Normalization ^^^^^^^^^^^^^^^^^^^^^ Commonly, a heatmap of the attribution is produced by removing the color-channel either by taking the (absolute) sum and normalizing to fit into an interval. :py:func:`~zennit.image.imsave` (through :py:func:`~zennit.image.imgify`) will shift and scale the input such that the full range of colors is used, using the input's minimum and maximum respectively. This can be tweaked by supplying the ``vmin`` and ``vmax`` keyword arguments: .. code-block:: python absrel = relevance.abs().sum(1) # vmin and vmax works for both imsave and imgify imsave('relevance_abs_0.png', absrel[0], vmin=0, vmax=absrel[0].amax()) image = imgify(absrel[0], vmin=0, vmax=absrel[0].amax()) image.show() Another way to normalize the attribution which can be used with both :py:func:`~zennit.image.imsave` and :py:func:`~zennit.image.imgify` is to use the ``symmetric`` keyword argument, which provides two normalization strategies: ``symmetric=False`` (default) and ``symmetric=True``. Keep in mind that the normalization of the attribution can greatly change how interpretable the heatmap will be. Let us consider a more interesting image to compare the two normalization strategies with signed and unsigned data: .. code-block:: python from itertools import product grid = torch.stack(torch.meshgrid(*((torch.linspace(-1, 1, 128),) * 2), indexing='xy')) dist = ((grid + 0.25) ** 2).sum(0, keepdims=True) ** .5 ripples = (dist * 5 * torch.pi).cos().clip(-.5, 1.) * (-dist).exp() for norm, sign in product(('symmetric', 'unaligned'), ('signed', 'absolute')): array = ripples.abs() if sign == 'absolute' else ripples symmetric = norm == 'symmetric' imsave(f'ripples_{norm}_{sign}_bwr.png', array, symmetric=symmetric) imsave(f'ripples_{norm}_{sign}_wred.png', array, symmetric=symmetric, cmap='wred') The keyword argument ``cmap`` is used to control the **color map**. .. datatemplate:import-module:: importlib {% set join = data.import_module('os.path').join %} {% set torch = data.import_module('torch') %} {% set imsave = data.import_module('zennit.image').imsave %} {% set grid = torch.stack(torch.meshgrid(*((torch.linspace(-1, 1, 128),) * 2), indexing='xy')) %} {% set dist = ((grid + 0.25) ** 2).sum(0, keepdims=True) ** 0.5 %} {% set ripples = (dist * 5 * torch.pi).cos().clip(-0.5, 1.0) * (-dist).exp() %} {% macro generate_image(norm, cmap, sign) %} {% set name = 'ripples_{norm}_{cmap}_{sign}'.format(norm=norm, cmap=cmap, sign=sign) %} {% set filename = join(env.app.config.generated_path, '{name}.png'.format(name=name)) %} {% set _ = imsave(join(env.app.builder.srcdir, filename), (ripples ** 2) ** 0.5 if sign == 'absolute' else ripples, symmetric=norm == 'symmetric', cmap=cmap) %} .. |{{name}}| image:: /{{filename}} {% endmacro %} {% set norms = ('symmetric', 'unaligned') %} {% set cmaps = ('bwr', 'wred') %} {% set signs = ('signed', 'absolute') %} {% for norm in norms %} {% for cmap in cmaps %} {% for sign in signs %} {{ generate_image(norm=norm, cmap=cmap, sign=sign) }} {% endfor %} {% endfor %} {% endfor %} .. csv-table:: :widths: auto :width: 95% :header-rows: 2 :stub-columns: 1 {% for norm in norms -%} {%- for sign in signs -%} , ``symmetric={{norm == 'symmetric'}}`` {%- endfor -%} {%- endfor %} {% for norm in norms -%} {%- for sign in signs -%} , {{sign}} {%- endfor -%} {%- endfor %} {%- for cmap in cmaps %} ``cmap='{{cmap}}'`` {%- for norm in norms -%} {%- for sign in signs -%} , |{{ 'ripples_{norm}_{cmap}_{sign}'.format(norm=norm, cmap=cmap, sign=sign) }}| {%- endfor -%} {%- endfor %} {%- endfor %} Negative values were clipped to better see how the normalization modes work. The default color map is ``'bwr'``, which maps 0.0 to blue, 0.5 to white and 1.0 to red, which means it is a *signed* color map, as the center of 0.5 is a neutral point, with color intensities rising for values below and above. Color map ``'wred'`` maps 0.0 to white and 1.0 to red, which makes it an *unsigned* color map, as its color intensity is monotonically increasing. Using ``symmetric=False`` will simply map ``[min, max]`` to ``[0., 1.]``, i.e the minimum value to 0.0, and the maximum value to 1.0. This works best with *unsigned* color maps, when relevance is assumed to be monotonically increasing and a value of 0.0 does not have any special meaning. ``symmetric=True`` will find the absolute maximum per image, and will map the input range ``[-absmax, absmax]`` to ``[0., 1.]``. This means that the result will be centered around 0.5, which works best with *signed* color maps (like ``'bwr'``), as positive (here red) and negative (here blue) intensities in the produced heatmap are made comparable. In the example above, our input is in the range ``[-0.5, 1.0]``. If the negative and positive values are meaningful (generally the case for attribution methods), and the color map has a meaningful value at 0.5 (i.e. is *signed*), ``symmetric=True`` is usually the best choice for normalization. For ``symmetric=False`` the example above shows that with ``'bwr'`` gives the illusion of a shifted center, which makes it look like the attribution is predominantly negative. Using the *monotonic* ``wred`` is normally the better choice for the ``symmetric=False``, but with signed attributions the results are not as clear as they can be. Finally, the example above shows the different outcomes when the input is **signed** or its **absolute** is taken. Using ``vmin`` and ``vmax`` overrides the minimum and maximum values respectively determined by the normalization mode. This means that, for example, using ``vmin=0`` (and not setting vmax) with ``symmetric=True`` will clip all values below 0. Another useful setting is when the input is positive (or its absolute value was taken) to use ``vmin=0`` with ``symmetric=False``, as this will give the full range from 0 to the maximum value, since the smallest value may be larger than 0 when in cases where it is known that 0 would be the smallest possible value. This shows the importance of the choice of the normalization and the color map. Color Maps ^^^^^^^^^^ Color maps play an essential role in the production of heatmaps which highlight points of interest best. With the normalization modes we have seen the built-in signed color map ``bwr`` (blue-white-red) and unsigned color map ``wred`` (white-red). All built-in color maps are defined in :py:obj:`zennit.image.CMAPS`. .. datatemplate:import-module:: importlib {% set join = data.import_module('os.path').join %} {% set torch = data.import_module('torch') %} {% set imsave = data.import_module('zennit.image').imsave %} {% set image = torch.arange(256)[None].repeat((32, 1)) %} {% macro generate_palette(cmap) %} {% set name = 'palette_{cmap}'.format(cmap=cmap) %} {% set filename = join(env.app.config.generated_path, '{name}.png'.format(name=name)) %} {% set _ = imsave(join(env.app.builder.srcdir, filename), image, cmap=cmap) %} .. |{{name}}| image:: /{{filename}} {% endmacro %} {%- macro img(cmap) -%} {{'|palette_{cmap}|'.format(cmap=cmap)}} {%- endmacro -%} {% set cmaps = ('gray', 'wred', 'wblue', 'hot', 'cold', 'coldnhot', 'bwr', 'france', 'seismic', 'coolio', 'coleus') %} {% for cmap in cmaps %} {{ generate_palette(cmap=cmap) }} {% endfor %} The built-in *unsigned* color maps are: .. csv-table:: :widths: 5, 10, 10 :width: 95% :header-rows: 1 :delim: ; Identifier; CMSL-Source; Visualization ``'gray'``; ``'000,fff'``; {{img('gray')}} ``'wred'``; ``'fff,f00'``; {{img('wred')}} ``'wblue'``; ``'fff,00f'``; {{img('wblue')}} ``'hot'``; ``'000,f00,ff0,fff'``; {{img('hot')}} ``'cold'``; ``'000,00f,0ff'``; {{img('cold')}} and the built-in *signed* color maps are: .. csv-table:: :widths: 5, 10, 10 :width: 95% :header-rows: 1 :delim: ; Identifier; CMSL-Source; Visualization ``'coldnhot'``; ``'0ff,00f,80:000,f00,ff0,fff'``; {{img('coldnhot')}} ``'bwr'``; ``'00f,80:fff,f00'``; {{img('bwr')}} ``'france'``; ``'0055a4,80:fff,ef4135'``; {{img('france')}} ``'seismic'``; ``'0000d0,80:d0d0d0,d00000'``; {{img('seismic')}} ``'coolio'``; ``'00d0d0,80:d0d0d0,d000d0'``; {{img('coolio')}} ``'coleus'``; ``'00d000,80:d0d0d0,d000d0'``; {{img('coleus')}} *CMSL-Source* is the source code of the color map in *Color-Map Specification Language (CMSL)*. The color map for :py:func:`~zennit.image.imsave` and :py:func:`~zennit.image.imgify` may be specified in one of three ways: the identifier of a *built-in* color map (``wred``, ``coldnhot``, ...), a string containing CMSL source-code, or a :py:class:`zennit.cmap.ColorMap` instance. .. code-block:: python from zennit.cmap import ColorMap bar = torch.arange(256)[None].repeat((32, 1)) imsave('bar_wred.png', bar, cmap='wred') imsave('bar_string.png', bar, cmap='ff0,00f') cmap = ColorMap('000,f00,fff') imsave('bar_cmap.png', bar, cmap=cmap) Color-Map Specification Language ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *Color-Map Specification Language (CMSL)* is a domain-specific language to describe color maps in a quick and compact manner. It is implemented in :py:mod:`zennit.cmap`. Color-maps can be compiled using the :py:class:`zennit.cmap.ColorMap` class, of which the constructor expects CMSL source code as a string. Alternatively, a ColorMap instance may be obtained using :py:func:`zennit.image.get_cmap`, which first looks up its argument string in the built-in color-map dictionary :py:obj:`zennit.image.CMAPS`, and, if it fails, tries to compile the string as CMSL source code. .. code-block:: python from zennit.cmap import ColorMap from zennit.image import get_cmap bar = torch.arange(256)[None].repeat((32, 1)) cmap1 = ColorMap('000,a0:f00,fff') cmap2 = get_cmap('1f:fff,f0f,000') img1 = imgify(bar, cmap=cmap1) img2 = imgify(bar, cmap=cmap2) img1.show() img2.show() CMSL follows a simple grammar: .. productionlist:: cmsl_cmap: color_node ("," color_node)+ color_node: [index ":"] rgb_color index: half | full rgb_color: half half half | full full full full: half half half: <single hex digit 0-9a-fA-F> Values for both ``index`` and ``rgb_color`` are specified as hexadecimal values with either one (``half``) or two (``full``) digits, where ``index`` consists of a single value 0-255 (or half 0-15) and ``rgb_color`` consists of 3 values 0-255 (or half 0-15). The ``index`` of all ``color_nodes`` must be in **ascending** order. It describes the color-index of the color-map, where ``00`` (or half ``0``) is the lowest value and ``ff`` (i.e. decimal 255, or half ``f``) is the highest value. The same value of ``index`` may be repeated to produce hard color-transitions, however, using the same value of ``index`` more than twice will only use the two outermost color values. If the indices of the first or last ``color_nodes`` are omitted, they will be assumed as ``00`` and ``ff`` respectively. Two additional ``color_nodes`` with the same color as the ones with lowest and highest ``index`` will be implicitly created at indices ``00`` and ``ff`` respectively, which means that if the lowest and/or highest specified color node indices are larger or smaller than ``00`` or ``ff`` respectively, the colors between ``00`` and the lowest index, and the highest index and ``ff`` will be constant. A color map needs at least two ``color_nodes`` (i.e., a *useless* single-color color-map cannot be created by specifying a single ``color_node``). A color node will produce a color of its ``rgb_color`` for the value of its index. Colors for values between two color nodes will be linearly interpolated between their two colors, weighted by their respective proximity. Color nodes without indices will evenly spaced between color nodes with indices. The first and last color nodes, if not equipped with an index, will be assumed as ``00`` and ``ff`` respectively. While technically there does not exist a syntactic difference between *signed* and *unsigned* color maps, *signed* color maps often require a color node at the central index ``80``, while *unsigned* color maps should have monotonically increasing or decreasing intensities, which can be most easily done by only specifying two color nodes. The built-in color map ``cold`` could be understood as a *signed* color map, since it has an explicit color node ``blue`` at its center. Visually, however, due to its monotonicity, it is hard to interpret as such. The following shows a few examples of color maps along their CMSL source code: .. datatemplate:import-module:: importlib {% set enumerate = data.import_module('builtins').enumerate %} {% set join = data.import_module('os.path').join %} {% set torch = data.import_module('torch') %} {% set imsave = data.import_module('zennit.image').imsave %} {% set image = torch.arange(256)[None].repeat((32, 1)) %} {% macro generate_palette(identifier, cmap) %} {% set name = 'palette_c{identifier}'.format(identifier=identifier) %} {% set filename = join(env.app.config.generated_path, '{name}.png'.format(name=name)) %} {% set _ = imsave(join(env.app.builder.srcdir, filename), image, cmap=cmap) %} .. |{{name}}| image:: /{{filename}} {% endmacro %} {%- macro img(identifier) -%} {{'|palette_c{identifier}|'.format(identifier=identifier)}} {%- endmacro -%} {% set cmaps = [ '000,a0:f00,fff', '3f:ccc,f0f,000', '55:00f,55:ddd,aa:ddd,aa:f00', '2:800,2:f00,4:f00,80:fff,b:00f,d:00f,d:008', 'ff00ff,00a000', 'fff,000,fff,000,fff,d:000,fff,e:000,fff,000,fff,000', ] %} {% for n, cmap in enumerate(cmaps) %} {{ generate_palette(n, cmap) }} {% endfor %} .. csv-table:: :widths: 10, 10 :width: 95% :header-rows: 1 :delim: ; CMSL-Source; Visualization {% for n, cmap in enumerate(cmaps) %} ``'{{cmap}}'``; {{img(n)}} {% endfor %} Additionally, :py:class:`zennit.cmap.LazyColorMapCache` may be used to define color maps in bulk, and lazily compile them when they are accessed the first time. This is the way the built-in color maps are defined in :py:obj:`zennit.image.CMAPS`. .. code-block:: python from zennit.cmap import LazyColorMapCache cmaps = LazyColorMapCache({ 'reds': '111,f11', 'blues': '111,11f', 'greens': '111,1f1', }) img = imgify(ripples, cmap=cmaps['greens']) img.show() :py:class:`~zennit.cmap.LazyColorMapCache` stores the specified source code for each key, and if accesed with `cmaps[key]`, it will either compile the :py:class:`~zennit.cmap.ColorMap`, cache it if it has not been accessed before and return it, or it will return the previously cached :py:class:`~zennit.cmap.ColorMap`. Changing Palettes ^^^^^^^^^^^^^^^^^ When using :py:func:`~zennit.image.imgify` (or :py:func:`~zennit.image.imsave`), arrays with a single channel are converted to PIL images in palette mode (``P``), where the palette specifies the color map. This means that the *color map* of an image may be changed later without modifying its values. The palette for a color map can be generated using its :py:func:`zennit.cmap.ColorMap.palette` method. :py:func:`~zennit.cmap.ColorMap.palette` accepts an optional argument ``level`` (default 1.0), with which the resulting palette can be either stretched or compressed, resulting in heatmaps where either the maximum value threshold is moved closer to the center (`level > 1.0`) or farther away from it (`0.0 < level < 1.0`). A value of ``level=2.0`` proved to better highlight high values of a heatmap in print. .. code-block:: python img = imgify(ripples, symmetric=True) img.show() cmap = ColorMap('111,1f1') pal = cmap.palette(level=1.0) img.putpalette(pal) img.show() The convenience function :py:func:`zennit.image.palette` may also be used to directly get the palette from a built-in color map name or CMSL source code. This way, existing PNG-files of heatmaps may thus also be modified to use different color maps by changing their palette: .. code-block:: python from PIL import Image from zennit.image import palette # store a heatmap fname = 'newheatmap.png' imsave(fname, ripples, symmetric=True) # load the heatmap, change the palette and write it to the same file img = Image.open(fname) img = img.convert('P') pal = palette('f1f,111,ff1', level=1.0) img.putpalette(pal) img.save(fname) A utility CLI script which changes the color map is provided in :repo:`share/scripts/palette_swap.py`, which can be used in the following way: .. code-block:: shell $ python share/scripts/palette_swap.py newheatmap \ --cmap 'f1f,111,ff1' \ --level 1.0
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/visualize-results.rst
visualize-results.rst
========================= Writing Custom Canonizers ========================= **Canonizers** are used to temporarily transform models into a canonical form to mitigate the lack of implementation invariance of methods Layerwise Relevance Propagation (LRP). A general introduction to **Canonizers** can be found here: :ref:`use-canonizers`. As both **Canonizers** and **Composites** (via **Rules**) change the outcome of the attribution, it can be a little bit confusing in the beginning when challenged with the question whether a novel network architectures needs a new set of **Rules** and **Composites**, or if it should be adapted to the existing framework using **Canonizers**. While ultimately it depends on the design preference of the developer, our suggestion is to go through the following steps in order: 1. Check whether a custom **Composite** is enough to correctly attribute the model, i.e. the new layer-type is only a composition of existing layer types without any unaccounted intermediate steps or incapabilities with existing rules. 2. If some of the rules which should be used are incompatible without changes (e.g. subsequent linear layers), or some parts of a module has intermediate computations that are not implemented with sub-modules, it should be checked whether a **Canonizer** can be implemented to fix these issues. If you are in control of the module in question, check whether rewriting the module with sub-modules is easier than implementing a **Canonizer**. 3. If the module consists of computations which cannot be separated into existing modules with compatible rules, or would result in an overly complex architecture, a custom **Rule** may be the choice to go with. **Rules** and **Composites** are not designed to change the forward computation of a model. While **Canonizers** can change the outcome of the forward pass, this should be used with care, since a modified function output means that the function itself has been modified, which will therefore result in an attribution of the modified function instead. To implement a custom **Canonizer**, a class inheriting from :py:class:`zennit.canonizers.Canonizer` needs to implement the following four methods: * :py:meth:`~zennit.canonizers.Canonizer.apply`, which finds the sub-modules that should be modified by the **Canonizer** and passes their information to ... * :py:meth:`~zennit.canonizers.Canonizer.register`, which copies the current instance using :py:meth:`~zennit.canonizers.Canonizer.copy`, applies the changes that should be introduced by the **Canonizer**, and makes sure they can be reverted later, using ... * :py:meth:`~zennit.canonizers.Canonizer.remove`, which reverts the changes introduced by the **Canonizer**, by i.e. loading the original parameters which were temporarily stored, and * :py:meth:`~zennit.canonizers.Canonizer.copy`, which copies the current instance, to create an individual instance for each applicable module with the same parameters. Suppose we have a ReLU model (e.g. VGG11) for which we want to compute the second-order derivative, e.g. to find an adversarial explanation (see :cite:p:`dombrowski2019explanations`). The ReLU is not differentiable at 0, and its second order derivative is zero everywhere except at 0, where it is undefined. :cite:t:`dombrowski2019explanations` replace the ReLU activations in a model with *Softplus* activations, which when running *beta* towards infinity will be identical to the ReLU activation. For the numerical estimate, it is enough to set *beta* to a relatively large value, e.g. to 10. The following is an implementation of the **SoftplusCanonizer**, which will temporarily replace the ReLU activations in a model with Softplus activations: .. code-block:: python import torch from zennit.canonizers import Canonizer class SoftplusCanonizer(Canonizer): '''Replaces ReLUs with Softplus units.''' def __init__(self, beta=10.): self.beta = beta self.module = None self.relu_children = None def apply(self, root_module): '''Iterate all modules under root_module and register the Canonizer if they have immediate ReLU sub-modules. ''' # track the SoftplusCanonizer instances to remove them later instances = [] # iterate recursively over all modules for module in root_module.modules(): # get all the direct sub-module instances of torch.nn.ReLU relu_children = [ (name, child) for name, child in module.named_children() if isinstance(child, torch.nn.ReLU) ] # if there is at least on direct ReLU sub-module if relu_children: # create a copy (with the same beta parameter) instance = self.copy() # register the module instance.register(module, relu_children) # add the copy to the instance list instances.append(instance) return instances def register(self, module, relu_children): '''Store the module and the immediate ReLU-sub-modules, and then overwrite the attributes that corresponds to each ReLU-sub-modules with a new instance of ``torch.nn.Softplus``. ''' self.module = module self.relu_children = relu_children for name, _ in relu_children: # set each of the attributes corresponding to the ReLU to a new # instance of toch.nn.Softplus setattr(module, name, torch.nn.Softplus(beta=self.beta)) def remove(self): '''Undo the changes introduces by this Canonizer, by setting the appropriate attributes of the stored module back to the original ReLU sub-module instance. ''' for name, child in self.relu_children: setattr(self.module, name, child) def copy(self): '''Create a copy of this instance. Each module requires its own instance to call ``.register``. ''' return SoftplusCanonizer(beta=self.beta) Note that we can only replace modules by changing their immediate parent. This means that if ``root_module`` was a ``torch.nn.ReLU`` itself, it would be impossible to replace it with a ``torch.nn.Softplus`` without replacing the ``root_module`` itself. For demonstration purposes, we can compute the gradient w.r.t. a loss which uses the gradient of the modified model in the following way: .. code-block:: python import torch from torchvision.models import vgg11 from zennit.core import Composite from zennit.image import imgify # create a VGG11 model with random parameters model = vgg11() # use the Canonizer with an "empty" Composite (without specifying # module_map), which will not attach rules to any sub-module, thus resulting # in a plain gradient computation, but with a Canonizer applied composite = Composite( canonizers=[SoftplusCanonizer()] ) input = torch.randn(1, 3, 224, 224, requires_grad=True) target = torch.eye(1000)[[0]] with composite.context(model) as modified_model: out = modified_model(input) relevance, = torch.autograd.grad(out, input, target, create_graph=True) # find adversarial example such that input and its respective # attribution are close loss = ((relevance - input.detach()) ** 2).mean() # compute the gradient of input w.r.t. loss, using the second order # derivative w.r.t. input; note that this currently does not work when # using BasicHook, which detaches the gradient to avoid wrong values adv_grad, = torch.autograd.grad(loss, input) # visualize adv_grad imgify(adv_grad[0].abs().sum(0), cmap='hot').show()
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/write-custom-canonizers.rst
write-custom-canonizers.rst
============================== Getting Intermediate Relevance ============================== In some cases, intermediate gradients or relevances of a model may be needed. Since Zennit uses Pytorch's autograd engine, intermediate relevances can be retained simply as the intermediate gradients of accessible non-leaf tensors in the tensor's ``.grad`` attribute by calling ``tensor.retain_grad()`` before the gradient computation. In most cases when using ``torch.nn.Module``-based models, the intermediate outputs are not easily accessible, which we can solve by using forward-hooks. We create following setting with some random input data and a simple, randomly initialized model, for which we want to compute the LRP EpsilonPlus relevance: .. code-block:: python import torch from torch.nn import Sequential, Conv2d, ReLU, Linear, Flatten from zennit.attribution import Gradient from zennit.composites import EpsilonPlusFlat # setup the model and data model = Sequential( Conv2d(3, 10, 3, padding=1), ReLU(), Flatten(), Linear(10 * 32 * 32, 10), ) input = torch.randn(1, 3, 32, 32) # make sure the input requires a gradient input.requires_grad = True # create a composite instance composite = EpsilonPlusFlat() # create a gradient attributor attributor = Gradient(model, composite) Now we create a function ``store_hook`` which we register as a forward hook to all modules. The function sets the module's attribute ``.output`` to its output tensor, and ensures the gradient is stored in the tensor's ``.grad`` attribute even if it is not a leaf-tensor by using ``.retain_grad()``. .. code-block:: python # create a hook to keep track of intermediate outputs def store_hook(module, input, output): # set the current module's attribute 'output' to the its tensor module.output = output # keep the output tensor gradient, even if it is not a leaf-tensor output.retain_grad() # enter the attributor's context to register the rule-hooks with attributor: # register the store_hook AFTER the rule-hooks have been registered (by # entering the context) so we get the last output before the next module handles = [ module.register_forward_hook(store_hook) for module in model.modules() ] # compute the relevance wrt. output/class 1 output, relevance = attributor(input, torch.eye(10)[[1]]) # remove the hooks using store_hook for handle in handles: handle.remove() # print the gradient tensors for demonstration for name, module in model.named_modules(): print(f'{name}: {module.output.grad}') The hooks are registered within the attributor's with-context, such that they are applied after the rule hooks. Once we are finished, we can remove the store-hooks by calling ``.remove()`` on all handles returned when registering the hooks. Be aware that storing the intermediate outputs and their gradients may require significantly more memory, depending on the model. In practice, it may be better to register the store-hook only to modules for which the relevance is needed.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/get-intermediate-relevance.rst
get-intermediate-relevance.rst
======================================= Using Rules, Composites, and Canonizers ======================================= Zennit implements propagation-based attribution methods by overwriting the gradient of PyTorch modules within PyTorch's auto-differentiation engine. There are three building blocks in Zennit to achieve attributions: :ref:`use-rules`, :ref:`use-composites` and :ref:`use-canonizers`. In short, **Rules** specify how to overwrite the gradient, **Composites** map rules to modules, and **Canonizers** transform some module types and configurations to a canonical form, necessary in some cases. .. _use-rules: Rules ----- **Rules** are used to overwrite the gradient of individual modules by adding forward hooks to modules, which track the input and output :py:class:`torch.Tensors` and add `gradient hooks` to the tensors to intercept normal gradient computation. All available built-in rules can be found in :py:mod:`zennit.rules`, some of which are: * :py:class:`~zennit.rules.Epsilon`, the most basic LRP rule * :py:class:`~zennit.rules.AlphaBeta`, an LRP rule that splits positive and negative contributions and weights them * :py:class:`~zennit.rules.ZPlus`, an LRP rule that only takes positive contributions into account * :py:class:`~zennit.rules.Gamma`, an LRP rule that amplifies the positive weights * :py:class:`~zennit.rules.ZBox`, an LRP rule for bounded inputs, like pixel space, commonly used in the first layer of a network * :py:class:`~zennit.rules.ReLUGuidedBackprop`, a rule for ReLU activations to only propagate positive gradients back * :py:class:`~zennit.rules.Norm`, a rule which distributes the gradient weighted by each output's fraction of the full output * :py:class:`~zennit.rules.Pass`, a rule which passes the incoming gradient on without changing it * :py:class:`~zennit.rules.Flat`, a rule meant for linear (dense, convolutional) layers to equally distribute relevance as if inputs and weights were constant Rules can be instantiated, after which they may be used directly: .. code-block:: python import torch from torch.nn import Conv2d from zennit.rules import Epsilon # instantiate the rule rule = Epsilon(epsilon=1e-5) conv_layer = Conv2d(3, 10, 3, padding=1) # registering a rule adds hooks to the module which temporarily overwrites # its gradient computation; handles are returned to remove the hooks to undo # the modification handles = rule.register(conv_layer) # to compute the gradient (i.e. the attribution), requires_grad must be True input = torch.randn(1, 3, 32, 32, requires_grad=True) output = conv_layer(input) # torch.autograd.grad returns a tuple, the comma after `attribution` # unpacks the single element in the tuple; the `grad_outputs` are necessary # for non-scalar outputs, and can be used to target which output should be # attributed for; `ones_like` targets all outputs attribution, = torch.autograd.grad( output, input, grad_outputs=torch.ones_like(output) ) # remove the hooks handles.remove() See :doc:`/how-to/write-custom-rules` for further technical detail on how to write custom rules. Note that some rules, in particular the ones that modify parameters (e.g. :py:class:`~zennit.rules.ZPlus`, :py:class:`~zennit.rules.AlphaBeta`, ...) are not thread-safe in the backward-phase, because they modify the model parameters for a brief moment. For most users, this is unlikely to cause any problems, and may be avoided by using locks in appropriate locations. .. _use-composites: Composites ---------- For a model with multiple layers, it may be inconvenient to register each rule individually. Therefore, **Composites** are used to map rules to layers given various criterions. **Composites** also take care of registering all models, and removing their handles after use. All available **Composites** can be found in :py:mod:`zennit.composites`. Some built-in composites implement rule-mappings needed for some common attribution methods, some of which are * :py:class:`~zennit.composites.EpsilonPlus`, which uses :py:class:`~zennit.rules.ZPlus` for convolutional layers and :py:class:`~zennit.rules.Epsilon` for densely connected linear layers * :py:class:`~zennit.composites.EpsilonAlpha2Beta1`, which uses :py:class:`~zennit.rules.AlphaBeta`\ ``(alpha=2, beta=1)`` for convolutional and :py:class:`~zennit.rules.Epsilon` for densely connected linear layers * :py:class:`~zennit.composites.EpsilonPlusFlat` and :py:class:`~zennit.composites.EpsilonAlpha2Beta1Flat`, which, extending the previous two composites respectively, use the :py:class:`~zennit.rules.Flat` rule for the first linear (convolution or dense) layer * :py:class:`~zennit.composites.EpsilonGammaBox`, which uses :py:class:`~zennit.rules.Gamma`\ ``(gamma=0.25)`` for convolutional layers, :py:class:`~zennit.rules.Epsilon` for dense linear layers, and :py:class:`~zennit.rules.ZBox` for the first linear (dense, convolutional) layer * :py:class:`~zennit.composites.GuidedBackprop`, which implements Guided Backpropagation by using the :py:class:`~zennit.rules.GuidedBackprop` rule for all ReLUs * :py:class:`~zennit.composites.ExcitationBackprop`, which implements Excitation Backpropagation, by using :py:class:`~zennit.rules.ZPlus` for linear (dense or convolutional) layers Additionally, the :py:class:`~zennit.rules.Norm` rule, which normalizes the gradient by output fraction, is used for :py:class:`~zennit.layer.Sum` and :py:class:`~zennit.types.AvgPool` layers in all of the listed **Composites** except for :py:class:`~zennit.composites.GuidedBackprop`. Since the gradient is only *overwritten* by **Rules**, the gradient will be unchanged for layers without applicable rules. If layers should only pass their received gradient/relevance on, the :py:class:`~zennit.rules.Pass` rule should be used (which is done for all activations in all LRP **Composites**, but not in :py:class:`~zennit.composites.GuidedBackprop` or :py:class:`~zennit.composites.ExcitationBackprop`). Note on **MaxPool**: For LRP, the gradient of MaxPool assigns values only to the *largest* inputs (winner-takes-all), which is already the expected behaviour for LRP rules. Composites may require arguments, e.g. :py:class:`~zennit.composites.EpsilonGammaBox` requires keyword arguments ``high`` and ``low`` to specify the bounds of the first layer's :py:class:`~zennit.rules.ZBox`. .. code-block:: python import torch from torch.nn import Sequential, Conv2d, ReLU, Linear, Flatten from zennit.composites import EpsilonGammaBox # setup the model model = Sequential( Conv2d(3, 8, 3, padding=1), ReLU(), Conv2d(8, 16, 3, padding=1), ReLU(), Flatten(), Linear(16 * 32 * 32, 1024), ReLU(), Linear(1024, 10), ) # sigma of normal distribution, just for visual purposes sigma = 1. # some random input data, still requires grad input = torch.randn(1, 3, 32, 32, requires_grad=True) * sigma # low and high values for ZBox need to be Tensors in the shape of the input # the batch-dimension may be chosen larger, to support different sizes composite = EpsilonGammaBox( low=torch.full_like(input, -3 * sigma), high=torch.full_like(input, 3 * sigma) ) There are two basic ways using only the **Composite** to register the modules, either using :py:func:`~zennit.core.Composite.register`: .. code-block:: python # register hooks for rules to all modules that apply composite.register(model) # execute the hooked/modified model output = model(input) # compute the attribution via the gradient attribution, = torch.autograd.grad( output, input, grad_outputs=torch.ones_like(output) ) # remove all hooks, undoing the modification composite.remove() and using :py:func:`~zennit.core.Composite.context`: .. code-block:: python # register hooks for rules to all modules that apply within the context # note that model and modified_model are the same model, the context # variable is purely visual # hooks are removed when the context is exited with composite.context(model) as modified_model: # execute the hooked/modified model output = modified_model(input) # compute the attribution via the gradient attribution, = torch.autograd.grad( output, input, grad_outputs=torch.ones_like(output) ) There is a third option using :py:class:`zennit.attribution.Attributor`, which is explained in :doc:`/how-to/use-attributors`. Finally, there are abstract **Composites** which may be used to specify custom **Composites**: * :py:class:`~zennit.composites.LayerMapComposite`, which maps module types to rules * :py:class:`~zennit.composites.SpecialFirstLayerMapComposite`, which also maps module types to rules, with a special mapping for the first layer * :py:class:`~zennit.composites.NameMapComposite`, which maps module names to rules For example, the built-in :py:class:`~zennit.composites.EpsilonPlus` composite may be written like the following: .. code-block:: python from zennit.composites import LayerMapComposite from zennit.rules import Epsilon, ZPlus, Norm, Pass from zennit.types import Convolution, Activation, AvgPool # the layer map is a list of tuples, where the first element is the target # layer type, and the second is the rule template layer_map = [ (Activation, Pass()), # ignore activations (AvgPool, Norm()), # normalize relevance for any AvgPool (Convolution, ZPlus()), # any convolutional layer (Linear, Epsilon(epsilon=1e-6)) # this is the dense Linear, not any ] composite = LayerMapComposite(layer_map=layer_map) Note that rules used in composites are only used as templates and copied for each layer they apply to using :py:func:`zennit.core.Hook.copy`. If we want to map the :py:class:`~zennit.rules.ZBox` rule to the first convolutional layer, we can use :py:class:`~zennit.composites.SpecialFirstLayerMapComposite` instead: .. code-block:: python from zennit.composites import SpecialFirstLayerMapComposite from zennit.rules import ZBox # abstract base class to describe convolutions + dense linear layers from zennit.types import Linear as AnyLinear # shape of our data shape = (1, 3, 32, 32) low = torch.full(shape, -3) high = torch.full(shape, 3) # the first map is only used once, to the first module which applies to the # map, i.e. here the first layer of type AnyLinear first_map = [ (AnyLinear, ZBox(low, high)) ] # layer_map is used from the previous example composite = SpecialFirstLayerMapComposite( layer_map=layer_map, first_map=first_map ) If a composite is made to apply for a single model, a :py:class:`~zennit.composites.NameMapComposite` can provide a transparent mapping from module name to rule: .. code-block:: python from collections import OrderedDict from zennit.composites import NameMapComposite # setup the model, explicitly naming them model = Sequential(OrderedDict([ ('conv0', Conv2d(3, 8, 3, padding=1)), ('relu0', ReLU()), ('conv1', Conv2d(8, 16, 3, padding=1)), ('relu1', ReLU()), ('flatten', Flatten()), ('linear0', Linear(16 * 32 * 32, 1024)), ('relu2', ReLU()), ('linear1', Linear(1024, 10)), ])) # look at the available modules print(list(model.named_modules())) # manually write a rule mapping: composite = NameMapComposite([ (['conv0'], ZBox(low, high)), (['conv1'], ZPlus()), (['linear0', 'linear1'], Epsilon()), ]) Modules built using :py:class:`torch.nn.Sequential` without explicit names will have a number string as their name. Explicitly assigning a module to a parent module as an attribute will assign the attribute as the child module's name. Nested modules will have their names split by a dot ``.``. To create custom composites following more complex patterns, see :doc:`/how-to/write-custom-composites`. .. _use-canonizers: Canonizers ---------- Layerwise relevance propagation (LRP) is not implementation invariant. A good example for this is that for some rules, two consecutive linear layers do not produce the same attribution as a single linear layer with its weight parameter chosen as the product of the two linear layers. The most common case this happens is when models use :py:class:`~zennit.types.BatchNorm`, which is commonly used directly after, or sometimes before a linear (dense, convolutional) layer. **Canonizers** are used to avoid this by temporarily enforcing a canonical form of the model. They differ from **Rules** in that the model is actively changed while the :py:class:`~zennit.canonizers.Canonizer` is registered, as opposed to using hooks to modify the gradient during runtime. All available **Canonizers** can be found in :py:mod:`zennit.canonizers`. Some of the available basic ones are: * :py:class:`~zennit.canonizers.SequentialMergeBatchNorm`, which traverses the tree of submodules in-order to create a sequence of leave modules, which is then used to detect adjacent linear (dense, convolutional) and BatchNorm modules * :py:class:`~zennit.canonizers.NamedMergeBatchNorm`, which is used to specify explicitly by module name which linear (dense, convolutional) and BatchNorm modules should be merged * :py:class:`~zennit.canonizers.AttributeCanonizer`, which expects a function mapping from module name and type to a :py:class:`dict` of attribute names and values which should be changed for applicable modules * :py:class:`~zennit.canonizers.CompositeCanonizer`, which expects a list of canonizers which are then combined to a single canonizer :py:class:`~zennit.canonizers.SequentialMergeBatchNorm` traverses the module tree in-order leaves-only using :py:func:`zennit.core.collect_leaves`, and iterates the resulting list to detect adjacent linear (dense, convolutional) and batch-norm modules. The batch-norm's scale and shift are merged into the adjacent linear layer's weights and bias. While not recommended, **Canonizers** can be used on their own: .. code-block:: python import torch from torch.nn import Sequential, Conv2d, ReLU, Linear, Flatten, BatchNorm2d from zennit.canonizers import SequentialMergeBatchNorm # setup the model model = Sequential( Conv2d(3, 8, 3, padding=1), ReLU(), Conv2d(8, 16, 3, padding=1), BatchNorm2d(16), ReLU(), Flatten(), Linear(16 * 32 * 32, 1024), ReLU(), Linear(1024, 10), ) # create the canonizer canonizer = SequentialMergeBatchNorm() # apply the canonizer to the model, which creates multiple canonizer # instances, one per applicable case instances = canonizer.apply(model) # do something with the model input = torch.randn(1, 3, 32, 32) output = model(input) # remove the canonizer instances to revert the model to its original state for instance in instances: instance.remove() However, the recommended way is to use them with **Composites**, which will apply and remove canonizer instances automatically while the **Composite** is active: .. code-block:: python from zennit.composites import EpsilonPlusFlat # create the canonizer canonizer = SequentialMergeBatchNorm() # create the composite, with the canonizer as an argument composite = EpsilonPlusFlat(canonizers=[canonizer]) # create some input data input = torch.randn(1, 3, 32, 32, requires_grad=True) # register the composite within the context, which also applies the # canonizer with composite.context(model) as modified_model: output = modified_model(input) # compute the attribution attribution, = torch.autograd.grad(output, input, torch.eye(10)[[0]]) # print the absolute sum of the attribution print(attribution.abs().sum().item()) Be careful not to accidentally save a model's parameters (e.g. using ``model.state_dict()``) while **Canonizers** are applied, as this will store the modified state of the model. Some models implemented in :py:mod:`torchvision.models` have their own specific **Canonizer** implemented in :py:mod:`zennit.torchvision`, which currently are: * :py:class:`~zennit.torchvision.VGGCanonizer`, which applies to :py:mod:`torchvision`'s implementation of VGG networks and currently is an alias for :py:class:`~zennit.canonizers.SequentialMergeBatchNorm` * :py:class:`~zennit.torchvision.ResNetCanonizer`, which applies to :py:mod:`torchvision`'s implementation of ResNet networks, which merges BatchNorms and replaces residual connections with the explicit :py:class:`~zennit.layer.Sum` module, which makes it possible to assign a rule to the residual connection. More technical detail to implement custom **Canonizers** may be found in :doc:`/how-to/write-custom-canonizers`.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/use-rules-composites-and-canonizers.rst
use-rules-composites-and-canonizers.rst
==================== Writing Custom Rules ==================== **Rules** overwrite the gradient of specific modules. An introduction to their usage may be found under :ref:`use-rules`. A number of rules, specifically for Layer-wise Relevance Propagation :cite:p:`bach2015pixel` for common layers and networks, are available in Zennit. For novel or custom architectures and layer-types, as well as for custom layer-wise attribution methods or rules, it may become necessary to implement a custom **Rule**. For this case, Zennit provides the :py:class:`~zennit.core.Hook` class, with a straight-forward interface to change or create side-effects during the forward and backward passes. General Rules ------------- In most cases, if simply the gradient should be overwritten, it is enough inherit from :py:class:`~zennit.core.Hook` and implement the :py:meth:`~zennit.core.Hook.backward` method. The :py:meth:`~zennit.core.Hook.backward` method has three arguments: * ``module``, which is the current module to which the hook has been registered, * ``grad_input``, which is the gradient of the output of the full gradient chain with respect to the module's input (which is the gradient of the module wrt. its input multiplied by ``grad_output``), and * ``grad_output``, which is the gradient of the output of the full gradient chain with respect to the module's output. If we define ``module`` as :math:`f:\mathbb{R}^{d_\text{in}} \rightarrow \mathbb{R}^{d_\text{out}}` and the function after ``module`` :math:`g:\mathbb{R}^{d_\text{out}} \rightarrow \mathbb{R}^{d_\text{after}}` and the input :math:`x\in\mathbb{R}^{d_\text{in}}` and output :math:`y = g(f(x))`, with the chain rule we get .. math:: \frac{\partial g(f(x))}{\partial x} = \frac{\partial f(x)}{\partial x} \frac{\partial g(f(x))}{\partial f(x)}` where ``grad_input`` is :math:`\frac{\partial g(f(x))}{\partial x}` and ``grad_output`` is :math:`\frac{\partial g(f(x))}{\partial f(x)}``. Returning a value in the implementation of :py:meth:`~zennit.core.Hook.backward` overwrites the full gradient :math:`\frac{\partial g(f(x))}{\partial x}`` within the chain, which will become ``grad_output`` for modules before the current one. Usually, the current ``grad_output`` is multiplied with a modified gradient of ``module``, thus using the values after the current module (in forward perspective) in the chain, keeping the graph connected. :py:meth:`~zennit.core.Hook.backward` is always called *after* the gradient of the module with respect to its input has been computed, thus making ``grad_input`` available. We can, for example, implement a rule which ignores the gradient of the current module: .. code-block:: python import torch from zennit.core import Hook class Ignore(Hook): '''Ignore the module's gradient and pass through the output gradient.''' def backward(self, module, grad_input, grad_output): '''Directly return grad_output.''' return grad_output ignore_hook = Ignore() input = torch.randn(1, 4, requires_grad=True) module = torch.nn.ReLU() handles = ignore_hook.register(module) output = module(input) grad, = torch.autograd.grad(output, input, torch.ones_like(output)) handles.remove() This particular rule is already included as :py:class:`zennit.rules.Pass`, and, for the layer-wise relevance propagation (LRP)-based **Composites**, used for all activations. :py:class:`~zennit.core.Hook` has a dictionary attribute ``stored_tensors``, which is used to store the output gradient as ``stored_tensors['grad_output']``. :py:meth:`~zennit.core.Hook.forward` has 3 arguments: * ``module``, which is the current module the hook has been registered to, * ``input``, which is the module's input tensor, and * ``output``, which is the module's output tensor. :py:meth:`~zennit.core.Hook.forward` is always called *after* the forward has been called, thus making ``output`` available. Using the notation above, ``input`` is :math:`x` and ``output`` is :math:`f(x)`. A layer-wise *gradient times input* can be implemented by storing the input tensor in the forward pass and directly using ``grad_input`` in the backward pass: .. code-block:: python import torch from zennit.core import Hook class GradTimesInput(Hook): '''Hook for layer-wise gradient times input.''' def forward(self, module, input, output): '''Remember the input for the backward pass.''' self.stored_tensors['input'] = input def backward(self, module, grad_input, grad_output): '''Modify the gradient by element-wise multiplying the input.''' return (self.stored_tensors['input'][0] * grad_input[0],) grad_times_input_hook = GradTimesInput() input = torch.randn(1, 4, requires_grad=True) module = torch.nn.Linear(4, 4) handles = grad_times_input_hook.register(module) output = module(input) grad, = torch.autograd.grad(output, input, torch.ones_like(output)) handles.remove() The elements of ``stored_tensors`` will be removed once :py:meth:`~zennit.core.Hook.remove` is called, e.g. when the context of the **Rule**'s **Composite** is left. Returning ``None`` in :py:meth:`~zennit.core.Hook.forward` (like implicitly above) will not modify the output. This is also the case for :py:meth:`~zennit.core.Hook.backward` and the gradient. When Hooks are not directly registered, which is the usual case, they will be used as templates and copied by **Composites** using :py:meth:`zennit.core.Hook.copy`. The default ``copy()`` function will instantiate a new instance of the **Hook**'s direct ``type()`` without any arguments. If a **Hook** subtype implements a custom ``__init__()`` or otherwise has parameters that need to be copied, a ``copy()`` function needs to be implemented. As an example, if we implement a *gradient times input* where the negative part of the input is scaled by some parameter: .. code-block:: python import torch from zennit.core import Hook class GradTimesScaledNegativeInput(Hook): '''Gradient times input, where the negative part of the input is scaled. ''' def __init__(self, scale=0.2): super().__init__() self.scale = scale def forward(self, module, input, output): '''Remember the input for the backward pass.''' self.stored_tensors['input'] = input def backward(self, module, grad_input, grad_output): '''Modify the gradient by element-wise multiplication of the input, but with the negative part of the input scaled. ''' return ( grad_input[0] * ( self.stored_tensors['input'][0].clip(min=0.0) + self.stored_tensors['input'][0].clip(max=0.0) * self.scale ) ,) def copy(self): return self.__class__(scale=self.scale) scaled_negative_hook = GradTimesScaledNegativeInput(scale=0.23) hook_copy = scaled_negative_hook.copy() assert scaled_negative_hook.scale == hook_copy.scale input = torch.randn(1, 4, requires_grad=True) module = torch.nn.Linear(4, 4) handles = scaled_negative_hook.register(module) output = module(input) grad, = torch.autograd.grad(output, input, torch.ones_like(output)) handles.remove() Here, ``self.__class__`` returns the direct class of ``self``, which is ``GradTimesScaledNegativeInput`` unless a subtype of our class was created, and is called with the scale keyword argument to create a proper copy of our hook. An alternative is to use :py:func:`copy.deepcopy`, however, in *Zennit*'s implementation of **Hooks** we decided in favor of a transparent per-hook copy method instead. LRP-based Rules --------------- While it introduces a little more complexity, :py:class:`~zennit.core.BasicHook` abstracts over the components of all LRP-based **Rules**. The components are split into 3 :math:`K`-tuples of functions, and 2 *single* functions: * ``input_modifiers``, which is a tuple of :math:`K` functions, each with a single argument to modify the input tensor, * ``param_modifiers``, which is a tuple of :math:`K` functions,each with two arguments, the parameter tensor ``obj`` and its name ``name`` (e.g. ``weight`` or ``bias``), to modify the parameter, * ``output_modifiers``, which is a tuple of :math:`K` functions, each with a single argument to modify the output tensor, each produced by applying the module with a modified input and its respective modified parameters, * ``gradient_mapper``, which is a single function which produces a tuple of :math:`K` tensors with two arguments: the gradient with respect to the module's gradient ``grad_output`` and a :math:`K`-tuple of the modified outputs ``outputs``, and * ``reducer``, which is a single function with two arguments, a :math:`K`-tuple of the modified inputs, and a :math:`K`-tuple of the vector-Jacobian product of each element of the output of ``gradient_mapper`` and the Jacobian of each modified output with respect to its modified input. Formally, :py:meth:`~zennit.core.Hook.backward` computes the modified gradient :math:`R_\text{in}\in\mathbb{R}^{d_\text{out}}` as .. math:: :nowrap: \begin{align} x^{(k)} &= h^{(k)}_\text{in}(x) &x^{(k)}\in\mathbb{R}^{d_\text{in}} \\ y^{(k)} &= h^{(k)}_\text{out}\big( f(x^{(k)};h^{(k)}_\text{param}(\theta)) \big) &y^{(k)}\in\mathbb{R}^{d_\text{out}} \\ \gamma^{(k)} &= \Big[ h_\text{gmap}\big( R_\text{out}; (y^{(1)}, ..., y^{(K)}) \big) \Big]^{(k)} &\gamma^{(k)}\in\mathbb{R}^{d_\text{out}} \\ v^{(k)} &= \Big( \frac{\partial y^{(k)}}{\partial x^{(k)}} \Big)^\top \gamma^{(k)} &v^{(k)}\in\mathbb{R}^{d_\text{in}} \\ R_\text{in} &= h_\text{reduce}\Big[ (x^{(1)}, v^{(1)}); ...; (x^{(K)}, v^{(K)}) \Big] \end{align} where input :math:`x\in\mathbb{R}^{d_\text{in}}` with input dimensionality :math:`d_\text{in}`, ``module`` function :math:`f: \mathbb{R}^{d_\text{in}} \times \mathbb{R}^{d_\text{params}} \rightarrow \mathbb{R}^{d_\text{out}}` with parameters :math:`\theta \in \mathbb{R}^{d_\text{params}}`, ``grad_output`` :math:`R_\text{out}\in\mathbb{R}^{d_\text{out}}`, :math:`\big[\cdot\big]^{(k)}` denotes the element at index :math:`k` of the tuple within brackets, :math:`\frac{\partial y^{(k)}}{\partial x^{(k)}} \in \mathbb{R}^{d_\text{out}\times d_\text{in}}` is the Jacobian, :math:`K`-tuple functions with :math:`k\in\{1,...,K\}`: * input modifiers :math:`h^{(k)}_\text{in}: \mathbb{R}^{d_\text{in}} \rightarrow \mathbb{R}^{d_\text{in}}`, * output modifiers :math:`h^{(k)}_\text{out}: \mathbb{R}^{d_\text{out}} \rightarrow \mathbb{R}^{d_\text{out}}`, and * parameter modifiers :math:`h^{(k)}_\text{param}: \mathbb{R}^{d_\text{params}} \rightarrow \mathbb{R}^{d_\text{params}}`, and single functions * output gradient map :math:`h_\text{gmap}: \mathbb{R}^{d_\text{out}} \times(\mathbb{R}^{d_\text{out}})^K \rightarrow (\mathbb{R}^{d_\text{out}})^K`, and * combined input and gradient reduce function :math:`h_\text{reduce}: (\mathbb{R}^{d_\text{in}} \times \mathbb{R}^{d_\text{in}})^K \rightarrow \mathbb{R}^{d_\text{in}}`. With this abstraction, the basic, unstabilized LRP-0 Rule can be implemented using .. code-block:: python import torch from zennit.core import BasicHook lrp_zero_hook = BasicHook( input_modifiers=[lambda input: input], param_modifiers=[lambda param, _: param], output_modifiers=[lambda output: output], gradient_mapper=(lambda out_grad, outputs: [out_grad / outputs[0]]), reducer=(lambda inputs, gradients: inputs[0] * gradients[0]), ) input = torch.randn(1, 4, requires_grad=True) module = torch.nn.Linear(4, 4) handles = lrp_zero_hook.register(module) output = module(input) grad, = torch.autograd.grad(output, input, torch.ones_like(output)) handles.remove() This creates a single, usable hook, which can be copied with :py:meth:`zennit.core.BasicHook.copy`. The number of modifiers here is only 1, thus the modifiers are each a list of a single function, and the function for ``gradient_mapper`` only returns a list with a single element (here, it would also be valid to return a single element). The reducer has to return a single tensor in the end, which means that when there is more than 1 modifier each, ``inputs`` and ``gradients`` need to be reduced e.g. by summation. The default parameters for each modifier will be the identity, and specifying only one modifier with more than one function will automatically add more identity functions for the unspecified modifiers. The default gradient mapper is the ``tuple(out_grad / stabilize(output) for output in outputs)``, and the default reducer is ``sum(input * gradient for input, gradient in zip(inputs, gradients))``. This means that creating a :py:class:`~zennit.core.BasicHook` only with default arguments will result in the :py:class:`~zennit.rules.Epsilon` -Rule with a default epsilon-value which cannot be specified. .. code-block:: python import torch from zennit.core import BasicHook epsilon_hook = BasicHook() input = torch.randn(1, 4, requires_grad=True) module = torch.nn.Linear(4, 4) handles = epsilon_hook.register(module) output = module(input) grad, = torch.autograd.grad(output, input, torch.ones_like(output)) handles.remove() As another example, the :py:class:`~zennit.rules.ZPlus` -Rule for ReLU-networks with strictly positive inputs can be implemented as .. code-block:: python import torch from zennit.core import BasicHook class ZPlusReLU(BasicHook): '''LRP-ZPlus Rule for strictly negative inputs. All parameters not listed in names will be set to zero. ''' def __init__(self, names=None): self.names = [] if names is None else names super().__init__( param_modifiers=[self._param_modifier], ) def _param_modifier(self, param, name): '''Only take the positive part of parameters specified in self.names. Other parameters are set to zero.''' if name in self.names: return param.clip(min=0.0) return torch.zeros_like(param) hook = ZPlusReLU(['weight']) input = torch.randn(1, 4, requires_grad=True) module = torch.nn.Linear(4, 4) handles = hook.register(module) output = module(input) grad, = torch.autograd.grad(output, input, torch.ones_like(output)) handles.remove() Here, we first implemented the new rule hook as a class by inheriting from :py:class:`~zennit.core.BasicHook` and calling ``super().__init__()``. We also added the argument ``names`` to the ``__init__`` function, and implemented the single ``_param_modifier`` as a method, such that we can use ``self.names`` inside the modifier function. This ``_param_modifier`` allows us to choose which parameters of the module should be used and clipped, by specifying their name in the constructor. The rest will be set to zero (to not use the bias, for example). The internal implementation of :py:class:`~zennit.rules.ZPlus` uses two modifiers in order to take negative input values into account. We recommend taking a look at the implementation of each rule in :py:mod:`zennit.rules` for more examples. There are two more arguments to :py:class:`~zennit.core.BasicHook`: * ``param_keys``, an optional list of parameter names that should be modified, which when ``None`` (default), will modify all parameters, and * ``require_params``, an optional flag to indicate whether the specified ``param_keys`` are mandatory (``True``, default). A missing parameter with ``param_keys=True`` will cause a ``RuntimeError`` during the backward pass.
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/docs/source/how-to/write-custom-rules.rst
write-custom-rules.rst
import os from functools import partial import click import torch import numpy as np from torch.utils.data import DataLoader, Subset from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor from torchvision.datasets import ImageFolder from torchvision.models import vgg16, vgg16_bn, resnet50 from zennit.attribution import Gradient, SmoothGrad, IntegratedGradients, Occlusion from zennit.composites import COMPOSITES from zennit.image import imsave, CMAPS from zennit.torchvision import VGGCanonizer, ResNetCanonizer MODELS = { 'vgg16': (vgg16, VGGCanonizer), 'vgg16_bn': (vgg16_bn, VGGCanonizer), 'resnet50': (resnet50, ResNetCanonizer), } ATTRIBUTORS = { 'gradient': Gradient, 'smoothgrad': SmoothGrad, 'integrads': IntegratedGradients, 'occlusion': Occlusion, } class BatchNormalize: def __init__(self, mean, std, device=None): self.mean = torch.tensor(mean, device=device)[None, :, None, None] self.std = torch.tensor(std, device=device)[None, :, None, None] def __call__(self, tensor): return (tensor - self.mean) / self.std class AllowEmptyClassImageFolder(ImageFolder): '''Subclass of ImageFolder, which only finds non-empty classes, but with their correct indices given other empty classes. This counter-acts the changes in torchvision 0.10.0, in which DatasetFolder does not allow empty classes anymore by default. Versions before 0.10.0 do not expose `find_classes`, and thus this change does not change the functionality of `ImageFolder` in earlier versions. ''' def find_classes(self, directory): with os.scandir(directory) as scanit: class_info = sorted((entry.name, len(list(os.scandir(entry.path)))) for entry in scanit if entry.is_dir()) class_to_idx = {class_name: index for index, (class_name, n_members) in enumerate(class_info) if n_members} if not class_to_idx: raise FileNotFoundError(f'No non-empty classes found in \'{directory}\'.') return list(class_to_idx), class_to_idx @click.command() @click.argument('dataset-root', type=click.Path(file_okay=False)) @click.argument('relevance_format', type=click.Path(dir_okay=False, writable=True)) @click.option('--attributor', 'attributor_name', type=click.Choice(list(ATTRIBUTORS)), default='gradient') @click.option('--composite', 'composite_name', type=click.Choice(list(COMPOSITES))) @click.option('--model', 'model_name', type=click.Choice(list(MODELS)), default='vgg16_bn') @click.option('--parameters', type=click.Path(dir_okay=False)) @click.option( '--inputs', 'input_format', type=click.Path(dir_okay=False, writable=True), help='Input image format string. {sample} is replaced with the sample index.' ) @click.option('--batch-size', type=int, default=16) @click.option('--max-samples', type=int) @click.option('--n-outputs', type=int, default=1000) @click.option('--cpu/--gpu', default=True) @click.option('--shuffle/--no-shuffle', default=False) @click.option('--relevance-norm', type=click.Choice(['symmetric', 'absolute', 'unaligned']), default='symmetric') @click.option('--cmap', type=click.Choice(list(CMAPS)), default='coldnhot') @click.option('--level', type=float, default=1.0) @click.option('--seed', type=int, default=0xDEADBEEF) def main( dataset_root, relevance_format, attributor_name, composite_name, model_name, parameters, input_format, batch_size, max_samples, n_outputs, cpu, shuffle, cmap, level, relevance_norm, seed ): '''Generate heatmaps of an image folder at DATASET_ROOT to files RELEVANCE_FORMAT. RELEVANCE_FORMAT is a format string, for which {sample} is replaced with the sample index. ''' # set a manual seed for the RNG torch.manual_seed(seed) # use the gpu if requested and available, else use the cpu device = torch.device('cuda:0' if torch.cuda.is_available() and not cpu else 'cpu') # mean and std of ILSVRC2012 as computed for the torchvision models norm_fn = BatchNormalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225), device=device) # transforms as used for torchvision model evaluation transform = Compose([ Resize(256), CenterCrop(224), ToTensor(), ]) # the dataset is a folder containing folders with samples, where each folder corresponds to one label dataset = AllowEmptyClassImageFolder(dataset_root, transform=transform) # limit the number of output samples, if requested, by creating a subset if max_samples is not None: if shuffle: indices = sorted(np.random.choice(len(dataset), min(len(dataset), max_samples), replace=False)) else: indices = range(min(len(dataset), max_samples)) dataset = Subset(dataset, indices) loader = DataLoader(dataset, shuffle=shuffle, batch_size=batch_size) model = MODELS[model_name][0]() # load model parameters if requested; the parameter file may need to be downloaded separately if parameters is not None: state_dict = torch.load(parameters) model.load_state_dict(state_dict) model.to(device) model.eval() # disable requires_grad for all parameters, we do not need their modified gradients for param in model.parameters(): param.requires_grad = False # convenience identity matrix to produce one-hot encodings eye = torch.eye(n_outputs, device=device) # function to compute output relevance given the function output and a target def attr_output_fn(output, target): # output times one-hot encoding of the target labels of size (len(target), 1000) return output * eye[target] # create a composite if composite_name was set, otherwise we do not use a composite composite = None if composite_name is not None: composite_kwargs = {} if composite_name == 'epsilon_gamma_box': # the maximal input shape, needed for the ZBox rule shape = (batch_size, 3, 224, 224) # the highest and lowest pixel values for the ZBox rule composite_kwargs['low'] = norm_fn(torch.zeros(*shape, device=device)) composite_kwargs['high'] = norm_fn(torch.ones(*shape, device=device)) # use torchvision specific canonizers, as supplied in the MODELS dict composite_kwargs['canonizers'] = [MODELS[model_name][1]()] # create a composite specified by a name; the COMPOSITES dict includes all preset composites provided by zennit. composite = COMPOSITES[composite_name](**composite_kwargs) # specify some attributor-specific arguments attributor_kwargs = { 'smoothgrad': {'noise_level': 0.1, 'n_iter': 20}, 'integrads': {'n_iter': 20}, 'occlusion': {'window': (56, 56), 'stride': (28, 28)}, }.get(attributor_name, {}) # create an attributor, given the ATTRIBUTORS dict given above. If composite is None, the gradient will not be # modified for the attribution attributor = ATTRIBUTORS[attributor_name](model, composite, **attributor_kwargs) # the current sample index for creating file names sample_index = 0 # the accuracy accuracy = 0. # enter the attributor context outside the data loader loop, such that its canonizers and hooks do not need to be # registered and removed for each step. This registers the composite (and applies the canonizer) to the model # within the with-statement with attributor: for data, target in loader: # we use data without the normalization applied for visualization, and with the normalization applied as # the model input data_norm = norm_fn(data.to(device)) # create output relevance function of output with fixed target output_relevance = partial(attr_output_fn, target=target) # this will compute the modified gradient of model, where the output relevance is chosen by the as the # model's output for the ground-truth label index output, relevance = attributor(data_norm, output_relevance) # sum over the color channel for visualization relevance = np.array(relevance.sum(1).detach().cpu()) # normalize between 0. and 1. given the specified strategy if relevance_norm == 'symmetric': # 0-aligned symmetric relevance, negative and positive can be compared, the original 0. becomes 0.5 amax = np.abs(relevance).max((1, 2), keepdims=True) relevance = (relevance + amax) / 2 / amax elif relevance_norm == 'absolute': # 0-aligned absolute relevance, only the amplitude of relevance matters, the original 0. becomes 0. relevance = np.abs(relevance) relevance /= relevance.max((1, 2), keepdims=True) elif relevance_norm == 'unaligned': # do not align, the original minimum value becomes 0., the original maximum becomes 1. rmin = relevance.min((1, 2), keepdims=True) rmax = relevance.max((1, 2), keepdims=True) relevance = (relevance - rmin) / (rmax - rmin) for n in range(len(data)): fname = relevance_format.format(sample=sample_index + n) # zennit.image.imsave will create an appropriate heatmap given a cmap specification imsave(fname, relevance[n], vmin=0., vmax=1., level=level, cmap=cmap) if input_format is not None: fname = input_format.format(sample=sample_index + n) # if there are 3 color channels, imsave will not create a heatmap, but instead save the image with # its appropriate colors imsave(fname, data[n]) sample_index += len(data) # update the accuracy accuracy += (output.argmax(1) == target).sum().detach().cpu().item() accuracy /= len(dataset) print(f'Accuracy: {accuracy:.2f}') if __name__ == '__main__': main()
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/share/example/feed_forward.py
feed_forward.py
import click import numpy as np from PIL import Image from zennit.image import CMAPS, palette def gale_shapley(dist): '''Find a stable matching given a distance matrix.''' preference = np.argsort(dist, axis=1) proposed = np.zeros(dist.shape[0], dtype=int) loners = set(range(dist.shape[0])) guys = [-1] * dist.shape[0] gals = [-1] * dist.shape[1] while loners: loner = loners.pop() target = preference[loner, proposed[loner]] if gals[target] == -1: gals[target] = loner guys[loner] = target elif dist[gals[target], target] > dist[loner, target]: gals[target] = loner guys[loner] = target guys[gals[target]] = -1 loners.add(gals[target]) else: loners.add(loner) proposed[loner] += 1 return guys @click.command() @click.argument('source-file', type=click.Path(exists=True, dir_okay=False)) @click.argument('output-file', type=click.Path(writable=True, dir_okay=False)) @click.option('--strategy', type=click.Choice(['intensity', 'nearest', 'histogram']), default='intensity') @click.option('--source-cmap', type=click.Choice(list(CMAPS)), default='bwr') @click.option('--source-level', type=float, default=1.0) @click.option('--invert/--no-invert', default=False) @click.option('--cmap', type=click.Choice(list(CMAPS)), default='coldnhot') @click.option('--level', type=float, default=1.0) def main(source_file, output_file, strategy, source_cmap, source_level, invert, cmap, level): '''Fit an existing RGB heatmap image to a color palette.''' source = np.array(Image.open(source_file).convert('RGB')) matchpal = palette(source_cmap, source_level) if strategy == 'intensity': # matching based on the source image intensity/ brigthness values = source.astype(float).mean(2) elif strategy == 'nearest': # match by finding the neareast centroids in a source colormap dists = (np.abs(source[None].astype(float) - matchpal[:, None, None].astype(float))).sum(3) values = np.argmin(dists, axis=0) elif strategy == 'histogram': # match by finding a stable match between the color histogram of the source image and a source colormap source = np.concatenate([source, np.zeros_like(source[:, :, [0]])], axis=2).view(np.uint32)[..., 0] uniques, counts = np.unique(source, return_counts=True) uniques = uniques[np.argsort(counts)[-256:]] dist = (np.abs(uniques.view(np.uint8).reshape(-1, 1, 4)[..., :3] - matchpal[None])).sum(2) matches = np.array(gale_shapley(dist)) ind_bin, ind_h, ind_w = np.nonzero(source[None] == uniques[:, None, None]) values = np.zeros(source.shape[:2], dtype=np.uint8) values[ind_h, ind_w] = matches[ind_bin] values = values.clip(0, 255).astype(np.uint8) if invert: values = 255 - values img = Image.fromarray(values, mode='P') pal = palette(cmap, level) img.putpalette(pal) img.save(output_file) if __name__ == '__main__': main()
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/share/scripts/palette_fit.py
palette_fit.py
wnids=( n01440764 n01443537 n01484850 n01491361 n01494475 n01496331 n01498041 n01514668 n01514859 n01518878 n01530575 n01531178 n01532829 n01534433 n01537544 n01558993 n01560419 n01580077 n01582220 n01592084 n01601694 n01608432 n01614925 n01616318 n01622779 n01629819 n01630670 n01631663 n01632458 n01632777 n01641577 n01644373 n01644900 n01664065 n01665541 n01667114 n01667778 n01669191 n01675722 n01677366 n01682714 n01685808 n01687978 n01688243 n01689811 n01692333 n01693334 n01694178 n01695060 n01697457 n01698640 n01704323 n01728572 n01728920 n01729322 n01729977 n01734418 n01735189 n01737021 n01739381 n01740131 n01742172 n01744401 n01748264 n01749939 n01751748 n01753488 n01755581 n01756291 n01768244 n01770081 n01770393 n01773157 n01773549 n01773797 n01774384 n01774750 n01775062 n01776313 n01784675 n01795545 n01796340 n01797886 n01798484 n01806143 n01806567 n01807496 n01817953 n01818515 n01819313 n01820546 n01824575 n01828970 n01829413 n01833805 n01843065 n01843383 n01847000 n01855032 n01855672 n01860187 n01871265 n01872401 n01873310 n01877812 n01882714 n01883070 n01910747 n01914609 n01917289 n01924916 n01930112 n01943899 n01944390 n01945685 n01950731 n01955084 n01968897 n01978287 n01978455 n01980166 n01981276 n01983481 n01984695 n01985128 n01986214 n01990800 n02002556 n02002724 n02006656 n02007558 n02009229 n02009912 n02011460 n02012849 n02013706 n02017213 n02018207 n02018795 n02025239 n02027492 n02028035 n02033041 n02037110 n02051845 n02056570 n02058221 n02066245 n02071294 n02074367 n02077923 n02085620 n02085782 n02085936 n02086079 n02086240 n02086646 n02086910 n02087046 n02087394 n02088094 n02088238 n02088364 n02088466 n02088632 n02089078 n02089867 n02089973 n02090379 n02090622 n02090721 n02091032 n02091134 n02091244 n02091467 n02091635 n02091831 n02092002 n02092339 n02093256 n02093428 n02093647 n02093754 n02093859 n02093991 n02094114 n02094258 n02094433 n02095314 n02095570 n02095889 n02096051 n02096177 n02096294 n02096437 n02096585 n02097047 n02097130 n02097209 n02097298 n02097474 n02097658 n02098105 n02098286 n02098413 n02099267 n02099429 n02099601 n02099712 n02099849 n02100236 n02100583 n02100735 n02100877 n02101006 n02101388 n02101556 n02102040 n02102177 n02102318 n02102480 n02102973 n02104029 n02104365 n02105056 n02105162 n02105251 n02105412 n02105505 n02105641 n02105855 n02106030 n02106166 n02106382 n02106550 n02106662 n02107142 n02107312 n02107574 n02107683 n02107908 n02108000 n02108089 n02108422 n02108551 n02108915 n02109047 n02109525 n02109961 n02110063 n02110185 n02110341 n02110627 n02110806 n02110958 n02111129 n02111277 n02111500 n02111889 n02112018 n02112137 n02112350 n02112706 n02113023 n02113186 n02113624 n02113712 n02113799 n02113978 n02114367 n02114548 n02114712 n02114855 n02115641 n02115913 n02116738 n02117135 n02119022 n02119789 n02120079 n02120505 n02123045 n02123159 n02123394 n02123597 n02124075 n02125311 n02127052 n02128385 n02128757 n02128925 n02129165 n02129604 n02130308 n02132136 n02133161 n02134084 n02134418 n02137549 n02138441 n02165105 n02165456 n02167151 n02168699 n02169497 n02172182 n02174001 n02177972 n02190166 n02206856 n02219486 n02226429 n02229544 n02231487 n02233338 n02236044 n02256656 n02259212 n02264363 n02268443 n02268853 n02276258 n02277742 n02279972 n02280649 n02281406 n02281787 n02317335 n02319095 n02321529 n02325366 n02326432 n02328150 n02342885 n02346627 n02356798 n02361337 n02363005 n02364673 n02389026 n02391049 n02395406 n02396427 n02397096 n02398521 n02403003 n02408429 n02410509 n02412080 n02415577 n02417914 n02422106 n02422699 n02423022 n02437312 n02437616 n02441942 n02442845 n02443114 n02443484 n02444819 n02445715 n02447366 n02454379 n02457408 n02480495 n02480855 n02481823 n02483362 n02483708 n02484975 n02486261 n02486410 n02487347 n02488291 n02488702 n02489166 n02490219 n02492035 n02492660 n02493509 n02493793 n02494079 n02497673 n02500267 n02504013 n02504458 n02509815 n02510455 n02514041 n02526121 n02536864 n02606052 n02607072 n02640242 n02641379 n02643566 n02655020 n02666196 n02667093 n02669723 n02672831 n02676566 n02687172 n02690373 n02692877 n02699494 n02701002 n02704792 n02708093 n02727426 n02730930 n02747177 n02749479 n02769748 n02776631 n02777292 n02782093 n02783161 n02786058 n02787622 n02788148 n02790996 n02791124 n02791270 n02793495 n02794156 n02795169 n02797295 n02799071 n02802426 n02804414 n02804610 n02807133 n02808304 n02808440 n02814533 n02814860 n02815834 n02817516 n02823428 n02823750 n02825657 n02834397 n02835271 n02837789 n02840245 n02841315 n02843684 n02859443 n02860847 n02865351 n02869837 n02870880 n02871525 n02877765 n02879718 n02883205 n02892201 n02892767 n02894605 n02895154 n02906734 n02909870 n02910353 n02916936 n02917067 n02927161 n02930766 n02939185 n02948072 n02950826 n02951358 n02951585 n02963159 n02965783 n02966193 n02966687 n02971356 n02974003 n02977058 n02978881 n02979186 n02980441 n02981792 n02988304 n02992211 n02992529 n02999410 n03000134 n03000247 n03000684 n03014705 n03016953 n03017168 n03018349 n03026506 n03028079 n03032252 n03041632 n03042490 n03045698 n03047690 n03062245 n03063599 n03063689 n03065424 n03075370 n03085013 n03089624 n03095699 n03100240 n03109150 n03110669 n03124043 n03124170 n03125729 n03126707 n03127747 n03127925 n03131574 n03133878 n03134739 n03141823 n03146219 n03160309 n03179701 n03180011 n03187595 n03188531 n03196217 n03197337 n03201208 n03207743 n03207941 n03208938 n03216828 n03218198 n03220513 n03223299 n03240683 n03249569 n03250847 n03255030 n03259280 n03271574 n03272010 n03272562 n03290653 n03291819 n03297495 n03314780 n03325584 n03337140 n03344393 n03345487 n03347037 n03355925 n03372029 n03376595 n03379051 n03384352 n03388043 n03388183 n03388549 n03393912 n03394916 n03400231 n03404251 n03417042 n03424325 n03425413 n03443371 n03444034 n03445777 n03445924 n03447447 n03447721 n03450230 n03452741 n03457902 n03459775 n03461385 n03467068 n03476684 n03476991 n03478589 n03481172 n03482405 n03483316 n03485407 n03485794 n03492542 n03494278 n03495258 n03496892 n03498962 n03527444 n03529860 n03530642 n03532672 n03534580 n03535780 n03538406 n03544143 n03584254 n03584829 n03590841 n03594734 n03594945 n03595614 n03598930 n03599486 n03602883 n03617480 n03623198 n03627232 n03630383 n03633091 n03637318 n03642806 n03649909 n03657121 n03658185 n03661043 n03662601 n03666591 n03670208 n03673027 n03676483 n03680355 n03690938 n03691459 n03692522 n03697007 n03706229 n03709823 n03710193 n03710637 n03710721 n03717622 n03720891 n03721384 n03724870 n03729826 n03733131 n03733281 n03733805 n03742115 n03743016 n03759954 n03761084 n03763968 n03764736 n03769881 n03770439 n03770679 n03773504 n03775071 n03775546 n03776460 n03777568 n03777754 n03781244 n03782006 n03785016 n03786901 n03787032 n03788195 n03788365 n03791053 n03792782 n03792972 n03793489 n03794056 n03796401 n03803284 n03804744 n03814639 n03814906 n03825788 n03832673 n03837869 n03838899 n03840681 n03841143 n03843555 n03854065 n03857828 n03866082 n03868242 n03868863 n03871628 n03873416 n03874293 n03874599 n03876231 n03877472 n03877845 n03884397 n03887697 n03888257 n03888605 n03891251 n03891332 n03895866 n03899768 n03902125 n03903868 n03908618 n03908714 n03916031 n03920288 n03924679 n03929660 n03929855 n03930313 n03930630 n03933933 n03935335 n03937543 n03938244 n03942813 n03944341 n03947888 n03950228 n03954731 n03956157 n03958227 n03961711 n03967562 n03970156 n03976467 n03976657 n03977966 n03980874 n03982430 n03983396 n03991062 n03992509 n03995372 n03998194 n04004767 n04005630 n04008634 n04009552 n04019541 n04023962 n04026417 n04033901 n04033995 n04037443 n04039381 n04040759 n04041544 n04044716 n04049303 n04065272 n04067472 n04069434 n04070727 n04074963 n04081281 n04086273 n04090263 n04099969 n04111531 n04116512 n04118538 n04118776 n04120489 n04125021 n04127249 n04131690 n04133789 n04136333 n04141076 n04141327 n04141975 n04146614 n04147183 n04149813 n04152593 n04153751 n04154565 n04162706 n04179913 n04192698 n04200800 n04201297 n04204238 n04204347 n04208210 n04209133 n04209239 n04228054 n04229816 n04235860 n04238763 n04239074 n04243546 n04251144 n04252077 n04252225 n04254120 n04254680 n04254777 n04258138 n04259630 n04263257 n04264628 n04265275 n04266014 n04270147 n04273569 n04275548 n04277352 n04285008 n04286575 n04296562 n04310018 n04311004 n04311174 n04317175 n04325704 n04326547 n04328186 n04330267 n04332243 n04335435 n04336792 n04344873 n04346328 n04347754 n04350905 n04355338 n04355933 n04356056 n04357314 n04366367 n04367480 n04370456 n04371430 n04371774 n04372370 n04376876 n04380533 n04389033 n04392985 n04398044 n04399382 n04404412 n04409515 n04417672 n04418357 n04423845 n04428191 n04429376 n04435653 n04442312 n04443257 n04447861 n04456115 n04458633 n04461696 n04462240 n04465501 n04467665 n04476259 n04479046 n04482393 n04483307 n04485082 n04486054 n04487081 n04487394 n04493381 n04501370 n04505470 n04507155 n04509417 n04515003 n04517823 n04522168 n04523525 n04525038 n04525305 n04532106 n04532670 n04536866 n04540053 n04542943 n04548280 n04548362 n04550184 n04552348 n04553703 n04554684 n04557648 n04560804 n04562935 n04579145 n04579432 n04584207 n04589890 n04590129 n04591157 n04591713 n04592741 n04596742 n04597913 n04599235 n04604644 n04606251 n04612504 n04613696 n06359193 n06596364 n06785654 n06794110 n06874185 n07248320 n07565083 n07579787 n07583066 n07584110 n07590611 n07613480 n07614500 n07615774 n07684084 n07693725 n07695742 n07697313 n07697537 n07711569 n07714571 n07714990 n07715103 n07716358 n07716906 n07717410 n07717556 n07718472 n07718747 n07720875 n07730033 n07734744 n07742313 n07745940 n07747607 n07749582 n07753113 n07753275 n07753592 n07754684 n07760859 n07768694 n07802026 n07831146 n07836838 n07860988 n07871810 n07873807 n07875152 n07880968 n07892512 n07920052 n07930864 n07932039 n09193705 n09229709 n09246464 n09256479 n09288635 n09332890 n09399592 n09421951 n09428293 n09468604 n09472597 n09835506 n10148035 n10565667 n11879895 n11939491 n12057211 n12144580 n12267677 n12620546 n12768682 n12985857 n12998815 n13037406 n13040303 n13044778 n13052670 n13054560 n13133613 n15075141 ) wnid="n02814860" URLS=( 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/2006_09_06_180_Leuchtturm.jpg/640px-2006_09_06_180_Leuchtturm.jpg' 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/2014_Leuchtturm_Kap_Arkona_02.jpg/320px-2014_Leuchtturm_Kap_Arkona_02.jpg' 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/2013-12-06_Orkan_Xaver_in_Warnem%C3%BCnde_12.jpg/640px-2013-12-06_Orkan_Xaver_in_Warnem%C3%BCnde_12.jpg' 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/Leuchtturm_Dornbusch_2012.JPG/321px-Leuchtturm_Dornbusch_2012.JPG' 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Neuer_Leuchtturm_Arkona_2012.jpg/640px-Neuer_Leuchtturm_Arkona_2012.jpg' 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Pilsumer_Leuchtturm_2010-10_CN-I.jpg/640px-Pilsumer_Leuchtturm_2010-10_CN-I.jpg' 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Lindau_Harbor_Lake_Constance_01.jpg/640px-Lindau_Harbor_Lake_Constance_01.jpg' 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Heligoland_07-2016_photo28.jpg/640px-Heligoland_07-2016_photo28.jpg' ) process_file() { mime="$(file --brief --mime-type "${1}")" ext="$(file --brief --extension "${1}")" if [ "${mime%%/*}" == 'image' ] && [ ${ext%%/*} != '???' ]; then newfname="${1%/*}/$(md5sum "${1}" | cut -d' ' -f1).${ext%%/*}" mv "${1}" "${newfname}" return 0 fi rm -f "${1}" return 1 } die() { echo >&2 -e "${1-}" exit "${2-1}" } usage() { cat <<EOF Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-o output] [-w wnid] Create a ILSVRC2012 image folder with 1000 classes that can be used with PyTorch to 'output'. Download 8 images of light houses from https://commons.wikimedia.org and put them into folder 'n02814860'. Requires (magic-) file and cURL. Available options: -o, --output Path to store dataset (default = 'share/datasets/subimagenet') -h, --help Print this help and exit -v, --verbose Print script debug info EOF exit } parse_params() { output="share/datasets/lighthouses" while :; do case "${1-}" in -h | --help) usage ;; -v | --verbose) set -x ;; -o | --output) output="${2-}" shift ;; -?*) die "Unknown option: ${1}" ;; *) break ;; esac shift done [[ -z "${output}" ]] && die "Empty parameter: output" [[ ${#args[@]} -ne 0 ]] && die "Too many positional arguments" return 0 } parse_params "$@" command -v curl >/dev/null || die "curl not available!" 1 command -v file >/dev/null || die "file not available!" 1 mkdir -p "${wnids[@]/#/$output/}" echo -n "Downloading: " n_loaded=0 for url in "${URLS[@]}"; do fname="${output}/${wnid}/image.temp" curl "${url}" --silent --location --connect-timeout 3 --max-time 10 --output "${fname}" if process_file "$fname"; then (( n_loaded++ )) echo -ne "\x1b[32m+\x1b[0m" else echo -ne "\x1b[31m.\x1b[0m" fi done echo -e "\nDownloaded ${n_loaded} images to '$(readlink -f "${output}")'" exit 0
zennit
/zennit-0.4.7.tar.gz/zennit-0.4.7/share/scripts/download-lighthouses.sh
download-lighthouses.sh
from __future__ import annotations import hashlib import json import logging import os import pathlib from typing import Any def get_cache_path( cache_root: str, params: dict[str, Any], extension: str | None = None, ) -> str: """Get the path to a cache. Args: cache_root: The root of the cache path. params: The parameters that the cache should represent. extension: The extension to use for the cache file, or None for no extension. Returns: The path to the cache file or directory. """ _, cache_path = get_cache_id_and_path(cache_root, params, extension) return cache_path def get_cache_id_and_path( cache_root: str, params: dict[str, Any], extension: str | None = None, ) -> tuple[str, str]: """Get the ID and path to a cache. Args: cache_root: The root of the cache path. params: The parameters that the cache should represent. extension: The extension to use for the cache file, or None for no extension. Returns: - The cache ID, which is the hash of the parameters. - The path to the cache file or directory. """ if extension == "zbp": raise ValueError( 'Cannot use extension "zbp", as it is reserved for the parameters.' ) pathlib.Path(cache_root).mkdir(parents=True, exist_ok=True) # Find a name with no hash collisions dumped_params = json.dumps(params, sort_keys=True) m = hashlib.sha256() while True: m.update(dumped_params.encode("utf-8")) base_name = m.hexdigest() param_file = os.path.join(cache_root, f"{base_name}.zbp") if not os.path.exists(param_file): break with open(param_file, "r") as f: if f.read() == dumped_params: break with open(param_file, "w") as f: f.write(dumped_params) return base_name, os.path.join( cache_root, base_name if extension is None else f"{base_name}.{extension}" ) def fail_cache(cache_file: str, cache_message: str | None) -> None: """Mark a cache as failed.""" with open(cache_file + ".zbfail", "w") as f: if cache_message is not None: print(cache_message, file=f) class CacheLock: """A lock for a cache file.""" def __init__(self, filename: str): """Initialize the lock.""" self.lock_path = f"{filename}.zblock" self.fail_path = f"{filename}.zbfail" self.skipped = False def __enter__(self) -> bool: """Enter the cache lock. If the lock file does not exist, create it and return True. Otherwise, return False. """ # Skip if the lock file exists if os.path.exists(self.lock_path) or os.path.exists(self.fail_path): logging.getLogger(__name__).debug(f"Skipping {self.lock_path}") self.skipped = True return False # Create the lock file with open(self.lock_path, "w"): logging.getLogger(__name__).debug(f"Created lock file {self.lock_path}") pass return True def __exit__(self, exc_type, exc_val, exc_tb): """Remove the lock file.""" if not self.skipped: logging.getLogger(__name__).debug(f"Deleting lock {self.lock_path}") os.remove(self.lock_path)
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/cache_utils.py
cache_utils.py
from __future__ import annotations from collections.abc import Callable from typing import Any, TypeVar from pandas import DataFrame from vizier.service import clients from vizier.service import pyvizier as vz from zeno import DistillReturn, MetricReturn, ZenoOptions from zeno_build.experiments import search_space from zeno_build.optimizers.base import Optimizer T = TypeVar("T") class VizierOptimizer(Optimizer): """An optimizer using the Vizier toolkit.""" def __init__( self, space: search_space.SearchSpace, distill_functions: list[Callable[[DataFrame, ZenoOptions], DistillReturn]], metric: Callable[[DataFrame, ZenoOptions], MetricReturn], num_trials: int | None = None, algorithm: str = "RANDOM_SEARCH", owner: str = "zeno-build", study_id: str = "zeno-build", ): """Initialize a Vizier optimizer. Args: space: The space to search over. constants: The constants to use. distill_functions: The distill functions to use. metric: The metric to use. num_trials: The number of trials to run. algorithm: The algorithm to use for optimization. owner: The owner of the study. study_id: The ID of the study. """ if not isinstance(space, search_space.CombinatorialSearchSpace): raise NotImplementedError("Only combinatorial search spaces are supported.") super().__init__(space, distill_functions, metric, num_trials) self.algorithm = algorithm self.owner = owner self.study_id = study_id # Algorithm, search space, and metrics. study_config = vz.StudyConfig(algorithm=self.algorithm) for name, dimension in space.dimensions.items(): if isinstance(dimension, search_space.Categorical): study_config.search_space.root.add_categorical_param( name, dimension.choices ) elif isinstance(dimension, search_space.Discrete): study_config.search_space.root.add_discrete_param( name, dimension.choices ) elif isinstance(dimension, search_space.Float): study_config.search_space.root.add_float_param( name, dimension.lower, dimension.upper ) elif isinstance(dimension, search_space.Int): study_config.search_space.root.add_int_param( name, dimension.lower, dimension.upper ) else: raise ValueError(f"Unknown search space dimension: {dimension}") study_config.metric_information.append( vz.MetricInformation(metric.__name__, goal=vz.ObjectiveMetricGoal.MAXIMIZE) ) # Setup client and begin optimization. Vizier Service will be implicitly # created. self.study = clients.Study.from_study_config( study_config, owner=self.owner, study_id=self.study_id ) self.last_suggestion: Any | None = None def get_parameters(self) -> dict[str, Any]: """Get the next set of parameters to try. Returns: A dictionary of parameters. """ self.last_suggestion = self.study.suggest(count=1) return self.last_suggestion.parameters def calculate_metric( self, data: list[Any], labels: list[Any], outputs: list[Any] ) -> float: """Calculate the metric for the last set of parameters. This must be called only once after get_parameters. Args: data: The data to use. labels: The labels to use. outputs: The outputs to use. Returns: The metric value. """ if self.last_suggestion is None: raise ValueError("Must only call calculate_metric after get_parameters.") val = super().calculate_metric(data, labels, outputs) self.last_suggestion.complete(vz.Measurement({self.metric.__name__: val})) self.last_suggestion = None return val
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/optimizers/vizier.py
vizier.py
from __future__ import annotations import random from collections.abc import Callable from typing import Any, TypeVar import numpy as np from pandas import DataFrame from zeno import DistillReturn, MetricReturn, ZenoOptions from zeno_build.experiments import search_space from zeno_build.optimizers.base import Optimizer T = TypeVar("T") class RandomOptimizer(Optimizer): """An optimizer using random search.""" def __init__( self, space: search_space.SearchSpace, distill_functions: list[Callable[[DataFrame, ZenoOptions], DistillReturn]], metric: Callable[[DataFrame, ZenoOptions], MetricReturn], seed: int | None = None, num_trials: int | None = None, ): """Initialize a random optimizer. Args: space: The space to search over. constants: The constants to use. distill_functions: The distill functions to use. metric: The metric to use. seed: The random seed to use. num_trials: The maximum number of trials to run. """ super().__init__(space, distill_functions, metric, num_trials) # Set state without side-effects (for single thread) saved_state = random.getstate() random.seed(seed) self._state = random.getstate() random.setstate(saved_state) @staticmethod def _get_parameters_from_space( space: search_space.SearchSpace, ) -> dict[str, Any] | None: if isinstance(space, search_space.CombinatorialSearchSpace): params = {} for name, dimension in space.dimensions.items(): if isinstance(dimension, search_space.Categorical) or isinstance( dimension, search_space.Discrete ): params[name] = random.choice(dimension.choices) elif isinstance(dimension, search_space.Float): params[name] = random.uniform(dimension.lower, dimension.upper) elif isinstance(dimension, search_space.Int): params[name] = random.randint(dimension.lower, dimension.upper) elif isinstance(dimension, search_space.Constant): params[name] = dimension.value else: raise ValueError(f"Unknown search dimension: {dimension}") return params elif isinstance(space, search_space.CompositeSearchSpace): sub_space = space.spaces[ np.random.choice(len(space.spaces), p=space.weights) ] return RandomOptimizer._get_parameters_from_space(sub_space) else: raise NotImplementedError(f"Unsupported search space {type(space)}.") def get_parameters(self) -> dict[str, Any] | None: """Randomize the parameters in a space. Args: space: The space to randomize. Returns: A dictionary of randomized parameters. """ saved_state = random.getstate() random.setstate(self._state) params = RandomOptimizer._get_parameters_from_space(self.space) self._state = random.getstate() random.setstate(saved_state) return params
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/optimizers/random.py
random.py
from __future__ import annotations import abc from collections.abc import Callable from typing import Any, TypeVar from pandas import DataFrame from zeno import DistillReturn, MetricReturn, ZenoOptions from zeno_build.experiments import search_space T = TypeVar("T") class Optimizer(abc.ABC): """An optimizer for hyperparameter search.""" def __init__( self, space: search_space.SearchSpace, distill_functions: list[Callable[[DataFrame, ZenoOptions], DistillReturn]], metric: Callable[[DataFrame, ZenoOptions], MetricReturn], num_trials: int | None, ): """Initialize an optimizer.""" self.space = space self.distill_functions = distill_functions self.metric = metric self.num_trials = num_trials def calculate_metric( self, data: list[Any], labels: list[Any], outputs: list[Any] ) -> float: """Calculate the metric for a set of data, labels, and outputs. This must be called only once after get_parameters. Args: data: The data to use. labels: The labels to use. outputs: The outputs to use. Returns: The metric value. """ ops = ZenoOptions( data_column="data", label_column="labels", output_column="outputs", id_column="data", distill_columns={x.__name__: x.__name__ for x in self.distill_functions}, data_path="", label_path="", output_path="", ) df = DataFrame({"data": data, "labels": labels, "outputs": outputs}) for distill_function in self.distill_functions: df[distill_function.__name__] = distill_function(df, ops).distill_output return self.metric(df, ops).metric def is_complete( self, output_dir: str, include_in_progress: bool = False, ) -> bool: """Check if the optimizer has completed. Args: output_dir: The directory where the results of each trial are stored. include_in_progress: Whether to include trials that are still running. Returns: True if the optimizer run has completed. """ if self.num_trials is None: return False valid_param_files = self.space.get_valid_param_files( output_dir=output_dir, include_in_progress=include_in_progress ) return len(valid_param_files) >= self.num_trials @abc.abstractmethod def get_parameters(self) -> dict[str, Any] | None: """Get the next parameters to optimize. Returns: A dictionary of instantiated parameters, or None if there are no more parameters to return. """ ...
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/optimizers/base.py
base.py
from __future__ import annotations import itertools from collections.abc import Callable, Iterator from typing import Any, TypeVar from pandas import DataFrame from zeno import DistillReturn, MetricReturn, ZenoOptions from zeno_build.experiments import search_space from zeno_build.optimizers.base import Optimizer T = TypeVar("T") class ExhaustiveOptimizer(Optimizer): """An optimizer that enumerates all possibilities.""" def __init__( self, space: search_space.SearchSpace, distill_functions: list[Callable[[DataFrame, ZenoOptions], DistillReturn]], metric: Callable[[DataFrame, ZenoOptions], MetricReturn], seed: int | None = None, num_trials: int | None = None, ): """Initialize an exhaustive optimizer. Args: space: The space to search over. distill_functions: The distill functions to use. metric: The metric to use. seed: The random seed to use. num_trials: The maximum number of trials to run. Raises: ValueError: If the space contains floats. """ super().__init__(space, distill_functions, metric, num_trials) self._keys, self._values_iterator = ExhaustiveOptimizer._get_keys_and_iter( space ) @staticmethod def _get_keys_and_iter( space: search_space.SearchSpace, ) -> tuple[list[str], Iterator[Any]]: """Get the keys and iterator for a search space. Args: space: The space to get the keys and iterator for. Returns: A tuple of the keys and iterator. """ if isinstance(space, search_space.CombinatorialSearchSpace): return ( list(space.dimensions.keys()), itertools.product( *[ ExhaustiveOptimizer._dimension_iter(x) for x in space.dimensions.values() ] ), ) elif isinstance(space, search_space.CompositeSearchSpace): keys: list[list[str]] = [] iterators = [] for sub_space in space.spaces: sub_keys, sub_iterator = ExhaustiveOptimizer._get_keys_and_iter( sub_space ) keys.append(sub_keys) iterators.append(sub_iterator) if any(x != keys[0] for x in keys): raise ValueError("Composite search spaces must have the same keys.") return keys[0], itertools.chain.from_iterable(iterators) else: raise NotImplementedError(f"Unsupported search space {type(space)}.") @staticmethod def _dimension_iter(dimension: search_space.SearchDimension) -> Iterator[Any]: """Iterate over a search dimension. Args: dimension: The search dimension to iterate over. Returns: An iterator over the search dimension. """ if isinstance(dimension, search_space.Categorical) or isinstance( dimension, search_space.Discrete ): return iter(dimension.choices) elif isinstance(dimension, search_space.Int): return iter(range(dimension.lower, dimension.upper + 1)) elif isinstance(dimension, search_space.Constant): return iter([dimension.value]) else: raise ValueError( f"Unknown supported dimension type {type(dimension)} for {dimension}" ) def get_parameters(self) -> dict[str, Any] | None: """Randomize the parameters in a space. Args: space: The space to randomize. Returns: A dictionary of randomized parameters. """ # If there is no next value, we have exhausted the search space. try: values = next(self._values_iterator) return dict(zip(self._keys, values)) except StopIteration: return None
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/optimizers/exhaustive.py
exhaustive.py
from __future__ import annotations import argparse import json import os def aggregate_results( cache_dir: str | None, cache_files: list[str] | None, output_file: str, ): """Aggregate results from a cache directory into a single json. Args: cache_dir: The directory containing the cached results. cache_files: The files containing the cached results. Must all be .json files with a corresponding .zbp in the same directory. output_file: The file to write the aggregated results to. Returns: None """ # Get files if cache_dir is not None: cache_files = [ os.path.join(cache_dir, x) for x in os.listdir(cache_dir) if x.endswith(".json") ] elif cache_files is not None: cache_files = [os.path.abspath(x) for x in cache_files] else: raise ValueError("Must provide either cache_dir or cache_files.") # Validation if len(cache_files) == 0: raise FileNotFoundError("No cache files found.") for cache_file in cache_files: if not cache_file.endswith(".json"): raise ValueError(f"Cache file {cache_file} must be a .json file.") if not os.path.exists(cache_file): raise FileNotFoundError(f"Cache file {cache_file} does not exist.") if not os.path.exists(cache_file[:-5] + ".zbp"): raise FileNotFoundError( f"Cache file {cache_file} does not have a corresponding .zbp file." ) # Load the results all_results = [] for cache_file in cache_files: with open(cache_file, "r") as f: predictions = json.load(f) with open(cache_file[:-5] + ".zbp", "r") as f: parameters = json.load(f) all_results.append( { "parameters": parameters, "predictions": predictions, "eval_result": 0.0, } ) # Write the results with open(output_file, "w") as f: json.dump(all_results, f) if __name__ == "__main__": p = argparse.ArgumentParser() cache_group = p.add_mutually_exclusive_group(required=True) cache_group.add_argument( "--cache_dir", type=str, default=None, help="The directory containing the cached results.", ) cache_group.add_argument( "--cache_files", type=str, nargs="+", default=None, help="The files containing the cached results. " "Must all be .json files with a corresponding .zbp in the same directory.", ) p.add_argument( "--output_file", type=str, help="The file to write the aggregated results to.", required=True, ) args = p.parse_args() aggregate_results(**vars(args))
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/reporting/aggregate_results.py
aggregate_results.py
import asyncio from zeno_build.models import lm_config from zeno_build.models.providers.cohere_utils import generate_from_cohere from zeno_build.models.providers.huggingface_utils import generate_from_huggingface from zeno_build.models.providers.openai_utils import ( generate_from_openai_chat_completion, generate_from_openai_completion, ) from zeno_build.models.providers.vllm_utils import generate_from_vllm from zeno_build.prompts import chat_prompt def _contexts_to_prompts( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, context_length: int, ) -> list[str]: return [ prompt_template.to_text_prompt( full_context=full_context.limit_length(context_length), name_replacements=model_config.name_replacements, ) for full_context in full_contexts ] def generate_from_chat_prompt( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, requests_per_minute: int = 150, ) -> list[str]: """Generate from a list of chat-style prompts. Args: variables: The variables to be replaced in the prompt template. prompt_template: The template for the prompt. api_based_model_config: The API-based model configuration. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top p value to use. context_length: The length of the context to use. requests_per_minute: Limit on the number of OpenAI requests per minute Returns: The generated text. """ if model_config.provider == "openai" or model_config.provider == "openai_chat": return [ x[0] for x in multiple_generate_from_chat_prompt( full_contexts, prompt_template, model_config, temperature, max_tokens, top_p, context_length, num_responses=1, requests_per_minute=150, ) ] print( f"Generating with {prompt_template=}, {model_config.model=}, " f"{temperature=}, {max_tokens=}, {top_p=}, {context_length=}..." ) if model_config.provider == "cohere": return asyncio.run( generate_from_cohere( _contexts_to_prompts( full_contexts, prompt_template, model_config, context_length ), model_config, temperature, max_tokens, top_p, requests_per_minute, ) ) elif model_config.provider == "huggingface": return generate_from_huggingface( _contexts_to_prompts( full_contexts, prompt_template, model_config, context_length ), model_config, temperature, max_tokens, top_p, ) elif model_config.provider == "vllm": return generate_from_vllm( _contexts_to_prompts( full_contexts, prompt_template, model_config, context_length ), model_config, temperature, max_tokens, top_p, ) else: raise ValueError("Unknown provider, but you can add your own!") def multiple_generate_from_chat_prompt( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, num_responses: int, requests_per_minute: int = 150, ) -> list[list[str]]: """Generate from a list of chat-style prompts. Args: variables: The variables to be replaced in the prompt template. prompt_template: The template for the prompt. api_based_model_config: The API-based model configuration. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top p value to use. context_length: The length of the context to use. num_responses: The number of responses to generate requests_per_minute: Limit on the number of OpenAI requests per minute Returns: The generated text. """ print( f"Generating with {prompt_template=}, {model_config.model=}, " f"{temperature=}, {max_tokens=}, {top_p=}, {context_length=}, {num_responses=}" ) if model_config.provider == "openai": return asyncio.run( generate_from_openai_completion( _contexts_to_prompts( full_contexts, prompt_template, model_config, context_length ), model_config, temperature, max_tokens, top_p, num_responses, requests_per_minute, ) ) elif model_config.provider == "openai_chat": return asyncio.run( generate_from_openai_chat_completion( full_contexts, prompt_template, model_config, temperature, max_tokens, top_p, context_length, num_responses, requests_per_minute, ) ) else: raise ValueError("Unknown provider, but you can add your own!")
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/models/chat_generate.py
chat_generate.py
import asyncio import tqdm from zeno_build.models import global_models, lm_config from zeno_build.models.providers.huggingface_utils import generate_from_huggingface from zeno_build.models.providers.openai_utils import ( generate_from_openai_chat_completion, generate_from_openai_completion, ) from zeno_build.prompts.chat_prompt import ChatMessages, ChatTurn from zeno_build.prompts.prompt_utils import replace_variables def generate_from_text_prompt( variables: list[dict[str, str]], prompt_template: str, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, requests_per_minute: int = 150, ) -> list[str]: """Generate from a textual prompt. Args: variables: The source set of variables to consume. prompt_template: The template for the prompt. model_config: Configuration of the model. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. num_responses: The number of responses to generate. top_p: The top p value to use. requests_per_minute: Limit on the number of OpenAI requests per minute. Returns: The generated text. """ if model_config.provider == "openai" or model_config.provider == "openai_chat": return [ x[0] for x in multiple_generate_from_text_prompt( variables, prompt_template, model_config, temperature, max_tokens, top_p, num_responses=1, requests_per_minute=150, ) ] print( f"Generating with {prompt_template=}, {model_config.model=}, " f"{temperature=}, {max_tokens=}, {top_p=}..." ) if model_config.provider == "huggingface": prompts = [replace_variables(prompt_template, vars) for vars in variables] return generate_from_huggingface( prompts, model_config, temperature, max_tokens, top_p, ) elif model_config.provider == "cohere": import cohere results = [] for vars in tqdm.tqdm(variables, "Generating synchronously from Cohere"): try: prompt = replace_variables(prompt_template, vars) assert global_models.cohere_client is not None response = global_models.cohere_client.generate( model=model_config.model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, p=top_p, ) results.append(response.generations[0].text) except cohere.CohereAPIError as e: # Cohere API sometimes rejects queries, if so output a blank line print(f"Warning! Cohere API rejected query for {prompt=}: {e.message}") results.append("") return results else: raise ValueError("Unknown model_config.provider, but you can add your own!") def multiple_generate_from_text_prompt( variables: list[dict[str, str]], prompt_template: str, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, num_responses: int, requests_per_minute: int = 150, ) -> list[list[str]]: """Generate from a list of chat-style prompts. Args: variables: The variables to be replaced in the prompt template. prompt_template: The template for the prompt. api_based_model_config: The API-based model configuration. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top p value to use. context_length: The length of the context to use. num_responses: The number of responses to generate requests_per_minute: Limit on the number of OpenAI requests per minute Returns: The generated text. """ print( f"Generating with {prompt_template=}, {model_config.model=}, " f"{temperature=}, {max_tokens=}, {top_p=}, {num_responses=}..." ) if model_config.provider == "openai": prompts = [replace_variables(prompt_template, vars) for vars in variables] return asyncio.run( generate_from_openai_completion( prompts, model_config, temperature, max_tokens, top_p, num_responses, requests_per_minute, ) ) elif model_config.provider == "openai_chat": full_contexts = [ ChatMessages( [ ChatTurn( role="user", content=replace_variables(prompt_template, vars), ) ] ) for vars in variables ] return asyncio.run( generate_from_openai_chat_completion( full_contexts=full_contexts, prompt_template=ChatMessages([]), model_config=model_config, temperature=temperature, max_tokens=max_tokens, top_p=top_p, context_length=1, n=num_responses, requests_per_minute=requests_per_minute, ) ) else: raise ValueError("Unknown provider, but you can add your own!")
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/models/text_generate.py
text_generate.py
import asyncio import logging import os from typing import Any import aiolimiter import openai from aiohttp import ClientSession from openai import error from tqdm.asyncio import tqdm_asyncio from zeno_build.models import lm_config from zeno_build.prompts import chat_prompt ERROR_ERRORS_TO_MESSAGES = { error.InvalidRequestError: "OpenAI API Invalid Request: Prompt was filtered", error.RateLimitError: "OpenAI API rate limit exceeded. Sleeping for 10 seconds.", error.APIConnectionError: "OpenAI API Connection Error: Error Communicating with OpenAI", # noqa E501 error.Timeout: "OpenAI APITimeout Error: OpenAI Timeout", error.ServiceUnavailableError: "OpenAI service unavailable error: {e}", error.APIError: "OpenAI API error: {e}", } async def _throttled_openai_completion_acreate( engine: str, prompt: str, temperature: float, max_tokens: int, top_p: float, n: int, limiter: aiolimiter.AsyncLimiter, ) -> dict[str, Any]: async with limiter: for _ in range(3): try: return await openai.Completion.acreate( engine=engine, prompt=prompt, temperature=temperature, max_tokens=max_tokens, top_p=top_p, n=n, ) except tuple(ERROR_ERRORS_TO_MESSAGES.keys()) as e: if isinstance(e, (error.ServiceUnavailableError, error.APIError)): logging.warning(ERROR_ERRORS_TO_MESSAGES[type(e)].format(e=e)) elif isinstance(e, error.InvalidRequestError): logging.warning(ERROR_ERRORS_TO_MESSAGES[type(e)]) return { "choices": [ { "message": { "content": "Invalid Request: Prompt was filtered" } } ] } else: logging.warning(ERROR_ERRORS_TO_MESSAGES[type(e)]) await asyncio.sleep(10) return {"choices": [{"message": {"content": ""}}]} async def generate_from_openai_completion( prompts: list[str], model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, n: int, requests_per_minute: int = 150, ) -> list[list[str]]: """Generate from OpenAI Completion API. Args: prompts: List of prompts to generate from. model_config: Model configuration. temperature: Temperature to use. max_tokens: Maximum number of tokens to generate. n: Number of completions to generate for each API call. top_p: Top p to use. requests_per_minute: Number of requests per minute to allow. Returns: List of generated responses. """ if "OPENAI_API_KEY" not in os.environ: raise ValueError( "OPENAI_API_KEY environment variable must be set when using OpenAI API." ) openai.api_key = os.environ["OPENAI_API_KEY"] openai.aiosession.set(ClientSession()) limiter = aiolimiter.AsyncLimiter(requests_per_minute) async_responses = [ _throttled_openai_completion_acreate( engine=model_config.model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, top_p=top_p, n=n, limiter=limiter, ) for prompt in prompts ] responses = await tqdm_asyncio.gather(*async_responses) # Note: will never be none because it's set, but mypy doesn't know that. await openai.aiosession.get().close() # type: ignore all_responses = [] for x in responses: all_responses.append([x["choices"][i]["message"]["content"] for i in range(n)]) return all_responses async def _throttled_openai_chat_completion_acreate( model: str, messages: list[dict[str, str]], temperature: float, max_tokens: int, top_p: float, n: int, limiter: aiolimiter.AsyncLimiter, ) -> dict[str, Any]: async with limiter: for _ in range(3): try: return await openai.ChatCompletion.acreate( model=model, messages=messages, temperature=temperature, max_tokens=max_tokens, top_p=top_p, n=n, ) except tuple(ERROR_ERRORS_TO_MESSAGES.keys()) as e: if isinstance(e, (error.ServiceUnavailableError, error.APIError)): logging.warning(ERROR_ERRORS_TO_MESSAGES[type(e)].format(e=e)) elif isinstance(e, error.InvalidRequestError): logging.warning(ERROR_ERRORS_TO_MESSAGES[type(e)]) return { "choices": [ { "message": { "content": "Invalid Request: Prompt was filtered" } } ] } else: logging.warning(ERROR_ERRORS_TO_MESSAGES[type(e)]) await asyncio.sleep(10) return {"choices": [{"message": {"content": ""}}]} async def generate_from_openai_chat_completion( full_contexts: list[chat_prompt.ChatMessages], prompt_template: chat_prompt.ChatMessages, model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, context_length: int, n: int, requests_per_minute: int = 150, ) -> list[list[str]]: """Generate from OpenAI Chat Completion API. Args: full_contexts: List of full contexts to generate from. prompt_template: Prompt template to use. model_config: Model configuration. temperature: Temperature to use. max_tokens: Maximum number of tokens to generate. n: Number of responses to generate for each API call. top_p: Top p to use. context_length: Length of context to use. requests_per_minute: Number of requests per minute to allow. Returns: List of generated responses. """ if "OPENAI_API_KEY" not in os.environ: raise ValueError( "OPENAI_API_KEY environment variable must be set when using OpenAI API." ) openai.api_key = os.environ["OPENAI_API_KEY"] openai.aiosession.set(ClientSession()) limiter = aiolimiter.AsyncLimiter(requests_per_minute) async_responses = [ _throttled_openai_chat_completion_acreate( model=model_config.model, messages=prompt_template.to_openai_chat_completion_messages( full_context=full_context.limit_length(context_length), ), temperature=temperature, max_tokens=max_tokens, top_p=top_p, n=n, limiter=limiter, ) for full_context in full_contexts ] responses = await tqdm_asyncio.gather(*async_responses) # Note: will never be none because it's set, but mypy doesn't know that. await openai.aiosession.get().close() # type: ignore all_responses = [] for x in responses: all_responses.append([x["choices"][i]["message"]["content"] for i in range(n)]) return all_responses
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/models/providers/openai_utils.py
openai_utils.py
import logging import os import aiolimiter from tqdm.asyncio import tqdm_asyncio from zeno_build.models import global_models, lm_config async def _throttled_cohere_acreate( model: str, prompt: str, temperature: float, max_tokens: int, top_p: float, limiter: aiolimiter.AsyncLimiter, ) -> str: try: import cohere except ImportError: raise ImportError( "Please `pip install cohere` to perform cohere-based inference" ) async with limiter: assert global_models.cohere_client is not None try: response = global_models.cohere_client.generate( model=model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, p=top_p, ) return response.generations[0].text except cohere.CohereAPIError as e: # Cohere API sometimes rejects queries, if so output a blank line logging.getLogger(__name__).warn( f"Warning! Cohere API rejected query for {prompt=}: {e.message}" ) return "" async def generate_from_cohere( prompts: list[str], model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, requests_per_minute: int, ) -> list[str]: """Generate outputs from the Cohere API. COHERE_API_KEY must be set in order for this function to work. Args: prompts: The prompts to generate from. model_config: The model configuration. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top-p value to use. requests_per_minute: The number of requests per minute to make. Returns: The generated outputs. """ if "COHERE_API_KEY" not in os.environ: raise ValueError( "COHERE_API_KEY environment variable must be set when using the " "Cohere API." ) try: import cohere except ImportError: raise ImportError( "Please `pip install cohere` to perform cohere-based inference" ) global_models.cohere_client = cohere.Client(os.environ["COHERE_API_KEY"]) limiter = aiolimiter.AsyncLimiter(requests_per_minute) async_responses = [ _throttled_cohere_acreate( model=model_config.model, prompt=prompt, temperature=temperature, max_tokens=max_tokens, top_p=top_p, limiter=limiter, ) for prompt in prompts ] return await tqdm_asyncio.gather(*async_responses)
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/models/providers/cohere_utils.py
cohere_utils.py
import re import torch import tqdm import transformers from zeno_build.models import lm_config def generate_from_huggingface( prompts: list[str], model_config: lm_config.LMConfig, temperature: float, max_tokens: int, top_p: float, ) -> list[str]: """Generate outputs from a huggingface model. Args: prompts: The prompts to generate from. model_config: The model configuration. temperature: The temperature to use. max_tokens: The maximum number of tokens to generate. top_p: The top-p value to use. Returns: The generated outputs. """ # Load model torch_device = "cuda" if torch.cuda.is_available() else "cpu" model_cls = ( model_config.model_cls if model_config.model_cls is not None else transformers.AutoModelForCausalLM ) tokenizer_cls = ( model_config.tokenizer_cls if model_config.tokenizer_cls is not None else transformers.AutoTokenizer ) model: transformers.PreTrainedModel = model_cls.from_pretrained( model_config.model, **model_config.model_loader_kwargs, ).to(torch_device) tokenizer: transformers.PreTrainedTokenizer = tokenizer_cls.from_pretrained( model_config.model ) tokenizer.padding_side = "left" if not tokenizer.pad_token: tokenizer.pad_token = tokenizer.eos_token tokenizer.pad_token_id = tokenizer.eos_token_id gen_config = transformers.GenerationConfig( do_sample=True, temperature=temperature, max_new_tokens=max_tokens, top_p=top_p, eos_token_id=tokenizer.eos_token_id, pad_token_id=tokenizer.pad_token_id, ) # Process in batches results = [] batch_size = 8 for i in tqdm.trange(0, len(prompts), batch_size): batch_prompts = prompts[i : i + batch_size] encoded_prompts = tokenizer( batch_prompts, padding=True, return_tensors="pt", return_token_type_ids=False, ).to(torch_device) with torch.no_grad(): outputs = model.generate(**encoded_prompts, generation_config=gen_config) outputs = outputs[:, encoded_prompts["input_ids"].shape[-1] :] decoded_outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True) results.extend(decoded_outputs) # Post-processing to get only the system utterance results = [re.split("\n\n", x)[0].strip() for x in results] return results
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/models/providers/huggingface_utils.py
huggingface_utils.py
from __future__ import annotations import itertools import json import os from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Any, Generic, TypeVar T = TypeVar("T") class SearchDimension(ABC): """A dimension along which a hyperparameter can be searched.""" @abstractmethod def value_in_scope(self, value: Any) -> bool: """Check if a value is in the scope of this dimension.""" ... @dataclass(frozen=True) class Constant(SearchDimension, Generic[T]): """A constant.""" value: T def value_in_scope(self, value: Any) -> bool: """See base class.""" return value == self.value @dataclass(frozen=True) class Categorical(SearchDimension, Generic[T]): """A categorical hyperparameter.""" choices: list[T] def value_in_scope(self, value: Any) -> bool: """See base class.""" return value in self.choices @dataclass(frozen=True) class Discrete(SearchDimension, Generic[T]): """A discrete hyperparameter. The difference between a discrete and categorical hyperparameter is that the values of a discrete hyperparameter are ordered, while the values of a categorical hyperparameter are not. """ choices: list[T] def value_in_scope(self, value: Any) -> bool: """See base class.""" return value in self.choices @dataclass(frozen=True) class Float(SearchDimension): """A float hyperparameter range.""" lower: float upper: float def value_in_scope(self, value: Any) -> bool: """See base class.""" return self.lower <= value <= self.upper @dataclass(frozen=True) class Int(SearchDimension): """An integer hyperparameter range. Attributes: lower: The lower bound of the range (inclusive). upper: The upper bound of the range (inclusive). """ lower: int upper: int def value_in_scope(self, value: Any) -> bool: """See base class.""" return self.lower <= value <= self.upper class SearchSpace(ABC): """A search space for hyperparameter optimization.""" @abstractmethod def contains_params(self, params: dict[str, Any]) -> bool: """Check whether the search space contains the given parameters.""" ... @abstractmethod def get_non_constant_dimensions(self) -> list[str]: """Get the names of the non-constant dimensions.""" ... def get_valid_param_files( self, output_dir: str, include_in_progress: bool ) -> list[str]: """Get the valid parameter files in the given directory. Args: output_dir: The directory where the parameter files are stored. include_in_progress: Whether to include parameter files for runs that are still running. Returns: Paths to the valid parameter files. """ os.makedirs(output_dir, exist_ok=True) param_files = [ os.path.join(output_dir, x) for x in os.listdir(output_dir) if x.endswith(".zbp") ] finished_files = [] for param_file in param_files: fail_file = f"{param_file[:-4]}.zbfail" lock_file = f"{param_file[:-4]}.zblock" if os.path.exists(fail_file): continue with open(param_file, "r") as f: params = json.load(f) if self.contains_params(params): if include_in_progress or not os.path.exists(lock_file): finished_files.append(param_file) return finished_files class CombinatorialSearchSpace(SearchSpace): """A search space that includes the cross product of dimensions.""" def __init__(self, dimensions: dict[str, SearchDimension]): """Initialize the search space. Args: dimensions: The dimensions of the search space. """ self.dimensions = dimensions def contains_params(self, params: dict[str, Any]) -> bool: """See base class.""" for k, v in params.items(): if k not in self.dimensions: return False if not self.dimensions[k].value_in_scope(v): return False return True def get_non_constant_dimensions(self) -> list[str]: """See base class.""" return [k for k, v in self.dimensions.items() if not isinstance(v, Constant)] class CompositeSearchSpace(SearchSpace): """A search space consisting of multiple search spaces.""" def __init__(self, spaces: list[SearchSpace], weights: list[float] | None = None): """Initialize the search space. Args: spaces: The search spaces to combine. weights: The weights of the search spaces. If None, all search spaces have equal weight. """ self.spaces = spaces self.weights = weights or [1.0 / len(spaces)] * len(spaces) def contains_params(self, params: dict[str, Any]) -> bool: """Check if the parameters are contained in the search space.""" return any([s.contains_params(params) for s in self.spaces]) def get_non_constant_dimensions(self) -> list[str]: """See base class.""" return sorted( list( set( itertools.chain.from_iterable( s.get_non_constant_dimensions() for s in self.spaces ) ) ) )
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/experiments/search_space.py
search_space.py
from __future__ import annotations from dataclasses import dataclass from typing import Any, Literal @dataclass class ChatTurn: """A single turn for a chat prompt. Attributes: role: The role of the thing making the utterance. content: The utterance itself. """ role: Literal["system", "assistant", "user"] content: str @dataclass(frozen=True) class ChatMessages: """A set of chat messages. Attributes: messages: A set of messages for the chat turn """ messages: list[ChatTurn] @staticmethod def from_dict(data: dict[str, Any]) -> ChatMessages: """Create a chat messages object from a dictionary.""" return ChatMessages( messages=[ ChatTurn(role=x["role"], content=x["content"]) for x in data["messages"] ] ) def to_dict(self) -> dict[str, Any]: """Convert a chat messages object to a dictionary.""" return { "messages": [ { "role": x.role, "content": x.content, } for x in self.messages ] } def to_openai_chat_completion_messages( self, full_context: ChatMessages, ) -> list[dict[str, str]]: """Build an OpenAI ChatCompletion message. Args: variables: The variables to be replaced in the prompt template. Returns: A list of dictionaries that can be consumed by the OpenAI API. """ messages = [ { "role": x.role, "content": x.content, } for x in self.messages + full_context.messages ] return messages def to_text_prompt( self, full_context: ChatMessages, name_replacements: dict[str, str], ) -> str: """Create a normal textual prompt of a chat history. Args: variables: The variables to be replaced in the prompt template. system_name: The name of the system. user_name: The name of the user. Returns: str: _description_ """ messages = [ f"{name_replacements.get(x.role, x.role)}: {x.content}" for x in self.messages + full_context.messages ] assistant_name = name_replacements.get("assistant", "assistant") messages += [f"{assistant_name}:"] return "\n\n".join(messages) def limit_length(self, context_length: int) -> ChatMessages: """Limit the length of the chat history. Args: context_length: The maximum length of the context, or 0 or less for no limit. Returns: The chat messages with the context length limited. """ return ( ChatMessages(messages=self.messages[-context_length:]) if context_length >= 0 else self )
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/prompts/chat_prompt.py
chat_prompt.py
import logging import os import tqdm from inspiredco.critique import Critique from inspiredco.critique_utils.exceptions import CritiqueError from pandas import DataFrame from requests.exceptions import ConnectionError from zeno import DistillReturn, MetricReturn, ZenoOptions, distill, metric def call_critique( df: DataFrame, ops: ZenoOptions, metric_name: str, config: dict, batch_size: int = 20000, ) -> MetricReturn: """Call Critique. Args: metric_name: Name of the metric df: Zeno DataFrame ops: Zeno options config: Metric configuration Returns: MetricReturn: Metric results """ eval_dict = df[[ops.output_column, ops.label_column, ops.data_column]].to_dict( "records" ) for d in eval_dict: d["references"] = [d.pop(ops.label_column)] d["target"] = d.pop(ops.output_column) if len(d["references"][0]) == 0: raise ValueError(f"Empty reference at {d}") data = d.pop(ops.data_column) # Metrics with OpenAI dialog dict as an input if metric_name == "uni_eval" and config.get("task") == "dialog": if not isinstance(data, list) or any("content" not in x for x in data): raise ValueError( f"Dialog metrics must have input in OpenAI " f"dialog dict format but {data} was not" ) d["source"] = data[-1]["content"] if len(data) >= 1 else "..." d["context"] = data[-2]["content"] if len(data) >= 2 else "..." # Metrics with a string as an input elif isinstance(data, str): d["source"] = data client = Critique(api_key=os.environ["INSPIREDCO_API_KEY"]) # evaluate batch by batch all_results = [] for i in tqdm.tqdm( range(0, len(eval_dict), batch_size), desc=f"Evaluating {metric_name}" ): # Allow up to 3 retries for j in range(3): try: result = client.evaluate( metric=metric_name, config=config, dataset=eval_dict[i : i + batch_size], ) for r in result["examples"]: all_results.append(round(r["value"], 6)) break except (CritiqueError, ConnectionError): if j == 2: raise else: logging.warning(f"Error evaluating {metric_name}, retrying...") return DistillReturn(distill_output=all_results) @distill def bert_score(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """BERT score (with the bert-base-uncased model). Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: BERT scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "bert_score", {"model": "bert-base-uncased"}, batch_size=1000 ) @distill def sentence_bleu(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Sentence-level BLEU score (with add-1 smoothing). Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Sentence-level BLEU scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "bleu", {"smooth_method": "add_k", "smooth-value": 1.0} ) @distill def chrf(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """CHRF score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: CHRF scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique(df, ops, "chrf", {}) @distill def comet(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """COMET score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: COMET scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "comet", {"model": "unbabel_comet/wmt21-comet-qe-da"}, batch_size=150 ) @distill def length_ratio(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Length ratio. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Length ratios """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "length_ratio", {"tokenizer": {"name": "character", "config": {}}} ) @distill def rouge_1(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """ROUGE-1 score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: ROUGE-1 scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique(df, ops, "rouge", {"variety": "rouge_1"}) @distill def rouge_2(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """ROUGE-2 score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: ROUGE-2 scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique(df, ops, "rouge", {"variety": "rouge_2"}) @distill def rouge_l(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """ROUGE-L score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: ROUGE-L scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique(df, ops, "rouge", {"variety": "rouge_l"}) @distill def toxicity(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Toxicity score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Toxicity scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "detoxify", {"model": "unitary/toxic-bert"}, batch_size=1000 ) @distill def coherence(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Coherence score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Coherence scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "uni_eval", {"task": "dialog", "evaluation_aspect": "coherence"}, batch_size=150, ) @distill def engagingness(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Engagingness score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: engagingness scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "uni_eval", {"task": "dialog", "evaluation_aspect": "engagingness"}, batch_size=150, ) @distill def groundedness(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Groundedness score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: groundedness scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "uni_eval", {"task": "dialog", "evaluation_aspect": "groundedness"}, batch_size=150, ) @distill def naturalness(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Naturalness score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: naturalness scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "uni_eval", {"task": "dialog", "evaluation_aspect": "naturalness"}, batch_size=150, ) @distill def understandability(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Understandability score. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: understandability scores """ # NOTE: It is necessary to mention "ops.output_column" in this function # to work-around a hack in Zeno (as of v0.4.11): # https://github.com/zeno-ml/zeno/blob/5c064e74b5276173fa354c4a546ce0d762d8f4d7/zeno/backend.py#L187 # noqa: E501 return call_critique( df, ops, "uni_eval", {"task": "dialog", "evaluation_aspect": "understandability"}, batch_size=150, ) @metric def avg_bert_score(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average BERT score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average BERT score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["bert_score"]].fillna(0).mean()) @metric def avg_sentence_bleu(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average Sentence-level BLEU score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average Sentence-level BLEU score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn( metric=df[ops.distill_columns["sentence_bleu"]].fillna(0).mean() ) @metric def avg_chrf(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average CHRF score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average CHRF score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["chrf"]].fillna(0).mean()) @metric def avg_comet(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average comet score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average comet score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["comet"]].fillna(0).mean()) @metric def avg_length_ratio(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average length ratio. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average length ratio """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["length_ratio"]].fillna(0).mean()) @metric def avg_rouge_1(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average ROUGE-1 score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average ROUGE-1 score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["rouge_1"]].fillna(0).mean()) @metric def avg_rouge_2(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average ROUGE-2 score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average ROUGE-2 score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["rouge_2"]].fillna(0).mean()) @metric def avg_rouge_l(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average ROUGE-L score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average ROUGE-L score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["rouge_l"]].fillna(0).mean()) @metric def avg_toxicity(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average toxicity score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average toxicity score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["toxicity"]].fillna(0).mean()) @metric def avg_coherence(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average coherence score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average coherence score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["coherence"]].fillna(0).mean()) @metric def avg_engagingness(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average engagingness score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average engagingness score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["engagingness"]].fillna(0).mean()) @metric def avg_groundedness(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average groundedness score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average groundedness score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["groundedness"]].fillna(0).mean()) @metric def avg_naturalness(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average naturalness score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average naturalness score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn(metric=df[ops.distill_columns["naturalness"]].fillna(0).mean()) @metric def avg_understandability(df: DataFrame, ops: ZenoOptions) -> MetricReturn: """Average understandability score. Args: df: Zeno DataFrame ops: Zeno options Returns: MetricReturn: Average understandability score """ if len(df) == 0: return MetricReturn(metric=0) return MetricReturn( metric=df[ops.distill_columns["understandability"]].fillna(0).mean() )
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/evaluation/text_metrics/critique.py
critique.py
"""Tokenizer based on unicode character classes.""" import unicodedata DEFAULT_MERGE_SYMBOL = "▁" def _is_weird(c): return not ( unicodedata.category(c)[0] in "LMN" or c.isspace() ) # Caution: python's isalnum(c) does not accept Marks (category M*)! def tokenize( instring: str, merge_symbol: str = DEFAULT_MERGE_SYMBOL, ) -> str: """Tokenize on unicode categories, but keep weird characters separate. Args: instring: The string to tokenize. merge_symbol: The symbol to use for merges. Returns: A list of tokens. """ # Walk through the string! outsequence = [] for i in range(len(instring)): c = instring[i] c_p = instring[i - 1] if i > 0 else c c_n = instring[i + 1] if i < len(instring) - 1 else c # Is it a letter (i.e. Unicode category starts with 'L')? # Or alternatively, is it just whitespace? # So if it's not weird, just copy. if not _is_weird(c): outsequence.append(c) # Otherwise it should be separated! else: # Was there something non-spacey before? # Then we have to introduce a new space and a merge marker. if not c_p.isspace(): outsequence.append(" " + merge_symbol) # Copy character itself outsequence.append(c) # Is there something non-spacey after? # Then we have to introduce a new space and a merge marker. # If, however the next character would just want to merge left # anyway, no need to do it now. if not c_n.isspace() and not _is_weird(c_n): outsequence.append(merge_symbol + " ") return "".join(outsequence) def detokenize( instring: str, merge_symbol: str = DEFAULT_MERGE_SYMBOL, ) -> str: """Detokenize a string tokenized with tokenize().""" # Walk through the string! outsequence = [] i = 0 while i < len(instring): c = instring[i] c_n = instring[i + 1] if i < len(instring) - 1 else c c_nn = instring[i + 2] if i < len(instring) - 2 else c # It could be one of the spaces we introduced if c + c_n == " " + merge_symbol and _is_weird(c_nn): i += 2 elif _is_weird(c) and c_n + c_nn == merge_symbol + " ": outsequence.append(c) i += 3 else: outsequence.append(c) i += 1 return "".join(outsequence)
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/evaluation/text_tokenizers/unicode.py
unicode.py
"""Utilities for evaluating the execution accuracy of a program.""" from __future__ import annotations import contextlib import faulthandler import io import itertools import multiprocessing import os import platform import signal import tempfile from collections import Counter from concurrent.futures import ThreadPoolExecutor, as_completed from dataclasses import dataclass from multiprocessing.managers import ListProxy from typing import Any, Literal import numpy as np import tqdm @dataclass(frozen=True) class CheckCorrectnessResult: """A result of checking the correctness of a program prediction. Attributes: task_id: The task ID of the task that was tested. prediction_id: The place in the n-best list. success_value: Whether the program passed the test suite. error_message: The error message if the program failed. """ task_id: int prediction_id: int success_value: Literal["passed", "failed", "timed out"] error_message: str | None def check_correctness( check_program: str, timeout: float, task_id: int, prediction_id: int, ) -> CheckCorrectnessResult: """Evaluates functional correctness by running the test suite. Args: check_program: A string containing the program to be checked. timeout: The maximum time to run the program. task_id: The task ID of the task that was tested. prediction_id: The place in the n-best list. Returns: A CheckCorrectnessResult object containing the result of the test. """ manager = multiprocessing.Manager() result: ListProxy = manager.list() p = multiprocessing.Process( target=_unsafe_execute, args=(check_program, result, timeout) ) p.start() p.join(timeout=timeout + 1) if p.is_alive(): p.kill() if result: success_value, error_message = result[0] else: success_value, error_message = "timed out", None return CheckCorrectnessResult( task_id=task_id, prediction_id=prediction_id, success_value=success_value, error_message=error_message, ) def _unsafe_execute( check_program: str, result: ListProxy, timeout: float, ) -> None: """Execute a program and record the result.""" with _create_tempdir(): # These system calls are needed when cleaning up tempdir. import os import shutil rmtree = shutil.rmtree rmdir = os.rmdir chdir = os.chdir unlink = os.unlink # Disable functionalities that can make destructive changes to the test. reliability_guard() # Run program. try: exec_globals: dict[str, Any] = {} with _swallow_io(): with _time_limit(timeout): exec(check_program, exec_globals) my_result: tuple[Literal["passed", "failed", "timed out"], str | None] = ( "passed", None, ) except TimeoutException: my_result = "timed out", None except AssertionError: my_result = "failed", "AssertionError" except BaseException as e: my_result = "failed", f"{e}" result.append(my_result) # Needed for cleaning up. shutil.rmtree = rmtree os.rmdir = rmdir os.chdir = chdir os.unlink = unlink @contextlib.contextmanager def _time_limit(seconds): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0) @contextlib.contextmanager def _swallow_io(): stream = WriteOnlyStringIO() with contextlib.redirect_stdout(stream): with contextlib.redirect_stderr(stream): with _redirect_stdin(stream): yield @contextlib.contextmanager def _create_tempdir(): with tempfile.TemporaryDirectory() as dirname: with _chdir(dirname): yield dirname class TimeoutException(Exception): """An exception for when execution times out.""" pass class WriteOnlyStringIO(io.StringIO): """StringIO that throws an exception when it's read from.""" def read(self, *args, **kwargs): """See base class.""" raise OSError def readline(self, *args, **kwargs): """See base class.""" raise OSError def readlines(self, *args, **kwargs): """See base class.""" raise OSError def readable(self, *args, **kwargs): """Returns True if the IO object can be read.""" return False class _redirect_stdin(contextlib._RedirectStream): # type: ignore _stream = "stdin" @contextlib.contextmanager def _chdir(root): if root == ".": yield return cwd = os.getcwd() os.chdir(root) try: yield except BaseException as exc: raise exc finally: os.chdir(cwd) def reliability_guard(maximum_memory_bytes=None): """Disables various destructive functions. This prevents the generated code from interfering with the test (e.g. fork bomb, killing other processes, removing filesystem files, etc.). This function is NOT a security sandbox. Untrusted code, including, model- generated code, should not be blindly executed outside of one. See the Codex paper for more information about OpenAI's code sandbox, and proceed with caution. """ if maximum_memory_bytes is not None: import resource resource.setrlimit( resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes) ) resource.setrlimit( resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes) ) if not platform.uname().system == "Darwin": resource.setrlimit( resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes) ) faulthandler.disable() import builtins builtins.exit = None builtins.quit = None import os os.environ["OMP_NUM_THREADS"] = "1" os.kill = None os.system = None os.putenv = None os.remove = None os.removedirs = None os.rmdir = None os.fchdir = None os.setuid = None os.fork = None os.forkpty = None os.killpg = None os.rename = None os.renames = None os.truncate = None os.replace = None os.unlink = None os.fchmod = None os.fchown = None os.chmod = None os.chown = None os.chroot = None os.fchdir = None os.lchflags = None os.lchmod = None os.lchown = None os.getcwd = None os.chdir = None import shutil shutil.rmtree = None shutil.move = None shutil.chown = None import subprocess subprocess.Popen = None # type: ignore __builtins__["help"] = None import sys sys.modules["ipdb"] = None sys.modules["joblib"] = None sys.modules["resource"] = None sys.modules["psutil"] = None sys.modules["tkinter"] = None def estimate_pass_at_k( num_samples: np.ndarray, num_correct: np.ndarray, k: int, ) -> np.ndarray: """Estimates pass@k of each problem and returns them in an array.""" def estimator(n: int, c: int, k: int) -> float: """Calculates 1 - comb(n - c, k) / comb(n, k).""" if n - c < k: return 1.0 return 1.0 - np.prod(1.0 - k / np.arange(n - c + 1, n + 1)) if isinstance(num_samples, int): num_samples_it = itertools.repeat(num_samples, len(num_correct)) else: assert len(num_samples) == len(num_correct) num_samples_it = iter(num_samples) return np.array( [estimator(int(n), int(c), k) for n, c in zip(num_samples_it, num_correct)] ) def compute_execution_accuracy( predictions: list[list[str]], tests: list[str], k: list[int] = [1, 10, 100], num_workers: int = 4, timeout: float = 10.0, ) -> tuple[dict[str, np.ndarray], list[list[CheckCorrectnessResult]]]: """Compute execution accuracy. Args: predictions: A list of candidate lists for each example. tests: A list of test programs. k: A list of k values to compute pass@k. num_workers: The number of workers to use. timeout: The timeout in seconds for each test program. Returns: A tuple of: - A dictionary from string "pass@k"->pass@k values for each task - Results for each individual test program. """ if os.name == "nt": raise NotImplementedError("This metric is currently not supported on Windows.") with ThreadPoolExecutor(max_workers=num_workers) as executor: futures = [] completion_id: Counter = Counter() n_samples = 0 all_results: list[list[CheckCorrectnessResult | None]] = [ [None for _ in candidates] for candidates in predictions ] for task_id, (candidates, test_case) in enumerate(zip(predictions, tests)): for candidate in candidates: test_program = candidate + "\n" + test_case args = (test_program, timeout, task_id, completion_id[task_id]) future = executor.submit(check_correctness, *args) futures.append(future) completion_id[task_id] += 1 n_samples += 1 for future in tqdm.tqdm( as_completed(futures), total=n_samples, desc="Execution-based eval" ): result = future.result() assert isinstance(result, CheckCorrectnessResult) all_results[result.task_id][result.prediction_id] = result # Make sure all tasks have results clean_results: list[list[CheckCorrectnessResult]] = [] for task_results in all_results: clean_task_results: list[CheckCorrectnessResult] = [] for task_result in task_results: assert task_result is not None clean_task_results.append(task_result) clean_results.append(clean_task_results) total, correct = [], [] for task_id, clean_result in enumerate(clean_results): passed = [r.success_value == "passed" for r in clean_result] total.append(len(passed)) correct.append(sum(passed)) total_arr = np.array(total) correct_arr = np.array(correct) ks = k pass_at_k = { f"pass@{k}": estimate_pass_at_k(total_arr, correct_arr, k) for k in ks if (total_arr >= k).all() } return pass_at_k, clean_results
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/evaluation/code_metrics/execution_accuracy_utils.py
execution_accuracy_utils.py
from pandas import DataFrame from sklearn.cluster import KMeans from zeno import DistillReturn, ZenoOptions, distill def _cluster_docs( documents: list[str], num_clusters: int, model_name: str, ) -> list[str]: try: from sentence_transformers import SentenceTransformer except ImportError: raise ImportError( "Please install sentence-transformers to use clustering features." ) # Load the model. Models are downloaded automatically if not present model = SentenceTransformer(model_name) # Encode the documents to get their embeddings document_embeddings = model.encode(documents) # Perform kmeans clustering kmeans = KMeans(n_clusters=num_clusters) kmeans.fit(document_embeddings) # Get the cluster number for each document return [f"C{x}" for x in kmeans.labels_.tolist()] @distill def data_clusters(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Cluster the labels together to find similar sentences. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Cluster IDs of each of the input data points """ documents = [str(x) for x in df[ops.data_column]] # TODO(gneubig): having something like the sqrt of the number of documents seems # like a good number of clusters, but more than 20 are not supported in Zeno: # https://github.com/zeno-ml/zeno/issues/873 # num_clusters = int(math.sqrt(len(documents))) num_clusters = 20 document_clusters = _cluster_docs(documents, num_clusters, "all-MiniLM-L6-v2") return DistillReturn(distill_output=document_clusters) @distill def label_clusters(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Cluster the labels together to find similar sentences. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Cluster IDs of each of the labels """ documents = [str(x) for x in df[ops.label_column]] # TODO(gneubig): having something like the sqrt of the number of documents seems # like a good number of clusters, but more than 20 are not supported in Zeno: # https://github.com/zeno-ml/zeno/issues/873 # num_clusters = int(math.sqrt(len(documents))) num_clusters = 20 document_clusters = _cluster_docs(documents, num_clusters, "all-MiniLM-L6-v2") return DistillReturn(distill_output=document_clusters)
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/evaluation/text_features/clustering.py
clustering.py
from pandas import DataFrame from zeno import DistillReturn, ZenoOptions, distill from zeno_build.prompts.chat_prompt import ChatMessages @distill def output_length(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Length of model output. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Lengths of outputs """ return DistillReturn(distill_output=df[ops.output_column].str.len()) @distill def label_length(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Length of the gold-standard label. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Lengths of labels """ return DistillReturn(distill_output=df[ops.label_column].str.len()) @distill def input_length(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Length of model input. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: Lengths of inputs """ return DistillReturn(distill_output=df[ops.data_column].str.len()) @distill def chat_context_length(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Length of the input context (e.g. for chatbots). Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: """ chat_context_lengths = [] for data in df[ops.data_column]: if isinstance(data, ChatMessages): chat_context_lengths.append(len(data.messages)) elif isinstance(data, list): chat_context_lengths.append(len(data)) else: raise ValueError( f"Expected {ops.data_column} column to be ChatMessages, or list " f"but got {type(data)} instead." ) return DistillReturn(distill_output=chat_context_lengths) @distill def doc_context_length(df: DataFrame, ops: ZenoOptions) -> DistillReturn: """Length of the previous document context, e.g. for document-level translation. Args: df: Zeno DataFrame ops: Zeno options Returns: DistillReturn: The document context """ doc_context_lengths = [] # Count the number of times `doc_id` has appeared so far doc_ids: dict[str, int] = {} for x in df["doc_id"]: doc_ids[x] = doc_ids.get(x, 0) + 1 doc_context_lengths.append(doc_ids[x]) return DistillReturn(distill_output=doc_context_lengths)
zeno-build
/zeno_build-0.0.6-py3-none-any.whl/zeno_build/evaluation/text_features/length.py
length.py
# Zeno Python Client The Zeno Python client lets you create and manage Zeno projects from Python. ## Example A Zeno project has a base `dataset` and any number of `systems` (AI models) to evaluate on a dataset. The following example shows how to upload a dataset and a system to a Zeno project. ```python from zeno_client import ZenoClient import pandas as pd # Create a Zeno client with your API key client = ZenoClient("YOUR_API_KEY") # Create a project with a specific data renderer. # See view options at https://zenoml.com/docs/views/ project = client.create_project("my_project", "my_view") # Upload a simple dataset df = df.load_csv("my_dataset.csv") project.upload_dataset(df, id_column="id", label_column="label", data_column='text') # Upload a system to the project # ... run inference on your model ... df = pd.DataFrame({"id": [1, 2, 3], "output": ["A", "B", "A"]}) project.upload_system("my_system", df, output_column="output", id_column="id") ``` See the [examples](./examples) directory for more in-depth examples. ## API Documentation Documentation generated with [pydoc-markdown](https://niklasrosenstein.github.io/pydoc-markdown/). `pydoc-markdown -I zeno_client -m client --render-toc > docs.md` # Table of Contents - [client](#client) - [ZenoMetric](#client.ZenoMetric) - [ZenoProject](#client.ZenoProject) - [\_\_init\_\_](#client.ZenoProject.__init__) - [upload_dataset](#client.ZenoProject.upload_dataset) - [upload_system](#client.ZenoProject.upload_system) - [ZenoClient](#client.ZenoClient) - [\_\_init\_\_](#client.ZenoClient.__init__) - [create_project](#client.ZenoClient.create_project) - [get_project](#client.ZenoClient.get_project) <a id="client"></a> # client Functions to upload data to Zeno's backend. <a id="client.ZenoMetric"></a> ## ZenoMetric Objects ```python class ZenoMetric(BaseModel) ``` A metric to calculate for a Zeno project. **Attributes**: - `id` _int_ - The ID of the metric. -1 if not set. - `name` _str_ - The name of the metric. - `type` _str_ - The type of metric to calculate. - `columns` _list[str]_ - The columns to calculate the metric on. <a id="client.ZenoProject"></a> ## ZenoProject Objects ```python class ZenoProject() ``` Provides data upload functionality for a Zeno project. **Attributes**: - `api_key` _str_ - The API key to authenticate uploads with. - `project_uuid` _str_ - The ID of the project to add data to. - `endpoint` _str_ - The base URL of the Zeno backend. <a id="client.ZenoProject.__init__"></a> #### \_\_init\_\_ ```python def __init__(api_key: str, project_uuid: str, endpoint: str = DEFAULT_BACKEND) ``` Initialize the Project object for API upload calls. **Arguments**: - `api_key` _str_ - the API key to authenticate uploads with. - `project_uuid` _str_ - the ID of the project to add data to. - `endpoint` _str, optional_ - the base URL of the Zeno backend. <a id="client.ZenoProject.upload_dataset"></a> #### upload_dataset ```python def upload_dataset(df: pd.DataFrame, id_column: str, label_column: Optional[str] = None, data_column: Optional[str] = None) ``` Upload a dataset to a Zeno project. **Arguments**: - `df` _pd.DataFrame_ - The dataset to upload. - `id_column` _str_ - The name of the column containing the instance IDs. - `label_column` _str | None, optional_ - The name of the column containing the instance labels. Defaults to None. - `data_column` _str | None, optional_ - The name of the column containing the raw data. Only works for small text data. Defaults to None. <a id="client.ZenoProject.upload_system"></a> #### upload_system ```python def upload_system(system_name: str, df: pd.DataFrame, output_column: str, id_column: str) ``` Upload a system to a Zeno project. **Arguments**: - `df` _pd.DataFrame_ - The dataset to upload. - `system_name` _str_ - The name of the system to upload. - `output_column` _str_ - The name of the column containing the system output. - `id_column` _str_ - The name of the column containing the instance IDs. <a id="client.ZenoClient"></a> ## ZenoClient Objects ```python class ZenoClient() ``` Client class for data upload functionality to Zeno. **Attributes**: - `api_key` _str_ - The API key to authenticate uploads with. - `endpoint` _str_ - The base URL of the Zeno backend. <a id="client.ZenoClient.__init__"></a> #### \_\_init\_\_ ```python def __init__(api_key, endpoint=DEFAULT_BACKEND) -> None ``` Initialize the ZenoClient object for API upload calls. **Arguments**: - `api_key` _str_ - the API key to authenticate uploads with. - `endpoint` _str, optional_ - the base URL of the Zeno backend. Defaults to DEFAULT_BACKEND. <a id="client.ZenoClient.create_project"></a> #### create_project ```python def create_project(name: str, view: str, metrics: List[ZenoMetric] = [], data_url: str = "", calculate_histogram_metrics: bool = True, samples_per_page: int = 10, public: bool = True) -> ZenoProject ``` Creates an empty project in Zeno's backend. **Arguments**: - `name` _str_ - The name of the project to be created. The project will be created under the current user, e.g. username/name. - `project` - str, - `view` _str_ - The view to use for the project. - `metrics` _list[ZenoMetric], optional_ - The metrics to calculate for the project. Defaults to []. - `data_url` _str, optional_ - The URL to the data to use for the project. Defaults to "". - `calculate_histogram_metrics` _bool, optional_ - Whether to calculate histogram metrics. Defaults to True. - `samples_per_page` _int, optional_ - The number of samples to show per page. Defaults to 10. - `public` _bool, optional_ - Whether the project is public. Defaults to False. **Returns**: ZenoProject | None: The created project object or None if the project could not be created. **Raises**: - `ValidationError` - If the config does not match the ProjectConfig schema. - `HTTPError` - If the project could not be created. <a id="client.ZenoClient.get_project"></a> #### get_project ```python def get_project(project_name: str) -> ZenoProject ``` Get a project object by its name. Names are split into owner/project_name. **Arguments**: - `project_name` _str_ - The owner/project_name of the project to get. **Returns**: Project | None: The project object or None if the project could not be found. **Raises**: - `HTTPError` - If the project could not be found.
zeno-client
/zeno_client-0.1.2.tar.gz/zeno_client-0.1.2/README.md
README.md
import io from typing import List, Optional import pandas as pd import requests from pydantic import BaseModel DEFAULT_BACKEND = "https://api.hub.zenoml.com" class ZenoMetric(BaseModel): """A metric to calculate for a Zeno project. Attributes: id (int): The ID of the metric. -1 if not set. name (str): The name of the metric. type (str): The type of metric to calculate. columns (list[str]): The columns to calculate the metric on. """ id: int = -1 name: str type: str columns: List[str] class ZenoProject: """Provides data upload functionality for a Zeno project. Attributes: api_key (str): The API key to authenticate uploads with. project_uuid (str): The ID of the project to add data to. endpoint (str): The base URL of the Zeno backend. """ api_key: str project_uuid: str endpoint: str def __init__( self, api_key: str, project_uuid: str, endpoint: str = DEFAULT_BACKEND ): """Initialize the Project object for API upload calls. Args: api_key (str): the API key to authenticate uploads with. project_uuid (str): the ID of the project to add data to. endpoint (str, optional): the base URL of the Zeno backend. """ self.api_key = api_key self.project_uuid = project_uuid self.endpoint = endpoint def upload_dataset( self, df: pd.DataFrame, id_column: str, label_column: Optional[str] = None, data_column: Optional[str] = None, ): """Upload a dataset to a Zeno project. Args: df (pd.DataFrame): The dataset to upload. id_column (str): The name of the column containing the instance IDs. label_column (str | None, optional): The name of the column containing the instance labels. Defaults to None. data_column (str | None, optional): The name of the column containing the raw data. Only works for small text data. Defaults to None. """ if len(id_column) == 0: raise Exception("ERROR: id_column name must be non-empty") b = io.BytesIO() df.to_feather(b) b.seek(0) response = requests.post( f"{self.endpoint}/api/dataset/{self.project_uuid}", data={ "id_column": id_column, "label_column": label_column if label_column is not None else "", "data_column": data_column if data_column is not None else "", }, files={"file": (b)}, headers={"Authorization": "Bearer " + self.api_key}, ) if response.status_code == 200: print("Successfully uploaded data") else: raise Exception(response.json()["detail"]) def upload_system( self, system_name: str, df: pd.DataFrame, output_column: str, id_column: str ): """Upload a system to a Zeno project. Args: df (pd.DataFrame): The dataset to upload. system_name (str): The name of the system to upload. output_column (str): The name of the column containing the system output. id_column (str): The name of the column containing the instance IDs. """ if len(system_name) == 0 or len(output_column) == 0: raise Exception("System_name and output_column must be non-empty.") b = io.BytesIO() df.to_feather(b) b.seek(0) response = requests.post( f"{self.endpoint}/api/system/{self.project_uuid}", data={ "system_name": system_name, "output_column": output_column, "id_column": id_column, }, files={"file": (b)}, headers={"Authorization": "Bearer " + self.api_key}, ) if response.status_code == 200: print("Successfully uploaded system") else: raise Exception(response.json()["detail"]) class ZenoClient: """Client class for data upload functionality to Zeno. Attributes: api_key (str): The API key to authenticate uploads with. endpoint (str): The base URL of the Zeno backend. """ api_key: str endpoint: str def __init__(self, api_key, endpoint=DEFAULT_BACKEND) -> None: """Initialize the ZenoClient object for API upload calls. Args: api_key (str): the API key to authenticate uploads with. endpoint (str, optional): the base URL of the Zeno backend. Defaults to DEFAULT_BACKEND. """ self.api_key = api_key self.endpoint = endpoint def create_project( self, name: str, view: str, metrics: List[ZenoMetric] = [], data_url: str = "", calculate_histogram_metrics: bool = True, samples_per_page: int = 10, public: bool = True, ) -> ZenoProject: """Creates an empty project in Zeno's backend. Args: name (str): The name of the project to be created. The project will be created under the current user, e.g. username/name. project: str, view (str): The view to use for the project. metrics (list[ZenoMetric], optional): The metrics to calculate for the project. Defaults to []. data_url (str, optional): The URL to the data to use for the project. Defaults to "". calculate_histogram_metrics (bool, optional): Whether to calculate histogram metrics. Defaults to True. samples_per_page (int, optional): The number of samples to show per page. Defaults to 10. public (bool, optional): Whether the project is public. Defaults to False. Returns: ZenoProject | None: The created project object or None if the project could not be created. Raises: ValidationError: If the config does not match the ProjectConfig schema. HTTPError: If the project could not be created. """ response = requests.post( f"{self.endpoint}/api/project", json={ "uuid": "", "name": name, "view": view, "metrics": [m.model_dump() for m in metrics], "owner_name": "", "data_url": data_url, "calculate_histogram_metrics": calculate_histogram_metrics, "samplesPerPage": samples_per_page, "public": public, "editor": True, }, headers={"Authorization": "Bearer " + self.api_key}, ) if response.status_code == 200: print("Successfully created project ", response.text[1:-1]) return ZenoProject(self.api_key, response.text[1:-1], self.endpoint) else: raise Exception(response.json()["detail"]) def get_project(self, project_name: str) -> ZenoProject: """Get a project object by its name. Names are split into owner/project_name. Args: project_name (str): The owner/project_name of the project to get. Returns: Project | None: The project object or None if the project could not be found. Raises: HTTPError: If the project could not be found. """ # Get owner and project name from project_name. # If no owner, assume current user. split_project_name = project_name.split("/") if len(split_project_name) == 1: raise Exception("Project name must be in the format owner/project_name") else: user = split_project_name[0] project_name = split_project_name[1] response = requests.get( f"{self.endpoint}/api/project-uuid/{user}/{project_name}", headers={"Authorization": "Bearer " + self.api_key}, ) if response.status_code == 200: return ZenoProject(self.api_key, response.text[1:-1], self.endpoint) else: raise Exception(response.json()["detail"])
zeno-client
/zeno_client-0.1.2.tar.gz/zeno_client-0.1.2/zeno_client/client.py
client.py
import math import matplotlib.pyplot as plt from .Generaldistribution import Distribution class Gaussian(Distribution): """ Gaussian distribution class for calculating and visualizing a Gaussian distribution. Attributes: mean (float) representing the mean value of the distribution stdev (float) representing the standard deviation of the distribution data_list (list of floats) a list of floats extracted from the data file """ def __init__(self, mu=0, sigma=1): Distribution.__init__(self, mu, sigma) def calculate_mean(self): """Function to calculate the mean of the data set. Args: None Returns: float: mean of the data set """ avg = 1.0 * sum(self.data) / len(self.data) self.mean = avg return self.mean def calculate_stdev(self, sample=True): """Function to calculate the standard deviation of the data set. Args: sample (bool): whether the data represents a sample or population Returns: float: standard deviation of the data set """ if sample: n = len(self.data) - 1 else: n = len(self.data) mean = self.calculate_mean() sigma = 0 for d in self.data: sigma += (d - mean) ** 2 sigma = math.sqrt(sigma / n) self.stdev = sigma return self.stdev def plot_histogram(self): """Function to output a histogram of the instance variable data using matplotlib pyplot library. Args: None Returns: None """ plt.hist(self.data) plt.title('Histogram of Data') plt.xlabel('data') plt.ylabel('count') def pdf(self, x): """Probability density function calculator for the gaussian distribution. Args: x (float): point for calculating the probability density function Returns: float: probability density function output """ return (1.0 / (self.stdev * math.sqrt(2*math.pi))) * math.exp(-0.5*((x - self.mean) / self.stdev) ** 2) def plot_histogram_pdf(self, n_spaces = 50): """Function to plot the normalized histogram of the data and a plot of the probability density function along the same range Args: n_spaces (int): number of data points Returns: list: x values for the pdf plot list: y values for the pdf plot """ mu = self.mean sigma = self.stdev min_range = min(self.data) max_range = max(self.data) # calculates the interval between x values interval = 1.0 * (max_range - min_range) / n_spaces x = [] y = [] # calculate the x values to visualize for i in range(n_spaces): tmp = min_range + interval*i x.append(tmp) y.append(self.pdf(tmp)) # make the plots fig, axes = plt.subplots(2,sharex=True) fig.subplots_adjust(hspace=.5) axes[0].hist(self.data, density=True) axes[0].set_title('Normed Histogram of Data') axes[0].set_ylabel('Density') axes[1].plot(x, y) axes[1].set_title('Normal Distribution for \n Sample Mean and Sample Standard Deviation') axes[0].set_ylabel('Density') plt.show() return x, y def __add__(self, other): """Function to add together two Gaussian distributions Args: other (Gaussian): Gaussian instance Returns: Gaussian: Gaussian distribution """ result = Gaussian() result.mean = self.mean + other.mean result.stdev = math.sqrt(self.stdev ** 2 + other.stdev ** 2) return result def __repr__(self): """Function to output the characteristics of the Gaussian instance Args: None Returns: string: characteristics of the Gaussian """ return "mean {}, standard deviation {}".format(self.mean, self.stdev)
zeno-distributions
/zeno_distributions-0.1.tar.gz/zeno_distributions-0.1/zeno_distributions/Gaussiandistribution.py
Gaussiandistribution.py
import math import matplotlib.pyplot as plt from .Generaldistribution import Distribution class Binomial(Distribution): """ Binomial distribution class for calculating and visualizing a Binomial distribution. Attributes: mean (float) representing the mean value of the distribution stdev (float) representing the standard deviation of the distribution data_list (list of floats) a list of floats to be extracted from the data file p (float) representing the probability of an event occurring n (int) number of trials TODO: Fill out all functions below """ def __init__(self, prob=.5, size=20): self.n = size self.p = prob Distribution.__init__(self, self.calculate_mean(), self.calculate_stdev()) def calculate_mean(self): """Function to calculate the mean from p and n Args: None Returns: float: mean of the data set """ self.mean = self.p * self.n return self.mean def calculate_stdev(self): """Function to calculate the standard deviation from p and n. Args: None Returns: float: standard deviation of the data set """ self.stdev = math.sqrt(self.n * self.p * (1 - self.p)) return self.stdev def replace_stats_with_data(self): """Function to calculate p and n from the data set Args: None Returns: float: the p value float: the n value """ self.n = len(self.data) self.p = 1.0 * sum(self.data) / len(self.data) self.mean = self.calculate_mean() self.stdev = self.calculate_stdev() def plot_bar(self): """Function to output a histogram of the instance variable data using matplotlib pyplot library. Args: None Returns: None """ plt.bar(x = ['0', '1'], height = [(1 - self.p) * self.n, self.p * self.n]) plt.title('Bar Chart of Data') plt.xlabel('outcome') plt.ylabel('count') def pdf(self, k): """Probability density function calculator for the gaussian distribution. Args: x (float): point for calculating the probability density function Returns: float: probability density function output """ a = math.factorial(self.n) / (math.factorial(k) * (math.factorial(self.n - k))) b = (self.p ** k) * (1 - self.p) ** (self.n - k) return a * b def plot_bar_pdf(self): """Function to plot the pdf of the binomial distribution Args: None Returns: list: x values for the pdf plot list: y values for the pdf plot """ x = [] y = [] # calculate the x values to visualize for i in range(self.n + 1): x.append(i) y.append(self.pdf(i)) # make the plots plt.bar(x, y) plt.title('Distribution of Outcomes') plt.ylabel('Probability') plt.xlabel('Outcome') plt.show() return x, y def __add__(self, other): """Function to add together two Binomial distributions with equal p Args: other (Binomial): Binomial instance Returns: Binomial: Binomial distribution """ try: assert self.p == other.p, 'p values are not equal' except AssertionError as error: raise result = Binomial() result.n = self.n + other.n result.p = self.p result.calculate_mean() result.calculate_stdev() return result def __repr__(self): """Function to output the characteristics of the Binomial instance Args: None Returns: string: characteristics of the Gaussian """ return "mean {}, standard deviation {}, p {}, n {}".\ format(self.mean, self.stdev, self.p, self.n)
zeno-distributions
/zeno_distributions-0.1.tar.gz/zeno_distributions-0.1/zeno_distributions/Binomialdistribution.py
Binomialdistribution.py
# Zeno Etl Libs custom library Custom library holds all the helper functions that are commonly utilized across the entire project. ## config: - Holds a common.py file, which is being utilized to get the secrets from AWS secrets manager. ## db: - Holds a db.py, which holds up the connections and various related functions for various databases such as MySQL, - Redshift, MSSQL and PostgreSQL, MongoDB. - Various functions in db.py include starting of connections, executing of certain user queries and ultimately close of - the connections. ## django - This particular folder holds an api.py file, which in turn helps in Django integration via APIs. ## helper - This folder holds multiple.py files to help with the execution of main job scripts ### 1. aws - This sub folder contains a s3.py files, which basically lets user do s3 operations from a glue or sagemaker job, - such as save a file to s3, download a specific file from s3 etc. ### 2. clevertap - This folder contains a .py file with name clevertap.py, the basic functions within this .py file helps one to - effectively run clevertap job by using clevertap API. ### 3. disprz - This folder holds disprz.py, which just like the clevertap, helps jobs related to lnd-ma-disprz via API. ### 4. email - this folder holds email.py, which basically allows user to send out a specific file from s3 as a mail attachment, - this .py file can be used to send out error logs as well as output files as email attachment. ### 5. fresh_service - This folder contains a .py file which helps in the execution of freshservice job, this .py file contains the API. ### 6. google - This folder holds 2 folders, related to google API, the use case is as follows -- #### i. playstore - This folder holds a .py file named playstore.py, this .py helps with connecting with google playstore and getting the - reviews from playstore. #### ii. sheet - This folder contains a sheet.py file, which helps user to download or upload data to a Google sheet. ### 7. Parameter - This folder houses a job-parameter.py, which basically helps user in connecting to redshift table and getting the - input parameters based on job_id. ### 8. run_notebook - This folder houses a .py file which helps user to execute certain functions related to Sagemaker jobs, such as - executing a sagemaker job.
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/README.md
README.md
import base64 import json import os import boto3 from botocore.exceptions import ClientError def get_secret_from_file(secrets_name, key=None): """ :param key: a specific key from secrets :param secrets_name: secrets file name where secrets are stored """ try: from secret.zeno_secrets import all_secrets secrets = all_secrets[secrets_name] try: if key: return secrets[key] else: return secrets except KeyError: error_message = "Set the {0} environment variable".format(key) raise EnvironmentError(error_message) except FileNotFoundError: error_message = "secrets.json not found in config folder" raise EnvironmentError(error_message) class Config: __shared_instance = 'getsecrets' @staticmethod def get_instance(): """Static Access Method""" if Config.__shared_instance == 'getsecrets': Config() return Config.__shared_instance def __init__(self): """virtual private constructor""" if Config.__shared_instance != 'getsecrets': raise Exception("This class is a config class !") else: Config.__shared_instance = self self.secrets = None def download_secret(self, secrets_name=None): if self.secrets: return self.secrets secret_name = f"arn:aws:secretsmanager:ap-south-1:921939243643:secret:{secrets_name}" region_name = "ap-south-1" # Create a Secrets Manager client session = boto3.session.Session( aws_access_key_id=os.environ.get("AWS_ACCESS_KEY_ID"), aws_secret_access_key=os.environ.get("AWS_SECRET_ACCESS_KEY"), region_name=os.environ.get("REGION_NAME") ) client = session.client( service_name='secretsmanager', region_name=region_name ) try: print("connecting with secrets manager for getting secrets") get_secret_value_response = client.get_secret_value( SecretId=secret_name ) except ClientError as e: if e.response['Error']['Code'] == 'DecryptionFailureException': raise e elif e.response['Error']['Code'] == 'InternalServiceErrorException': raise e elif e.response['Error']['Code'] == 'InvalidParameterException': raise e elif e.response['Error']['Code'] == 'InvalidRequestException': raise e elif e.response['Error']['Code'] == 'ResourceNotFoundException': raise e else: if 'SecretString' in get_secret_value_response: secret = get_secret_value_response['SecretString'] return json.loads(secret) else: decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary']) return json.loads(decoded_binary_secret) def get_secrets(self): if os.environ.get('env') == 'stage': self.secrets = self.download_secret(secrets_name='staging/etl') elif os.environ.get('env') == 'preprod': self.secrets = self.download_secret(secrets_name='preproduction/etl') elif os.environ.get('env') == 'prod': self.secrets = self.download_secret(secrets_name='production/etl') else: self.secrets = get_secret_from_file(secrets_name='development/etl') return self.secrets
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/config/common.py
common.py
from zeno_etl_libs.helper.email.email import Email, any_email_in_string def create_temp_table(db, table): """ creates table_temp table and truncates the data if table already exists """ temp_table = table.replace("-", "_") + "_temp" query = """ create temporary table if not exists "%s" (LIKE "prod2-generico"."%s"); """ % (temp_table, table) db.execute(query=query) query = """truncate "%s";""" % (temp_table) db.execute(query=query) return temp_table def month_diff(date_a, date_b): """ This function returns month difference between calendar dates 'date_a' and 'date_b' """ return 12 * (date_a.dt.year - date_b.dt.year) + (date_a.dt.month - date_b.dt.month) def drop_table(db, table_name, is_temp=True): try: if is_temp: db.execute(query="""drop table "%s";""" % table_name) else: db.execute(query="""drop table "prod2-generico"."%s";""" % table_name) print(f"table dropped: {table_name}") except Exception as e: print(f"Error in table drop: {str(e)}") def get_table_info(db, table_name, schema=None): """ :param db: db class object :param table_name: table name :param schema: is schema is None --> temp table without schema :return: table info data frame """ if schema: schema_filter = f"and table_schema = '{schema}'" else: schema_filter = '' query = f""" select ordinal_position as position, column_name, data_type, case when character_maximum_length is not null then character_maximum_length else numeric_precision end as max_length, is_nullable, column_default as default_value from information_schema.columns where table_name = '{table_name}' -- enter table name here {schema_filter} order by ordinal_position; """ db.execute(query=query) return db.cursor.fetch_dataframe() def batch(iterable, n=1): """ splits any iterable in batches Example: data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # list of data for x in batch(data, 3): print(x) # Output [0, 1, 2] [3, 4, 5] [6, 7, 8] [9, 10] :param iterable: list, tuple, df :param n: batch size """ l = len(iterable) for ndx in range(0, l, n): yield iterable[ndx:min(ndx + n, l)] def log_or_email_error(logger, exception: Exception, email_to, subject='132-supply-chain JOB FAILURE'): """ if email_to string has any email id in it, email will be sent and code will be terminated. else exception will be raised. :param logger: logger object :param exception: Exception object :param email_to: csv list of emails eg: [email protected],[email protected] """ email = Email() if any_email_in_string(email_to): logger.exception(exception) email.send_email_file(subject=subject, mail_body='The subject job has failed due to: {}'.format(exception), to_emails=email_to) exit(0) else: raise exception
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/helper.py
helper.py
import io class Reviews: def __init__(self): pass # credentials = ServiceAccountCredentials.from_json_keyfile_name( # '/Users/kuldeep/Downloads/playconsole-345511-8f3ba450d69b.json', # scopes=['https://www.googleapis.com/auth/androidpublisher']) self.keyfile_dict = { "type": "service_account", "project_id": "playconsole-345511", "private_key_id": "8f3ba450d69bbe9694ae00f49f740912052694b8", "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC0dsBllub9kOcE\nHoYA1uwFOxDQnNeuh52nPIogA+cwIWH4WfAALh2aVVf30Gf6rsjecUrCZz+8gCnd\nbHGOTbIIm+4F3XOXDRyyUIndEFTvr2/TxF2K+qW6PNSywzMHcWWKmxdJxyBrzqUi\ncTsXyH2S9WDOVelmhPMdjEvuPsU6h39zFUbITNw8YjhH1SrAZm0toOYZ/+D7RZJp\nwQApy8rxmi/6kiUNS/R9GNn0tuNE4PPT1xzpFn4stbqf2XWYtYVCb325jWmbAlj6\nfxYiWmGVlgeNlm8XiU0IVvOiCyj9WqPcG0ohOZ5OZxpf6lzkAfO+cYoBpOp7PCzN\nAKvcAB5TAgMBAAECggEASG/KNnzl5y38rLibzUZ3onnc+/+Yy2OAMpqoTsWCTN15\nd7iSD2BzXXmYP86VjhgOZMtQ2Muc18sSAuD+i8JADhiY6B3FwgHvXNvbGrLthPAE\nkRom+hw13ZWBQuf7Wad4vLQYGvMk3mEqA7Mzpw5A6XY5D1mIwC/pbhjceZsUi7Md\nS0YgX+d3WdMi6yCOT4ulOr5SBvtTt8KdQ2y3gMzRApP73PzdoygbQ/PhehTncf3I\nhSgZPnjU2612KFukWONSipuHzgWkaq20HmQEgVYyzohpntJFD6KQrtOL038yUEGm\n8sBhRkc1p9AQB21kjD/XNlH0koSBHwEVJM5KTWiP4QKBgQD5dEqBDKYuQC0HcKm9\nWuVyJPFzMGmbailTGEZb19HCsXh5jSSnH/BxeZ1HPW25d16vuH98x6pbbr1e85F5\nSJXYgU2hlWF34MVZTsTzf+uZ5aZa86fpSmoWcUVEu5L4Ygy7NxdjI0YJqZqWXNB5\npFQR6PeIRhdn5tAahxlwLXkYywKBgQC5MwSA+BYEQM+a2q2aWwh06LExbOlpckv7\nD3DD1f4kDMcqUlPpP0zaEnVPvknySO7JwHFkFLZRD0PWkM0RgNfrXkwXVVdXzx8E\nfyyl9ZKmPgaFx5L3/jY8M5IdYggaWJWtbvjsvyiKb2ACeEGI2HFCaK9nvCBOK4hj\nknUq8kBHmQKBgGuwLEmxsDvfMJE5rc005EB2elWD3NNe7SAWJqmXbdJi0uOGbwBG\n5YHXQnJyrl+WjKXHPCIeAAkgsVfARljZYPbqOx06Y61gt1Fqk9OasZbqcPpqnV40\n5b9yfrjBUR0xFtXrXolJvP6G3Vl0D/uzWSeyLsoBmDEej1AkanLm7pQpAoGBAIf7\n3u23u6rJz+Y7dUcmWpJFHX5WIxjq9MFWuA0DvsTHoSIBK13TveFNtlekOHWvea4o\nINpEnw3r8HrG/dxBR8mqBqMHZcey7GqH2sfNBi4M0ws93Ds9rKMNltb+WUbHDrg3\nCI4FWoYze0K0/CG4E4mYhlrb9riPHGlIa8Hp+KrZAoGBAI/m6ygHRllcJtl4BlJn\no4Py06mgQO2PT4lP2CEIVStF8mu4PY/oO7HHzDQe6EnvHRW0e8cKAepmSqa8kW4R\ndedaVm8SjGeU74mwGheWzQy7vsaDLafy3FDRAtFTvmE4kyyydVExWr2vAUi84TXM\nAuV9FqvKzQc5zcrT4xIEtRGu\n-----END PRIVATE KEY-----\n", "client_email": "[email protected]", "client_id": "103055631091973407668", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/playconsoleserviceacc%40playconsole-345511.iam.gserviceaccount.com" } self.package_name = "com.zenohealth.android" self.bucket_name = "pubsite_prod_rev_08705363625182098208" def get(self): from apiclient.discovery import build from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials credentials = ServiceAccountCredentials.from_json_keyfile_dict( keyfile_dict=self.keyfile_dict, scopes=['https://www.googleapis.com/auth/androidpublisher'] ) service = build('androidpublisher', 'v3', http=credentials.authorize(Http())) reviews_resource = service.reviews() reviews_page = reviews_resource.list(packageName=self.package_name, maxResults=100).execute() reviews_list = reviews_page["reviews"] infinite_loop_canary = 100 while "tokenPagination" in reviews_page: reviews_page = reviews_resource.list( packageName=self.package_name, token=reviews_page["tokenPagination"]["nextPageToken"], maxResults=100).execute() reviews_list.extend(reviews_page["reviews"]) infinite_loop_canary -= 1 if infinite_loop_canary < 0: break for i in reviews_list: print((i['comments'][0]['userComment']['text'].encode("utf-8"))) return reviews_list def download_blob_to_stream(self, source_blob_name): from google.cloud import storage from oauth2client.service_account import ServiceAccountCredentials """Downloads a blob to a stream or other file-like object.""" # The ID of your GCS bucket # bucket_name = "your-bucket-name" # The ID of your GCS object (blob) # source_blob_name = "storage-object-name" # The stream or file (file-like object) to which the blob will be written file_obj = io.BytesIO() # credentials = service_account.Credentials.from_service_account_info(self.keyfile_dict) credentials = ServiceAccountCredentials.from_json_keyfile_dict( keyfile_dict=self.keyfile_dict, ) storage_client = storage.Client(credentials=credentials) # storage_client = storage.Client.from_service_account_json('/Users/kuldeep/Downloads/playconsole-345511-8f3ba450d69b.json') # buckets = storage_client.list_buckets() # print(f"OK: ") # for bucket in buckets: # print(bucket.name) # # blobs = storage_client.list_blobs(self.bucket_name) # # print("Blobs:") # for blob in blobs: # print(blob.name) # print(f"buckets: {buckets}") bucket = storage_client.bucket(self.bucket_name) # Construct a client-side representation of a blob. # Note `Bucket.blob` differs from `Bucket.get_blob` in that it doesn't # retrieve metadata from Google Cloud Storage. As we don't use metadata in # this example, using `Bucket.blob` is preferred here. blob = bucket.blob(source_blob_name) blob.download_to_file(file_obj) # print(f"Downloaded blob {source_blob_name} to file-like object.") # return file_obj # Before reading from file_obj, remember to rewind with file_obj.seek(0). def get_all_review(self, count=100): """ returns: latest reviews by count """ from google_play_scraper import Sort, reviews result, continuation_token = reviews( self.package_name, lang='en', # defaults to 'en' country='us', # defaults to 'us' sort=Sort.NEWEST, # defaults to Sort.NEWEST count=50 # batch size ) # If you pass `continuation_token` as an argument to the reviews function at this point, # it will crawl the items after count review items. all_results = result while len(all_results) < count and result: print(f"continuation_token: {continuation_token}") result, continuation_token = reviews( self.package_name, continuation_token=continuation_token # defaults to None(load from the beginning) ) all_results += result return all_results
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/google/playstore/playstore.py
playstore.py
import json import requests from zeno_etl_libs.config.common import Config class GoogleSheet: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.url = secrets['NODE_NOTIFICATION_BASE_URL'] self.auth_token = secrets['NOTIFICATION_AUTH_TOKEN'] def download(self, data): """ sample data { "spreadsheet_id":"1fhQPO7qkbly1q-iDoMN6jmc9VNhzmYBwIFpSsh76m0M", "sheet_name":"Sheet1", "listedFields": ["posting_date","id","store_id"] } """ payload = { 'spreadsheet_id': data['spreadsheet_id'], 'sheet_name': data['sheet_name'], 'listedFields': data['listedFields'] } response = requests.post( url=self.url + "api/v1/googlesheet-access/read", headers={'Authorization': self.auth_token, 'Content-Type': 'application/json'}, data=json.dumps(payload), timeout=(1, 60)) if 200 >= response.status_code <= 299: response_data = response.json() if not response_data.get('is_error'): return response.json().get('data', []) else: raise ValueError(f"Error: {str(response.text)}") else: raise Exception(f"API call failed, error: {response.text}") def upload(self, data): data = { 'spreadsheet_id': data['spreadsheet_id'], 'sheet_name': data['sheet_name'], 'headers': data['headers'], 'data': data['data'], } response = requests.post( url=self.url + "api/v1/googlesheet-access/write", headers={'Authorization': self.auth_token, 'Content-Type': 'application/json'}, data=json.dumps(data), timeout=(1, 60)) if 200 >= response.status_code <= 299: print(f"Data uploaded successfully. spreadsheet_id: {data['spreadsheet_id']}, name: {data['sheet_name']}") else: raise Exception(f"API call failed, error: {response.text}")
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/google/sheet/sheet.py
sheet.py
import datetime import time import pandas as pd import requests from zeno_etl_libs.config.common import Config class Dizprz: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.api_token = secrets["LEARNTRON_API_TOKEN"] def __send_payload(self, offset, fetch_count, from_date, to_date): fromTimePeriod = from_date.strftime("%Y-%m-%d") toTimePeriod = to_date.strftime("%Y-%m-%d") url = f"https://disprzexternalapi.disprz.com/api/analytics/getLearnerAnalytics?offset={offset}" \ f"&fetchCount={fetch_count}&fetchCompletionDetails=true&fetchUdfDetails=true" \ f"&fromTimePeriod={fromTimePeriod}&toTimePeriod={toTimePeriod}&fetchJourneySpecificDetails=true" \ f"&journeyId=0&fetchModuleSpecificDetails=true" """ this to get the total count when offset is zero (first call) """ if offset == 0: url = url + "&fetchTotalCount=true" headers = { 'Learntron-Api-Token': self.api_token, } response = requests.get(url, headers=headers) return response.json() def get_disprz_dataframe(self): total_count = 20000 # some value > offset to start with fetch_count = 1000 # batch size offset = 0 disprz_data = list() # entire data list print("Start fetching the disprz data") start_time = time.time() to_date = datetime.datetime.today() from_date = to_date - datetime.timedelta(days=400) # last 365 days while offset < total_count: data = self.__send_payload(offset=offset, fetch_count=fetch_count, from_date=from_date, to_date=to_date) if offset == 0 and data: total_count = data[0]['totalCount'] print(f"total data at disprz is: {total_count}") offset += fetch_count disprz_data += data print(f"total: {total_count}, offset: {offset}, length: {len(data)}") try: df = pd.DataFrame(disprz_data) except Exception as error: raise Exception("Error while fetching data: {}". format(error)) print(f"total count: {total_count}, df len: {len(df)}") print(f"total time taken was: {time.time() - start_time} sec") if total_count == len(df): print(f"fetched all data from disprz successfully") return df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/disprz/disprz.py
disprz.py
import re import requests from zeno_etl_libs.config.common import Config from zeno_etl_libs.helper.aws.s3 import S3 FILE_TYPES_MIME_MAPPING = { 'csv': 'application/csv', 'txt': 'text/plain', 'log': 'text/plain', 'xls': 'application/vnd.ms-excel', 'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'zip': 'application/zip' } def determine_file_mime_type(file_name): if file_name.endswith(".csv"): return FILE_TYPES_MIME_MAPPING['csv'] elif file_name.endswith(".txt"): return FILE_TYPES_MIME_MAPPING['txt'] elif file_name.endswith(".log"): return FILE_TYPES_MIME_MAPPING['log'] elif file_name.endswith(".xls"): return FILE_TYPES_MIME_MAPPING['xls'] elif file_name.endswith(".xlsx"): return FILE_TYPES_MIME_MAPPING['xlsx'] elif file_name.endswith(".zip"): return FILE_TYPES_MIME_MAPPING['zip'] raise ValueError("No MIME type available") class Email: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.url = secrets['NODE_NOTIFICATION_BASE_URL'] self.NOTIFICATION_EMAIL_FILE_POSTFIX_URL = "api/v1/queueing-notification/send-mail" self.auth_token = secrets['NOTIFICATION_AUTH_TOKEN'] self.s3 = S3() def send_email_file(self, subject, mail_body, to_emails, file_uris=None, file_paths=None, from_email='[email protected]'): file_paths = file_paths if file_paths else [] multiple_files = list() url = self.url + self.NOTIFICATION_EMAIL_FILE_POSTFIX_URL headers = {'Authorization': self.auth_token} if isinstance(to_emails, list): to_emails = ','.join(email_id for email_id in to_emails) data = { 'subject': subject, 'body': mail_body, 'to_emails': to_emails, 'from_email': from_email, 'is_html': 'false', } if file_uris is not None: for file_uri in file_uris: file_name = file_uri.split('/')[-1] mime_type = determine_file_mime_type(file_name) file_bytes = self.s3.get_file_object(uri=file_uri) multiple_files.append(('file', (file_name, file_bytes, mime_type))) for file_path in file_paths: file_name = file_path.split('/')[::-1][0] mime_type = determine_file_mime_type(file_name) multiple_files.append(('file', (file_name, open(file_path, 'rb'), mime_type))) response = requests.post(url, data=data, files=multiple_files, headers=headers) if response.status_code != 200: raise Exception(f"Email sending failed: {response.text}") else: print(f"Email sending successful: {response.text}") def is_string_an_email(email: str): """ Checks if a given string is proper email or some fake string. Eg: email = "[email protected]" is_string_an_email(email) --> True :param email: email id string :return: True | False """ email_regx = "^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$" if re.match(email_regx, email): return True return False def any_email_in_string(csv_emails): """ In a given comma separated string, if any email id is present then returns True :param csv_emails: comma separated email id strings :returns: True | False """ for string in csv_emails.split(","): if is_string_an_email(email=string): return True return False
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/email/email.py
email.py
import errno import json import os import re import time from subprocess import Popen from shlex import split from zeno_etl_libs.config.common import Config import boto3 def ensure_session(session=None): """If session is None, create a default session and return it. Otherwise return the session passed in""" if session is None: session = boto3.session.Session() return session def get_execution_role(session): """Return the role ARN whose credentials are used to call the API. Throws an exception if the current AWS identity is not a role. Returns: (str): The role ARN """ assumed_role = session.client("sts").get_caller_identity()["Arn"] if ":user/" in assumed_role: user_name = assumed_role[assumed_role.rfind("/") + 1:] raise ValueError( f"You are running as the IAM user '{user_name}'. You must supply an IAM role to run SageMaker jobs." ) if "AmazonSageMaker-ExecutionRole" in assumed_role: role = re.sub( r"^(.+)sts::(\d+):assumed-role/(.+?)/.*$", r"\1iam::\2:role/service-role/\3", assumed_role, ) return role role = re.sub( r"^(.+)sts::(\d+):assumed-role/(.+?)/.*$", r"\1iam::\2:role/\3", assumed_role ) # Call IAM to get the role's path role_name = role[role.rfind("/") + 1:] arn = session.client("iam").get_role(RoleName=role_name)["Role"]["Arn"] if ":role/" in arn: return arn message = "The current AWS identity is not a role: {}, therefore it cannot be used as a SageMaker execution role" raise ValueError(message.format(arn)) def execute_notebook( *, image="notebook-runner", input_path, output_prefix, notebook, parameters, role="arn:aws:iam::921939243643:role/service-role/AmazonSageMaker-ExecutionRole-20220412T145187", instance_type="ml.m5.large", session, in_vpc=True, env="stage", timeout_in_sec=7200 ): session = ensure_session(session) configobj = Config.get_instance() secrets = configobj.get_secrets() if not role: role = get_execution_role(session) elif "/" not in role: account = session.client("sts").get_caller_identity()["Account"] role = "arn:aws:iam::{}:role/{}".format(account, role) if "/" not in image: # account = session.client("sts").get_caller_identity()["Account"] # region = session.region_name account = secrets['AWS_ACCOUNT_ID'] region = secrets['AWS_REGION'] image = "{}.dkr.ecr.{}.amazonaws.com/{}:latest".format(account, region, image) if notebook == None: notebook = input_path base = os.path.basename(notebook) nb_name, nb_ext = os.path.splitext(base) timestamp = time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) job_name = ( (env + "-" + re.sub(r"[^-a-zA-Z0-9]", "-", nb_name))[: 62 - len(timestamp)] + "-" + timestamp ) input_directory = "/opt/ml/processing/input/" local_input = input_directory + os.path.basename(input_path) result = "{}-{}{}".format(nb_name, timestamp, nb_ext) local_output = "/opt/ml/processing/output/" api_args = { "ProcessingInputs": [ { "InputName": "notebook", "S3Input": { "S3Uri": input_path, "LocalPath": input_directory, "S3DataType": "S3Prefix", "S3InputMode": "File", "S3DataDistributionType": "FullyReplicated", }, }, ], "ProcessingOutputConfig": { "Outputs": [ { "OutputName": "result", "S3Output": { "S3Uri": output_prefix, "LocalPath": local_output, "S3UploadMode": "EndOfJob", }, }, ], }, "ProcessingJobName": job_name, "ProcessingResources": { "ClusterConfig": { "InstanceCount": 1, "InstanceType": instance_type, "VolumeSizeInGB": 40, } }, "StoppingCondition": {"MaxRuntimeInSeconds": timeout_in_sec}, "AppSpecification": { "ImageUri": image, "ContainerArguments": [ "run_notebook", ], }, "RoleArn": role, "Environment": {}, } if in_vpc: if env == "stage": api_args["NetworkConfig"] = { 'EnableInterContainerTrafficEncryption': False, 'EnableNetworkIsolation': False, 'VpcConfig': { 'SecurityGroupIds': [ "sg-0101c938006dab959" ], 'Subnets': [ 'subnet-0446eb5f39df5ceca' ] } } elif env == "prod": api_args["NetworkConfig"] = { 'EnableInterContainerTrafficEncryption': False, 'EnableNetworkIsolation': False, 'VpcConfig': { 'SecurityGroupIds': [ "sg-08f218fe121e66d6e" ], 'Subnets': [ 'subnet-0e5470f4100505343' ] } } else: raise Exception(f"env(ie. stage, prod) input is must if is_vpc = True") api_args["Environment"]["PAPERMILL_INPUT"] = local_input api_args["Environment"]["PAPERMILL_OUTPUT"] = local_output + result if os.environ.get("AWS_DEFAULT_REGION") is not None: api_args["Environment"]["AWS_DEFAULT_REGION"] = os.environ["AWS_DEFAULT_REGION"] api_args["Environment"]["PAPERMILL_PARAMS"] = json.dumps(parameters) api_args["Environment"]["PAPERMILL_NOTEBOOK_NAME"] = notebook api_args["Environment"]["AWS_ACCESS_KEY_ID"] = secrets["AWS_ACCESS_KEY_ID"] api_args["Environment"]["AWS_SECRET_ACCESS_KEY"] = secrets["AWS_SECRET_ACCESS_KEY_ID"] api_args["Environment"]["REGION_NAME"] = "ap-south-1" client = boto3.client("sagemaker") result = client.create_processing_job(**api_args) job_arn = result["ProcessingJobArn"] job = re.sub("^.*/", "", job_arn) return job def default_bucket(): return "sagemaker-ap-south-1-921939243643" def upload_notebook(notebook, session=None): """Uploads a notebook to S3 in the default SageMaker Python SDK bucket for this user. The resulting S3 object will be named "s3://<bucket>/papermill-input/notebook-YYYY-MM-DD-hh-mm-ss.ipynb". Args: notebook (str): The filename of the notebook you want to upload. (Required) session (boto3.Session): A boto3 session to use. Will create a default session if not supplied. (Default: None) Returns: The resulting object name in S3 in URI format. """ with open(notebook, "rb") as f: return upload_fileobj(f, session) def upload_fileobj(notebook_fileobj, session=None): """Uploads a file object to S3 in the default SageMaker Python SDK bucket for this user. The resulting S3 object will be named "s3://<bucket>/papermill-input/notebook-YYYY-MM-DD-hh-mm-ss.ipynb". Args: notebook_fileobj (fileobj): A file object (as returned from open) that is reading from the notebook you want to upload. (Required) session (boto3.Session): A boto3 session to use. Will create a default session if not supplied. (Default: None) Returns: The resulting object name in S3 in URI format. """ session = ensure_session(session) snotebook = "notebook-{}.ipynb".format( time.strftime("%Y-%m-%d-%H-%M-%S", time.gmtime()) ) s3 = session.client("s3") key = "papermill_input/" + snotebook bucket = default_bucket() s3path = "s3://{}/{}".format(bucket, key) s3.upload_fileobj(notebook_fileobj, bucket, key) return s3path def get_output_prefix(): """Returns an S3 prefix in the Python SDK default bucket.""" return "s3://{}/papermill_output".format(default_bucket()) def wait_for_complete(job_name, progress=True, sleep_time=10, session=None): """Wait for a notebook execution job to complete. Args: job_name (str): The name of the SageMaker Processing Job executing the notebook. (Required) progress (boolean): If True, print a period after every poll attempt. (Default: True) sleep_time (int): The number of seconds between polls. (Default: 10) session (boto3.Session): A boto3 session to use. Will create a default session if not supplied. (Default: None) Returns: A tuple with the job status and the failure message if any. """ session = ensure_session(session) client = session.client("sagemaker") done = False while not done: if progress: print(".", end="") desc = client.describe_processing_job(ProcessingJobName=job_name) status = desc["ProcessingJobStatus"] if status != "InProgress": done = True else: time.sleep(sleep_time) if progress: print() return status, desc.get("FailureReason") def download_notebook(job_name, output=".", session=None): """Download the output notebook from a previously completed job. Args: job_name (str): The name of the SageMaker Processing Job that executed the notebook. (Required) output (str): The directory to copy the output file to. (Default: the current working directory) session (boto3.Session): A boto3 session to use. Will create a default session if not supplied. (Default: None) Returns: The filename of the downloaded notebook. """ session = ensure_session(session) client = session.client("sagemaker") desc = client.describe_processing_job(ProcessingJobName=job_name) prefix = desc["ProcessingOutputConfig"]["Outputs"][0]["S3Output"]["S3Uri"] notebook = os.path.basename(desc["Environment"]["PAPERMILL_OUTPUT"]) s3path = "{}/{}".format(prefix, notebook) if not os.path.exists(output): try: os.makedirs(output) except OSError as e: if e.errno != errno.EEXIST: raise p1 = Popen(split("aws s3 cp --no-progress {} {}/".format(s3path, output))) p1.wait() return "{}/{}".format(output.rstrip("/"), notebook) def run_notebook( image, notebook, parameters={}, role=None, instance_type="ml.m5.large", output_prefix=None, output=".", session=None, in_vpc=False, env="stage", timeout_in_sec=7200, check_completion_status=True ): """Run a notebook in SageMaker Processing producing a new output notebook. Args: image (str): The ECR image that defines the environment to run the job (required). notebook (str): The local notebook to upload and run (required). parameters (dict): The dictionary of parameters to pass to the notebook (default: {}). role (str): The name of a role to use to run the notebook (default: calls get_execution_role()). instance_type (str): The SageMaker instance to use for executing the job (default: ml.m5.large). output_prefix (str): The prefix path in S3 for where to store the output notebook (default: determined based on SageMaker Python SDK) output (str): The directory to copy the output file to (default: the current working directory). session (boto3.Session): The boto3 session to use. Will create a default session if not supplied (default: None). Returns: A tuple with the processing job name, the job status, the failure reason (or None) and the the path to the result notebook. The output notebook name is formed by adding a timestamp to the original notebook name. """ session = ensure_session(session) if output_prefix is None: output_prefix = get_output_prefix() s3path = upload_notebook(notebook, session) job_name = execute_notebook( image=image, input_path=s3path, output_prefix=output_prefix, notebook=notebook, parameters=parameters, role="arn:aws:iam::921939243643:role/service-role/AmazonSageMaker-ExecutionRole-20220412T145187", instance_type=instance_type, session=session, in_vpc=in_vpc, env=env, timeout_in_sec=timeout_in_sec ) print("Job {} started".format(job_name)) if check_completion_status: status, failure_reason = wait_for_complete(job_name) else: status = 'Completed' failure_reason = None if status == "Completed": local = download_notebook(job_name, output=output) else: local = None return (job_name, status, local, failure_reason) if __name__ == '__main__': # run_notebook( # image="notebook-runner", # notebook="send-glue-job-logs.ipynb", # role="arn:aws:iam::921939243643:role/service-role/AmazonSageMaker-ExecutionRole-20220412T145187", # parameters={"job_name": "prod-8-sales"}, # in_vpc=False # ) run_notebook( notebook="redshift-write-demo.ipynb", parameters={"env": "stage"}, in_vpc=True, env="stage" ) # run_notebook( # image="notebook-runner", # notebook="s3_read_write.ipynb", # role="arn:aws:iam::921939243643:role/service-role/AmazonSageMaker-ExecutionRole-20220412T145187", # in_vpc=False # ) # run_notebook( # image="notebook-runner", # notebook="ml-demo.ipynb", # role="arn:aws:iam::921939243643:role/service-role/AmazonSageMaker-ExecutionRole-20220412T145187", # in_vpc=True # )
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/run_notebook/run_notebook.py
run_notebook.py
import json import requests from zeno_etl_libs.config.common import Config class Tableau: """ class helps us to integrate with tableau APIs NOTES: > api_version: 3.7, server_version: v2020.1 > rest-api fundamental concepts https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_concepts_fundamentals.htm > rest-api-samples https://github.com/tableau/rest-api-samples/tree/master/python https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api_concepts_example_requests.htm (rq_bdy xml to json) > Pagination https://help.tableau.com/v2020.1/api/rest_api/en-us/REST/rest_api_concepts_paging.htm > List Data Sources https://help.tableau.com/v2020.1/api/rest_api/en-us/REST/rest_api_ref.htm#query_data_sources > List Data Source Connections https://help.tableau.com/v2020.1/api/rest_api/en-us/REST/rest_api_ref.htm#query_data_source_connections > Update a Data Source https://help.tableau.com/v2020.1/api/rest_api/en-us/REST/rest_api_ref.htm#update_data_source_connection > Sign Out https://help.tableau.com/v2020.1/api/rest_api/en-us/REST/rest_api_ref.htm#sign_out """ def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets self.url = secrets['TABLEAU_BASE_URL'] self.password = secrets['TABLEAU_ADMIN_PASSWORD'] self.site_id = secrets['TABLEAU_SITE_ID'] self.api_version = "3.7" self.token = None def login(self): self.get_token() def get_token(self): # sign in login_data = { "credentials": { "name": 'admin', "password": self.password, "site": { "contentUrl": "" } } } headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} response = requests.post( f"{self.url}/api/{self.api_version}/auth/signin", headers=headers, data=json.dumps(login_data) ) login_resp = response.json() if response.status_code == 200 and 'credentials' in login_resp: login_resp = login_resp["credentials"] self.token = login_resp["token"] return self.token raise f"tableau api call failed with status code: {response.status_code}" def logout(self): if self.token: headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Tableau-Auth': self.token} response = requests.post( f"{self.url}/api/{self.api_version}/auth/signout", headers=headers ) print(response) # logout_resp = response.json() if response.status_code == 204: print(f"Tableau logged out successfully.") else: print(f"Tableau logged failed.")
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/tableau/tableau.py
tableau.py
# Copyright 2015 CleverTap # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. __all__ = ['CleverTap'] import json import urllib.request, urllib.parse, urllib.error import time class CleverTap(object): api_hostname = 'api.clevertap.com' api_version = 1 class TargetActions(object): CREATE = "create" ESTIMATE = "estimate" LIST = "list" RESULT = "result" STOP = "stop" @classmethod def valid_actions(cls): return [cls.CREATE, cls.ESTIMATE, cls.LIST, cls.RESULT, cls.STOP] def __init__(self, account_id, account_passcode, region=None): self.account_id = account_id self.account_passcode = account_passcode self.account_region = region self.cursor = None self.url = None self.records = [] if self.account_region is not None: self.__class__.api_hostname = "%s.%s" % (self.account_region, self.__class__.api_hostname) def __repr__(self): return "%s(account_id=%s, passcode=%s, region=%s, endpoint=%s)" % (self.__class__.__name__, self.account_id, self.account_passcode, self.account_region, self.api_endpoint) @property def api_endpoint(self): return 'https://%s/%s' % (self.__class__.api_hostname, self.__class__.api_version) def upload(self, data, dryRun=False): """upload an array of profile and/or event dicts""" # validate data validation_error = self._validate("upload", data) if validation_error: raise Exception(validation_error) return # construct the base request url self.url = '/'.join([self.api_endpoint, "upload"]) if dryRun: self.url += "?dryRun=1" # the request body is the json encoded data body = json.dumps({"d":data}) # request headers headers_params = {'Content-Type':'application/json;charset=utf-8'} # make the request return self._call(body=body, headers_params=headers_params) def targets(self, action, payload): # validate validation_error = self._validate(action, payload) if validation_error: raise Exception(validation_error) return url_action = None if action in self.TargetActions.valid_actions(): url_action = action if not url_action: print(("unknown targets action %s" % action)) return if url_action == self.TargetActions.ESTIMATE: payload['estimate_only'] = True url_action = self.TargetActions.CREATE # construct the request url self.url = '/'.join([self.api_endpoint, "targets", "%s.json"%url_action]) # the request body is the json encoded payload body = json.dumps(payload) if payload else None # request headers headers_params = {'Content-Type':'application/json'} # make the request return self._call(body=body, headers_params=headers_params) def profile(self, email=None, identity=None, objectId=None): """fetch an individual user profile by ID, one of email, identity or CleverTap objectID""" if email is None and identity is None and objectId is None: raise Exception("profile requires one of email, identity or objectId") return # construct the request url self.url = '/'.join([self.api_endpoint, "profile.json"]) if email is not None: self.url += "?email=%s" % email elif identity is not None: self.url += "?identity=%s" % identity elif objectId is not None: self.url += "?objectId=%s" % objectId # request headers headers_params = {'Content-Type':'application/json'} # make the request return self._call(headers_params=headers_params) def profiles(self, query, batch_size=10): """download profiles defined by query""" return self._fetch_records("profiles", query, batch_size=batch_size) def events(self, query, batch_size=10): """download events defined by query""" return self._fetch_records("events", query, batch_size=batch_size) def _fetch_records(self, type, query, batch_size=10): # reset our records cache self.records = [] # validate query validation_error = self._validate(type, query) if validation_error: raise Exception(validation_error) return # construct the base request url self.baseurl = '/'.join([self.api_endpoint, "%s.json"%type]) _args = urllib.parse.urlencode({"query":json.dumps(query), 'batch_size':batch_size}) # add query and batch_size as query args self.url = "%s?%s"%(self.baseurl, _args) headers_params = {'Content-Type':'application/json;charset=utf-8'} # fetch initial cursor while True: print('fetching initial cursor') res = self._call(headers_params=headers_params) or {} if 'error' in res: print(res) if res.get('code', -1) == 1: print("request throttled, retrying in 30") time.sleep(30) else: # some other error abort return self.records else: break self.cursor = res.get("cursor", None) # if we have a cursor then make a second request with the cursor if self.cursor: # construct the request url # add the cursor self.url = "%s?cursor=%s"%(self.baseurl, self.cursor) # convenience inner function to handle cursor requests def call_records(): print(("calling %s records" % batch_size)) # make the request res = self._call() or {} # make sure the cursor is ready with data cursor_ready = not 'error' in res if not cursor_ready: print(res) if res.get('code', -1) == 2: wait_interval = 5 print(("cursor not ready, retrying again in %s" % wait_interval)) time.sleep(wait_interval) return # parse response self.cursor = res.get("next_cursor", None) new_records = res.get("records", []) # add the new records array to our records array self.records += new_records print(("Received %s records; have %s total records" % (len(new_records), len(self.records)))) # if the request returns a new cursor, update the api url with the new cursor if self.cursor: self.url = "%s?cursor=%s"%(self.baseurl, self.cursor) else: self.url = None # keep making requests with the new cursor as long as we have a cursor while True: if self.cursor is None: print("no cursor, finished fetching records") break else: print(("have cursor %s" % self.cursor)) call_records() return self.records def _call(self, **kwargs): if self.url == None: print("api url is None") return None headers_params = kwargs.get('headers_params', {}) # add account_id, and passcode to request headers headers_params['X-CleverTap-Account-Id'] = self.account_id headers_params['X-CleverTap-Passcode'] = self.account_passcode args = kwargs.get("args", None) if args: args = urllib.parse.urlencode(args) body = kwargs.get("body", None) # Create the request req = urllib.request.Request(self.url, args, headers_params) if body: req.data = bytes(json.dumps(body), 'utf-8') try: # Open the request f = urllib.request.urlopen(req) # Get the response response = f.read() # Close the opened request f.close() except Exception as e: print(e) try: return e.read() except Exception as e: pass return None # Parse and return the response try: res = self._parse_response(response) except Exception as e: print(e) res = None return res def _parse_response(self, response): """Parse a response from the API""" try: res = json.loads(response) except Exception as e: e.args += ('API response was: %s' % response) raise e return res def _validate(self, type, data): """Simple data validation""" validation_error = None if not self.account_id: validation_error = "clevertap account id missing" return validation_error if not self.account_passcode: validation_error = "clevertap account passcode missing" return validation_error if type in [self.TargetActions.CREATE, self.TargetActions.ESTIMATE]: if data is None: return "Push targets action %s requires a payload"%type if (data.get("name", None) is None): return "Push targets action %s requires a name"%type if (data.get("where", None) is None) and (data.get("segment", None) is None): return "Push targets action %s requires a where or segment value"%type if (data.get("where", None) is not None) and (data.get("segment", None) is not None): return "Push targets action %s does not support both a where value and a segment value, specify one or the other"%type if (data.get("segment", None) is not None): if data.get("segment") != "all": return "Push targets action %s segment value must be 'all'"%type if (data.get("content", None) is None): return "Push targets action %s requires a content dict"%type if (data.get("content", None) is not None): if (data.get("content", {}).get("title", None) is None) or (data.get("content", {}).get("body", None) is None): return "Push targets action %s content dict requires a title and a body"%type if (data.get("devices", None) is None): return "Push targets action %s requires a devices array"%type return validation_error if type == self.TargetActions.LIST: # no-op return validation_error if type in [self.TargetActions.RESULT, self.TargetActions.STOP]: if (data is None) or (data.get("id", None) is None): validation_error = "Push targets %s action requires a target id"%type return validation_error if type == "upload": for record in data or []: identity = record.get("identity", None) or record.get("FBID", None) or record.get("GPID", None) or record.get("objectId", None) if identity is None: validation_error = "record must contain an identity, FBID, GPID or objectId field: %s"%record return validation_error break record_type = record.get("type", None) if record_type not in ['profile', 'event']: validation_error = "record type must be profile or event: %s"%record return validation_error break if record_type == "profile": profileData = record.get("profileData", None) if profileData is None or not isinstance(profileData, dict): validation_error = "record with type profile must contain a profileData dict: %s"%record return validation_error break Phone = profileData.get("Phone", None) if Phone and (not isinstance(Phone, str) or not Phone.startswith("+")): validation_error = "profile Phone must be a string and start with +<country code>: %s"%record return validation_error break if record_type == "event": evtData = record.get("evtData", None) if evtData is None or not isinstance(evtData, dict): validation_error = "record with type event must contain an evtData dict: %s"%record return validation_error break return validation_error
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/clervertap/clevertap_2.py
clevertap_2.py
import json import requests from zeno_etl_libs.config.common import Config from zeno_etl_libs.logger import get_logger class CleverTap: """ class: is used to fetch the data from CleverTap using API calls """ def __init__(self, api_name=None, event_name=None, batch_size=None, from_date=None, to_date=None, query=None): """ :params account_id: CleverTap Account ID :params passcode: CleverTap passcode :params api_name: CleverTap api name to fetch the different type of data eg: "profiles.json" :params batch_size: no of records to be fetched per batch :params from_date: start date filter, YYYYMMDD :params to_date: end date filter, YYYYMMDD :params query: """ self.logger = get_logger() configobj = Config.get_instance() secrets = configobj.get_secrets() account_id = secrets['CLEVERTAP_ACCOUNT_ID'] passcode = secrets['CLEVERTAP_PASSCODE'] self.uri = "api.clevertap.com" self.account_id = account_id if account_id else "TEST-K5Z-K95-RZ6Z" self.passcode = passcode if passcode else 'd1d2cc1f8624434dbb3038b77d3fcf9d' self.api_name = api_name if api_name else "profiles.json" self.event_name = event_name if event_name else "App Launched" self.batch_size = batch_size if batch_size else 5 self.from_date = from_date if from_date else 20220101 self.to_date = to_date if to_date else 20220101 self.cursor = None self.all_records = list() self.query = query or { "event_name": self.event_name, "from": self.from_date, "to": self.to_date } self.headers = { 'X-CleverTap-Account-Id': self.account_id, 'X-CleverTap-Passcode': self.passcode, 'Content-Type': 'application/json' } self.logger.info(f"account_id: {account_id}, api_name: {api_name}, event_name:{event_name}") def set_cursor(self): url = f"https://{self.uri}/1/{self.api_name}?batch_size={self.batch_size}" response = requests.request("POST", url, headers=self.headers, data=json.dumps(self.query)) if response.status_code == 200: self.cursor = response.json()['cursor'] self.logger.info("Cursor set successfully!") return self.cursor else: raise Exception(f"CleverTap cursor getting failed: {str(response.text)}") def get_profile_data_batch(self): url = f"https://{self.uri}/1/{self.api_name}?cursor={self.cursor}" response = requests.request("GET", url, headers=self.headers, data="") if response.status_code == 200: data = response.json() """ it will be set to None if all records fetched """ self.cursor = data.get("next_cursor") """ adding batch records to all records list """ self.all_records += data['records'] self.logger.info(f"Batch records fetched successfully, count: {len(data['records'])}") # self.logger.info(f"Batch records: {data['records']}") return data['records'] else: raise Exception(f"CleverTap profile_data getting failed: {str(response.text)}") def get_profile_data_all_records(self): self.set_cursor() while self.cursor: self.get_profile_data_batch() def create_target(self): """ eg. query: {"name": "My Sms API campaign", "estimate_only": True, "target_mode": "sms", "where": {"event_name": "Charged", "from": 20171001, "to": 20171220, "common_profile_properties": { "profile_fields": [{"name": "Customer Type", "operator": "equals", "value": "Platinum"}]}}, "respect_frequency_caps": True, "content": {"body": "Sms body"}, "when": "now"} """ response = requests.post(f'https://in1.{self.uri}/1/targets/{self.api_name}', headers=self.headers, data=json.dumps(self.query)) if 200 <= response.status_code <= 299: res = response.text self.logger.info(f"create target successful, status code:{response.status_code} text: {res}") return res else: raise Exception(f"create target failed, status code:{response.status_code} text: {response.text}")
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/clervertap/clevertap.py
clevertap.py
import boto3 import datetime from dateutil.tz import tzutc from zeno_etl_libs.config.common import Config class Redshift: def __init__(self): configobj = Config.get_instance() self.secrets = configobj.get_secrets() self.client = boto3.client('redshift') self.cluster_identifier = self.secrets["CLUSTER_IDENTIFIER"] self.database_name = self.secrets["REDSHIFT_WRITE_DB"] def get_snapshot_identifier(self, snapshot_type='automated', utc_date: str = None) -> str: """ :param snapshot_type: 'automated' | 'manual' :param utc_date: format "%Y-%m-%d" Returns: Automated snapshot identifier of given utc_date, if utc_date is not given then latest snapshot """ end_time = datetime.datetime.strptime(utc_date, "%Y-%m-%d") if utc_date else datetime.datetime.now(tz=tzutc()) end_time = end_time.replace(hour=23, minute=59, second=59) # utc_date = str(end_time.date()) """ last 1 year """ start_time = end_time - datetime.timedelta(days=365) response = self.client.describe_cluster_snapshots( ClusterIdentifier=self.cluster_identifier, SnapshotType=snapshot_type, StartTime=start_time, EndTime=end_time, MaxRecords=100, ClusterExists=True, SortingEntities=[ { 'Attribute': 'CREATE_TIME', 'SortOrder': 'DESC' }, ] ) for snap in response['Snapshots']: print(snap['SnapshotIdentifier']) if not utc_date: return snap['SnapshotIdentifier'] else: if utc_date in snap['SnapshotIdentifier']: return snap['SnapshotIdentifier'] return "" def create_manual_snapshot(self): """ resource to read: 1. https://n2ws.com/blog/aws-automation/3-reasons-to-automate-your-manual-redshift-snapshots 2. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/redshift.html#Redshift.Client.create_cluster_snapshot 3. https://aws.amazon.com/redshift/pricing/ """ # Keeping the minutes and seconds constant time_now = datetime.datetime.now(tz=tzutc()).strftime('%Y-%m-%d-%H-00-00') snapshot_identifier = f"{self.cluster_identifier}-{time_now}" print(f"snapshot_identifier: {snapshot_identifier}") print("MANUAL_SNAPSHOT_RETENTION_PERIOD type: ", type(int(self.secrets["MANUAL_SNAPSHOT_RETENTION_PERIOD"]))) # TODO: take MANUAL_SNAPSHOT_RETENTION_PERIOD period from secrets(type int needed str was coming, so fix it) response = self.client.create_cluster_snapshot( SnapshotIdentifier=snapshot_identifier, ClusterIdentifier=self.cluster_identifier, ManualSnapshotRetentionPeriod=int(self.secrets["MANUAL_SNAPSHOT_RETENTION_PERIOD"]), Tags=[ { 'Key': 'backup_type', 'Value': 'monthly' }, ] ) return response
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/aws/redshift.py
redshift.py
import os import time from io import StringIO, BytesIO # python3; python2: BytesIO import boto3 from pandas import ExcelWriter from zeno_etl_libs.config.common import Config class S3: def __init__(self, bucket_name=None, region=None): configobj = Config.get_instance() secrets = configobj.get_secrets() self.secrets = secrets self.bucket_name = bucket_name or 'aws-glue-temporary-921939243643-ap-south-1' self.region = region if region else 'ap-south-1' self.aws_access_key_id = secrets['AWS_ACCESS_KEY_ID'] self.aws_secret_access_key = secrets['AWS_SECRET_ACCESS_KEY_ID'] self.s3_resource = boto3.resource('s3', self.region, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key) self.s3_client = boto3.client('s3', self.region, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key) def save_df_to_s3(self, df, file_name=None, index_label=False, index=False, header=True): file_name = file_name or f"temp_{int(time.time() * 1000)}.csv" csv_buffer = StringIO() df.to_csv(csv_buffer, index_label=index_label, index=index, header=header) self.s3_resource.Object(self.bucket_name, file_name).put(Body=csv_buffer.getvalue()) s3_uri = f"s3://{self.bucket_name}/{file_name}" return s3_uri def get_file_object(self, uri, encoding="utf-8"): names = uri.split("//")[-1].split("/") self.bucket_name = names[0] key = "/".join(names[1:]) obj = self.s3_resource.Object(self.bucket_name, key).get() big_str = obj["Body"].read().decode(encoding) return big_str def unload_redshift_s3(self, table_name, file_s3_uri, db, schema=None): if schema: table_location = f"""{schema}"."{table_name}""" else: """ temp tables have session specific schema which has no name """ table_location = table_name query = f""" UNLOAD ('select * from "{table_location}"') TO '{file_s3_uri}' CREDENTIALS 'aws_access_key_id={self.aws_access_key_id};aws_secret_access_key={self.aws_secret_access_key}' FORMAT CSV --HEADER --ADDQUOTES; """ db.execute(query=query) def write_to_db_from_s3_csv(self, table_name, file_s3_uri, db, schema=None, delete_folder=False): if schema: table_location = f"""{schema}"."{table_name}""" else: """ temp tables have session specific schema which has no name """ table_location = table_name query = f""" COPY "{table_location}" FROM '{file_s3_uri}' CREDENTIALS 'aws_access_key_id={self.aws_access_key_id};aws_secret_access_key={self.aws_secret_access_key}' -- for better copy performance COMPUPDATE OFF STATUPDATE OFF REGION 'ap-south-1' IGNOREHEADER 1 FORMAT AS csv MAXERROR 1 ; """ db.execute(query=query) if delete_folder: self.delete_s3_obj(uri=file_s3_uri, delete_folder=True) else: self.delete_s3_obj(uri=file_s3_uri) def write_df_to_db(self, df, table_name, db, schema=None): file_name = f"temp_{int(time.time() * 1000)}.csv" file_s3_uri = self.save_df_to_s3(df=df, file_name=file_name) # eg. "s3://{self.bucket_name}/df.csv" self.write_to_db_from_s3_csv(table_name=table_name, file_s3_uri=file_s3_uri, db=db, schema=schema) self.delete_s3_obj(uri=file_s3_uri) def write_to_text_file_on_s3(self, file_name): file_name = f"temp_{int(time.time() * 1000)}.txt" or file_name csv_buffer = StringIO() self.s3_resource.Object(self.bucket_name, file_name).put(Body=csv_buffer.getvalue()) s3_uri = f"s3://{self.bucket_name}/{file_name}" return s3_uri def write_df_to_excel(self, data, file_name): """ df: data frame file_name: with reference to /tmp folder sheet_name: """ path = "/".join(os.getcwd().split("/")[:-2]) + "/tmp/" if not os.path.exists(path): os.mkdir(path, 0o777) local_file_full_path = path + file_name with ExcelWriter(local_file_full_path) as writer: for sheet_name, df in data.items(): df.to_excel(writer, sheet_name=sheet_name) return local_file_full_path def write_text_to_file(self, text, file_name): path = "/".join(os.getcwd().split("/")[:-2]) + "/tmp/" if not os.path.exists(path): os.mkdir(path, 0o777) local_file_full_path = path + file_name file_p = open(local_file_full_path, "w") file_p.writelines(text) return local_file_full_path def upload_file_to_s3(self, file_name): local_file_full_path = "/".join(os.getcwd().split("/")[:-2]) + "/tmp/" + file_name s3_file_full_path = file_name self.s3_client.upload_file( Filename=local_file_full_path, Bucket=self.bucket_name, Key=s3_file_full_path, ) s3_uri = f"s3://{self.bucket_name}/{s3_file_full_path}" return s3_uri def download_file_from_s3(self, file_name): path = "/".join(os.getcwd().split("/")[:-2]) + "/tmp/" print(f"path: {path}") if not os.path.exists(path): os.mkdir(path, 0o777) head, tail = os.path.split(file_name) local_file_full_path = path + tail s3_file_full_path = file_name self.s3_client.download_file( Bucket=self.bucket_name, Key=s3_file_full_path, Filename=local_file_full_path ) return local_file_full_path def delete_s3_obj(self, uri, delete_folder=False): if delete_folder: response = self.s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix="unload/" + uri.split('/')[-2] + "/") files_in_folder = response["Contents"] files_to_delete = [] for f in files_in_folder: files_to_delete.append({"Key": f["Key"]}) response = self.s3_client.delete_objects( Bucket=self.bucket_name, Delete={"Objects": files_to_delete} ) print(response) else: names = uri.split("//")[-1].split("/") self.bucket_name = names[0] key = "/".join(names[1:]) response = self.s3_resource.Object(self.bucket_name, key).delete() print(f"S3 object(uri: {uri}) delete response: {response}") def move_s3_obj(self, source, target_key): try: self.s3_client.copy_object( Bucket=self.bucket_name, CopySource=source, Key=target_key ) except self.s3_client.exceptions.ObjectNotInActiveTierError as e: print(f"Copying s3 obj failed: {e}") raise e self.delete_s3_obj(uri=f"s3:/{source}") def read_df_from_s3_csv(self, bucket_name, object_key): try: import pandas as pd client = boto3.client('s3') csv_obj = client.get_object(Bucket=bucket_name, Key=object_key) body = csv_obj['Body'] csv_string = body.read().decode('utf-8') df = pd.read_csv(StringIO(csv_string)) return df except Exception as error: print(f"Read from S3 failed: {error}") raise error
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/helper/aws/s3.py
s3.py
import datetime import json import os import requests from zeno_etl_libs.config.common import Config class Sql: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.url = secrets['DJANGO_ACCOUNTS_BASE_URL'] self.node_url = secrets['NODE_NOTIFICATION_BASE_URL'] self.auth_token = secrets['DJANGO_OAUTH_TOKEN'] self.node_auth_token = secrets['NOTIFICATION_AUTH_TOKEN'] self.env = os.environ.get('env', 'dev') def create_big_query_log_for_mysql_db_update(self, request_body, event_name): url = self.node_url + "api/v1/bigQuery-access/event-logs-write" headers = {'Content-Type': 'application/json', 'Authorization': self.node_auth_token} __BIG_QUERY_PLATFORM = 'data-science-server' __BIG_QUERY_PLATFORM_ID = 2 __BIG_QUERY_PLATFORM_CODE = 'DSS' request_dict = { "__environment": self.env, "logs": [{ "__platform": __BIG_QUERY_PLATFORM, "__platform_properties": { "__id": __BIG_QUERY_PLATFORM_ID, "__code": __BIG_QUERY_PLATFORM_CODE }, "__user_email": "[email protected]", "user_properties": { "_id": 5 }, "__event": event_name, "event_properties": { "_entity_details": { "_entity_name": request_body['database_table_name'] }, "updated_fields": request_body['data'] }, "__trigger_at": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") }] } requests.post(url, json=request_dict, headers=headers, timeout=(1, 10)) def update(self, script_data, logger=None) -> tuple: """ Used to hit the django-accounts dynamic-update-api that will make changes to the Mysql Table. If you pass the logger_obj as a parameter to this function it will log to the file in case of errors. """ # construct request_body from data request_body = {'database_table_name': script_data['table']} request_body_data = list() for entry in script_data['data_to_be_updated']: update_data_dict = dict() update_data_dict['id'] = entry.pop('id') update_data_dict['update_data'] = entry request_body_data.append(update_data_dict) request_body['data'] = request_body_data url = self.url + 'api/web/v1/dynamic-update-api/' authorization_token = 'Bearer {}'.format(self.auth_token) headers = {"Content-Type": "application/json", 'Authorization': authorization_token} try: response = requests.post(url, data=json.dumps(request_body), headers=headers, timeout=(1, 60)) if response.status_code == 200: self.create_big_query_log_for_mysql_db_update(request_body, "DSS_MYSQL_UPDATE") return True, response.json() raise Exception(f"api/web/v1/dynamic-update-api failed: {response.text}") except Exception as exc: raise Exception(exc) class Django: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.url = secrets['DJANGO_ACCOUNTS_BASE_URL'] self.node_url = secrets['NODE_NOTIFICATION_BASE_URL'] self.auth_token = secrets['DJANGO_OAUTH_TOKEN'] self.node_auth_token = secrets['NOTIFICATION_AUTH_TOKEN'] self.env = os.environ.get('env', 'dev') def django_model_execution_log_create_api(self, request_body, logger=None) -> tuple: """ Used to hit django-accounts model-execution-log-admin api that will create entries in model_execution_log table. If you pass the logger_obj as a parameter to this function it will log to the file in case of errors. e.g. request_body = { "object_id": 1, # PK (line-item) "content_type": 74 # PK of django tables } """ url = self.url + 'api/web/v1/model-execution-log-admin/' headers = {"Content-Type": "application/json", 'Authorization': f"Bearer {self.auth_token}"} response = requests.post(url, data=json.dumps(request_body), headers=headers) if response.status_code in [200, 201]: return True, response.json() return False, response.text
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/django/api.py
api.py
sales = """ select z."store-id", z."drug-id", avg(z."mean-fptr") as "mean-fptr", sum(z.quantity) as quantity from ( select f."patient-id", f."store-id", f."id" as "bill-id", c."id" as "drug-id", a."quantity" as "sold-quantity", coalesce(g."returned-quantity", 0) as "returned-quantity", (a."quantity" - coalesce(g."returned-quantity", 0)) as "quantity", a."rate", case when coalesce( ii."actual-quantity", 1) = 0 then coalesce(ii."net-value" , b."final-ptr") else coalesce(ii."net-value" / coalesce( ii."actual-quantity", 1), b."final-ptr") end as "mean-fptr", ((a."quantity" - coalesce(g."returned-quantity", 0)) * a."rate") as "value" from "prod2-generico"."bills-1" f join "prod2-generico"."bill-items-1" a on f."id" = a."bill-id" left join "prod2-generico"."inventory-1" b on a."inventory-id" = b."id" left join "prod2-generico"."invoice-items-1" ii on b."invoice-item-id" = ii."id" left join "prod2-generico"."drugs" c on c."id" = b."drug-id" left join "prod2-generico"."customer-return-items-1" g on g."inventory-id" = a."inventory-id" and g."bill-id" = a."bill-id" where DATEDIFF(d,date(a."created-at"),current_date) <= {days} and (a."quantity" - coalesce(g."returned-quantity", 0)) > 0 and f."store-id" = {store_id} )z group by z."store-id", z."drug-id" """ expiry = """ -- short book select case when date(inv.expiry) > current_date then 'Near Expiry' else 'Expired' end as "inventory-type", date(i."invoice-date") as "invoice-date", case when "auto-short" = 1 and "home-delivery" = 0 and "patient-id" = 4480 then 'Auto Short' when "auto-short" = 1 and "home-delivery" = 0 and "patient-id" != 4480 then 'Manual Short' when "auto-short" = 0 and "auto-generated" = 0 and "home-delivery" = 0 then 'Patient Request' when "auto-short" = 0 and "auto-generated" = 0 and "home-delivery" = 1 then 'Patient Request with HD' when sb.id is null then 'Source not found' else 'Unclassified' end as "request-type", inv.id as "inventory-id", inv."store-id", inv."drug-id", d."drug-name", date(inv."created-at") as created_date, date(inv.expiry) as expiry, inv.barcode, inv."invoice-item-id", inv.quantity, i."id" as "invoice-id", i."invoice-number", e."name" as "store-name", i."distributor-id", f.name as "distributor-name", d."type" as "drug-type", d."category", df."drug-grade", d."cold-chain", df.min, df."safe-stock", df.max, inv."ptr" as fptr, inv."ptr" * inv.quantity as value, sb.id as "short-book-id", e."franchisee-id" , case when (i."invoice-date") < (e."opened-at") then 'launch-stock' else 'normal' end as "launch-flag", inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer" as "locked-quantity", (inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer") * inv."ptr" as "locked-value" from "prod2-generico"."inventory-1" inv left join "prod2-generico".invoices i on inv."invoice-id" = i.id left join "prod2-generico"."invoice-items-1" ii on inv."invoice-item-id" = ii."id" left join "prod2-generico"."short-book-invoice-items" sbii on ii."invoice-item-reference" = sbii."invoice-item-id" left join "prod2-generico"."short-book-1" sb on sbii."short-book-id" = sb.id join "prod2-generico"."drugs" d on d."id" = inv."drug-id" join "prod2-generico"."stores" e on e."id" = inv."store-id" left join "prod2-generico"."distributors" f on f."id" = i."distributor-id" left join "prod2-generico"."drug-order-info" df on df."drug-id" = inv."drug-id" and df."store-id" = inv."store-id" left join "prod2-generico"."invoices-1" i2 on inv."franchisee-invoice-id" = i2.id where ((e."franchisee-id" = 1 and DATEDIFF(d,current_date,date(inv.expiry))< {expiry_days}) or( e."franchisee-id" != 1 and DATEDIFF(d,current_date,date(inv.expiry))< {fofo_expiry_days})) -- and extract(yrs from (inv.expiry)) <= extract(yrs from (current_date)) + 1 and extract(yrs from (inv.expiry)) >= 2017 and ( (inv.quantity != 0) or (inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer" > 0) ) and (e."franchisee-id" = 1 or (e."franchisee-id" != 1 and i2."franchisee-invoice" = 0)) """ return_and_rotation = """ select date(i."invoice-date") as "invoice-date", '{inventory_type}' as "inventory-type", case when "auto-short" = 1 and "home-delivery" = 0 and "patient-id" = 4480 then 'Auto Short' when "auto-short" = 1 and "home-delivery" = 0 and "patient-id" != 4480 then 'Manual Short' when "auto-short" = 0 and "auto-generated" = 0 and "home-delivery" = 0 then 'Patient Request' when "auto-short" = 0 and "auto-generated" = 0 and "home-delivery" = 1 then 'Patient Request with HD' when sb.id is null then 'Source not found' else 'Unclassified' end as "request-type", inv.id as "inventory-id", inv."store-id", inv."drug-id", d."drug-name", date(inv."created-at") as created_date, date(inv.expiry) as expiry, inv.barcode, inv."invoice-item-id", inv.quantity, i."id" as "invoice-id", i."invoice-number", e."name" as "store-name", i."distributor-id", f.name as "distributor-name", d."type" as "drug-type", d."category", df."drug-grade", d."cold-chain", df.min, df."safe-stock", df.max, inv."ptr" as fptr, inv."ptr" * inv.quantity as value, e."franchisee-id" , case when (i."invoice-date") < (e."opened-at") then 'launch-stock' else 'normal' end as "launch-flag", sb.id as "short-book-id", inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer" as "locked-quantity", (inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer") * inv."ptr" as "locked-value" from "prod2-generico"."inventory-1" inv left join "prod2-generico".invoices i on inv."invoice-id" = i.id left join "prod2-generico"."invoice-items-1" ii on inv."invoice-item-id" = ii."id" left join "prod2-generico"."short-book-invoice-items" sbii on ii."invoice-item-reference" = sbii."invoice-item-id" left join "prod2-generico"."short-book-1" sb on sbii."short-book-id" = sb.id join "prod2-generico"."drugs" d on d."id" = inv."drug-id" join "prod2-generico"."stores" e on e."id" = inv."store-id" left join "prod2-generico"."distributors" f on f."id" = i."distributor-id" left join "prod2-generico"."drug-order-info" df on df."drug-id" = inv."drug-id" and df."store-id" = inv."store-id" left join "prod2-generico"."invoices-1" i2 on inv."franchisee-invoice-id" = i2.id where concat(inv."store-id", CONCAT('-', inv."drug-id")) in {store_drug_list} and DATEDIFF(d,date(inv."created-at"),current_date)>= {days} and ((e."franchisee-id" = 1 and DATEDIFF(d,current_date,date(inv.expiry))>={expiry_days}) or( e."franchisee-id" != 1 and DATEDIFF(d,current_date,date(inv.expiry))>={fofo_expiry_days}) or( e."franchisee-id" != 1 and (sb."created-at") < (e."opened-at") and {FIFO_boolean_negative})) and ( (inv.quantity != 0) or (inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer" > 0) ) and DATEDIFF(d, date(d."created-at"),current_date)>= 270 and (e."franchisee-id" = 1 or (e."franchisee-id" != 1 and i2."franchisee-invoice" = 0)) """ dead_liquidation = """ select transfer."origin-store", transfer."origin-store-name", transfer."destination-store", transfer."destination-store-name", transfer."transferred-quantity", transfer."inventory-id", transfer."drug-id", d."drug-name", d.type as "drug-type", d.category, doi."drug-grade", transfer."received-at", n."created-at" as "bill-timestamp", m."rate", transfer."final-ptr", case when n."created-at" is null then 0 else coalesce(m."quantity", 0) end as "sold-quantity", case when n."created-at" is null then 0 else coalesce(m."returned-quantity", 0) end as "returned-quantity", case when n."created-at" is null then 0 else coalesce((m."quantity" - m."returned-quantity") * m."rate", 0) end as "net-value" from ( select c."origin-store", a."destination-store", sum(b.quantity) as "transferred-quantity", e."name" as "origin-store-name", f."name" as "destination-store-name", b."inventory-id", c."drug-id", avg(c."final-ptr") as "final-ptr", min(b."received-at") as "received-at" from "prod2-generico"."stock-transfers-1" a join "prod2-generico"."stock-transfer-items-1" b on a."id" = b."transfer-id" join "prod2-generico"."inventory-1" c on b."inventory-id" = c."id" left join "prod2-generico"."drugs" d on c."drug-id" = d."id" left join "prod2-generico"."stores" e on c."origin-store" = e."id" left join "prod2-generico"."stores" f on a."destination-store" = f."id" where "source-store" = 111 and d."type" != 'category-4' and date(b."transferred-at") <= current_date and d."id" != 406872 and d."id" != 424692 and d."id" != 401179 and d."id" != 444213 and cast(b."received-at" as varchar) <> '0000-00-00 00:00:00' group by c."origin-store", a."destination-store", e."name", f."name", b."inventory-id", c."drug-id" )transfer left join "prod2-generico"."bill-items-1" m on transfer."inventory-id" = m."inventory-id" left join "prod2-generico"."bills-1" n on n."id" = m."bill-id" and transfer."destination-store" = n."store-id" left join "prod2-generico".drugs d on transfer."drug-id" = d.id left join "prod2-generico"."drug-order-info" doi on doi."drug-id" = transfer."drug-id" and doi."store-id" = transfer."destination-store" """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/dead_stock/dead_stock_queries.py
dead_stock_queries.py
import pandas as pd import datetime import numpy as np import argparse import os from zeno_etl_libs.queries.dead_stock import dead_stock_queries def dead_data_prep(store_id=None, days=270, logger=None, connection = None): # from zeno_etl_libs.db.db import DB # parser = argparse.ArgumentParser(description="This is ETL script.") # parser.add_argument('-e', '--env', default="dev", type=str, required=False) # args, unknown = parser.parse_known_args() # env = args.env # os.environ['env'] = env # rs_db = DB() # rs_db.open_connection() rs_db = connection '''Getting sales data''' sales_query = dead_stock_queries.sales.format(days=days, store_id=str(store_id)) sales = rs_db.get_df(sales_query) # removing DC inventory sales_store = sales[~sales['store-id'].isin([92, 111, 156, 160, 169, 172])] logger.info('Sales: Distinct # of drugs' + str(sales['drug-id'].nunique())) logger.info('Sales: Stores' + str(sales['store-id'].nunique())) '''Getting inventory data''' inventory_query = ''' select inv."store-id", inv."drug-id", avg(coalesce(ii."net-value" / ii."actual-quantity", inv."final-ptr")) as "mean-fptr", sum(inv."quantity") as "inventory-oh" from "prod2-generico"."inventory-1" inv left join "prod2-generico"."invoice-items" ii on inv."invoice-item-id" = ii."franchisee-invoice-item-id" left join "prod2-generico"."stores" s on s.id = inv."store-id" left join "prod2-generico"."invoices-1" i on i.id = inv."franchisee-invoice-id" where inv.quantity > 0 and inv."store-id" = {store_id} and (s."franchisee-id" = 1 or (s."franchisee-id" != 1 and i."franchisee-invoice" = 0)) group by inv."store-id", inv."drug-id" '''.format(store_id=store_id) inventory = rs_db.get_df(inventory_query) # removing DC inventory inventory_store = inventory[~inventory['store-id'].isin([92, 111, 156, 160, 169, 172])] logger.info('Inv: Distinct # of drugs ' + str(inventory_store['drug-id'].nunique())) logger.info('Inv: Stores ' + str(inventory_store['store-id'].nunique())) store_inventory_sales = inventory_store.merge( sales_store, on=['store-id', 'drug-id'], how='outer', suffixes=('', '-y')) # print(store_inventory_sales.columns) store_inventory_sales['mean-fptr'] = store_inventory_sales['mean-fptr'].combine_first( store_inventory_sales['mean-fptr-y']) store_inventory_sales.drop('mean-fptr-y', axis=1, inplace=True) store_inventory_sales['quantity'].fillna(0, inplace=True) store_inventory_sales['inventory-oh'].fillna(0, inplace=True) # print('# of line items', store_inventory_sales.shape[0]) logger.info('Distinct # of drugs '+ str(store_inventory_sales['drug-id'].nunique())) logger.info('Store - '+ str(store_id)) logger.info('Total inventory count '+ str(store_inventory_sales['inventory-oh'].sum())) logger.info('Total sales count ' + str(store_inventory_sales.quantity.sum())) '''Getting drug and store info ''' drug_store_info_query = ''' select store.id, store.name, d.id, d."drug-name", d.type, d.category, coalesce(doi."drug-grade", 'NA') as "drug-grade" from "prod2-generico".stores store cross join "prod2-generico".drugs d left join "prod2-generico"."drug-order-info" doi on d.id = doi."drug-id" and store.id = doi."store-id" where store.id = {store_id} '''.format(store_id=store_id) drug_store_info = rs_db.get_df(drug_store_info_query) drug_store_info.columns = ['store-id', 'store-name', 'drug-id', 'drug-name', 'type', 'category', 'drug-grade'] store_inventory_sales = store_inventory_sales.merge( drug_store_info, on=['store-id', 'drug-id'], how='left') #rs_db.close_connection() # store_inventory_sales = store_inventory_sales.merge(ptr, how='left', on='drug_id') return sales, inventory, store_inventory_sales def dead_stock_categorization( sales, inventory, store_inventory_sales, stores_list, logger=None, days=270, expiry_days=120,fofo_expiry_days=210, connection = None): # from zeno_etl_libs.db.db import DB # parser = argparse.ArgumentParser(description="This is ETL script.") # parser.add_argument('-e', '--env', default="dev", type=str, required=False) # args, unknown = parser.parse_known_args() # env = args.env # os.environ['env'] = env # rs_db = DB() # rs_db.open_connection() rs_db = connection '''Dead Stock Categorization 1. Expiry - within 4 months or already expired: Return 2. No sales at enterprise level (in 9 months): Return 3. No sales at Store "A", but at other stores (in 9 months): Rotate 4. FIFO dead: sold in stores but inventory created more than 9 months ago: Rotate ''' # 1. '''Getting expired inventory data''' expiry_query = dead_stock_queries.expiry.format(expiry_days=expiry_days,fofo_expiry_days=fofo_expiry_days) expiry_barcodes = rs_db.get_df(expiry_query) expiry_agg = expiry_barcodes.groupby(['store-id', 'drug-id'])['quantity'].sum().reset_index() expiry_agg.columns = ['store-id', 'drug-id', 'expired-quantity'] store_inventory_sales_with_exp = store_inventory_sales.merge(expiry_agg, on=['store-id', 'drug-id'], how='left') store_inventory_sales_with_exp['expired-quantity'].fillna(0, inplace=True) store_inventory_sales_with_exp['quantity-rem-after-expiry'] = ( store_inventory_sales_with_exp['inventory-oh'] - store_inventory_sales_with_exp['expired-quantity'] ) logger.info('Expired to be returned: units to be returned' + str(store_inventory_sales_with_exp['expired-quantity'].sum())) logger.info('Expired to be returned: value' + str(round( (expiry_barcodes['value']).sum()/10000000,2)) + 'Crs') logger.info('Post expiry drugs inventory') logger.info('# of line items' + str(store_inventory_sales_with_exp.shape[0])) logger.info('Distinct # of drugs' + str(store_inventory_sales_with_exp['drug-id'].nunique())) logger.info('Stores' + str(store_inventory_sales_with_exp['store-id'].nunique())) # 2. drug_wise_sales = store_inventory_sales_with_exp[ store_inventory_sales_with_exp['store-id'].isin(stores_list) ].groupby(['drug-id'])['quantity'].sum().reset_index() drugs_no_enterprise_sales = drug_wise_sales.loc[drug_wise_sales['quantity'] == 0, 'drug-id'] drug_returns = store_inventory_sales_with_exp[ (store_inventory_sales_with_exp['drug-id'].isin(drugs_no_enterprise_sales.values)) & (store_inventory_sales_with_exp['store-id'].isin(stores_list)) ] store_inventory_sales_with_exp_post_returns = store_inventory_sales_with_exp[ (~store_inventory_sales_with_exp['drug-id'].isin(drugs_no_enterprise_sales.values)) ] logger.info('Drug with no enterprise sales: units to be returned ' + str(drug_returns['quantity-rem-after-expiry'].sum())) logger.info('Drug with no enterprise sales: value ' + str(round((drug_returns['mean-fptr'].astype(float)*drug_returns['quantity-rem-after-expiry'].astype(float)).sum()/10000000, 2)) + 'Crs') logger.info('Post returns, drugs inventory') logger.info('# of line items ' + str(store_inventory_sales_with_exp_post_returns.shape[0])) logger.info('Distinct # of drugs ' + str(store_inventory_sales_with_exp_post_returns['drug-id'].nunique())) logger.info('Stores ' + str(store_inventory_sales_with_exp_post_returns['store-id'].nunique())) # getting barcode level info for drugs to return return_store_drug_comb = drug_returns['store-id'].astype(int).astype(str) + '-' + drug_returns['drug-id'].astype(int).astype(str) return_store_drug_list = str(list( return_store_drug_comb.values)).replace('[', '(').replace(']', ')') return_query = dead_stock_queries.return_and_rotation.format( store_drug_list=return_store_drug_list, inventory_type='Return', days=0, expiry_days=expiry_days, fofo_expiry_days=fofo_expiry_days, FIFO_boolean_negative = True) return_barcodes = rs_db.get_df(return_query) separate_old_inv_query = ''' select concat("store-id", CONCAT('-', "drug-id")) as "store-drug-id" from "prod2-generico"."inventory-1" inv left join "prod2-generico"."stores" s on inv."store-id" = s.id where "store-id" in {stores} and "drug-id" in {drugs} and (quantity > 0 or ((inv.quantity != 0) or (inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer" > 0))) and (((s."franchisee-id" = 1) and(inv."created-at" <DATEADD(d,-{days},CURRENT_DATE))) or ((s."franchisee-id" != 1) and(inv."created-at" <DATEADD(d,-10,CURRENT_DATE)))) group by "store-id", "drug-id" '''.format(days=days, stores=tuple(drug_returns['store-id'].unique()) + (0,0), drugs=tuple(drug_returns['drug-id'].unique()) + (0,0)) separate_old_inv = rs_db.get_df(separate_old_inv_query) return_barcodes['store-drug-id'] = return_barcodes['store-id'].astype(str) + '-' + return_barcodes['drug-id'].astype(str) return_barcodes = return_barcodes[return_barcodes['store-drug-id'].isin(tuple(separate_old_inv['store-drug-id']))] return_barcodes = return_barcodes.drop(columns=['store-drug-id']) # FOFO - No Baby food in return/rotate except launch stock, No PR conditions = [((return_barcodes['franchisee-id'].astype(int) != 1) & (return_barcodes['launch-flag'].astype(str) != 'launch-stock') & (return_barcodes['drug-type'].astype(str) == 'baby-food')), ((return_barcodes['franchisee-id'].astype(int) != 1) & (return_barcodes['request-type'].isin(['Patient Request', 'Patient Request with HD'])))] choices = [1, 1] return_barcodes['delete'] = np.select(conditions, choices) return_barcodes = return_barcodes[return_barcodes['delete']!=1] return_barcodes = return_barcodes.drop(columns=['delete']) # 3. # store drug combination (store active for than 6 months) store_inventory_sales_active = store_inventory_sales_with_exp_post_returns[ store_inventory_sales_with_exp_post_returns['store-id'].isin(stores_list)] store_drug_no_sale = store_inventory_sales_active.loc[ (store_inventory_sales_active['quantity'] == 0)] store_drug_with_sale = store_inventory_sales_with_exp_post_returns.loc[ (store_inventory_sales_with_exp_post_returns['quantity'] != 0) ] zippin_inventory = store_drug_no_sale.groupby( ['type', 'category', 'drug-id'])['quantity-rem-after-expiry'].\ sum().reset_index() logger.info('Rotating inventory stats') logger.info('# of drugs to rotate ' + str(zippin_inventory.shape[0])) logger.info('Quantity to be rotated ' + str(zippin_inventory['quantity-rem-after-expiry'].sum())) logger.info('Rotation Drugs value ' + str(round((store_drug_no_sale['mean-fptr'].astype(float) *store_drug_no_sale['quantity-rem-after-expiry'].astype(float)).sum()/10000000,2)) + 'Crs') # getting barcode level info for drugs to rotate rotate_store_drug_comb = store_drug_no_sale['store-id'].astype(int).astype(str) + '-' + store_drug_no_sale['drug-id'].astype(int).astype(str) #logger.info(list(rotate_store_drug_comb.values)) #logger.info(len(list(rotate_store_drug_comb.values))) rotation_drug_list = str(list( rotate_store_drug_comb.values)).replace('[', '(').replace(']', ')') if len(list(rotate_store_drug_comb.values))==0: rotation_drug_list = [0] rotation_drug_list = str(list(rotation_drug_list)).replace('[', '(').replace(']', ')') rotation_query = dead_stock_queries.return_and_rotation.format( store_drug_list=rotation_drug_list, inventory_type='Rotate', days=0, expiry_days=expiry_days, fofo_expiry_days=fofo_expiry_days, FIFO_boolean_negative = True) rotation_barcodes = rs_db.get_df(rotation_query) separate_old_inv_query = ''' select concat("store-id", CONCAT('-', "drug-id")) as "store-drug-id" from "prod2-generico"."inventory-1" inv left join "prod2-generico"."stores" s on inv."store-id" = s.id where "store-id" in {stores} and "drug-id" in {drugs} and (quantity > 0 or ((inv.quantity != 0) or (inv."locked-quantity" + inv."locked-for-audit" + inv."locked-for-check" + inv."locked-for-transfer" > 0))) and (((s."franchisee-id" = 1) and(inv."created-at" <DATEADD(d,-{days},CURRENT_DATE))) or ((s."franchisee-id" != 1) and(inv."created-at" <DATEADD(d,-10,CURRENT_DATE)))) group by "store-id", "drug-id" '''.format(days=days,stores=tuple(store_drug_no_sale['store-id'].unique()) + (0,0), drugs=tuple(store_drug_no_sale['drug-id'].unique()) + (0,0)) separate_old_inv = rs_db.get_df(separate_old_inv_query) rotation_barcodes['store-drug-id'] = rotation_barcodes['store-id'].astype(str) + '-' + rotation_barcodes['drug-id'].astype(str) rotation_barcodes = rotation_barcodes[rotation_barcodes['store-drug-id'].isin(tuple(separate_old_inv['store-drug-id']))] rotation_barcodes = rotation_barcodes.drop(columns=['store-drug-id']) # FOFO - No Baby food in return/rotate except launch stock, No PR conditions = [((rotation_barcodes['franchisee-id'].astype(int) != 1) & (rotation_barcodes['launch-flag'].astype(str) != 'launch-stock') & (rotation_barcodes['drug-type'].astype(str) == 'baby-food')), ((rotation_barcodes['franchisee-id'].astype(int) != 1) & (rotation_barcodes['request-type'].isin(['Patient Request', 'Patient Request with HD'])))] choices = [1, 1] rotation_barcodes['delete'] = np.select(conditions, choices) rotation_barcodes = rotation_barcodes[rotation_barcodes['delete'] != 1] rotation_barcodes = rotation_barcodes.drop(columns=['delete']) # 4. fifo_drug = store_drug_with_sale.loc[store_drug_with_sale['inventory-oh'] != 0, ['store-id', 'drug-id']].drop_duplicates() fifo_drug_list = fifo_drug['store-id'].astype(str) + '-' + fifo_drug['drug-id'].astype(str) fifo_drug_list = str(list(fifo_drug_list)).replace('[','(').replace(']',')') #print('fifo drug list - {}'.format(fifo_drug_list)) fifo_query = dead_stock_queries.return_and_rotation.format( store_drug_list=fifo_drug_list, inventory_type='FIFO Dead', days=days, expiry_days=expiry_days, fofo_expiry_days=fofo_expiry_days, FIFO_boolean_negative=False) # logger.info(fifo_query) fifo_barcodes = rs_db.get_df(fifo_query) logger.info('FIFO dead stock stats') logger.info('Quantity to be rotated' + str(fifo_barcodes['quantity'].sum())) logger.info('FIFO Drugs value' + str(round((fifo_barcodes['fptr'].astype(float) *fifo_barcodes['quantity'].astype(float)).sum()/10000000,2)) + 'Crs') # rs_db.close_connection() return zippin_inventory, store_drug_no_sale, store_drug_with_sale, expiry_barcodes, return_barcodes, rotation_barcodes, fifo_barcodes def dead_value_bucket(dead_rotate): dead_rotate = dead_rotate.groupby('inventory-id')['value']. sum().reset_index() dead_rotate = dead_rotate.sort_values('value', ascending=False) dead_rotate['cumsum-percentage'] = ( dead_rotate['value'].cumsum()/dead_rotate['value'].sum()) dead_rotate['bucket'] = np.select( [dead_rotate['cumsum-percentage'] <= 0.01, (dead_rotate['cumsum-percentage'] > 0.01) & (dead_rotate['cumsum-percentage'] <= 0.02), (dead_rotate['cumsum-percentage'] > 0.02) & (dead_rotate['cumsum-percentage'] <= 0.05), (dead_rotate['cumsum-percentage'] > 0.05) & (dead_rotate['cumsum-percentage'] <= 0.10), (dead_rotate['cumsum-percentage'] > 0.10) & (dead_rotate['cumsum-percentage'] <= 0.20), (dead_rotate['cumsum-percentage'] > 0.20) & (dead_rotate['cumsum-percentage'] <= 0.40), (dead_rotate['cumsum-percentage'] > 0.40) & (dead_rotate['cumsum-percentage'] <= 0.60), (dead_rotate['cumsum-percentage'] > 0.60) & (dead_rotate['cumsum-percentage'] <= 0.80), dead_rotate['cumsum-percentage'] > 0.80 ], ['under 1%', '1-2%', '2-5%', '5-10%', '10-20%', '20-40%', '40-60%', '60-80%', 'more than 80%']) return dead_rotate
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/dead_stock/dead_stock_categorisation.py
dead_stock_categorisation.py
max_bill_id = """ select max("bill-id") as "bill-id-max" from "prod2-generico"."{}" """ max_return_id = """ select max("return-item-id") as "return-item-id-max" from "prod2-generico"."{}" """ insert_sales_record = """ insert into "prod2-generico"."{}" ( "updated-by", "updated-at", "bill-id", "patient-id", "store-id", "inventory-id", "drug-id", "drug-name", "type", "category", "composition", "company", "company-id", "composition-master-id", "quantity", "created-at", "year-created-at", "month-created-at", "rate", "net-rate", "net-quantity", "revenue-value", "purchase-rate", "ptr", "mrp", "substitution-status", "created-by", "bill-flag", "old-new", "first-bill-date", "p-reference", "patient-category", "lp-flag", "min", "max", "safe-stock", "promo-code-id", "payment-method", "doctor-id", "code-type", "pc-type", "promo-code", "campaign", "pr-flag", "hd-flag", "ecom-flag", "substitution-status-g", "substitution-status-trend", "goodaid-availablity-flag", "cgst-rate", "cgst-amt", "cgst", "sgst-rate", "sgst-amt", "sgst", "tax-rate", "igst-rate", "igst-amt", "utgst-rate", "utgst-amt", "serial", "hsncode", "is-repeatable", "store-name", "store-manager", "line-manager", "abo", "city", "store-b2b", "store-month-diff", "store-opened-at", "franchisee-id", "franchisee-name", "cluster-id", "cluster-name", "return-item-id", "bill-item-id", "promo-discount", "type-at-selling", "category-at-selling", "created-date", "invoice-item-reference", "distributor-id", "distributor-name", "billed-at-return-ref", "return-reason", "drug-grade", "acquired" , "old-new-static", "crm-flag", "invoice-id", "franchisee-invoice", "group" ) select 'etl-automation' as "updated-by", convert_timezone('Asia/Calcutta', GETDATE()) as "updated-at", f."id" as "bill-id", f."patient-id" as "patient-id", f."store-id" as "store-id", b."id" as "inventory-id" , b."drug-id" as "drug-id", c."drug-name" as "drug-name", c."type", c."category", c."composition", c."company", c."company-id" as "company-id", c."composition-master-id" as "composition-master-id", a."quantity", f."created-at" as "created-at" , extract(year from f."created-at") as "year-created-at", extract(month from f."created-at") as "month-created-at", a."rate", a."rate" as "net-rate", a."quantity" as "net-quantity", (a."rate" * a."quantity") as "revenue-value", b."purchase-rate" as "purchase-rate", b."ptr", b."mrp", s."substitution-status" as "substitution-status", f."created-by" as "created-by", 'gross' as "bill-flag", case when (12 * (extract (year from f."created-at") - extract (year from pm."first-bill-date")) + (extract (month from f."created-at") - extract (month from pm."first-bill-date")))>= 1 then 'old' else 'new' end as "old-new", pm."first-bill-date" as "first-bill-date", p."reference" as "p-reference", p."patient-category" as "patient-category", b."franchisee-inventory" as "lp-flag", doi."min", doi."max" , doi."safe-stock" as "safe-stock", f."promo-code-id" as "promo-code-id", f."payment-method" as "payment-method", f."doctor-id" as "doctor-id", pc."code-type" as "code-type" , pc."type" as "pc-type", pc."promo-code" as "promo-code", ca."campaign", NVL(pso2."pr-flag", false), NVL(pso2."hd-flag", false), NVL(pso2."ecom-flag", false), case when (s."substitution-status" = 'substituted' and c."company-id" = 6984 and f."created-at" >= mca."store-first-inv-date") then 'ga-substituted' when (s."substitution-status" = 'substituted' and mca."store-first-inv-date" is null ) then 'ga-not-available' else s."substitution-status" end as "substitution-status-g", case when (s."substitution-status" = 'substituted' and c."company-id" = 6984 and f."created-at" >= mca."store-first-inv-date") then 'substituted' else s."substitution-status" end as "substitution-status-trend", case when (f."created-at" >= casl."system-first-inv-date") then 'available' else 'not-available' end as "goodaid-availablity-flag", a."cgst-rate" as "cgst-rate" , a."cgst-amt" as "cgst-amt" , a."cgst" , a."sgst-rate" as "sgst-rate" , a."sgst-amt" as "sgst-amt" , a."sgst" , (a."cgst-rate" + a."sgst-rate") as "tax-rate", a."igst-rate" as "igst-rate" , a."igst-amt" as "igst-amt" , a."utgst-rate" as "utgst-rate" , a."utgst-amt" as "utgst-amt" , f."serial" , c."hsncode", c."is-repeatable" as "is-repeatable", msm.store as "store-name", msm."store-manager" , msm."line-manager", msm.abo, msm.city, (case when (f."gst-number" is not null and f."gst-number"!='') then 'B2B' else msm."store-b2b" end) as "store-b2b", msm."month-diff" as "store-month-diff", msm."opened-at" as "store-opened-at", msm."franchisee-id", msm."franchisee-name", msm."cluster-id", msm."cluster-name", NULL as "return-item-id", a.id as "bill-item-id", a."promo-discount" as "promo-discount", c."type" as "type-at-selling", c."category" as "category-at-selling", date(f."created-at") as "created-date", ii1."invoice-item-reference", i2."distributor-id", ds."name" as "distributor-name", f."created-at" as "billed-at-return-ref", NULL as "return-reason", doi."drug-grade", msm."acquired" , msm."old-new-static", NVL(pso2."crm-flag", false), i2.id as "invoice-id", i2."franchisee-invoice", d1."group" from "prod2-generico"."bills-1" f left join "prod2-generico"."bill-items-1" a on f."id" = a."bill-id" left join "prod2-generico"."inventory-1" b on a."inventory-id" = b."id" left join "prod2-generico"."invoice-items-1" ii1 on b."invoice-item-id" = ii1.id left join "prod2-generico".invoices i2 on ii1."invoice-id" = i2.id left join "prod2-generico".distributors ds ON i2."distributor-id" = ds.id left join "prod2-generico"."drugs" c on c."id" = b."drug-id" left join "prod2-generico"."drug-unique-composition-mapping" d1 on b."drug-id" = d1."drug-id" left join "prod2-generico"."bill-items-substitutes" s on a."id" = s."bill-item-id" left join "prod2-generico"."patients-metadata-2" pm on f."patient-id" = pm."id" left join "prod2-generico"."patients" p on f."patient-id" = p."id" left join "prod2-generico"."drug-order-info" doi on (doi."store-id" = f."store-id" and doi."drug-id" = b."drug-id") left join "prod2-generico"."promo-codes" pc on NVL(f."promo-code-id", 0) = pc."id" left join "prod2-generico"."campaigns" ca on NVL(pc."campaign-id", 0) = ca."id" left join "prod2-generico"."{}" as pso2 on a."bill-id" = pso2."id" left join "prod2-generico"."group-activation" mca on f."store-id" = mca."store-id" and d1."group" = mca."group" left join "prod2-generico"."group-activation-system-level" casl on d1."group" = casl."group" inner join "prod2-generico"."{}" as msm on f."store-id" = msm."id" where f."id" > {} union all select 'etl-automation' as "updated-by", convert_timezone('Asia/Calcutta', GETDATE()) as "updated-at", a."bill-id" as "bill-id", f."patient-id" as "patient-id", f."store-id" as "store-id", b."id" as "inventory-id" , b."drug-id" as "drug-id", c."drug-name" as "drug-name", c."type", c."category", c."composition", c."company", c."company-id" as "company-id", c."composition-master-id" as "composition-master-id", (a."returned-quantity") as "quantity", f."returned-at" as "created-at", extract(year from f."returned-at") as "year-created-at", extract(month from f."returned-at") as "month-created-at", (a."rate") as "rate", (a."rate" *-1) as "net-rate", (a."returned-quantity" *-1) as "net-quantity", (a."rate" * a."returned-quantity" *-1) as "revenue-value", b."purchase-rate" as "purchase-rate", b."ptr", b."mrp", 'return' as "substitution-status", f."processed-by" as "created-by", 'return' as "bill-flag", case when (12 * (extract (year from f."returned-at") - extract (year from pm."first-bill-date")) + (extract (month from f."returned-at") - extract (month from pm."first-bill-date")))>= 1 then 'old' else 'new' end as "old-new", pm."first-bill-date" as "first-bill-date", p."reference" as "p-reference", p."patient-category" as "patient-category", b."franchisee-inventory" as "lp-flag", doi."min", doi."max" , doi."safe-stock" as "safe-stock", b2."promo-code-id" as "promo-code-id", b2."payment-method" as "payment-method", b2."doctor-id" as "doctor-id", pc."code-type" as "code-type" , pc."type" as "pc-type", pc."promo-code" as "promo-code", ca."campaign", NVL(pso2."pr-flag", false), NVL(pso2."hd-flag", false), NVL(pso2."ecom-flag", false), 'return' as "substitution-status-g", 'return' as "substitution-status-trend", case when (f."returned-at" >= casl."system-first-inv-date") then 'available' else 'not-available' end as "goodaid-availablity-flag", a."cgst-rate" as "cgst-rate" , 0 as "cgst-amt" , 0 as "cgst" , a."sgst-rate" as "sgst-rate" , 0 as "sgst-amt" , 0 as "sgst" , (a."cgst-rate" + a."sgst-rate") as "tax-rate", a."igst-rate" as "igst-rate" , 0 as "igst-amt" , a."utgst-rate" as "utgst-rate" , 0 as "utgst-amt" , f1."serial" , c."hsncode", c."is-repeatable" as "is-repeatable", msm.store as "store-name", msm."store-manager" , msm."line-manager", msm.abo, msm.city, (case when (f1."gst-number" is not null and f1."gst-number"!='') then 'B2B' else msm."store-b2b" end) as "store-b2b", msm."month-diff" as "store-month-diff", msm."opened-at" as "store-opened-at", msm."franchisee-id", msm."franchisee-name", msm."cluster-id", msm."cluster-name", a."id" as "return-item-id", NULL as "bill-item-id", cast(NULL as numeric) as "promo-discount", c."type" as "type-at-selling", c."category" as "category-at-selling", date(f."returned-at") as "created-date", ii1."invoice-item-reference", i2."distributor-id", ds."name" as "distributor-name", b2."created-at" as "billed-at-return-ref", a."return-reason" as "return-reason", doi."drug-grade", msm."acquired" , msm."old-new-static", NVL(pso2."crm-flag", false), i2.id as "invoice-id", i2."franchisee-invoice", d1."group" from "prod2-generico"."customer-returns-1" f left join "prod2-generico"."customer-return-items-1" a on f."id" = a."return-id" left join "prod2-generico"."bills-1" f1 on a."bill-id" = f1."id" left join "prod2-generico"."inventory-1" b on a."inventory-id" = b."id" left join "prod2-generico"."invoice-items-1" ii1 ON b."invoice-item-id" = ii1.id left join "prod2-generico".invoices i2 on ii1."invoice-id" = i2.id left join "prod2-generico".distributors ds ON i2."distributor-id" = ds.id inner join "prod2-generico"."drugs" c on c."id" = b."drug-id" left join "prod2-generico"."drug-unique-composition-mapping" d1 on b."drug-id" = d1."drug-id" left join "prod2-generico"."patients-metadata-2" pm on f."patient-id" = pm."id" left join "prod2-generico"."patients" p on f."patient-id" = p."id" left join "prod2-generico"."drug-order-info" doi on (doi."store-id" = f."store-id" and doi."drug-id" = b."drug-id") inner join "prod2-generico"."bills-1" b2 on a."bill-id" = b2."id" left join "prod2-generico"."promo-codes" pc on NVL(b2."promo-code-id", 0) = pc."id" left join "prod2-generico"."campaigns" ca on NVL(pc."campaign-id", 0) = ca."id" left join "prod2-generico"."group-activation" mca on f."store-id" = mca."store-id" and d1."group" = mca."group" left join "prod2-generico"."group-activation-system-level" casl on d1."group" = casl."group" left join "prod2-generico"."{}" as pso2 on a."bill-id" = pso2."id" inner join "prod2-generico"."{}" as msm on f."store-id" = msm."id" where a."id" > {} """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/sales/sales_config.py
sales_config.py
max_short_book_id = """ select max("id") as "short-book-id-max" from "prod2-generico"."{}" """ insert_as_ms_query = """ insert into "prod2-generico"."{}" ( "id", "created-by", "updated-by", "updated-at", "patient-id", "store-name", "drug-name", "as-ms", "created-to-invoice-days", "created-to-invoice-hour", "created-to-dispatch-days", "created-to-dispatch-hour", "created-to-delivery-days", "created-to-delivery-hour", "created-to-re-order-days", "created-to-re-order-hour", "created-to-order-days", "created-to-order-hour", "status", "requested-quantity", "quantity", "required-quantity", "inventory-at-creation", "inventory-at-ordering", "created-at", "year-created-at", "month-created-at", "ordered-time", "invoiced-at", "dispatched-at", "delivered-at", "completed-at", "re-ordered-at", "store-delivered-at", "decline-reason", "type", "store-id", "drug-id", "company", "company-id", "composition", "composition-master-id", "category", "schedule", "sub-type", "preferred-distributor-id", "preferred-distributor-name", "drug-grade", "purchase-rate", "ptr", "distributor-type", "recieved-distributor-id", "received-distributor-name", "forward-dc-id", "dc-name", "abo", "line-manager", "store-manager", "city", "store-b2b", "franchisee-short-book" ) select a.id, 'etl-automation' as "created-by", 'etl-automation' as "updated-by", convert_timezone('Asia/Calcutta', GETDATE()) as "updated-at", a."patient-id" as "patient-id", b."name" as "store-name", a."drug-name" as "drug-name", case when a."auto-short" = 1 and a."home-delivery" = 0 and a."created-by" = 'Auto Short' and a."patient-id" = 4480 then 'AS' when a."auto-short" = 1 and a."home-delivery" = 0 and a."patient-id" != 4480 then 'MS' else 'NA' end as "as-ms", --Fulfillment on Invoice (case when (a."invoiced-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."invoiced-at") end) as "created-to-invoice-days", (case when (a."invoiced-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."invoiced-at") end) as "created-to-invoice-hour", --Fulfillment on dispatch (case when (a."dispatched-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."dispatched-at") end) as "created-to-dispatch-days", (case when (a."dispatched-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."dispatched-at") end) as "created-to-dispatch-hour", --Fulfillment on delivery (case when (msda."store-delivered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", msda."store-delivered-at") end) as "created-to-delivery-days", (case when (msda."store-delivered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", msda."store-delivered-at") end) as "created-to-delivery-hour", -- Re-order Timing -- (case when (a."re-ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."re-ordered-at") end) as "created-to-re-order-days", (case when (a."re-ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."re-ordered-at") end) as "created-to-re-order-hour", --order Timing-- (case when (a."ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."ordered-at") end) as "created-to-order-days", (case when (a."ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."ordered-at") end) as "created-to-order-hour", a."status" as "status", a."requested-quantity" as "requested-quantity", a."quantity" as "quantity", a."required-quantity" as "required-quantity", a."inventory-at-creation" as "inventory-at-creation" , a."inventory-at-ordering" as "inventory-at-ordering", case when a."created-at" = '0101-01-01' then null else a."created-at" end as "created-at", extract(year from a."created-at") as "year-created-at", extract(month from a."created-at") as "month-created-at", case when a."ordered-at" = '0101-01-01' then null else a."ordered-at" end as "ordered-time", case when a."invoiced-at" = '0101-01-01' then null else a."invoiced-at" end as "invoiced-at", case when a."dispatched-at" = '0101-01-01' then null else a."dispatched-at" end as "dispatched-at", case when a."delivered-at" = '0101-01-01' then null else a."delivered-at" end as "delivered-at", case when a."completed-at" = '0101-01-01' then null else a."completed-at" end as "completed-at", case when a."re-ordered-at" = '0101-01-01' then null else a."re-ordered-at" end as "re-ordered-at", case when msda."store-delivered-at" = '0101-01-01' then null else msda."store-delivered-at" end as "store-delivered-at", a."decline-reason" as "decline-reason", c."type", a."store-id" as "store-id", a."drug-id" as "drug-id", c."company", c."company-id" as "company-id" , c."composition" , c."composition-master-id" as "composition-master-id" , c."category" , c."schedule" , c."sub-type" as "sub-type" , f."id" as "preferred-distributor-id", f."name" as "preferred-distributor-name", e."drug-grade" as "drug-grade", dp."purchase-rate" as "purchase-rate", dp."ptr", d."type" as "distributor-type", d."id" as "recieved-distributor-id", d."name" as "received-distributor-name", j."forward-dc-id" as "forward-dc-id", ss."name" as "dc-name", msm."abo" , msm."line-manager" , msm."store-manager" , msm."city", msm."store-b2b", a."franchisee-short-book" as "franchisee-short-book" from "prod2-generico"."short-book-1" a left join "prod2-generico"."stores" b on b."id" = a."store-id" left join "prod2-generico"."drugs" c on c."id" = a."drug-id" left join ( select "drug-id", AVG("purchase-rate") as "purchase-rate", AVG(ptr) as "ptr" from "prod2-generico"."inventory-1" i where "created-at" >= dateadd(day, -360, CURRENT_DATE) group by "drug-id") as dp on a."drug-id" = dp."drug-id" left join "prod2-generico"."distributors" d on d."id" = a."distributor-id" left join "prod2-generico"."drug-order-info" e on e."store-id" = a."store-id" and e."drug-id" = a."drug-id" left join "prod2-generico"."distributors" f on a."preferred-distributor" = f."id" left join ( select "store-id", "forward-dc-id" from "prod2-generico"."store-dc-mapping" where "drug-type" = 'ethical') j on j."store-id" = a."store-id" left join "prod2-generico"."stores" ss on ss."id" = j."forward-dc-id" left join "prod2-generico"."store-delivered" msda on a."id" = msda."id" left join "prod2-generico"."stores-master" msm on a."store-id" = msm.id where a.id > {} and a."auto-short" = 1 and a."home-delivery" = 0 and a."status" not in ('deleted'); """ update_as_ms_query = """ update "prod2-generico"."{}" as s set "updated-at" = convert_timezone('Asia/Calcutta', GETDATE()), "created-to-invoice-days" = b."created-to-invoice-days", "created-to-invoice-hour" = b."created-to-invoice-hour", "created-to-dispatch-days" = b."created-to-dispatch-days", "created-to-dispatch-hour" = b."created-to-dispatch-hour", "created-to-delivery-days" = b."created-to-delivery-days", "created-to-delivery-hour" = b."created-to-delivery-hour", "created-to-re-order-days" = b."created-to-re-order-days", "created-to-re-order-hour" = b."created-to-re-order-hour", "created-to-order-days" = b."created-to-order-days", "created-to-order-hour" = b."created-to-order-hour", "status" = b."status", "quantity" = b."quantity", "required-quantity" = b."required-quantity", "inventory-at-ordering" = b."inventory-at-ordering", "ordered-time" = b."ordered-time", "invoiced-at" = b."invoiced-at", "dispatched-at" = b."dispatched-at", "delivered-at" = b."delivered-at", "completed-at" = b."completed-at", "re-ordered-at" = b."re-ordered-at", "store-delivered-at" = b."store-delivered-at", "decline-reason" = b."decline-reason", "type" = b."type", "category" = b."category", "schedule" = b."schedule", "sub-type" = b."sub-type", "preferred-distributor-id" = b."preferred-distributor-id", "preferred-distributor-name" = b."preferred-distributor-name", "drug-grade" = b."drug-grade", "distributor-type" = b."distributor-type", "recieved-distributor-id" = b."recieved-distributor-id", "received-distributor-name" = b."received-distributor-name", "abo" = b."abo", "line-manager" = b."line-manager", "store-manager" = b."store-manager", "franchisee-short-book" = b."franchisee-short-book" from ( select a.id, (case when (a."invoiced-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."invoiced-at") end) as "created-to-invoice-days", (case when (a."invoiced-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."invoiced-at") end) as "created-to-invoice-hour", --Fulfillment on dispatch (case when (a."dispatched-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."dispatched-at") end) as "created-to-dispatch-days", (case when (a."dispatched-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."dispatched-at") end) as "created-to-dispatch-hour", --Fulfillment on delivery (case when (msda."store-delivered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", msda."store-delivered-at") end) as "created-to-delivery-days", (case when (msda."store-delivered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", msda."store-delivered-at") end) as "created-to-delivery-hour", -- Re-order Timing -- (case when (a."re-ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."re-ordered-at") end) as "created-to-re-order-days", (case when (a."re-ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."re-ordered-at") end) as "created-to-re-order-hour", --order Timing-- (case when (a."ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(day, a."created-at", a."ordered-at") end) as "created-to-order-days", (case when (a."ordered-at" = '0101-01-01' or a."created-at" = '0101-01-01') then null else datediff(hours, a."created-at", a."ordered-at") end) as "created-to-order-hour", a."status" as "status", a."quantity" as "quantity", a."required-quantity" as "required-quantity", a."inventory-at-ordering" as "inventory-at-ordering", case when a."ordered-at" = '0101-01-01' then null else a."ordered-at" end as "ordered-time", case when a."invoiced-at" = '0101-01-01' then null else a."invoiced-at" end as "invoiced-at", case when a."dispatched-at" = '0101-01-01' then null else a."dispatched-at" end as "dispatched-at", case when a."delivered-at" = '0101-01-01' then null else a."delivered-at" end as "delivered-at", case when a."completed-at" = '0101-01-01' then null else a."completed-at" end as "completed-at", case when a."re-ordered-at" = '0101-01-01' then null else a."re-ordered-at" end as "re-ordered-at", case when msda."store-delivered-at" = '0101-01-01' then null else msda."store-delivered-at" end as "store-delivered-at", a."decline-reason" as "decline-reason", c."type", c."category" , c."schedule" , c."sub-type" as "sub-type" , f."id" as "preferred-distributor-id", f."name" as "preferred-distributor-name", e."drug-grade" as "drug-grade", d."type" as "distributor-type", d."id" as "recieved-distributor-id", d."name" as "received-distributor-name", msm."abo" , msm."line-manager" , msm."store-manager" , a."franchisee-short-book" as "franchisee-short-book" from "prod2-generico"."{}" s inner join "prod2-generico"."short-book-1" a on s.id = a.id left join "prod2-generico"."drugs" c on c."id" = a."drug-id" left join "prod2-generico"."distributors" d on d."id" = a."distributor-id" left join "prod2-generico"."drug-order-info" e on e."store-id" = a."store-id" and e."drug-id" = a."drug-id" left join "prod2-generico"."distributors" f on a."preferred-distributor" = f."id" left join "prod2-generico"."store-delivered" msda on a."id" = msda."id" left join "prod2-generico"."stores-master" msm on a."store-id" = msm.id where s."updated-at" < a."updated-at" or s."updated-at" < c."updated-at" or s."updated-at" < d."updated-at" or s."updated-at" < e."updated-at" or s."updated-at" < f."updated-at" or s."updated-at" < msda."updated-at" or s."updated-at" < msm."updated-at" ) as b where s.id = b.id; """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/as_ms/as_ms.py
as_ms.py
max_bill_id = """ select max("id") as "bill-id-max" from "prod2-generico"."{}" """ insert_bill_flags_query = """ insert into "prod2-generico"."{}" ( "id", "created-by", "created-at", "updated-by", "updated-at", "pr-flag", "hd-flag", "ecom-flag", "crm-flag" ) select pso."bill-id" as "id", 'etl-automation' as "created-by", convert_timezone('Asia/Calcutta',GETDATE()) as "created-at", 'etl-automation' as "updated-by", convert_timezone('Asia/Calcutta',GETDATE()) as "updated-at", bool_or(case when pso."patient-request-id" is null then false else true end) as "pr-flag", bool_or(case when pso."order-type" = 'delivery' then true else false end) as "hd-flag", bool_or(case when pso."order-source" = 'zeno' then true else false end) as "ecom-flag", bool_or(case when pso."order-source" = 'crm' then true else false end) as "crm-flag" from "prod2-generico"."patients-store-orders" pso left join "prod2-generico"."bill-flags" bf on NVL(pso."bill-id",0)= bf."id" where bf."id" is null group by pso."bill-id"; """ update_bill_flags_query = """ update "prod2-generico"."{}" as bf set "updated-at" = convert_timezone('Asia/Calcutta', GETDATE()), "pr-flag" = b."pr-flag", "hd-flag" = b."hd-flag", "ecom-flag" = b."ecom-flag", "crm-flag" = b."crm-flag" from ( select "bill-id" as "id", bool_or(case when pso."patient-request-id" is null then false else true end) as "pr-flag", bool_or(case when pso."order-type" = 'delivery' then true else false end) as "hd-flag", bool_or(case when pso."order-source" = 'zeno' then true else false end) as "ecom-flag", bool_or(case when pso."order-source" = 'crm' then true else false end) as "crm-flag" from "prod2-generico"."{}" bf inner join "prod2-generico"."patients-store-orders" pso on bf.id = pso."bill-id" where pso."updated-at" > bf."updated-at" group by pso."bill-id" ) as b where bf.id = b.id; """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/bill_flags/bill_flags_config.py
bill_flags_config.py
max_pso_id = """ select max("id") as "pso-id-max" from "prod2-generico"."{}" """ insert_query = """ insert into "prod2-generico"."{}" ( "id" , "created-at", "created-by", "updated-by", "updated-at", "year-created-at", "month-created-at", "patient-id", "doctor-id", "store-id", "bill-id", "drug-id", "zeno-order-id", "drug-name", "pso-requested-quantity", "pso-inventory-quantity", "order-number", "order-source", "order-type", "patient-request-id", "payment-type", "promo-id", "pso-status", "fulfilled-to-consumer", "type", "category", "company", "company-id", "composition", "composition-master-id", "lp-fulfilled-qty", "sb-id" , "ff-distributor", "ordered-distributor-id", "quantity", "required-quantity", "ordered-at", "completed-at", "invoiced-at", "dispatched-at", "received-at", "sb-status", "decline-reason", "inventory-at-ordering", "re-ordered-at", "dc-ff-time", "store-received-ff-time", "consumer-ff-time", "order-raised-at-dc", "order-raised-at-distributor", "billed-at", "store-name", "store-manager", "line-manager", "abo", "city", "store-b2b", "substituted", "gross-quantity", "gross-revenue-value", "net-quantity", "net-revenue-value", "selling-rate", "store-delivered-at", "franchisee-short-book" ) select pso."id" as "id", pso."created-at" as "created-at", pso."created-by" as "created-by", 'etl-automation' as "updated-by", convert_timezone('Asia/Calcutta',GETDATE()) as "updated-at", extract(year from pso."created-at") as "year-created-at", extract(month from pso."created-at") as "month-created-at", pso."patient-id" as "patient-id" , pso."doctor-id" as "doctor-id" , pso."store-id" as "store-id" , pso."bill-id" as "bill-id" , pso."drug-id" as "drug-id", pso."zeno-order-id" as "zeno-order-id", pso."drug-name" as "drug-name" , pso."requested-quantity" as "pso-requested-quantity", pso."inventory-quantity" as "pso-inventory-quantity", pso."order-number" as "order-number" , pso."order-source" as "order-source" , pso."order-type" as "order-type" , pso."patient-request-id" as "patient-request-id" , pso."payment-type" as "payment-type" , pso."promo-id" as "promo-id", pso.status as "pso-status", (case when ms."gross-quantity" > 0 then 1 else 0 end) as "fulfilled-to-consumer", d2."type" , d2."category" , d2."company" , d2."company-id" as "company-id" , d2."composition" , d2."composition-master-id" as "composition-master-id", NVL(prlp."lp-fulfilled-qty", 0) as "lp-fulfilled-qty", sb."id" as "sb-id", sb."distributor-id" as "ff-distributor", sb."ordered-distributor-id" as "ordered-distributor-id", sb."quantity" as "quantity" , sb."required-quantity" as "required-quantity" , case when sb."ordered-at" = '0101-01-01' then null else sb."ordered-at" end as "ordered-at", case when sb."completed-at" = '0101-01-01' then null else sb."completed-at" end as "completed-at", case when sb."invoiced-at" = '0101-01-01' then null else sb."invoiced-at" end as "invoiced-at", case when sb."dispatched-at" = '0101-01-01' then null else sb."dispatched-at" end as "dispatched-at", case when sb."received-at" = '0101-01-01' then null else sb."received-at" end as "received-at", sb."status" as "sb-status", sb."decline-reason" as "decline-reason", sb."inventory-at-ordering" as "inventory-at-ordering" , case when sb."re-ordered-at" = '0101-01-01' then null else sb."re-ordered-at" end as "re-ordered-at", (case when (pso."created-at" = '0101-01-01' or msda."store-delivered-at" = '0101-01-01') then null else datediff(hour, pso."created-at", msda."store-delivered-at") end) as "dc-ff-time", (case when (pso."created-at" = '0101-01-01' or sb."received-at" = '0101-01-01') then null else datediff(hour, pso."created-at", sb."received-at") end) as "store-received-ff-time", (case when (pso."created-at" = '0101-01-01' or b2."created-at" = '0101-01-01') then null else datediff(hour, pso."created-at", b2."created-at") end) as "consumer-ff-time", (case when sb."quantity">0 then 1 else 0 end) as "order-raised-at-dc", (case when ("ordered-at" = '0101-01-01' or "ordered-at" is null) then 0 else 1 end) as "order-raised-at-distributor", b2."created-at" as "billed-at", msm."store" as "store-name", msm."store-manager", msm."line-manager", msm."abo", msm."city", msm."store-b2b", case when "generic-flag" is null then 'not-available' when "generic-flag" is not null and d2."type" = 'generic' then 'substituted' when "generic-flag" is not null and d2."type" != 'generic' then 'not-substituted' else 'not-available' end as "substituted", ms."gross-quantity", ms."gross-revenue-value", ms."net-quantity", ms."net-revenue-value", case when sgdp."selling-rate" is null and d2."type" = 'generic' then 35 when sgdp."selling-rate" is null and d2."type" != 'generic' then 100 else sgdp."selling-rate" end as "selling-rate", msda."store-delivered-at", sb."franchisee-short-book" as "franchisee-short-book" from "prod2-generico"."patients-store-orders" pso left join ( select prlp."patient-request-id" , sum("fulfilled-quantity") as "lp-fulfilled-qty" from "prod2-generico"."patient-request-local-purchase" prlp inner join "prod2-generico"."patients-store-orders" pso on NVL(pso."patient-request-id", 0) = prlp."patient-request-id" group by prlp."patient-request-id" ) as prlp on prlp."patient-request-id" = NVL(pso."patient-request-id", 0) left join "prod2-generico"."patient-requests-short-books-map" mprsb on NVL(pso."patient-request-id", 0) = mprsb."patient-request-id" left join "prod2-generico"."short-book-1" sb on sb.id = mprsb."short-book-id" left join "prod2-generico"."store-delivered" msda on mprsb."short-book-id" = msda."id" left join "prod2-generico"."bills-1" b2 on b2.id = NVL(pso."bill-id", 0) left join "prod2-generico"."drugs" d2 on d2."id" = pso."drug-id" left join "prod2-generico"."substitutable-compositions" msc on msc."id" = d2."composition-master-id" left join "prod2-generico"."sales-agg" ms on ms."bill-id" = pso."bill-id" and ms."drug-id" = pso."drug-id" inner join "prod2-generico"."stores-master" msm on pso."store-id" = msm.id left join "prod2-generico"."store-group-drug-price" sgdp on msm."store-group-id" = sgdp."store-group-id" and pso."drug-id" = sgdp."drug-id" and sgdp."cluster-id" is null where pso."id" > {}; """ update_query = """ update "prod2-generico"."{}" as t set "updated-at" = convert_timezone('Asia/Calcutta', GETDATE()), "bill-id" = s."bill-id", "drug-id" = s."drug-id", "order-number" = s."order-number", "order-type" = s."order-type", "patient-request-id" = s."patient-request-id", "payment-type" = s."payment-type", "promo-id" = s."promo-id", "pso-status" = s."pso-status", "fulfilled-to-consumer" = s."fulfilled-to-consumer", "type" = s."type", "category" = s."category", "company" = s."company", "composition" = s."composition", "lp-fulfilled-qty" = s."lp-fulfilled-qty", "sb-id" = s."sb-id", "ff-distributor" = s."ff-distributor", "ordered-distributor-id" = s."ordered-distributor-id", "quantity" = s."quantity", "required-quantity" = s."required-quantity", "ordered-at" = s."ordered-at", "completed-at" = s."completed-at", "invoiced-at" = s."invoiced-at", "dispatched-at" = s."dispatched-at", "received-at" = s."received-at", "sb-status" = s."sb-status", "decline-reason" = s."decline-reason", "inventory-at-ordering" = s."inventory-at-ordering", "re-ordered-at" = s."re-ordered-at", "dc-ff-time" = s."dc-ff-time", "store-received-ff-time" = s."store-received-ff-time", "consumer-ff-time" = s."consumer-ff-time", "order-raised-at-dc" = s."order-raised-at-dc", "order-raised-at-distributor" = s."order-raised-at-distributor", "billed-at" = s."billed-at", "store-manager" = s."store-manager", "line-manager" = s."line-manager", "abo" = s."abo", "substituted" = s."substituted", "gross-quantity" = s."gross-quantity", "gross-revenue-value" = s."gross-revenue-value", "net-quantity" = s."net-quantity", "net-revenue-value" = s."net-revenue-value", "selling-rate" = s."selling-rate", "store-delivered-at" = s."store-delivered-at", "franchisee-short-book" = s."franchisee-short-book" from ( select pso."id" as "id", pso."bill-id" as "bill-id" , pso."drug-id" as "drug-id", pso."order-number" as "order-number" , pso."order-type" as "order-type" , pso."patient-request-id" as "patient-request-id" , pso."payment-type" as "payment-type" , pso."promo-id" as "promo-id", pso.status as "pso-status", (case when ms."gross-quantity" > 0 then 1 else 0 end) as "fulfilled-to-consumer", d2."type", d2."category" , d2."company" , d2."composition" , NVL(prlp."lp-fulfilled-qty", 0) as "lp-fulfilled-qty", sb."id" as "sb-id", sb."distributor-id" as "ff-distributor", sb."ordered-distributor-id" as "ordered-distributor-id", sb."quantity" as "quantity" , sb."required-quantity" as "required-quantity" , case when sb."ordered-at" = '0101-01-01' then null else sb."ordered-at" end as "ordered-at", case when sb."completed-at" = '0101-01-01' then null else sb."completed-at" end as "completed-at", case when sb."invoiced-at" = '0101-01-01' then null else sb."invoiced-at" end as "invoiced-at", case when sb."dispatched-at" = '0101-01-01' then null else sb."dispatched-at" end as "dispatched-at", case when sb."received-at" = '0101-01-01' then null else sb."received-at" end as "received-at", sb."status" as "sb-status", sb."decline-reason" as "decline-reason", sb."inventory-at-ordering" as "inventory-at-ordering" , case when sb."re-ordered-at" = '0101-01-01' then null else sb."re-ordered-at" end as "re-ordered-at", (case when (pso."created-at" = '0101-01-01' or msda."store-delivered-at" = '0101-01-01') then null else datediff(hour, pso."created-at", msda."store-delivered-at") end) as "dc-ff-time", (case when (pso."created-at" = '0101-01-01' or sb."received-at" = '0101-01-01') then null else datediff(hour, pso."created-at", sb."received-at") end) as "store-received-ff-time", (case when (pso."created-at" = '0101-01-01' or ms."created-at" = '0101-01-01') then null else datediff(hour, pso."created-at", ms."created-at") end) as "consumer-ff-time", (case when sb."quantity">0 then 1 else 0 end) as "order-raised-at-dc", (case when (sb."ordered-at" = '0101-01-01' or sb."ordered-at" is null) then 0 else 1 end) as "order-raised-at-distributor", ms."created-at" as "billed-at", msm."store-manager", msm."line-manager", msm."abo", case when msc."generic-flag" is null then 'not-available' when msc."generic-flag" is not null and d2."type" = 'generic' then 'substituted' when msc."generic-flag" is not null and d2."type" != 'generic' then 'not-substituted' else 'not-available' end as "substituted", ms."gross-quantity", ms."gross-revenue-value", ms."net-quantity", ms."net-revenue-value", case when sgdp."selling-rate" is null and d2."type" = 'generic' then 35 when sgdp."selling-rate" is null and d2."type" != 'generic' then 100 else sgdp."selling-rate" end as "selling-rate", msda."store-delivered-at", sb."franchisee-short-book" as "franchisee-short-book" from "prod2-generico"."{}" prm inner join "prod2-generico"."patients-store-orders" pso on prm.id = pso.id left join ( select prlp."patient-request-id" , sum("fulfilled-quantity") as "lp-fulfilled-qty" from "prod2-generico"."patient-request-local-purchase" prlp inner join "prod2-generico"."patients-store-orders" pso on NVL(pso."patient-request-id", 0) = prlp."patient-request-id" group by prlp."patient-request-id" ) as prlp on prlp."patient-request-id" = NVL(pso."patient-request-id", 0) left join "prod2-generico"."patient-requests-short-books-map" mprsb on NVL(pso."patient-request-id", 0) = mprsb."patient-request-id" left join "prod2-generico"."short-book-1" sb on sb.id = mprsb."short-book-id" left join "prod2-generico"."store-delivered" msda on mprsb."short-book-id" = msda."id" inner join "prod2-generico"."drugs" d2 on d2."id" = pso."drug-id" left join "prod2-generico"."substitutable-compositions" msc on msc."id" = d2."composition-master-id" left join "prod2-generico"."sales_agg" ms on ms."bill-id" = pso."bill-id" and ms."drug-id" = pso."drug-id" inner join "prod2-generico"."stores-master" msm on pso."store-id" = msm.id left join "prod2-generico"."store-group-drug-price" sgdp on msm."store-group-id" = sgdp."store-group-id" and pso."drug-id" = sgdp."drug-id" and sgdp."cluster-id" is null where prm."updated-at" < pso."updated-at" or prm."updated-at" < sb."updated-at" or prm."updated-at" < msc."updated-at" or prm."updated-at" < msda."updated-at" or prm."updated-at" < d2."updated-at" ) as s where t.id = s.id; """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/patient_request/__init__.py
__init__.py
sales_query = """ select a."bill-id", f."store-id", c."id" as "drug-id", c."drug-name", c."type", c."category", c."company", a."created-at", a."quantity", b."id" as "inventory-id", b."mrp", b."ptr" AS "zp-ptr", b."ptr" AS "final-ptr", b."purchase-rate" AS "wc-ptr", a."rate", a."cgst-rate", a."sgst-rate", a."igst-rate", f."payment-method", f."net-payable", f."patient-id", (a."rate" * a."quantity") as "value", d."franchisee-id" , ii."franchisee-invoice" from {schema}."bills-1{suffix_to_table}" f join {schema}."bill-items-1{suffix_to_table}" a on f."id" = a."bill-id" left join {schema}."inventory-1{suffix_to_table}" b on a."inventory-id" = b."id" left join {schema}."drugs{suffix_to_table}" c on c."id" = b."drug-id" left join {schema}."stores{suffix_to_table}" d on d."id"= b."store-id" left join {schema}."invoices-1{suffix_to_table}" ii on b."franchisee-invoice-id" = ii.id where a."created-at" >= '{analysis_start_time}' and a."created-at" <= '{analysis_end_time}' -- and f."store-id" = 2 """ customer_returns_query = """ SELECT b."return-id", b."bill-id", b."bill-id" as "returned-bill-id", a."store-id", a."patient-id", a."total-items", a."return-value", b."inventory-id", b."returned-quantity", b."cgst-rate", b."sgst-rate", b."igst-rate", b."rate", b."return-value", b."billed-at", b."returned-at", c."drug-id", c."drug-id" as "returned-drug-id", d."type", d."category", d."company", c."mrp", c."ptr" AS "zp-ptr", c."ptr" AS "final-ptr", c."purchase-rate" AS "wc-ptr", e."payment-method", s."franchisee-id" , ii."franchisee-invoice" FROM {schema}."customer-returns-1{suffix_to_table}" a JOIN {schema}."customer-return-items-1{suffix_to_table}" b ON a."id" = b."return-id" LEFT JOIN {schema}."inventory-1{suffix_to_table}" c ON c."id" = b."inventory-id" LEFT JOIN {schema}."drugs{suffix_to_table}" d ON d."id" = c."drug-id" LEFT JOIN {schema}."bills-1{suffix_to_table}" e ON e."id" = b."bill-id" left join {schema}."invoices-1{suffix_to_table}" ii on c."franchisee-invoice-id" = ii.id left join {schema}."stores{suffix_to_table}" s on a."store-id" = s.id WHERE b."returned-at" >='{analysis_start_time}' AND b."returned-at" <= '{analysis_end_time}' -- AND a."store-id" = 2 """ order_source_query = """ select pso."bill-id" as "zeno_bill_id", "order-source" from {schema}."patients-store-orders{suffix_to_table}" pso where "order-source" = 'zeno' and pso."bill-id" is not null group by pso."bill-id", "order-source" """ store_list_query = """ SELECT "id" AS "store-id", "name" AS "store-name", "franchisee-id" FROM {schema}."stores{suffix_to_table}" """ inventory_query = """ select a."store-id", a."drug-id", (a."quantity"+ a."locked-for-check" + a."locked-for-audit" + a."locked-for-return" + a."locked-for-transfer") as "quantity", a."ptr" AS "final-ptr", a."expiry", b."drug-name", b."type", b."category", b."company", a."created-at", c."vat", s."franchisee-id" , ii."franchisee-invoice" from {schema}."inventory-1{suffix_to_table}" a left join {schema}."drugs{suffix_to_table}" b on a."drug-id" = b."id" left join {schema}."invoice-items-1{suffix_to_table}" c on a."invoice-item-id" = c."id" left join {schema}."invoices-1{suffix_to_table}" ii on a."franchisee-invoice-id" = ii.id left join {schema}."stores{suffix_to_table}" s on a."store-id" = s.id where (a."quantity"> 0 OR a."locked-for-check" > 0 OR a."locked-for-audit" > 0 OR a."locked-for-return" > 0 OR a."locked-for-transfer" > 0 ) """ customers_initial_bill_date = """ select f."patient-id", f."store-id", min (f."created-at") as "created-at" from {schema}."bills-1{suffix_to_table}" f group by f."patient-id" , f."store-id" """ purchase_from_wc_query = """ select i."franchisee-invoice-item-id", b."franchisee-invoice-id", b."invoice-id", a."invoice-date", a."created-at", a."store-id", b."drug-id", c."type", c."category", c."company", b."actual-quantity", b."net-value" as "zp_received_net_value", b."vat" as "zp_vat", i."net-value" , i."actual-quantity" as "1_actual_quantity", b."actual-quantity" as "2_actual_quantity", i."vat" as "wc_vat", s."franchisee-id" , a."franchisee-invoice" , case when s."opened-at" is NULL then 'launch_stock' when date(s."opened-at") = '0101-01-01' then 'launch_stock' when (inv."invoice-date") < (s."opened-at") then 'launch_stock' else 'normal' end as "launch_flag" from {schema}."invoices-1{suffix_to_table}" a join {schema}."invoice-items-1{suffix_to_table}" b on a."id" = b."franchisee-invoice-id" left join {schema}."drugs{suffix_to_table}" c on c."id" = b."drug-id" join {schema}."invoice-items{suffix_to_table}" i on i."id" = b."invoice-item-reference" left join {schema}."invoices{suffix_to_table}" inv on inv."id" = a."invoice-reference" left join {schema}."stores{suffix_to_table}" s on a."store-id" = s.id where a."invoice-date" >= '{analysis_start_time}' and a."invoice-date"<= '{analysis_end_time}' and a.status not in ('live' , 'inbox') -- and a."franchisee-invoice" = 0 -- and a."store-id" = 2 """ zippin_return_data_query = """ select * from ( select row_number() over(partition by r.id order by r.id desc) as "row", date(d."dispatched-at") as "dispatch-date", date(r."settled-at") as "settled-date", e."name", d."serial" as "debit-note", d."credit-note-reference", d."status", d."store-id", s."name" as "cost-centre", e."gstn", r."id" as "return-item-id", r.taxable as "old-taxable-value", r.net /( (1 + r.gst / 100)) as "taxable-value", r."gst" as "tax-rate", r."gst-amount" as "tax-value", r."net" as "net-value", e."id" as "distributor-id", d.id as "debit-note-id", y."drug-id", p."type", p."category", p."company", y."purchase-rate" , y."purchase-rate" * r."returned-quantity" as "cogs", ii2."vat", s."franchisee-id" , ii."franchisee-invoice" from {schema}."return-items-1{suffix_to_table}" r left join {schema}."inventory-1{suffix_to_table}" y on y."id" = r."inventory-id" left join {schema}."drugs{suffix_to_table}" p on p."id" = y."drug-id" left join {schema}."debit-note-items-1{suffix_to_table}" dni on r.id = dni."item-id" left join {schema}."debit-notes-1{suffix_to_table}" d on dni."debit-note-id" = d."id" join {schema}."distributors{suffix_to_table}" e on d."dist-id" = e."id" join {schema}."stores{suffix_to_table}" s on d."store-id" = s."id" left join {schema}."invoices-1{suffix_to_table}" ii on y."franchisee-invoice-id" = ii.id left join {schema}."invoice-items-1{suffix_to_table}" ii2 on y."invoice-item-id" = ii2.id where r.status = 'settled' and d."is-internal-debit-note" = 0 and dni."is-active" = 1 and r."settled-at" >= '{analysis_start_time}' and r."settled-at" <= '{analysis_end_time}' and d."dist-id" != 64)a where a."row" = 1 """ zippin_return_data_query_revised_1 = """ select date(d."dispatched-at") as "dispatch-date", date(r."settled-at") as "settled-date", e."name", d."serial" as "debit-note", d."credit-note-reference", d."status", d."store-id", s."name" as "cost-centre", e."gstn", r."id" as "return-item-id", r.taxable as "old-taxable-value", r.net/( (1 + r.gst / 100)) as "taxable-value", r."gst" as "tax-rate", r."gst-amount" as "tax-value", r."net" as "net-value", e."id" as "distributor-id", d.id as "debit-note-id", y."drug-id", p."type", p."category", p."company", y."purchase-rate" , y."purchase-rate" * r."returned-quantity" as "cogs", ii2."vat", s."franchisee-id" , ii."franchisee-invoice" from {schema}."return-items-1{suffix_to_table}" r left join {schema}."inventory-1{suffix_to_table}" y on y."id" = r."inventory-id" left join {schema}."drugs{suffix_to_table}" p on p."id" = y."drug-id" left join {schema}."debit-notes-1{suffix_to_table}" d on r."debit-note-reference" = d."id" join {schema}."distributors{suffix_to_table}" e on d."dist-id" = e."id" join {schema}."stores{suffix_to_table}" s on d."store-id" = s."id" left join {schema}."invoices-1{suffix_to_table}" ii on y."franchisee-invoice-id" = ii.id left join {schema}."invoice-items-1{suffix_to_table}" ii2 on y."invoice-item-id" = ii2.id where r.status = 'settled' and r."settled-at" >='{analysis_start_time}' and r."settled-at" <= '{analysis_end_time}' and d."dist-id" != 64 """ mysql_old_db_zippin_return_data_query = """ select date(d.`dispatched-at`) as `dispatch-date`, date(r.`settled-at`) as `settled-date`, e.`name`, d.`serial` as `debit-note`, d.`credit-note-reference`, d.`status`, d.`store-id`, s.`name` as `cost-centre`, e.`gstn`, r.`id` as `return-item-id`, r.taxable as `old-taxable-value`, r.net/( (1 + r.gst / 100)) as `taxable-value`, r.`gst` as `tax-rate`, r.`gst-amount` as `tax-value`, r.`net` as `net-value`, e.`id` as `distributor-id`, d.id as `debit-note-id`, y.`drug-id`, p.`type`, p.`category`, p.`company`, y.`purchase-rate` , y.`purchase-rate` * r.`returned-quantity` as `cogs`, ii2.`vat`, s.`franchisee-id` , ii.`franchisee-invoice` from `return-items-1` r left join `inventory-1` y on y.`id` = r.`inventory-id` left join `drugs` p on p.`id` = y.`drug-id` left join `debit-notes-1` d on r.`debit-note-reference` = d.`id` join `distributors` e on d.`dist-id` = e.`id` join `stores` s on d.`store-id` = s.`id` left join `invoices-1` ii on y.`franchisee-invoice-id` = ii.id left join `invoice-items-1` ii2 on y.`invoice-item-id` = ii2.id where r.status = 'settled' and r.`settled-at` >='{analysis_start_time}' and r.`settled-at` <= '{analysis_end_time}' and d.`dist-id` != 64 """ old_donotuse_zippin_return_data_query = """ select date(d."dispatched-at") as "dispatch-date", e."name", d."serial" as "debit-note", d."credit-note-reference", d."status", d."store-id", s."name" as "cost-centre", e."gstn", r."id" as "return-item-id", r."taxable" as "taxable-value", r."gst" as "tax-rate", r."gst-amount" as "tax-value", r."net" as "net-value", e."id" as "distributor-id", d.id as "debit-note-id", y."drug-id", p."type", p."category", p."company", y."purchase-rate" , y."purchase-rate" * r."returned-quantity" as "cogs", ii2."vat", s."franchisee-id" , ii."franchisee-invoice" from {schema}."return-items-1{suffix_to_table}" r left join {schema}."inventory-1{suffix_to_table}" y on y."id" = r."inventory-id" left join {schema}."drugs{suffix_to_table}" p on p."id" = y."drug-id" join {schema}."debit-notes-1{suffix_to_table}" d on r."debit-note-reference" = d."id" join {schema}."distributors{suffix_to_table}" e on d."dist-id" = e."id" join {schema}."stores{suffix_to_table}" s on d."store-id" = s."id" left join {schema}."invoices-1{suffix_to_table}" ii on y."franchisee-invoice-id" = ii.id left join {schema}."invoice-items-1{suffix_to_table}" ii2 on y."invoice-item-id" = ii2.id where d."dispatched-at">='{analysis_start_time}' and d."dispatched-at"<= '{analysis_end_time}' and d."dist-id" != 64 """ workcell_return_data_query = """ select date(d."dispatched-at") as "dispatch-date", d."created-at" as "created-date", e."name", d."serial" as "debit-note", d."credit-note-reference", d."status", d."store-id", s."name" as "cost-centre", e."gstn", r."id" as "return-item-id", r."taxable" as "taxable-value", r."gst" as "tax-rate", r."gst-amount" as "tax-value", r."net" as "net-value", y."drug-id", o."type", o."category", o."company", y."purchase-rate" , y."purchase-rate" * r."returned-quantity" as "cogs", ii2."vat", s."franchisee-id" , ii1."franchisee-invoice" from {schema}."return-items{suffix_to_table}" as r left join {schema}."inventory{suffix_to_table}" y on r."inventory-id"= y."id" left join {schema}."drugs{suffix_to_table}" o on o."id" = y."drug-id" join {schema}."debit-notes{suffix_to_table}" as d on r."debit-note-reference" = d."id" join {schema}."distributors{suffix_to_table}" as e on d."dist-id" = e."id" join {schema}."stores{suffix_to_table}" as s on d."store-id" = s."id" left join {schema}."invoices{suffix_to_table}" ii on y."invoice-id" = ii.id left join {schema}."invoices-1{suffix_to_table}" ii1 on ii1."invoice-reference" = ii.id left join {schema}."invoice-items{suffix_to_table}" ii2 on y."invoice-item-id" = ii2.id where d."dispatched-at">='{analysis_start_time}' and d."dispatched-at"<='{analysis_end_time}' """ local_purchase_data_query = """ select a."id" as "inventory-id", "invoice-reference", b."franchisee-invoice-id", c."distributor-id", x."name", a."store-id", s."name" as "store-name", a."drug-id", d."drug-name", d."type", d."category", d."company", "vat", b."actual-quantity", b."net-value", a."ptr", a."purchase-rate", a."created-at", c."dispatch-status", s."franchisee-id" , c."franchisee-invoice" from {schema}."inventory-1{suffix_to_table}" a join {schema}."invoice-items-1{suffix_to_table}" b on a."invoice-item-id" = b."id" left join {schema}."invoices-1{suffix_to_table}" c on a."franchisee-invoice-id" = c."id" left join {schema}."drugs{suffix_to_table}" d on a."drug-id" = d."id" left join {schema}."stores{suffix_to_table}" s on s."id" = a."store-id" left join {schema}."distributors{suffix_to_table}" x on x."id" = c."distributor-id" where ((s."franchisee-id" = 1 and "invoice-reference" is null) or (s."franchisee-id" != 1 and c."distributor-id" = 76 )) and c."invoice-date" >= '{analysis_start_time}' and c."invoice-date" <= '{analysis_end_time}' """ # Note - Local Purchase data was based on inventory -> created-at, till Nov 2022, changed to invoice-date in dec 2022 generic_composition_count_query = """ select count(distinct t."composition") as "count" from ( select "id", "drug-name", "type", "composition" from {schema}."drugs{suffix_to_table}" where "type" = 'generic' and "composition" != '' ) t """ ethical_margin_query = """ select sum(a."actual-quantity" * a."mrp") as "value1", sum(a."net-value") as "net-value" from {schema}."invoice-items{suffix_to_table}" a join {schema}."invoices{suffix_to_table}" b on a."invoice-id" = b."id" join {schema}."distributors{suffix_to_table}" c on c."id" = b."distributor-id" join {schema}."drugs{suffix_to_table}" d on d."id" = a."drug-id" where c."credit-period">0 and d."type" = 'ethical' and a."created-at" >= '{analysis_start_time}' and a."created-at" <= '{analysis_end_time}' group by date_part(year, a."created-at"), date_part (month,a."created-at") """ ethical_margin_fofo_query = """ select sum(a."actual-quantity" * a."mrp") as "value1", sum(a."net-value") as "net-value" from {schema}."invoice-items{suffix_to_table}" a join {schema}."invoices{suffix_to_table}" b on a."invoice-id" = b."id" left join {schema}."invoices-1{suffix_to_table}" ii on ii."invoice-reference" = b."id" join {schema}."distributors{suffix_to_table}" c on c."id" = b."distributor-id" join {schema}."drugs{suffix_to_table}" d on d."id" = a."drug-id" left join {schema}."stores{suffix_to_table}" s on s."id" = b."store-id" where c."credit-period">0 and d."type" = 'ethical' and a."created-at" >= '{analysis_start_time}' and a."created-at" <= '{analysis_end_time}' and s."franchisee-id" != 1 and ii."franchisee-invoice" {equality_symbol} 0 group by date_part(year, a."created-at"), date_part (month,a."created-at") """ home_delivery_data_query = """ select pso."order-number", pso.id as "patient-store-order-id", pso."patient-request-id", pso."zeno-order-id" , pso."patient-id" , pso."order-source" as "order-source-pso" , pso."order-type" , pso."status" as "pso-status", pso."created-at" as "pso-created-at", pso."store-id" , s."name" as "store-name", s."franchisee-id", pso."drug-id" , pso."drug-name" , pso."requested-quantity", pso."inventory-quantity" as "inventory-at-creation", pr."required-quantity", pr."quantity-to-order", pso."bill-id", b."created-at" as "bill-date", dt."delivered-at", ss."type" as "slot-type" from {schema}."patients-store-orders{suffix_to_table}" pso left join {schema}."patient-requests{suffix_to_table}" pr on pso."patient-request-id" = pr.id join {schema}."stores{suffix_to_table}" s on s."id" = pso."store-id" left join {schema}."bills-1{suffix_to_table}" b on b."id" = pso."bill-id" left join {schema}."delivery-tracking{suffix_to_table}" dt on dt."patient-store-order-id" = pso."id" left join {schema}."store-slots{suffix_to_table}" ss on pso."slot-id" = ss.id where dt."delivered-at" >= '{analysis_start_time}' and dt."delivered-at"<= '{analysis_end_time}' and pso."order-type" = 'delivery' and pso."bill-id" is not null order by pso."created-at" desc """ delivery_bill_ids_query = """ select "bill-id" from {schema}."patients-store-orders{suffix_to_table}" pso where pso."order-type" = 'delivery' group by "bill-id" """ cumulative_consumers_data_query = """ select f."store-id" , count(distinct "patient-id") as "total-cons", count(distinct case when c.company != 'GOODAID' and c."type" in ('generic', 'high-value-generic') then "patient-id" end) as "generic-without-gaid-cons", count(distinct case when c."type" in ('generic', 'high-value-generic') then "patient-id" end) as "generic-cons", count(distinct case when c.company in ('GOODAID') then "patient-id" end) as "total-gaid-cons", count(distinct case when c.category in ('chronic') then "patient-id" end) as "total-chronic-cons" from {schema}."bills-1{suffix_to_table}" f join {schema}."bill-items-1{suffix_to_table}" a on f."id" = a."bill-id" left join {schema}."inventory-1{suffix_to_table}" b on a."inventory-id" = b."id" left join {schema}."drugs{suffix_to_table}" c on c."id" = b."drug-id" -- where -- f."store-id" = 2 group by f."store-id" """ cumulative_consumers_fofo_data_query = """ select f."store-id" , count(distinct "patient-id") as "total-cons", count(distinct case when c.company != 'GOODAID' and c."type" in ('generic', 'high-value-generic') then "patient-id" end) as "generic-without-gaid-cons", count(distinct case when c."type" in ('generic', 'high-value-generic') then "patient-id" end) as "generic-cons", count(distinct case when c.company in ('GOODAID') then "patient-id" end) as "total-gaid-cons", count(distinct case when c.category in ('chronic') then "patient-id" end) as "total-chronic-cons" from {schema}."bills-1{suffix_to_table}" f join {schema}."bill-items-1{suffix_to_table}" a on f."id" = a."bill-id" left join {schema}."inventory-1{suffix_to_table}" b on a."inventory-id" = b."id" left join {schema}."drugs{suffix_to_table}" c on c."id" = b."drug-id" left join {schema}."invoices-1{suffix_to_table}" ii on b."franchisee-invoice-id" = ii.id left join {schema}."stores{suffix_to_table}" s on s."id" = f."store-id" where s."franchisee-id" != 1 and ii."franchisee-invoice" {equality_symbol} 0 group by f."store-id" """ other_files_ethical_margin_query = """ select date_part (year, a."created-at") as "year", date_part(month, a."created-at") as "month", sum(a."actual-quantity" * a."mrp") as "actual-quantity * mrp", sum(a."net-value") as "net-value" from {schema}."invoice-items{suffix_to_table}" a join {schema}."invoices{suffix_to_table}" b on a."invoice-id" = b."id" join {schema}."distributors{suffix_to_table}" c on c."id" = b."distributor-id" join {schema}."drugs{suffix_to_table}" d on d."id" = a."drug-id" where c."credit-period">0 and d."type" = 'ethical' and date(a."created-at") >= '2021-08-01' group by date_part(year, a."created-at"), date_part(month, a."created-at") """ other_files_distributor_margin_query = """ select date_part(year,a."created-at") as "year", date_part(month,a."created-at") as "month", c."name", sum(a."actual-quantity"* a."mrp") as "actual-quantity * mrp", sum(a."net-value") as "net-value" from {schema}."invoice-items{suffix_to_table}" a join {schema}."invoices{suffix_to_table}" b on a."invoice-id"= b."id" join {schema}."distributors{suffix_to_table}" c on c."id"= b."distributor-id" join {schema}."drugs{suffix_to_table}" d on d."id"= a."drug-id" where c."credit-period">0 and date_part (year ,a."created-at") = {choose_year} and date_part (month ,a."created-at") = {choose_month} group by date_part (year,a."created-at"), date_part (month,a."created-at"), c."name" """ other_files_inventory_at_dc_near_expiry_data_query = """ select a."invoice-number", b."invoice-id", b."vat", a."invoice-date", a."store-id", b."drug-id", a."net-payable", b."net-value", case when b."actual-quantity" = 0 then 0 else (b."net-value"*1.0 / b."actual-quantity"*1.0) end as "final-ptr", b."actual-quantity", a."created-at", a."received-at", "status", "dispatch-status", c."type", c."category", b."expiry" from {schema}."invoices{suffix_to_table}" a join {schema}."invoice-items{suffix_to_table}" b on a."id" = b."invoice-id" join {schema}."drugs{suffix_to_table}" c on b."drug-id" = c."id" where "status" = 'approved' and "dispatch-status" in ('dispatch-status-na') """ goodaid_store_sales_query = """ select date_part (year, b."created-AT") as "YEAR", date_part (month, b."created-AT") as "MONTH", b."store-id", max(s."name") as "store-name", SUM(c."mrp" * a."quantity") as "gross_mrp", SUM((c."mrp" * a."quantity") / (1 + ((a."cgst-rate" + a."sgst-rate")/ 100))) as "gross_mrp_taxable", SUM(a."rate" * a."quantity") as "gross_revenue", SUM((a."rate" * a."quantity") / (1 + ((a."cgst-rate" + a."sgst-rate")/ 100))) as "gross_revenue_taxable", SUM(c."purchase-rate" * a."quantity") as "gross_cogs", SUM((c."purchase-rate" * a."quantity") / (1 + ((a."cgst-rate" + a."sgst-rate")/ 100))) as "gross_cogs_taxable", sum(a."quantity") as "gross_quantity" from {schema}."bill-items-1{suffix_to_table}" a left join {schema}."bills-1{suffix_to_table}" b on b."id" = a."bill-id" left join {schema}."inventory-1{suffix_to_table}" c on c."id" = a."inventory-id" left join {schema}."stores{suffix_to_table}" s on s."id" = b."store-id" left join {schema}."drugs{suffix_to_table}" d on d."id" = c."drug-id" where date_part (year, a."created-AT") = {choose_year} and date_part (month, a."created-AT") ={choose_month} and d."company" = 'GOODAID' group by date_part(year, b."created-AT"), date_part(month, b."created-AT"), b."store-id" """ goodaid_store_returns_query = """ select date_part (year,b."returned-at") as "year", date_part (month,b."returned-at") as "month", b."store-id", max(s."name") as "store-name", (SUM(c."mrp" * a."returned-quantity") * -1) as "returns_mrp", (SUM((c."mrp" * a."returned-quantity") / (1 + ((a."cgst-rate" + a."sgst-rate")/ 100))) * -1) as "returns_mrp_taxable", (SUM(a."rate" * a."returned-quantity") * - 1) as "returns", (SUM((a."rate" * a."returned-quantity") / (1 + ((a."cgst-rate" + a."sgst-rate")/ 100))) * -1) as "returns_taxable", (SUM(c."purchase-rate" * a."returned-quantity") * -1) as "returns_cogs", (SUM((c."purchase-rate" * a."returned-quantity") / (1 + ((a."cgst-rate" + a."sgst-rate")/ 100))) * -1) as "returns_cogs_taxable", (sum(a."returned-quantity") * -1) as "returned_quantity" from {schema}."customer-return-items-1{suffix_to_table}" a left join {schema}."customer-returns-1{suffix_to_table}" b on b."id" = a."return-id" left join {schema}."inventory-1{suffix_to_table}" c on c."id" = a."inventory-id" left join {schema}."stores{suffix_to_table}" s on s."id" = b."store-id" left join {schema}."drugs{suffix_to_table}" d on d."id" = c."drug-id" where date_part(year,a."returned-at") = {choose_year} and date_part(month,a."returned-at") = {choose_month} and d."company" = 'GOODAID' group by date_part(year,b."returned-at"), date_part(month,b."returned-at"), b."store-id" """ goodaid_zippin_inventory_query = """ select a."store-id", s."name" as "store-name", a."drug-id", b."drug-name", b."type", b."category", a."expiry", c."vat", ((a."quantity"+ a."locked-for-check" + a."locked-for-audit" + a."locked-for-return" + a."locked-for-transfer")) as "quantity", ((a."quantity"+ a."locked-for-check" + a."locked-for-audit" + a."locked-for-return" + a."locked-for-transfer") * a."ptr") as "value", a."ptr", a."created-at" from {schema}."inventory-1{suffix_to_table}" a join {schema}."invoice-items-1{suffix_to_table}" c on a."invoice-item-id" = c."id" left join {schema}."drugs{suffix_to_table}" b on a."drug-id" = b."id" left join {schema}."stores{suffix_to_table}" s on s."id" = a."store-id" where (a."quantity"> 0 or a."locked-for-check" > 0 or a."locked-for-audit" > 0 or a."locked-for-return" > 0 or a."locked-for-transfer" > 0) and b."company" = 'GOODAID' """ goodaid_dc_inventory_query = """ select m."dc-id" as store_id, ( select "name" from {schema}."stores{suffix_to_table}" where id = m."dc-id" limit 1) as store_name, vat, sum("post_tax") post_tax, sum(taxable_amount) as taxable_amount from ( select a."store-id", ( select "forward-dc-id" from {schema}."store-dc-mapping{suffix_to_table}" where max(dgs.type) = "drug-type" and "store-id" = a."store-id" limit 1) as "dc-id", round(sum( coalesce (a."locked-quantity" * "purchase-rate", 0) )) "post_tax", b.vat, round(sum(coalesce (a."locked-quantity" * a."purchase-rate", 0) / (1 + b.vat / 100) )) as "taxable_amount" from {schema}."inventory{suffix_to_table}" a join {schema}."invoice-items{suffix_to_table}" b on a."invoice-item-id" = b.id join {schema}."drugs{suffix_to_table}" dgs on dgs.id = a."drug-id" where "dgs"."company" = 'GOODAID' group by a."store-id", b.vat union select a."store-id", ( select "forward-dc-id" from {schema}."store-dc-mapping{suffix_to_table}" where max(dgs."type") = "drug-type" and "store-id" = a."store-id" limit 1) as "dc-id", round(sum( coalesce (a."locked-quantity" * "purchase-rate", 0) )) "post_tax", b.vat, round(sum( coalesce (a."locked-quantity" * a."purchase-rate", 0) / (1 + b.vat / 100) )) as "taxable_amount" from {schema}."inventory-1{suffix_to_table}" a join {schema}."invoice-items-1{suffix_to_table}" b on a."invoice-item-id" = b.id join {schema}."drugs{suffix_to_table}" dgs on dgs.id = a."drug-id" where "dgs"."company" = 'GOODAID' group by a."store-id", b.vat union select b."store-id", ( select "return-dc-id" from {schema}."store-dc-mapping{suffix_to_table}" where max(dgs.type) = "drug-type" and "store-id" = b."store-id" limit 1) as "dc-id", round(sum( coalesce (a."returned-quantity" * c."purchase-rate", 0))) "post_tax", d.vat, round(sum( coalesce (a."returned-quantity" * c."purchase-rate", 0) / (1 + d.vat / 100) )) as "taxable_amount" from {schema}."return-items{suffix_to_table}" a join {schema}."returns-to-dc{suffix_to_table}" b on a."return-id" = b.id join {schema}."inventory{suffix_to_table}" c on a."inventory-id" = c.id join {schema}."drugs{suffix_to_table}" dgs on dgs.id = c."drug-id" join {schema}."invoice-items{suffix_to_table}" d on c."invoice-item-id" = d.id where a."status" in ('saved', 'approved') and "dgs"."company" = 'GOODAID' group by b."store-id", d.vat ) m group by "dc-id", vat; """ goodaid_wh_inventory_query = """ select "drug-id", "drug-name", sum("balance-quantity") as wh_qty, sum("balance-value") as wh_value from "prod2-generico"."wh-inventory-ss" wis where date("created-at") = '{date}' group by "drug-id", "drug-name" """ goodaid_drugs_query = """ select "id" as "drug_id", "company" from {schema}."drugs{suffix_to_table}" where "company" = 'GOODAID' """ store_info_query = """ select sm.id as "store-id", sm."name" as "store-name", case when sm."franchisee-id" = 1 then 'COCO' else 'FOFO' end as "franchise-flag", zc."name" as "city-name", sg."name" as "store-group-name", zis."name" as "state-name" from {schema}."stores{suffix_to_table}" sm left join "prod2-generico"."zeno-city" zc on sm."city-id" = zc.id left join "prod2-generico"."store-groups" sg on sm."store-group-id" = sg.id left join "prod2-generico"."zeno-indian-states" zis on zc."indian-state-id" = zis.id """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/mis/mis_queries.py
mis_queries.py
import decimal import os # from zeno_etl_libs.queries.mis import mis_queries # # from zeno_etl_libs.helper.aws.s3 import S3 # from zeno_etl_libs.db.db import DB # from zeno_etl_libs.helper import helper import json import datetime from datetime import date, timedelta from dateutil.relativedelta import relativedelta import pandas as pd import numpy as np class Mis: def __init__(self,analysis_start_time,analysis_end_time,suffix_to_table,schema_to_select,choose_year,choose_month,rs_db=None,logger=None,mis_queries=None): self.analysis_start_time = analysis_start_time self.analysis_end_time = analysis_end_time self.suffix_to_table = suffix_to_table self.schema_to_select = schema_to_select self.choose_year = choose_year self.choose_month = choose_month self.rs_db = rs_db self.logger = logger self.logger.info('You have instantiated Mis class') self.mis_queries = mis_queries def sales(self): sales_query = self.mis_queries.sales_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) df = self.rs_db.get_df(sales_query) df.columns = [c.replace('-', '_') for c in df.columns] return df def customer_returns(self): customer_returns_query = self.mis_queries.customer_returns_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) df = self.rs_db.get_df(customer_returns_query) df.columns = [c.replace('-', '_') for c in df.columns] return df def order_source(self): order_source_query = self.mis_queries.order_source_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) order_source = self.rs_db.get_df(order_source_query) order_source.columns = [c.replace('-', '_') for c in order_source.columns] return order_source def store_list(self): store_list_query = self.mis_queries.store_list_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) store_list = self.rs_db.get_df(store_list_query) store_list.columns = [c.replace('-', '_') for c in store_list.columns] return store_list def inventory(self): inventory_query = self.mis_queries.inventory_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) inventory = self.rs_db.get_df(inventory_query) inventory.columns = [c.replace('-', '_') for c in inventory.columns] return inventory def cumulative_consumers_data(self): cumulative_consumers_data_query = self.mis_queries.cumulative_consumers_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) cumulative_consumers_data = self.rs_db.get_df(cumulative_consumers_data_query) cumulative_consumers_data.columns = [c.replace('-', '_') for c in cumulative_consumers_data.columns] return cumulative_consumers_data def cumulative_consumers_fofo_data(self): workcell_cumulative_consumers_fofo_data_query = self.mis_queries.cumulative_consumers_fofo_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table,equality_symbol='=') workcell_cumulative_consumers_fofo_data = self.rs_db.get_df(workcell_cumulative_consumers_fofo_data_query) workcell_cumulative_consumers_fofo_data.columns = [c.replace('-', '_') for c in workcell_cumulative_consumers_fofo_data.columns] others_cumulative_consumers_fofo_data_query = self.mis_queries.cumulative_consumers_fofo_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table,equality_symbol='!=') others_cumulative_consumers_fofo_data = self.rs_db.get_df(others_cumulative_consumers_fofo_data_query) others_cumulative_consumers_fofo_data.columns = [c.replace('-', '_') for c in others_cumulative_consumers_fofo_data.columns] return workcell_cumulative_consumers_fofo_data,others_cumulative_consumers_fofo_data def purchase_from_wc_data(self): purchase_from_wc_query = self.mis_queries.purchase_from_wc_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) df = self.rs_db.get_df(purchase_from_wc_query) df.columns = [c.replace('-', '_') for c in df.columns] df['wc_purchase_net_value'] = (df['net_value'] / df['1_actual_quantity']) * df['2_actual_quantity'] return df def zippin_return_data(self): zippin_return_data_query = self.mis_queries.zippin_return_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) df = self.rs_db.get_df(zippin_return_data_query) df.columns = [c.replace('-', '_') for c in df.columns] return df def workcell_return_data(self): workcell_return_data_query = self.mis_queries.workcell_return_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) df = self.rs_db.get_df(workcell_return_data_query) df.columns = [c.replace('-', '_') for c in df.columns] return df def cons_initial_bill_date(self,): customers_initial_bill_date_query = self.mis_queries.customers_initial_bill_date.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) customers_initial_bill_date = self.rs_db.get_df(customers_initial_bill_date_query) customers_initial_bill_date.columns = [c.replace('-', '_') for c in customers_initial_bill_date.columns] return customers_initial_bill_date def local_purchase_data(self): local_purchase_data_query = self.mis_queries.local_purchase_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table , analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) local_purchase_data = self.rs_db.get_df(local_purchase_data_query ) local_purchase_data.columns = [c.replace('-', '_') for c in local_purchase_data.columns] return local_purchase_data def home_delivery_data(self): home_delivery_data_query = self.mis_queries.home_delivery_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) df = self.rs_db.get_df(home_delivery_data_query) df.columns = [c.replace('-', '_') for c in df.columns] return df def delivery_bill_ids(self): delivery_bill_ids_query = self.mis_queries.delivery_bill_ids_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) delivery_bill_ids = self.rs_db.get_df(delivery_bill_ids_query) delivery_bill_ids.columns = [c.replace('-', '_') for c in delivery_bill_ids.columns] return delivery_bill_ids def order_type_tag(self,company, type_, tag): if tag == 'breakup': if company == 'GOODAID': return 'GOODAID' # elif type_ in ('ethical'): # return 'ethical' # elif type_ in ('generic'): # return 'generic' # else: # return 'others' elif type_ in ('ethical', 'high-value-ethical'): return 'ethical' elif type_ in ('generic', 'high-value-generic'): return 'generic' else: return 'others' elif tag == 'unified': # if type_ in ('ethical'): # return 'ethical' # elif type_ in ('generic'): # return 'generic' # else: # return 'others' if type_ in ('ethical', 'high-value-ethical'): return 'ethical' elif type_ in ('generic', 'high-value-generic'): return 'generic' else: return 'others' else: self.logger.info('please provide valid tag') def taxable_value(self,quantity,rate,cgst,sgst,igst): # igst = 0 return quantity*rate/(1 + ((cgst+sgst+igst)/100)) def taxable_value_vat_based(self,quantity,rate,vat): quantity = float(quantity) rate = float(rate) vat = float(vat) taxable = (quantity*rate)/(1 + ((vat)/100)) taxable = float(taxable) return taxable def taxable_value_vat_based_2(self,value,vat): value = float(value) vat = float(vat) taxable = (value)/(1 + ((vat)/100)) taxable = float(taxable) return taxable def fofo_final_distributor(self,franchisee_id,franchisee_invoice): if franchisee_id ==1: if franchisee_invoice == 0 or franchisee_invoice is None: return 'workcell' else: return 'other' if franchisee_id != 1: if franchisee_invoice == 0: return 'workcell' else: return 'other' def fofo_distributor_bifurcation(self,df): columns = [x for x in df.columns if x not in ['tag_flag', 'fofo_distributor', 'order_source','type1','category']] workcell = df.loc[df['fofo_distributor'] == 'workcell'][columns].reset_index() other = df.loc[df['fofo_distributor'] == 'other'][columns].reset_index() combined = df.loc[df['fofo_distributor'] == 'combined'][columns].reset_index() if other.empty: other.loc[0] = 0 if workcell.empty: workcell.loc[0] = 0 if combined.empty: combined.loc[0] = 0 both = workcell + other - combined both['fofo_distributor'] = 'both' both = both.loc[both['fofo_distributor'] == 'both'].reset_index() only_workcell = workcell - both only_workcell['fofo_distributor'] = 'only_workcell' only_other = other - both only_other['fofo_distributor'] = 'only_other' fofo_distributor_bifurcation_ = pd.concat([only_workcell, only_other, both], sort=True) fofo_distributor_bifurcation_.replace(0, np.nan, inplace=True) return fofo_distributor_bifurcation_ def fofo_distributor_bifurcation_next_calculation_steps(self,df, df_fofo, groupbylist): df['fofo_distributor'] = 'combined' df_1 = df[df_fofo.columns] df_fofo = pd.concat([df_fofo, df_1], sort=True) df_fofo.fillna(0, inplace=True) order = df_fofo['tag_flag'].drop_duplicates(keep='first').to_frame() df_fofo = df_fofo.reset_index(drop=True) df_fofo = df_fofo.groupby(groupbylist).apply(lambda x: self.fofo_distributor_bifurcation(x)).reset_index()[ [x for x in df_fofo.columns if x not in ['level_1', 'level_2', 'index']]] df_fofo = order.merge(df_fofo, on='tag_flag', how='left') return df_fofo def gmv_gross_payment(self, Gross, stores, fofo_tag = 'no'): gross = Gross.copy(deep = True) gross['GMV_sale'] = gross['quantity'] * gross['mrp'] gross['gross_sale'] = gross['quantity'] * gross['rate'] if fofo_tag=='no': gross_sale_summary = gross.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source'], as_index=False).agg({ 'quantity': ['sum'], 'GMV_sale': ['sum'], 'gross_sale': ['sum'] }).reset_index(drop=True) gross_sale_summary.columns = ["_".join(x) for x in gross_sale_summary.columns.ravel()] gross_sale_summary.fillna(0, inplace=True) gross_sale = pd.merge(left=gross_sale_summary, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) gross_sale.rename(columns={'type1_': 'type1', 'category_': 'category', 'payment_method_': 'payment_method', 'order_source_': 'order_source', 'quantity_sum': 'quantity', 'GMV_sale_sum': 'GMV_sales', 'gross_sale_sum': 'gross_sales'}, inplace=True) gross_sale[['GMV_sales', 'gross_sales']] = gross_sale[['GMV_sales', 'gross_sales']].astype(float) gross_sale.fillna(0, inplace=True) # #GMV df_gross_returns2a = gross_sale.groupby(['store_id', 'store_name', 'type1', 'order_source'])[['GMV_sales']].sum().reset_index() df_gross_returns2 = pd.pivot_table(df_gross_returns2a, values='GMV_sales', index=['type1', 'order_source'], columns=['store_name']).reset_index() df_gross_returns2['tag_flag'] = 'gmv' # GROSS df_gross_returns3a = gross_sale.groupby(['store_id', 'store_name', 'type1', 'order_source'])[['gross_sales']].sum().reset_index() df_gross_returns3 = pd.pivot_table(df_gross_returns3a, values='gross_sales', index=['type1', 'order_source'], columns=['store_name']).reset_index() df_gross_returns3['tag_flag'] = 'gross' # Payment gross_sale['payment'] = np.where(gross_sale['payment_method'].isin(['cash', 'card']), gross_sale['payment_method'], 'upi') df_gross_returns4a = gross_sale.groupby(['store_id', 'store_name', 'payment', 'order_source'])[['gross_sales']].sum().reset_index() df_gross_returns4 = pd.pivot_table(df_gross_returns4a, values='gross_sales', index=['payment', 'order_source'], columns=['store_name']).reset_index() df_gross_returns4['tag_flag'] = 'payment' gmv_gross_payment = pd.concat([df_gross_returns2, df_gross_returns3, df_gross_returns4], sort=True) cols_to_move = ['tag_flag', 'type1', 'payment', 'order_source'] gmv_gross_payment = gmv_gross_payment[cols_to_move + [col for col in gmv_gross_payment.columns if col not in cols_to_move]] return gmv_gross_payment elif fofo_tag == 'yes': gross = gross[gross['franchisee_id']!=1] gross_sale_summary = gross.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source','fofo_distributor'], as_index=False).agg({ 'quantity': ['sum'], 'GMV_sale': ['sum'], 'gross_sale': ['sum'] }).reset_index(drop=True) gross_sale_summary.columns = ["_".join(x) for x in gross_sale_summary.columns.ravel()] gross_sale_summary.fillna(0, inplace=True) gross_sale = pd.merge(left=gross_sale_summary, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) gross_sale.rename(columns={'type1_': 'type1', 'category_': 'category', 'payment_method_': 'payment_method', 'order_source_': 'order_source', 'fofo_distributor_':'fofo_distributor', 'quantity_sum': 'quantity', 'GMV_sale_sum': 'GMV_sales', 'gross_sale_sum': 'gross_sales'}, inplace=True) gross_sale[['GMV_sales', 'gross_sales']] = gross_sale[['GMV_sales', 'gross_sales']].astype(float) gross_sale.fillna(0, inplace=True) # #GMV df_gross_returns2a = gross_sale.groupby(['store_id', 'store_name', 'type1', 'order_source','fofo_distributor'])[['GMV_sales']].sum().reset_index() df_gross_returns2 = pd.pivot_table(df_gross_returns2a, values='GMV_sales', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() df_gross_returns2['tag_flag'] = 'gmv' # GROSS df_gross_returns3a = gross_sale.groupby(['store_id', 'store_name', 'type1', 'order_source','fofo_distributor'])[['gross_sales']].sum().reset_index() df_gross_returns3 = pd.pivot_table(df_gross_returns3a, values='gross_sales', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() df_gross_returns3['tag_flag'] = 'gross' # Payment gross_sale['payment'] = np.where(gross_sale['payment_method'].isin(['cash', 'card']), gross_sale['payment_method'], 'upi') df_gross_returns4a = gross_sale.groupby(['store_id', 'store_name', 'payment', 'order_source','fofo_distributor'])[['gross_sales']].sum().reset_index() df_gross_returns4 = pd.pivot_table(df_gross_returns4a, values='gross_sales', index=['payment', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() df_gross_returns4['tag_flag'] = 'payment' gmv_gross_payment = pd.concat([df_gross_returns2, df_gross_returns3, df_gross_returns4], sort=True) cols_to_move = ['tag_flag', 'type1', 'payment', 'order_source','fofo_distributor'] gmv_gross_payment = gmv_gross_payment[cols_to_move + [col for col in gmv_gross_payment.columns if col not in cols_to_move]] return gmv_gross_payment def netsale_tax_cogs(self,Gross,Returns,stores,fofo_tag = 'no'): gross = Gross.copy(deep = True) returns = Returns.copy(deep = True) if fofo_tag=='no': gross['gross_sale'] = gross['quantity'] * gross['rate'] gross['gross_COGS'] = gross['quantity'] * gross['wc_ptr'] gross['gross_sale_taxable'] = np.vectorize(self.taxable_value)(gross['quantity'], gross['rate'], gross['cgst_rate'], gross['sgst_rate'], gross['igst_rate']) gross['gross_COGS_taxable'] = np.vectorize(self.taxable_value)(gross['quantity'], gross['wc_ptr'], gross['cgst_rate'], gross['sgst_rate'], gross['igst_rate']) gross_sale_summary = gross.groupby(['store_id', 'type1', 'order_source'], as_index=False).agg({ 'quantity': ['sum'], 'gross_sale': ['sum'], 'gross_COGS': ['sum'], 'gross_sale_taxable':['sum'], 'gross_COGS_taxable':['sum'] }).reset_index(drop=True) gross_sale_summary.columns = ["_".join(x) for x in gross_sale_summary.columns.ravel()] returns['gross_returns'] = returns['rate'] * returns['returned_quantity'] returns['returns_COGS'] = returns['wc_ptr'] * returns['returned_quantity'] returns['gross_returns_taxable'] = np.vectorize(self.taxable_value)(returns['returned_quantity'], returns['rate'], returns['cgst_rate'], returns['sgst_rate'], returns['igst_rate']) returns['returns_COGS_taxable'] = np.vectorize(self.taxable_value)(returns['returned_quantity'], returns['wc_ptr'], returns['cgst_rate'], returns['sgst_rate'], returns['igst_rate']) returns_summary = returns.groupby(['store_id', 'type1', 'order_source'], as_index=False).agg({ 'returned_quantity':['sum'], 'gross_returns':['sum'], 'returns_COGS':['sum'], 'gross_returns_taxable': ['sum'], 'returns_COGS_taxable': ['sum']}).reset_index(drop=True) returns_summary.columns = ["_".join(x) for x in returns_summary.columns.ravel()] gross_returns = pd.merge(left=gross_sale_summary, right=returns_summary, how='outer', on=['store_id_', 'type1_', 'order_source_']) gross_returns.fillna(0, inplace=True) gross_returns['net_sale'] = gross_returns['gross_sale_sum'] - gross_returns['gross_returns_sum'] gross_returns['net_sale_taxable'] = gross_returns['gross_sale_taxable_sum'] - gross_returns['gross_returns_taxable_sum'] gross_returns['net_COGS'] = gross_returns['gross_COGS_sum'] - gross_returns['returns_COGS_sum'] gross_returns['net_COGS_taxable'] = gross_returns['gross_COGS_taxable_sum'] - gross_returns[ 'returns_COGS_taxable_sum'] gross_returns1 = pd.merge(left=gross_returns, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) gross_returns1.rename(columns={ 'type1_': 'type1', 'order_source_':'order_source', 'quantity_sum':'quantity', 'gross_sale_sum': 'gross_sales', 'gross_COGS_sum': 'gross_COGS', 'gross_sale_taxable_sum': 'gross_sale_taxable', 'gross_COGS_taxable_sum': 'gross_COGS_taxable', 'returned_quantity_sum': 'returned_quantity', 'gross_returns_sum': 'gross_returns', 'returns_COGS_sum': 'returns_COGS', 'gross_returns_taxable_sum': 'gross_returns_taxable', 'returns_COGS_taxable_sum': 'returns_COGS_taxable'}, inplace=True) gross_returns1[['net_sale','net_sale_taxable','net_COGS_taxable']] = gross_returns1[['net_sale','net_sale_taxable','net_COGS_taxable']].astype(float) gross_returns2 = pd.pivot_table(gross_returns1, values='net_sale', index=['type1', 'order_source'], columns=['store_name']).reset_index() gross_returns2['tag_flag'] = 'net_sale' gross_returns3 = pd.pivot_table(gross_returns1, values='net_sale_taxable', index=['type1', 'order_source'], columns=['store_name']).reset_index() gross_returns3['tag_flag'] = 'net_sale_taxable' gross_returns4 = pd.pivot_table(gross_returns1, values='net_COGS_taxable', index=['type1', 'order_source'], columns=['store_name']).reset_index() gross_returns4['tag_flag'] = 'net_COGS_taxable' net_sale_taxes_cogs = pd.concat([gross_returns2, gross_returns3, gross_returns4]) cols_to_move = ['tag_flag', 'type1', 'order_source'] net_sale_taxes_cogs = net_sale_taxes_cogs[cols_to_move + [col for col in net_sale_taxes_cogs.columns if col not in cols_to_move]] return net_sale_taxes_cogs if fofo_tag == 'yes': gross = gross[gross['franchisee_id'] != 1] returns = returns[returns['franchisee_id'] != 1] gross['gross_sale'] = gross['quantity'] * gross['rate'] gross['gross_COGS'] = gross['quantity'] * gross['wc_ptr'] gross['gross_sale_taxable'] = np.vectorize(self.taxable_value)(gross['quantity'], gross['rate'], gross['cgst_rate'], gross['sgst_rate'], gross['igst_rate']) gross['gross_COGS_taxable'] = np.vectorize(self.taxable_value)(gross['quantity'], gross['wc_ptr'], gross['cgst_rate'], gross['sgst_rate'], gross['igst_rate']) gross_sale_summary = gross.groupby(['store_id', 'type1', 'order_source','fofo_distributor'], as_index=False).agg({ 'quantity': ['sum'], 'gross_sale': ['sum'], 'gross_COGS': ['sum'], 'gross_sale_taxable': ['sum'], 'gross_COGS_taxable': ['sum'] }).reset_index(drop=True) gross_sale_summary.columns = ["_".join(x) for x in gross_sale_summary.columns.ravel()] returns['gross_returns'] = returns['rate'] * returns['returned_quantity'] returns['returns_COGS'] = returns['wc_ptr'] * returns['returned_quantity'] returns['gross_returns_taxable'] = np.vectorize(self.taxable_value)(returns['returned_quantity'], returns['rate'], returns['cgst_rate'], returns['sgst_rate'], returns['igst_rate']) returns['returns_COGS_taxable'] = np.vectorize(self.taxable_value)(returns['returned_quantity'], returns['wc_ptr'], returns['cgst_rate'], returns['sgst_rate'], returns['igst_rate']) returns_summary = returns.groupby(['store_id', 'type1', 'order_source','fofo_distributor'], as_index=False).agg({ 'returned_quantity': ['sum'], 'gross_returns': ['sum'], 'returns_COGS': ['sum'], 'gross_returns_taxable': ['sum'], 'returns_COGS_taxable': ['sum']}).reset_index(drop=True) returns_summary.columns = ["_".join(x) for x in returns_summary.columns.ravel()] gross_returns = pd.merge(left=gross_sale_summary, right=returns_summary, how='outer', on=['store_id_', 'type1_', 'order_source_','fofo_distributor_']) gross_returns.fillna(0, inplace=True) gross_returns['net_sale'] = gross_returns['gross_sale_sum'] - gross_returns['gross_returns_sum'] gross_returns['net_sale_taxable'] = gross_returns['gross_sale_taxable_sum'] - gross_returns[ 'gross_returns_taxable_sum'] gross_returns['net_COGS'] = gross_returns['gross_COGS_sum'] - gross_returns['returns_COGS_sum'] gross_returns['net_COGS_taxable'] = gross_returns['gross_COGS_taxable_sum'] - gross_returns[ 'returns_COGS_taxable_sum'] gross_returns1 = pd.merge(left=gross_returns, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) gross_returns1.rename(columns={'type1_': 'type1', 'order_source_': 'order_source', 'fofo_distributor_':'fofo_distributor', 'quantity_sum': 'quantity', 'gross_sale_sum': 'gross_sales', 'gross_COGS_sum': 'gross_COGS', 'gross_sale_taxable_sum': 'gross_sale_taxable', 'gross_COGS_taxable_sum': 'gross_COGS_taxable', 'returned_quantity_sum': 'returned_quantity', 'gross_returns_sum': 'gross_returns', 'returns_COGS_sum': 'returns_COGS', 'gross_returns_taxable_sum': 'gross_returns_taxable', 'returns_COGS_taxable_sum': 'returns_COGS_taxable'}, inplace=True) gross_returns1[['net_sale', 'net_sale_taxable', 'net_COGS_taxable']] = gross_returns1[ ['net_sale', 'net_sale_taxable', 'net_COGS_taxable']].astype(float) gross_returns2 = pd.pivot_table(gross_returns1, values='net_sale', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() gross_returns2['tag_flag'] = 'net_sale' gross_returns3 = pd.pivot_table(gross_returns1, values='net_sale_taxable', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() gross_returns3['tag_flag'] = 'net_sale_taxable' gross_returns4 = pd.pivot_table(gross_returns1, values='net_COGS_taxable', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() gross_returns4['tag_flag'] = 'net_COGS_taxable' net_sale_taxes_cogs = pd.concat([gross_returns2, gross_returns3, gross_returns4]) cols_to_move = ['tag_flag', 'type1', 'order_source','fofo_distributor'] net_sale_taxes_cogs = net_sale_taxes_cogs[cols_to_move + [col for col in net_sale_taxes_cogs.columns if col not in cols_to_move]] return net_sale_taxes_cogs def inventory_ageing(self, Inventory, stores, mis_tag = 'breakup',fofo_tag = 'no'): inventory_data = Inventory.copy(deep = True) inventory_data['value'] = inventory_data['quantity'] * inventory_data['final_ptr'] inventory_data['days'] = (pd.to_datetime(self.analysis_end_time) - inventory_data['created_at']).dt.days conditions = [ (inventory_data['days'] >= 0) & (inventory_data['days'] <= 30), (inventory_data['days'] >= 31) & (inventory_data['days'] <= 60), (inventory_data['days'] >= 61) & (inventory_data['days'] <= 90), (inventory_data['days'] >= 91)] choices = ['0_30', '31_60', '61_90', '90+'] inventory_data['age_bracket'] = np.select(conditions, choices) inventory_data['vat'] = inventory_data['vat'].fillna(0) inventory_data['vat'] = inventory_data['vat'].astype(float) inventory_data['taxable'] = np.vectorize(self.taxable_value_vat_based)(inventory_data['quantity'], inventory_data['final_ptr'], inventory_data['vat']) if fofo_tag == 'no': df_ageing = inventory_data.groupby(['store_id', 'type1', 'category', 'age_bracket'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'value': ['sum'], 'taxable': ['sum']}).reset_index(drop=True) df_ageing.columns = ["_".join(x) for x in df_ageing.columns.ravel()] df_ageing = pd.merge(left=df_ageing, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) df_ageing_grp = df_ageing.groupby(['store_id_', 'store_name', 'type1_', 'age_bracket_'])[['taxable_sum']].sum().reset_index() # generic df_ageing_generic = df_ageing_grp[df_ageing_grp['type1_'] == 'generic'] df_ageing_generic1 = pd.pivot_table(df_ageing_generic, values='taxable_sum', index=['type1_', 'age_bracket_'], columns=['store_name']).reset_index() # ethical df_ageing_ethical = df_ageing_grp[df_ageing_grp['type1_'] == 'ethical'] df_ageing_ethical1 = pd.pivot_table(df_ageing_ethical, values='taxable_sum', index=['type1_', 'age_bracket_'], columns=['store_name']).reset_index() # others df_ageing_grp_others = df_ageing.groupby(['store_id_', 'store_name', 'type1_'])[['taxable_sum']].sum().reset_index() df_ageing_others = df_ageing_grp_others[df_ageing_grp_others['type1_'] == 'others'] df_ageing_others1 = pd.pivot_table(df_ageing_others, values='taxable_sum', index=['type1_'], columns=['store_name']).reset_index() if mis_tag == 'breakup': # GOODAID df_ageing_goodaid = df_ageing_grp[df_ageing_grp['type1_'] == 'GOODAID'] df_ageing_goodaid1 = pd.pivot_table(df_ageing_goodaid, values='taxable_sum', index=['type1_', 'age_bracket_'], columns=['store_name']).reset_index() inventory_ageing = pd.concat([df_ageing_generic1, df_ageing_ethical1, df_ageing_others1, df_ageing_goodaid1],sort=True) elif mis_tag=='unified': inventory_ageing = pd.concat([df_ageing_generic1, df_ageing_ethical1, df_ageing_others1],sort=True) else: self.logger.info('please pass correct mis_tag') return None inventory_ageing['tag_flag'] = 'inventory_ageing' cols_to_move = ['tag_flag', 'type1_', 'age_bracket_'] inventory_ageing = inventory_ageing[cols_to_move + [col for col in inventory_ageing.columns if col not in cols_to_move]] inventory_ageing.rename(columns={'type1_': 'type1'}, inplace=True) return inventory_ageing elif fofo_tag == 'yes': inventory_data = inventory_data[inventory_data['franchisee_id'] != 1] df_ageing = inventory_data.groupby(['store_id', 'type1', 'category','fofo_distributor', 'age_bracket'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'value': ['sum'], 'taxable': ['sum']}).reset_index(drop=True) df_ageing.columns = ["_".join(x) for x in df_ageing.columns.ravel()] df_ageing = pd.merge(left=df_ageing, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) df_ageing_grp = df_ageing.groupby(['store_id_', 'store_name', 'type1_','fofo_distributor_' ,'age_bracket_'])[['taxable_sum']].sum().reset_index() # generic df_ageing_generic = df_ageing_grp[df_ageing_grp['type1_'] == 'generic'] df_ageing_generic1 = pd.pivot_table(df_ageing_generic, values='taxable_sum', index=['type1_', 'age_bracket_','fofo_distributor_'], columns=['store_name']).reset_index() # ethical df_ageing_ethical = df_ageing_grp[df_ageing_grp['type1_'] == 'ethical'] df_ageing_ethical1 = pd.pivot_table(df_ageing_ethical, values='taxable_sum', index=['type1_', 'age_bracket_','fofo_distributor_'], columns=['store_name']).reset_index() # others df_ageing_grp_others = df_ageing.groupby(['store_id_', 'store_name', 'type1_','fofo_distributor_'])[['taxable_sum']].sum().reset_index() df_ageing_others = df_ageing_grp_others[df_ageing_grp_others['type1_'] == 'others'] df_ageing_others1 = pd.pivot_table(df_ageing_others, values='taxable_sum', index=['type1_','fofo_distributor_'], columns=['store_name']).reset_index() if mis_tag == 'breakup': # GOODAID df_ageing_goodaid = df_ageing_grp[df_ageing_grp['type1_'] == 'GOODAID'] df_ageing_goodaid1 = pd.pivot_table(df_ageing_goodaid, values='taxable_sum', index=['type1_','fofo_distributor_' ,'age_bracket_'], columns=['store_name']).reset_index() inventory_ageing = pd.concat([df_ageing_generic1, df_ageing_ethical1, df_ageing_others1, df_ageing_goodaid1], sort=True) elif mis_tag == 'unified': inventory_ageing = pd.concat([df_ageing_generic1, df_ageing_ethical1, df_ageing_others1], sort=True) else: self.logger.info('please pass correct mis_tag') return None inventory_ageing['tag_flag'] = 'inventory_ageing' cols_to_move = ['tag_flag', 'type1_', 'fofo_distributor_','age_bracket_'] inventory_ageing = inventory_ageing[cols_to_move + [col for col in inventory_ageing.columns if col not in cols_to_move]] inventory_ageing.rename(columns={'type1_': 'type1', 'fofo_distributor_':'fofo_distributor'}, inplace=True) return inventory_ageing def near_expiry(self,Inventory,stores,mis_tag = 'breakup',fofo_tag = 'no'): inventory_data = Inventory.copy(deep=True) inventory_data['value'] = inventory_data['quantity'] * inventory_data['final_ptr'] inventory_data['expiry_date'] = pd.to_datetime(inventory_data['expiry'], format='%Y-%m-%d %H:%M:%S', errors='coerce') inventory_data['days_to_expiry'] = (pd.to_datetime(self.analysis_end_time) - inventory_data['expiry_date']).dt.days inventory_data1 = inventory_data[ (inventory_data['days_to_expiry'] < 0) & (inventory_data['days_to_expiry'] > -90)] inventory_data1['taxable'] = np.vectorize(self.taxable_value_vat_based)(inventory_data1['quantity'], inventory_data1['final_ptr'],inventory_data1['vat']) if fofo_tag == 'no': near_expiry = inventory_data1.groupby(['store_id', 'type1'], as_index=False).agg({ 'value': ['sum'], 'taxable': ['sum']}).reset_index(drop=True) near_expiry.columns = ["_".join(x) for x in near_expiry.columns.ravel()] near_expiry = pd.merge(left=near_expiry, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) # generic near_expiry_generic = near_expiry[near_expiry['type1_'] == 'generic'] near_expiry_generic1 = pd.pivot_table(near_expiry_generic, values='taxable_sum', index=['type1_'], columns=['store_name']).reset_index() # ethical near_expiry_ethical = near_expiry[near_expiry['type1_'] == 'ethical'] near_expiry_ethical1 = pd.pivot_table(near_expiry_ethical, values='taxable_sum', index=['type1_'], columns=['store_name']).reset_index() # others near_expiry_others = near_expiry[near_expiry['type1_'] == 'others'] near_expiry_others1 = pd.pivot_table(near_expiry_others, values='taxable_sum', index=['type1_'], columns=['store_name']).reset_index() if mis_tag == 'breakup': near_expiry_goodaid = near_expiry[near_expiry['type1_'] == 'GOODAID'] # If there are no items in near expiry for goodaid if len(near_expiry_goodaid)!= 0: near_expiry_goodaid1 = pd.pivot_table(near_expiry_goodaid, values='taxable_sum', index=['type1_'], columns=['store_name']).reset_index() else: near_expiry_goodaid1 = near_expiry_ethical1.copy(deep=True) near_expiry_goodaid1.loc[:] = np.nan near_expiry_goodaid1['type1_'][0] = 'GOODAID' near_expiry = pd.concat([near_expiry_generic1, near_expiry_ethical1, near_expiry_others1, near_expiry_goodaid1],sort=True) elif mis_tag=='unified': near_expiry = pd.concat([near_expiry_generic1, near_expiry_ethical1, near_expiry_others1],sort=True) else: self.logger.info('please pass correct mis_tag') return None near_expiry['tag_flag'] = 'near_expiry' cols_to_move = ['tag_flag', 'type1_'] near_expiry = near_expiry[cols_to_move + [col for col in near_expiry.columns if col not in cols_to_move]] near_expiry.rename(columns={'type1_': 'type1'}, inplace=True) return near_expiry elif fofo_tag == 'yes': inventory_data1 = inventory_data1[inventory_data1['franchisee_id']!=1] near_expiry = inventory_data1.groupby(['store_id', 'type1','fofo_distributor'], as_index=False).agg({ 'value': ['sum'], 'taxable': ['sum']}).reset_index(drop=True) near_expiry.columns = ["_".join(x) for x in near_expiry.columns.ravel()] near_expiry = pd.merge(left=near_expiry, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) # generic near_expiry_generic = near_expiry[near_expiry['type1_'] == 'generic'] near_expiry_generic1 = pd.pivot_table(near_expiry_generic, values='taxable_sum', index=['type1_','fofo_distributor_'], columns=['store_name']).reset_index() # ethical near_expiry_ethical = near_expiry[near_expiry['type1_'] == 'ethical'] near_expiry_ethical1 = pd.pivot_table(near_expiry_ethical, values='taxable_sum', index=['type1_','fofo_distributor_'], columns=['store_name']).reset_index() # others near_expiry_others = near_expiry[near_expiry['type1_'] == 'others'] near_expiry_others1 = pd.pivot_table(near_expiry_others, values='taxable_sum', index=['type1_','fofo_distributor_'], columns=['store_name']).reset_index() if mis_tag == 'breakup': near_expiry_goodaid = near_expiry[near_expiry['type1_'] == 'GOODAID'] # If there are no items in near expiry for goodaid if len(near_expiry_goodaid) != 0: near_expiry_goodaid1 = pd.pivot_table(near_expiry_goodaid, values='taxable_sum', index=['type1_','fofo_distributor_'], columns=['store_name']).reset_index() else: near_expiry_goodaid1 = near_expiry_ethical1.copy(deep=True) near_expiry_goodaid1.loc[:] = np.nan near_expiry_goodaid1['type1_'][0] = 'GOODAID' near_expiry = pd.concat([near_expiry_generic1, near_expiry_ethical1, near_expiry_others1, near_expiry_goodaid1], sort=True) elif mis_tag == 'unified': near_expiry = pd.concat([near_expiry_generic1, near_expiry_ethical1, near_expiry_others1], sort=True) else: self.logger.info('please pass correct mis_tag') return None near_expiry['tag_flag'] = 'near_expiry' cols_to_move = ['tag_flag', 'type1_','fofo_distributor_'] near_expiry = near_expiry[cols_to_move + [col for col in near_expiry.columns if col not in cols_to_move]] near_expiry.rename(columns={'type1_': 'type1', 'fofo_distributor_':'fofo_distributor'}, inplace=True) return near_expiry def sales_by_volume(self,Sales,stores): sales = Sales.copy(deep = True) generic_volume = sales.groupby(['store_id', 'type1', 'order_source'])[['quantity']].sum().reset_index().rename( columns={'quantity': "generic_volume"}) generic_volume = pd.merge(left=generic_volume, right=stores, how='left', left_on=['store_id'], right_on=['store_id']) generic_volume = pd.pivot_table(generic_volume, values='generic_volume', index=['type1', 'order_source'], columns=['store_name']).reset_index() generic_volume['tag_flag'] = 'sales_by_volume' return generic_volume def gross_rev_chronic_acute(self,Sales,Returns,stores): sales = Sales.copy(deep = True) returns = Returns.copy(deep = True) sales['COGS'] = sales['quantity'] * sales['final_ptr'] df_a1a = sales.groupby(['store_id', 'type1', 'category', 'order_source'], as_index=False).agg({ 'value': ['sum'], 'quantity': ['sum'], 'COGS': ['sum'], 'bill_id': pd.Series.nunique, 'drug_id': pd.Series.nunique}).reset_index(drop=True) df_a1a.columns = ["_".join(x) for x in df_a1a.columns.ravel()] returns['returned_value'] = returns['returned_quantity'] * returns['rate'] returns['returned_COGS'] = returns['returned_quantity'] * returns['final_ptr'] df_b2 = returns.groupby(['store_id', 'type1', 'category', 'order_source'], as_index=False).agg({ 'returned_value': ['sum'], 'returned_quantity': ['sum'], 'returned_COGS': ['sum'], 'returned_bill_id': pd.Series.nunique, 'returned_drug_id': pd.Series.nunique}).reset_index(drop=True) df_b2.columns = ["_".join(x) for x in df_b2.columns.ravel()] df_a_b = pd.merge(left=df_a1a, right=df_b2, how='outer', on=['store_id_', 'type1_', 'category_', 'order_source_']) df_a_b.fillna(0, inplace=True) df_a_b['net_sale'] = df_a_b['value_sum'] - df_a_b['returned_value_sum'] df_a_b['net_COGS'] = df_a_b['COGS_sum'] - df_a_b['returned_COGS_sum'] df_a_b['net_quantity'] = df_a_b['quantity_sum'] - df_a_b['returned_quantity_sum'] df_a4 = df_a_b.groupby(['store_id_', 'category_', 'type1_', 'order_source_'], as_index=False).agg({ 'net_sale': ['sum'], 'net_quantity': ['sum'], 'net_COGS': ['sum'], 'bill_id_nunique': ['sum'], 'drug_id_nunique': ['sum']}).reset_index(drop=True) df_a4.columns = ["_".join(x) for x in df_a4.columns.ravel()] df_a4 = pd.merge(left=df_a4, right=stores, how='left', left_on=['store_id__'], right_on=['store_id']) df_a5 = df_a4[df_a4['category__'] == 'chronic'] df_a5_sale = df_a5.groupby(['store_id__', 'store_name', 'category__', 'type1__', 'order_source__'])[['net_sale_sum']].sum().reset_index() df_a5_qty = df_a5.groupby(['store_id__', 'store_name', 'category__', 'type1__', 'order_source__'])[['net_quantity_sum']].sum().reset_index() df_a5_sale['net_sale_sum'] = df_a5_sale['net_sale_sum'].astype(float) gross_rev_chronic_sale = pd.pivot_table(df_a5_sale, values='net_sale_sum', index=['category__', 'type1__', 'order_source__'], columns=['store_name']).reset_index() df_a5_qty['net_quantity_sum'] = df_a5_qty['net_quantity_sum'].astype(float) gross_rev_chronic_vol = pd.pivot_table(df_a5_qty, values='net_quantity_sum', index=['category__', 'type1__', 'order_source__'], columns=['store_name']).reset_index() df_a6 = df_a4[df_a4['category__'] == 'acute'] df_a6_sale = df_a6.groupby(['store_id__', 'store_name', 'category__', 'type1__', 'order_source__'])[['net_sale_sum']].sum().reset_index() df_a6_qty = df_a6.groupby(['store_id__', 'store_name', 'category__', 'type1__', 'order_source__'])[['net_quantity_sum']].sum().reset_index() df_a6_sale['net_sale_sum'] = df_a6_sale['net_sale_sum'].astype(float) gross_rev_acute_sale = pd.pivot_table(df_a6_sale, values='net_sale_sum', index=['category__', 'type1__', 'order_source__'], columns=['store_name']).reset_index() df_a6_qty['net_quantity_sum'] = df_a6_qty['net_quantity_sum'].astype(float) gross_rev_acute_vol = pd.pivot_table(df_a6_qty, values='net_quantity_sum', index=['category__', 'type1__', 'order_source__'], columns=['store_name']).reset_index() gross_rev_chronic_sale_vol = pd.concat([gross_rev_chronic_sale, gross_rev_chronic_vol]) gross_rev_chronic_sale_vol['tag_flag'] = 'gross_rev_chronic_sale_vol' gross_rev_chronic_sale_vol.rename(columns={'type1__': 'type1'}, inplace=True) gross_rev_chronic_sale_vol.rename(columns={'category__': 'category'}, inplace=True) gross_rev_chronic_sale_vol.rename(columns={'order_source__': 'order_source'}, inplace=True) gross_rev_acute_sale_vol = pd.concat([gross_rev_acute_sale, gross_rev_acute_vol]) gross_rev_acute_sale_vol['tag_flag'] = 'gross_rev_acute_sale_vol' gross_rev_acute_sale_vol.rename(columns={'type1__': 'type1'}, inplace=True) gross_rev_acute_sale_vol.rename(columns={'category__': 'category'}, inplace=True) gross_rev_acute_sale_vol.rename(columns={'order_source__': 'order_source'}, inplace=True) return gross_rev_chronic_sale_vol, gross_rev_acute_sale_vol def cummulative_cons(self, Cumulative_consumers_data, mis_tag): cumulative_consumers_data = Cumulative_consumers_data.copy(deep=True) all_time_cons1 = pd.pivot_table(cumulative_consumers_data, values='total_cons', columns=['store_name']).reset_index() if mis_tag == 'breakup': cumulative_consumers_data.rename(columns={'generic_without_gaid_cons': 'total_generic_cons'}, inplace=True) all_time_generic_cons1 = pd.pivot_table(cumulative_consumers_data, values='total_generic_cons', columns=['store_name']).reset_index() all_time_gaid_cons1 = pd.pivot_table(cumulative_consumers_data, values='total_gaid_cons', columns=['store_name']).reset_index() else: cumulative_consumers_data.rename(columns={'generic_cons': 'total_generic_cons'}, inplace=True) all_time_generic_cons1 = pd.pivot_table(cumulative_consumers_data, values='total_generic_cons', columns=['store_name']).reset_index() all_time_chronic_cons1 = pd.pivot_table(cumulative_consumers_data, values='total_chronic_cons', columns=['store_name']).reset_index() cumulative_consumers_data['total_acute_cons'] = cumulative_consumers_data['total_cons'] - \ cumulative_consumers_data['total_chronic_cons'] all_time_acute_cons1 = pd.pivot_table(cumulative_consumers_data, values='total_acute_cons', columns=['store_name']).reset_index() if mis_tag == 'breakup': cummulative_cons = pd.concat([all_time_cons1, all_time_generic_cons1, all_time_gaid_cons1, all_time_chronic_cons1, all_time_acute_cons1], sort=True) else: cummulative_cons = pd.concat([all_time_cons1, all_time_generic_cons1, all_time_chronic_cons1, all_time_acute_cons1], sort=True) cummulative_cons.rename(columns={'index': 'tag_flag'}, inplace=True) return cummulative_cons def cummulative_cons_fofo(self, Workcell_cumulative_consumers_fofo_data,Others_cumulative_consumers_fofo_data, mis_tag): workcell_cumulative_consumers_fofo_data = Workcell_cumulative_consumers_fofo_data.copy(deep=True) others_cumulative_consumers_data = Others_cumulative_consumers_fofo_data.copy(deep=True) workcell_all_time_cons1 = pd.pivot_table(workcell_cumulative_consumers_fofo_data, values='total_cons', columns=['store_name']).reset_index() others_all_time_cons1 = pd.pivot_table(others_cumulative_consumers_data, values='total_cons', columns=['store_name']).reset_index() if mis_tag == 'breakup': workcell_cumulative_consumers_fofo_data.rename(columns={'generic_without_gaid_cons': 'total_generic_cons'}, inplace=True) workcell_all_time_generic_cons1 = pd.pivot_table(workcell_cumulative_consumers_fofo_data, values='total_generic_cons', columns=['store_name']).reset_index() workcell_all_time_gaid_cons1 = pd.pivot_table(workcell_cumulative_consumers_fofo_data, values='total_gaid_cons', columns=['store_name']).reset_index() others_cumulative_consumers_data.rename(columns={'generic_without_gaid_cons': 'total_generic_cons'}, inplace=True) others_all_time_generic_cons1 = pd.pivot_table(others_cumulative_consumers_data, values='total_generic_cons', columns=['store_name']).reset_index() others_all_time_gaid_cons1 = pd.pivot_table(others_cumulative_consumers_data, values='total_gaid_cons', columns=['store_name']).reset_index() else: workcell_cumulative_consumers_fofo_data.rename(columns={'generic_cons': 'total_generic_cons'}, inplace=True) workcell_all_time_generic_cons1 = pd.pivot_table(workcell_cumulative_consumers_fofo_data, values='total_generic_cons', columns=['store_name']).reset_index() others_cumulative_consumers_data.rename(columns={'generic_cons': 'total_generic_cons'}, inplace=True) others_all_time_generic_cons1 = pd.pivot_table(others_cumulative_consumers_data, values='total_generic_cons', columns=['store_name']).reset_index() workcell_all_time_chronic_cons1 = pd.pivot_table(workcell_cumulative_consumers_fofo_data, values='total_chronic_cons', columns=['store_name']).reset_index() others_all_time_chronic_cons1 = pd.pivot_table(others_cumulative_consumers_data, values='total_chronic_cons', columns=['store_name']).reset_index() workcell_cumulative_consumers_fofo_data['total_acute_cons'] = workcell_cumulative_consumers_fofo_data['total_cons'] - \ workcell_cumulative_consumers_fofo_data['total_chronic_cons'] others_cumulative_consumers_data['total_acute_cons'] = others_cumulative_consumers_data['total_cons'] - \ others_cumulative_consumers_data['total_chronic_cons'] workcell_all_time_acute_cons1 = pd.pivot_table(workcell_cumulative_consumers_fofo_data, values='total_acute_cons', columns=['store_name']).reset_index() others_all_time_acute_cons1 = pd.pivot_table(others_cumulative_consumers_data, values='total_acute_cons', columns=['store_name']).reset_index() workcell_all_time_cons1['fofo_distributor'] = 'workcell' workcell_all_time_generic_cons1['fofo_distributor'] = 'workcell' workcell_all_time_chronic_cons1['fofo_distributor'] = 'workcell' workcell_all_time_acute_cons1['fofo_distributor'] = 'workcell' others_all_time_cons1['fofo_distributor'] = 'other' others_all_time_generic_cons1['fofo_distributor'] = 'other' others_all_time_chronic_cons1['fofo_distributor'] = 'other' others_all_time_acute_cons1['fofo_distributor'] = 'other' if mis_tag == 'breakup': workcell_all_time_gaid_cons1['fofo_distributor'] = 'workcell' others_all_time_gaid_cons1['fofo_distributor'] = 'other' cummulative_cons_fofo = pd.concat([workcell_all_time_cons1, others_all_time_cons1, workcell_all_time_generic_cons1, others_all_time_generic_cons1, workcell_all_time_gaid_cons1, others_all_time_gaid_cons1, workcell_all_time_chronic_cons1, others_all_time_chronic_cons1, workcell_all_time_acute_cons1, others_all_time_acute_cons1], sort=True) else: cummulative_cons_fofo = pd.concat([workcell_all_time_cons1, others_all_time_cons1, workcell_all_time_generic_cons1, others_all_time_generic_cons1, workcell_all_time_chronic_cons1, others_all_time_chronic_cons1, workcell_all_time_acute_cons1, others_all_time_acute_cons1], sort=True) cummulative_cons_fofo.rename(columns={'index': 'tag_flag'}, inplace=True) return cummulative_cons_fofo def total_cons_mis_month(self,Sales, Stores,fofo_tag = 'no'): sales = Sales.copy(deep = True) stores = Stores.copy(deep = True) sales = pd.merge(left=sales, right=stores, how='left', on=['store_id']) if fofo_tag == 'yes': sales = sales[sales['franchisee_id'] != 1] if fofo_tag == 'no': total_cons = sales.groupby(['store_id', 'store_name', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "total_consumers_MIS_month"}) total_cons['tag_flag'] = 'total_cons_mis_month' total_cons_mis_month = pd.pivot_table(total_cons, values='total_consumers_MIS_month', index=['tag_flag', 'order_source'], columns=['store_name']).reset_index() elif fofo_tag =='yes': total_cons = sales.groupby(['store_id', 'store_name', 'fofo_distributor' ,'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "total_consumers_MIS_month"}) total_cons['tag_flag'] = 'total_cons_mis_month' total_cons_mis_month = pd.pivot_table(total_cons, values='total_consumers_MIS_month', index=['tag_flag','fofo_distributor' , 'order_source'], columns=['store_name']).reset_index() # total_cons_mis_month.rename(columns={'index': 'tag_flag'}, inplace=True) return total_cons_mis_month def category_wise_customer_type_count(self,Sales,Store): sales = Sales.copy(deep = True) stores = Store.copy(deep = True) sales = pd.merge(left=sales, right=stores, how='left', on=['store_id']) sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) sales_chronic = sales[sales['category'] == 'chronic'] sales_chronic.loc[:,'tag_flag'] = "customer_type_chronic" df49 = sales_chronic.groupby(['store_id', 'store_name', 'category', 'order_source', 'tag_flag'])[ ['patient_id']].nunique().reset_index().rename( columns={'patient_id': "customer_type_chronic"}) customer_type_chronic = pd.pivot_table(df49, values='customer_type_chronic', index=['tag_flag', 'category', 'order_source'], columns=['store_name']).reset_index() sales_acute = sales[sales['category'] == 'acute'] sales_acute.loc[:,'tag_flag'] = "customer_type_acute" df50 = sales_acute.groupby(['store_id', 'store_name', 'category', 'order_source', 'tag_flag'])[ ['patient_id']].nunique().reset_index().rename( columns={'patient_id': "customer_type_acute"}) customer_type_acute = pd.pivot_table(df50, values='customer_type_acute', index=['tag_flag', 'category', 'order_source'], columns=['store_name']).reset_index() category_wise_customer_type = pd.concat([customer_type_chronic, customer_type_acute]) # customer_type.rename(columns={'index': 'tag_flag'}, inplace=True) return category_wise_customer_type def new_customers(self,Sales,All_cons_initial_bill_date,Stores): sales = Sales.copy(deep = True) all_cons_initial_bill_date = All_cons_initial_bill_date.copy(deep = True) stores = Stores.copy(deep= True) mis_month_cons_min_bill_date = sales.groupby(['store_id', 'order_source', 'patient_id'])[['created_at']].min().reset_index() new_cons_1 = pd.merge(left=all_cons_initial_bill_date, right=mis_month_cons_min_bill_date, how='inner', on=['store_id', 'patient_id', 'created_at']) new_cons_1['flag'] = "new_cons" new_cons = new_cons_1.groupby(['store_id', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "new_consumers"}) new_cons = pd.merge(left=new_cons, right=stores, how='left', on=['store_id']) new_cons['tag_flag'] = 'new_consumers' new_cons_total = pd.pivot_table(new_cons, values='new_consumers', index=['tag_flag', 'order_source'], columns=['store_name']).reset_index() # new chronic consumers df_fe = pd.merge(left=new_cons_1[['store_id', 'order_source', 'patient_id', 'flag']], right=sales, how='left', on=['store_id', 'order_source', 'patient_id']) new_cons_chronic = df_fe.groupby(['store_id', 'category', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "new_chronic_consumers"}) new_cons_chronic1 = new_cons_chronic[new_cons_chronic['category'].isin(['chronic'])] new_cons_chronic1 = pd.merge(left=new_cons_chronic1, right=stores, how='left', on=['store_id']) new_cons_chronic1['tag_flag'] = 'new_chronic_consumers' new_cons_chronic2 = pd.pivot_table(new_cons_chronic1, values='new_chronic_consumers', index=['tag_flag', 'order_source'], columns=['store_name']).reset_index() new_customers = pd.concat([new_cons_total, new_cons_chronic2],sort=True) return new_customers def tot_repeat_consumers(self, Sales,All_cons_initial_bill_date,Stores ): sales = Sales.copy(deep=True) all_cons_initial_bill_date = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) total_cons = sales.groupby(['store_id', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "total_consumers"}) aug_cons_min = sales.groupby(['store_id', 'order_source', 'patient_id'])[['created_at']].min().reset_index() aug_new_cons = pd.merge(left=all_cons_initial_bill_date, right=aug_cons_min, how='inner', on=['store_id', 'patient_id', 'created_at']) new_cons = aug_new_cons.groupby(['store_id', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "new_consumers"}) repeat_cons = pd.merge(left=total_cons, right=new_cons, how='left', on=['store_id', 'order_source']) repeat_cons['repeat_consumers'] = repeat_cons['total_consumers'] - repeat_cons['new_consumers'] repeat_cons = pd.merge(left=repeat_cons, right=stores, how='left', on=['store_id']) repeat_cons['tag_flag'] = 'repeat_consumers' repeat_cons1 = pd.pivot_table(repeat_cons, values='repeat_consumers', index=['tag_flag', 'order_source'], columns=['store_name']).reset_index() # repeat_cons1.rename(columns={'index': 'tag_flag'}, inplace=True) return repeat_cons1 def new_cons_vol_qty(self, Sales, All_cons_initial_bill_date, Stores): sales = Sales.copy(deep=True) all_cons_initial_bill_date = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) aug_cons_min = sales.groupby(['store_id', 'order_source', 'patient_id'])[['created_at']].min().reset_index() aug_new_cons = pd.merge(left=all_cons_initial_bill_date, right=aug_cons_min, how='inner', on=['store_id', 'patient_id', 'created_at']) new_cons = aug_new_cons.groupby(['store_id', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={'patient_id': "new_consumers"}) aug_value_volumne = sales.groupby(['store_id', 'order_source', 'patient_id'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'quantity': ['sum'], 'value': ['sum']}).reset_index(drop=True) aug_value_volumne.columns = ["_".join(x) for x in aug_value_volumne.columns.ravel()] aug_value_volumne.rename(columns={'store_id_': "store_id", 'patient_id_': "patient_id", 'order_source_': "order_source"}, inplace=True) new_cons_value_vol = pd.merge(left=aug_new_cons, right=aug_value_volumne, how='left', on=['store_id', 'order_source', 'patient_id']) new_cons_value_vol1 = new_cons_value_vol.groupby(['store_id', 'order_source'], as_index=False).agg({ 'quantity_sum': ['sum'], 'value_sum': ['sum']}).reset_index(drop=True) new_cons_value_vol1.columns = ["_".join(x) for x in new_cons_value_vol1.columns.ravel()] new_cons_value_vol2 = pd.merge(left=new_cons_value_vol1, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) new_cons_value_vol2['value_sum_sum'] = new_cons_value_vol2['value_sum_sum'].astype(float) new_cons_volume = pd.pivot_table(new_cons_value_vol2, values='value_sum_sum', index=['order_source_'], columns=['store_name']).reset_index() new_cons_volume['tag_flag'] = 'new_consumer_value' new_cons_value_vol2['quantity_sum_sum'] = new_cons_value_vol2['quantity_sum_sum'].astype(float) new_cons_qty = pd.pivot_table(new_cons_value_vol2, values='quantity_sum_sum', index=['order_source_'], columns=['store_name']).reset_index() new_cons_qty['tag_flag'] = 'new_consumer_qty' new_cons_vol_qty = pd.concat([new_cons_volume, new_cons_qty], sort=True) new_cons_vol_qty.rename(columns={'order_source_': "order_source"}, inplace=True) # new_cons_vol_qty.rename(columns={'index': 'tag_flag'}, inplace=True) # new_cons_vol_qty.loc[new_cons_vol_qty.tag_flag == 'quantity_sum_sum', 'tag_flag'] = 'new_consumer_qty' # new_cons_vol_qty.loc[new_cons_vol_qty.tag_flag == 'value_sum_sum', 'tag_flag'] = 'new_consumer_value' return new_cons_vol_qty def total_bills_new_repeat(self, Sales, All_cons_initial_bill_date, Stores,choose_year,choose_month,fofo_tag = 'no'): sales = Sales.copy(deep=True) df1 = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) if fofo_tag == 'yes': sales = sales[sales['franchisee_id'] != 1] df1['year'] = df1['created_at'].dt.year df1["month"] = df1['created_at'].dt.month df1 = df1[(df1['year'] == int(choose_year)) & (df1['month'] == int(choose_month))] sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) if fofo_tag=='no': df2 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['flag']].sum().reset_index() elif fofo_tag=='yes': df2 = sales.groupby(['store_id', 'order_source', 'fofo_distributor' ,'patient_id'])[['flag']].sum().reset_index() df2['check'] = np.where(df2['flag'] > 0, "chronic", "acute") df3 = pd.merge(left=df1, right=df2, how='left', on=['store_id', 'patient_id']) df5 = pd.merge(left=df2, right=df1, how='left', on=['store_id', 'patient_id']) df6 = df5[df5['year'].isnull()] if fofo_tag == 'no': df9 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['bill_id']].nunique().reset_index() df10 = pd.merge(left=df3, right=df9, how='left', on=['store_id', 'order_source', 'patient_id']) df11 = df10.groupby(['store_id', 'order_source'])[['bill_id']].sum().reset_index().rename(columns={ "bill_id": "new_consumers_bills_count"}) df12 = pd.merge(left=df6, right=df9, how='left', on=['store_id', 'order_source', 'patient_id']) df13 = df12.groupby(['store_id', 'order_source'])[['bill_id']].sum().reset_index().rename(columns={ "bill_id": "repeat_consumers_bills_count"}) df14 = pd.merge(left=df11, right=df13, how='left', on=['store_id', 'order_source']) elif fofo_tag == 'yes': df9 = sales.groupby(['store_id', 'order_source', 'fofo_distributor' , 'patient_id'])[['bill_id']].nunique().reset_index() df10 = pd.merge(left=df3, right=df9, how='left', on=['store_id', 'order_source', 'fofo_distributor' , 'patient_id']) df11 = df10.groupby(['store_id', 'fofo_distributor' , 'order_source'])[['bill_id']].sum().reset_index().rename(columns={ "bill_id": "new_consumers_bills_count"}) df12 = pd.merge(left=df6, right=df9, how='left', on=['store_id', 'fofo_distributor' , 'order_source', 'patient_id']) df13 = df12.groupby(['store_id', 'fofo_distributor' , 'order_source'])[['bill_id']].sum().reset_index().rename(columns={ "bill_id": "repeat_consumers_bills_count"}) df14 = pd.merge(left=df11, right=df13, how='left', on=['store_id', 'fofo_distributor' , 'order_source']) df14 = pd.merge(left=df14, right=stores, how='left', on=['store_id']) if fofo_tag == 'no': total_bills_new = pd.pivot_table(df14, values='new_consumers_bills_count', index='order_source', columns=['store_name']).reset_index() total_bills_new['tag_flag'] = 'new_consumers_bills_count' total_bills_repeat = pd.pivot_table(df14, values='repeat_consumers_bills_count', index='order_source', columns=['store_name']).reset_index() total_bills_repeat['tag_flag'] = 'repeat_consumers_bills_count' elif fofo_tag == 'yes': total_bills_new = pd.pivot_table(df14, values='new_consumers_bills_count', index=['order_source','fofo_distributor'], columns=['store_name']).reset_index() total_bills_new['tag_flag'] = 'new_consumers_bills_count' total_bills_repeat = pd.pivot_table(df14, values='repeat_consumers_bills_count', index=['order_source','fofo_distributor'], columns=['store_name']).reset_index() total_bills_repeat['tag_flag'] = 'repeat_consumers_bills_count' total_bills_new_repeat = pd.concat([total_bills_new, total_bills_repeat], sort=True) return total_bills_new_repeat def total_bills_chronic_acute(self, Sales, Customer_returns, Stores, fofo_tag = 'no'): sales = Sales.copy(deep=True) customer_returns = Customer_returns.copy(deep=True) stores = Stores.copy(deep=True) sales['value'] = sales['quantity'] * sales['rate'] sales['COGS'] = sales['quantity'] * sales['final_ptr'] customer_returns['returned_value'] = customer_returns['returned_quantity'] * customer_returns['rate'] customer_returns['returned_COGS'] = customer_returns['returned_quantity'] * customer_returns['final_ptr'] if fofo_tag=='no': df_a1a = sales.groupby(['store_id', 'order_source', 'type1', 'category'], as_index=False).agg({ 'value': ['sum'], 'quantity': ['sum'], 'COGS': ['sum'], 'bill_id': pd.Series.nunique, 'drug_id': pd.Series.nunique}).reset_index(drop=True) df_a1a.columns = ["_".join(x) for x in df_a1a.columns.ravel()] df_b2 = customer_returns.groupby(['store_id', 'order_source', 'type1', 'category'], as_index=False).agg({ 'returned_value': ['sum'], 'returned_quantity': ['sum'], 'returned_COGS': ['sum'], 'returned_bill_id': pd.Series.nunique, 'returned_drug_id': pd.Series.nunique}).reset_index(drop=True) df_b2.columns = ["_".join(x) for x in df_b2.columns.ravel()] df_a_b = pd.merge(left=df_a1a, right=df_b2, how='outer', on=['store_id_', 'order_source_', 'type1_', 'category_']) df_a_b.fillna(0, inplace=True) df_a_b['net_sale'] = df_a_b['value_sum'] - df_a_b['returned_value_sum'] df_a_b['net_COGS'] = df_a_b['COGS_sum'] - df_a_b['returned_COGS_sum'] df_a_b['net_quantity'] = df_a_b['quantity_sum'] - df_a_b['returned_quantity_sum'] df_a4 = df_a_b.groupby(['store_id_', 'order_source_', 'category_', 'type1_'], as_index=False).agg({ 'net_sale': ['sum'], 'net_quantity': ['sum'], 'net_COGS': ['sum'], 'bill_id_nunique': ['sum'], 'drug_id_nunique': ['sum']}).reset_index(drop=True) df_a4.columns = ["_".join(x) for x in df_a4.columns.ravel()] df_a4 = pd.merge(left=df_a4, right=stores, how='left', left_on=['store_id__'], right_on=['store_id']) df_a5 = df_a4[df_a4['category__'] == 'chronic'] total_bills_chronic = pd.pivot_table(df_a5, values='bill_id_nunique_sum', index=['order_source__', 'category__', 'type1__'], columns=['store_name']).reset_index() df_a6 = df_a4[df_a4['category__'] == 'acute'] total_bills_acute = pd.pivot_table(df_a6, values='bill_id_nunique_sum', index=['order_source__', 'category__', 'type1__'], columns=['store_name']).reset_index() total_bills_chronic_acute = pd.concat([total_bills_chronic, total_bills_acute]) total_bills_chronic_acute['tag_flag'] = 'total_bills' total_bills_chronic_acute.rename(columns={'type1__': 'type1', 'category__': 'category', 'order_source__': 'order_source'}, inplace=True) elif fofo_tag == 'yes': sales = sales[sales['franchisee_id'] != 1] customer_returns = customer_returns[customer_returns['franchisee_id'] != 1] df_a1a = sales.groupby(['store_id', 'order_source', 'fofo_distributor' ,'type1', 'category'], as_index=False).agg({ 'value': ['sum'], 'quantity': ['sum'], 'COGS': ['sum'], 'bill_id': pd.Series.nunique, 'drug_id': pd.Series.nunique}).reset_index(drop=True) df_a1a.columns = ["_".join(x) for x in df_a1a.columns.ravel()] df_b2 = customer_returns.groupby(['store_id', 'order_source', 'fofo_distributor', 'type1', 'category'], as_index=False).agg({ 'returned_value': ['sum'], 'returned_quantity': ['sum'], 'returned_COGS': ['sum'], 'returned_bill_id': pd.Series.nunique, 'returned_drug_id': pd.Series.nunique}).reset_index(drop=True) df_b2.columns = ["_".join(x) for x in df_b2.columns.ravel()] df_a_b = pd.merge(left=df_a1a, right=df_b2, how='outer', on=['store_id_', 'order_source_', 'fofo_distributor_', 'type1_', 'category_']) df_a_b.fillna(0, inplace=True) df_a_b['net_sale'] = df_a_b['value_sum'] - df_a_b['returned_value_sum'] df_a_b['net_COGS'] = df_a_b['COGS_sum'] - df_a_b['returned_COGS_sum'] df_a_b['net_quantity'] = df_a_b['quantity_sum'] - df_a_b['returned_quantity_sum'] df_a4 = df_a_b.groupby(['store_id_', 'order_source_', 'fofo_distributor_', 'category_', 'type1_'], as_index=False).agg({ 'net_sale': ['sum'], 'net_quantity': ['sum'], 'net_COGS': ['sum'], 'bill_id_nunique': ['sum'], 'drug_id_nunique': ['sum']}).reset_index(drop=True) df_a4.columns = ["_".join(x) for x in df_a4.columns.ravel()] df_a4 = pd.merge(left=df_a4, right=stores, how='left', left_on=['store_id__'], right_on=['store_id']) df_a5 = df_a4[df_a4['category__'] == 'chronic'] total_bills_chronic = pd.pivot_table(df_a5, values='bill_id_nunique_sum', index=['order_source__', 'fofo_distributor__', 'category__', 'type1__'], columns=['store_name']).reset_index() df_a6 = df_a4[df_a4['category__'] == 'acute'] total_bills_acute = pd.pivot_table(df_a6, values='bill_id_nunique_sum', index=['order_source__', 'fofo_distributor__', 'category__', 'type1__'], columns=['store_name']).reset_index() total_bills_chronic_acute = pd.concat([total_bills_chronic, total_bills_acute]) total_bills_chronic_acute['tag_flag'] = 'total_bills' total_bills_chronic_acute.rename(columns={'type1__': 'type1', 'category__': 'category', 'order_source__': 'order_source', 'fofo_distributor__': 'fofo_distributor'}, inplace=True) return total_bills_chronic_acute def bills_per_cons_new_repeat(self, Sales, All_cons_initial_bill_date ,Stores, choose_year,choose_month): sales = Sales.copy(deep=True) df1 = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) df1['year'] = df1['created_at'].dt.year df1["month"] = df1['created_at'].dt.month df1 = df1[(df1['year'] == int(choose_year)) & (df1['month'] == int(choose_month))] sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) df2 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['flag']].sum().reset_index() df2['check'] = np.where(df2['flag'] > 0, "chronic", "acute") df3 = pd.merge(left=df1, right=df2, how='left', on=['store_id', 'patient_id']) df5 = pd.merge(left=df2, right=df1, how='left', on=['store_id', 'patient_id']) df6 = df5[df5['year'].isnull()] df30 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['bill_id']].nunique().reset_index().rename(columns={ "bill_id": "no_of_bills"}) # new consumers df31 = pd.merge(left=df3, right=df30, how='left', on=['store_id', 'order_source', 'patient_id']) df32 = df31.groupby(['store_id', 'order_source'])[['no_of_bills']].mean().reset_index().rename(columns={ "no_of_bills": "new_consumers_avg_no_of_bills"}) # repeat consumers df33 = pd.merge(left=df6, right=df30, how='left', on=['store_id', 'order_source', 'patient_id']) df34 = df33.groupby(['store_id', 'order_source'])[['no_of_bills']].mean().reset_index().rename(columns={ "no_of_bills": "repeat_consumers_avg_no_of_bills"}) df35 = pd.merge(left=df32, right=df34, how='left', on=['store_id', 'order_source']) df35 = pd.merge(left=df35, right=stores, how='left', on=['store_id']) bills_per_cons_new = pd.pivot_table(df35, values='new_consumers_avg_no_of_bills', index='order_source', columns=['store_name']).reset_index() bills_per_cons_new['tag_flag'] = 'new_consumers_avg_no_of_bills' bills_per_cons_repeat = pd.pivot_table(df35, values='repeat_consumers_avg_no_of_bills', index='order_source', columns=['store_name']).reset_index() bills_per_cons_repeat['tag_flag'] = 'repeat_consumers_avg_no_of_bills' bills_per_cons_new_repeat = pd.concat([bills_per_cons_new, bills_per_cons_repeat]) # bills_per_cons_new_repeat.rename(columns={'index': 'tag_flag'}, inplace=True) return bills_per_cons_new_repeat def abv_new_repeat_chronic(self, Sales, All_cons_initial_bill_date ,Stores, choose_year,choose_month ): sales = Sales.copy(deep=True) df1 = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) df1['year'] = df1['created_at'].dt.year df1["month"] = df1['created_at'].dt.month df1 = df1[(df1['year'] == int(choose_year)) & (df1['month'] == int(choose_month))] sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) df2 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['flag']].sum().reset_index() df2['check'] = np.where(df2['flag'] > 0, "chronic", "acute") df3 = pd.merge(left=df1, right=df2, how='left', on=['store_id', 'patient_id']) df5 = pd.merge(left=df2, right=df1, how='left', on=['store_id', 'patient_id']) df6 = df5[df5['year'].isnull()] sales['value'] = sales['quantity'] * sales['rate'] sales['value'] = sales['value'].astype(float) df36 = sales.groupby(['store_id', 'order_source', 'patient_id', 'bill_id'])[['value']].sum().reset_index() df37 = df36.groupby(['store_id', 'order_source', 'patient_id'])[['value']].mean().reset_index() # new consumers df38 = pd.merge(left=df3, right=df37, how='left', on=['store_id', 'order_source', 'patient_id']) df39 = df38.groupby(['store_id', 'order_source'])[['value']].mean().reset_index().rename(columns={ "value": "new_consumers_avg_bill_value"}) # repeat consumers df40 = pd.merge(left=df6, right=df37, how='left', on=['store_id', 'order_source', 'patient_id']) df41 = df40.groupby(['store_id', 'order_source'])[['value']].mean().reset_index().rename(columns={ "value": "repeat_consumers_avg_bill_value"}) df42 = pd.merge(left=df39, right=df41, how='left', on=['store_id', 'order_source']) df42 = pd.merge(left=df42, right=stores, how='left', on=['store_id']) df42['new_consumers_avg_bill_value'] = df42['new_consumers_avg_bill_value'].astype(float) abv_new = pd.pivot_table(df42, values='new_consumers_avg_bill_value', index='order_source', columns=['store_name']).reset_index() abv_new['tag_flag'] = 'new_consumers_avg_bill_value' df42['repeat_consumers_avg_bill_value'] = df42['repeat_consumers_avg_bill_value'].astype(float) abv_repeat = pd.pivot_table(df42, values='repeat_consumers_avg_bill_value', index='order_source', columns=['store_name']).reset_index() abv_repeat['tag_flag'] = 'repeat_consumers_avg_bill_value' df_a9 = sales.groupby(['store_id', 'order_source', 'bill_id', 'category'])[['value']].sum().reset_index() df_a10 = df_a9.groupby(['store_id', 'order_source', 'category'])[['value']].mean().reset_index().rename(columns= { 'value': "chronic_consumers_avg_bill_value"}) df_a11 = df_a10[df_a10['category'] == 'chronic'] df_a11 = pd.merge(left=df_a11, right=stores, how='left', on=['store_id']) abv_chronic = pd.pivot_table(df_a11, values='chronic_consumers_avg_bill_value', index='order_source', columns=['store_name']).reset_index() abv_chronic['tag_flag'] = 'chronic_consumers_avg_bill_value' abv_new_repeat_chronic = pd.concat([abv_new, abv_repeat, abv_chronic]) return abv_new_repeat_chronic def items_per_cons_new_repeat(self,Sales, All_cons_initial_bill_date ,Stores, choose_year,choose_month ): sales = Sales.copy(deep=True) df1 = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) df1['year'] = df1['created_at'].dt.year df1["month"] = df1['created_at'].dt.month df1 = df1[(df1['year'] == int(choose_year)) & (df1['month'] == int(choose_month))] sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) df2 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['flag']].sum().reset_index() df2['check'] = np.where(df2['flag'] > 0, "chronic", "acute") df3 = pd.merge(left=df1, right=df2, how='left', on=['store_id', 'patient_id']) df5 = pd.merge(left=df2, right=df1, how='left', on=['store_id', 'patient_id']) df6 = df5[df5['year'].isnull()] df43 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['drug_id']].nunique().reset_index() # new consumers df44 = pd.merge(left=df3, right=df43, how='left', on=['store_id', 'order_source', 'patient_id']) df45 = df44.groupby(['store_id', 'order_source'])[['drug_id']].mean().reset_index().rename(columns={ "drug_id": "new_consumers_avg_items"}) # repeat consumers df46 = pd.merge(left=df6, right=df43, how='left', on=['store_id', 'order_source', 'patient_id']) df47 = df46.groupby(['store_id', 'order_source'])[['drug_id']].mean().reset_index().rename(columns={ "drug_id": "repeat_consumers_avg_items"}) df48 = pd.merge(left=df45, right=df47, how='left', on=['store_id', 'order_source']) df48 = pd.merge(left=df48, right=stores, how='left', on=['store_id']) items_per_cons_new = pd.pivot_table(df48, values='new_consumers_avg_items', index='order_source', columns=['store_name']).reset_index() items_per_cons_new['tag_flag'] = 'new_consumers_avg_items' items_per_cons_repeat = pd.pivot_table(df48, values='repeat_consumers_avg_items', index='order_source', columns=['store_name']).reset_index() items_per_cons_repeat['tag_flag'] = 'repeat_consumers_avg_items' items_per_cons_new_repeat = pd.concat([items_per_cons_new, items_per_cons_repeat]) # items_per_cons_new_repeat.rename(columns={'index': 'tag_flag'}, inplace=True) return items_per_cons_new_repeat def tot_items_sold_new_repeat(self, Sales, All_cons_initial_bill_date, Stores, choose_year, choose_month): sales = Sales.copy(deep=True) df1 = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) df1['year'] = df1['created_at'].dt.year df1["month"] = df1['created_at'].dt.month df1 = df1[(df1['year'] == int(choose_year)) & (df1['month'] == int(choose_month))] sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) df2 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['flag']].sum().reset_index() df2['check'] = np.where(df2['flag'] > 0, "chronic", "acute") df3 = pd.merge(left=df1, right=df2, how='left', on=['store_id', 'patient_id']) df5 = pd.merge(left=df2, right=df1, how='left', on=['store_id', 'patient_id']) df6 = df5[df5['year'].isnull()] df24 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['quantity']].sum().reset_index() # new consumers df25 = pd.merge(left=df3, right=df24, how='left', on=['store_id', 'order_source', 'patient_id']) df26 = df25.groupby(['store_id', 'order_source'])[['quantity']].sum().reset_index().rename(columns={ "quantity": "new_consumers_qty"}) # repeat consumers df27 = pd.merge(left=df6, right=df24, how='left', on=['store_id', 'order_source', 'patient_id']) df28 = df27.groupby(['store_id', 'order_source'])[['quantity']].sum().reset_index().rename(columns={ "quantity": "repeat_consumers_qty"}) df29 = pd.merge(left=df26, right=df28, how='left', on=['store_id', 'order_source']) df29 = pd.merge(left=df29, right=stores, how='left', on=['store_id']) tot_items_sold_new = pd.pivot_table(df29, values='new_consumers_qty', index='order_source', columns=['store_name']).reset_index() tot_items_sold_new['tag_flag'] = 'new_consumers_qty' tot_items_sold_repeat = pd.pivot_table(df29, values='repeat_consumers_qty', index='order_source', columns=['store_name']).reset_index() tot_items_sold_repeat['tag_flag'] = 'repeat_consumers_qty' tot_items_sold_new_repeat = pd.concat([tot_items_sold_new, tot_items_sold_repeat]) # tot_items_sold_new_repeat.rename(columns={'index': 'tag_flag'}, inplace=True) return tot_items_sold_new_repeat def generic_cons_overall_new(self, Sales, All_cons_initial_bill_date, Stores,fofo_tag = 'no'): sales = Sales.copy(deep=True) all_cons_first = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) if fofo_tag=='no': generic_cons = sales.groupby(['store_id', 'order_source', 'type'])[['patient_id']].nunique().reset_index().rename(columns={ "patient_id": "generic_cons_overall"}) generic_cons = generic_cons[generic_cons['type'].isin(['generic', 'high-value-generic'])] generic_cons = pd.merge(left=generic_cons, right=stores, how='left', on=['store_id']) generic_cons_overall = pd.pivot_table(generic_cons, values='generic_cons_overall', index='order_source', columns=['store_name']).reset_index() generic_cons_overall['tag_flag'] = 'generic_cons_overall' aug_cons_min = sales.groupby(['store_id', 'order_source', 'patient_id'])[['created_at']].min().reset_index() aug_new_cons = pd.merge(left=all_cons_first, right=aug_cons_min, how='inner', on=['store_id', 'patient_id', 'created_at']) aug_new_cons['flag'] = "aug_new" new_cons_generic = pd.merge(left=aug_new_cons[['store_id', 'order_source', 'patient_id', 'flag']], right=sales, how='left', on=['store_id', 'order_source', 'patient_id']) new_cons_generic1 = new_cons_generic.groupby(['store_id', 'order_source', 'type'])[ ['patient_id']].nunique().reset_index().rename(columns={ "patient_id": "generic_cons_new"}) new_cons_generic1 = new_cons_generic1[new_cons_generic1['type'].isin(['generic', 'high-value-generic'])] new_cons_generic1 = pd.merge(left=new_cons_generic1, right=stores, how='left', on=['store_id']) generic_cons_new = pd.pivot_table(new_cons_generic1, values='generic_cons_new', index='order_source', columns=['store_name']).reset_index() generic_cons_new['tag_flag'] = 'generic_cons_new' generic_cons_overall_new = pd.concat([generic_cons_overall, generic_cons_new]) elif fofo_tag=='yes': sales = sales[sales['franchisee_id'] != 1] generic_cons = sales.groupby(['store_id', 'order_source','fofo_distributor', 'type'])[['patient_id']].nunique().reset_index().rename( columns={ "patient_id": "generic_cons_overall"}) generic_cons = generic_cons[generic_cons['type'].isin(['generic', 'high-value-generic'])] generic_cons = pd.merge(left=generic_cons, right=stores, how='left', on=['store_id']) generic_cons_overall = pd.pivot_table(generic_cons, values='generic_cons_overall', index=['order_source','fofo_distributor'], columns=['store_name']).reset_index() generic_cons_overall['tag_flag'] = 'generic_cons_overall' aug_cons_min = sales.groupby(['store_id', 'order_source', 'patient_id'])[['created_at']].min().reset_index() aug_new_cons = pd.merge(left=all_cons_first, right=aug_cons_min, how='inner', on=['store_id', 'patient_id', 'created_at']) aug_new_cons['flag'] = "aug_new" new_cons_generic = pd.merge(left=aug_new_cons[['store_id', 'order_source', 'patient_id', 'flag']], right=sales, how='left', on=['store_id', 'order_source', 'patient_id']) new_cons_generic1 = new_cons_generic.groupby(['store_id', 'order_source','fofo_distributor', 'type'])[ ['patient_id']].nunique().reset_index().rename(columns={ "patient_id": "generic_cons_new"}) new_cons_generic1 = new_cons_generic1[new_cons_generic1['type'].isin(['generic', 'high-value-generic'])] new_cons_generic1 = pd.merge(left=new_cons_generic1, right=stores, how='left', on=['store_id']) generic_cons_new = pd.pivot_table(new_cons_generic1, values='generic_cons_new', index=['order_source','fofo_distributor'], columns=['store_name']).reset_index() generic_cons_new['tag_flag'] = 'generic_cons_new' generic_cons_overall_new = pd.concat([generic_cons_overall, generic_cons_new]) return generic_cons_overall_new def power_cons_overall_new(self, Sales, All_cons_initial_bill_date, Stores, power_consumer_value): sales = Sales.copy(deep=True) all_cons_first = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) power_cons_aug = sales.groupby(['store_id', 'order_source', 'patient_id'])[['value']].sum().reset_index() power_cons_aug1 = power_cons_aug[power_cons_aug['value'] > power_consumer_value] power_cons = power_cons_aug1.groupby(['store_id', 'order_source'])[ ['patient_id']].nunique().reset_index().rename( columns={ "patient_id": "power_cons_overall"}) power_cons = pd.merge(left=power_cons, right=stores, how='left', on=['store_id']) power_cons_overall = pd.pivot_table(power_cons, values='power_cons_overall', index='order_source', columns=['store_name']).reset_index() power_cons_overall['tag_flag'] = 'power_cons_overall' aug_cons_min = sales.groupby(['store_id', 'order_source', 'patient_id'])[['created_at']].min().reset_index() aug_new_cons = pd.merge(left=all_cons_first, right=aug_cons_min, how='inner', on=['store_id', 'patient_id', 'created_at']) aug_new_cons['flag'] = "aug_new" df_fg = pd.merge(left=aug_new_cons[['store_id', 'order_source', 'patient_id', 'flag']], right=power_cons_aug1, how='left', on=['store_id', 'order_source', 'patient_id']) df_fg1 = df_fg[df_fg['value'].notnull()] new_power_cons = df_fg1.groupby(['store_id', 'order_source'])[['patient_id']].nunique().reset_index().rename( columns={ "patient_id": "power_cons_new"}) new_power_cons = pd.merge(left=new_power_cons, right=stores, how='left', on=['store_id']) power_cons_new = pd.pivot_table(new_power_cons, values='power_cons_new', index='order_source', columns=['store_name']).reset_index() power_cons_new['tag_flag'] = 'power_cons_new' power_cons_overall_new = pd.concat([power_cons_overall, power_cons_new]) # power_cons_overall_new.rename(columns={'index': 'tag_flag'}, inplace=True) return power_cons_overall_new def power_consumers_sale(self, Sales, Stores, power_consumer_value, mis_type, fofo_tag = 'no'): sales = Sales.copy(deep=True) stores = Stores.copy(deep=True) if fofo_tag == 'no': ad1 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['value']].sum().reset_index() ad2 = ad1[ad1['value'] > power_consumer_value] ad3 = pd.merge(left=ad2[['store_id', 'order_source', 'patient_id']], right=sales, how='left', on=['store_id', 'order_source', 'patient_id']) power_cons_sales = ad3.groupby(['store_id', 'order_source', 'type1'])[['value']].sum().reset_index() power_cons_sales['value'] = power_cons_sales['value'].astype(float) power_cons_sales = pd.merge(left=power_cons_sales, right=stores, how='left', on=['store_id']) power_cons_sales_ethical = power_cons_sales[power_cons_sales['type1'] == 'ethical'] power_cons_sales_ethical = pd.pivot_table(power_cons_sales_ethical, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index() power_cons_sales_generic = power_cons_sales[power_cons_sales['type1'] == 'generic'] power_cons_sales_generic = pd.pivot_table(power_cons_sales_generic, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index() power_cons_sales_others = power_cons_sales[power_cons_sales['type1'] == 'others'] power_cons_sales_others = pd.pivot_table(power_cons_sales_others, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index() power_cons_sales_gaid = power_cons_sales[power_cons_sales['type1'] == 'GOODAID'] power_cons_sales_gaid = pd.pivot_table(power_cons_sales_gaid, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index() power_cons_sales_overall = power_cons_sales.groupby(['store_id', 'store_name', 'order_source'])[ ['value']].sum().reset_index().rename(columns={ "value": "total"}) power_cons_sales_overall = pd.pivot_table(power_cons_sales_overall, values='total', index=['order_source'], columns=['store_name']).reset_index().rename(columns={ "index": "type1"}) if mis_type == 'breakup': power_consumers_sale = pd.concat([power_cons_sales_overall, power_cons_sales_ethical, power_cons_sales_generic, power_cons_sales_others, power_cons_sales_gaid], sort=True) elif mis_type == 'unified': power_consumers_sale = pd.concat([power_cons_sales_overall, power_cons_sales_ethical, power_cons_sales_generic, power_cons_sales_others], sort=True) else: self.logger.info('provide valid mis_type') return None power_consumers_sale['tag_flag'] = 'power_cons_sale' return power_consumers_sale elif fofo_tag == 'yes': sales = sales[sales['franchisee_id']!=1] ad1 = sales.groupby(['store_id', 'order_source','patient_id'])[['value']].sum().reset_index() ad2 = ad1[ad1['value'] > power_consumer_value] ad3 = pd.merge(left=ad2[['store_id', 'order_source' ,'patient_id']], right=sales, how='left', on=['store_id', 'order_source' ,'patient_id']) power_cons_sales = ad3.groupby(['store_id', 'order_source', 'fofo_distributor' , 'type1'])[['value']].sum().reset_index() power_cons_sales['value'] = power_cons_sales['value'].astype(float) power_cons_sales = pd.merge(left=power_cons_sales, right=stores, how='left', on=['store_id']) power_cons_sales_ethical = power_cons_sales[power_cons_sales['type1'] == 'ethical'] power_cons_sales_ethical = pd.pivot_table(power_cons_sales_ethical, values='value', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() power_cons_sales_generic = power_cons_sales[power_cons_sales['type1'] == 'generic'] power_cons_sales_generic = pd.pivot_table(power_cons_sales_generic, values='value', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() power_cons_sales_others = power_cons_sales[power_cons_sales['type1'] == 'others'] power_cons_sales_others = pd.pivot_table(power_cons_sales_others, values='value', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() power_cons_sales_gaid = power_cons_sales[power_cons_sales['type1'] == 'GOODAID'] power_cons_sales_gaid = pd.pivot_table(power_cons_sales_gaid, values='value', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() power_cons_sales_overall = power_cons_sales.groupby(['store_id', 'store_name', 'order_source','fofo_distributor'])[ ['value']].sum().reset_index().rename(columns={ "value": "total"}) power_cons_sales_overall = pd.pivot_table(power_cons_sales_overall, values='total', index=['order_source','fofo_distributor'], columns=['store_name']).reset_index().rename(columns={ "index": "type1"}) if mis_type == 'breakup': power_consumers_sale = pd.concat([power_cons_sales_overall, power_cons_sales_ethical, power_cons_sales_generic, power_cons_sales_others, power_cons_sales_gaid], sort=True) elif mis_type == 'unified': power_consumers_sale = pd.concat([power_cons_sales_overall, power_cons_sales_ethical, power_cons_sales_generic, power_cons_sales_others], sort=True) else: self.logger.info('provide valid mis_type') return None power_consumers_sale['tag_flag'] = 'power_cons_sale' return power_consumers_sale def power_cons_bills(self, Sales, Stores, power_consumer_value,fofo_tag = 'no'): sales = Sales.copy(deep=True) stores = Stores.copy(deep=True) if fofo_tag == 'no': power_cons_aug = sales.groupby(['store_id', 'order_source', 'patient_id'])[['value']].sum().reset_index() power_cons_aug1 = power_cons_aug[power_cons_aug['value'] > power_consumer_value] df_lp = pd.merge(left=power_cons_aug1[['store_id', 'order_source', 'patient_id']], right=sales, how='left', on=['store_id', 'order_source', 'patient_id']) power_cons_bills = df_lp.groupby(['store_id', 'order_source'])[['bill_id']].nunique().reset_index().rename( columns={ "bill_id": "no_of_bills"}) power_cons_bills = pd.merge(left=power_cons_bills, right=stores, how='left', on=['store_id']) power_cons_bills = pd.pivot_table(power_cons_bills, values='no_of_bills', index='order_source', columns=['store_name']).reset_index() power_cons_bills['tag_flag'] = "no_of_bills" # power_cons_bills.rename(columns={'index': 'tag_flag'}, inplace=True) power_cons_bills.loc[power_cons_bills['tag_flag'] == 'no_of_bills', 'tag_flag'] = 'Power cons no_of_bills' return power_cons_bills elif fofo_tag == 'yes': sales = sales[sales['franchisee_id'] != 1] power_cons_aug = sales.groupby(['store_id', 'order_source' ,'patient_id'])[['value']].sum().reset_index() power_cons_aug1 = power_cons_aug[power_cons_aug['value'] > power_consumer_value] df_lp = pd.merge(left=power_cons_aug1[['store_id', 'order_source','patient_id']], right=sales, how='left', on=['store_id', 'order_source','patient_id']) power_cons_bills = df_lp.groupby(['store_id', 'order_source','fofo_distributor'])[['bill_id']].nunique().reset_index().rename( columns={ "bill_id": "no_of_bills"}) power_cons_bills = pd.merge(left=power_cons_bills, right=stores, how='left', on=['store_id']) power_cons_bills = pd.pivot_table(power_cons_bills, values='no_of_bills', index=['order_source','fofo_distributor'], columns=['store_name']).reset_index() power_cons_bills['tag_flag'] = "no_of_bills" # power_cons_bills.rename(columns={'index': 'tag_flag'}, inplace=True) power_cons_bills.loc[power_cons_bills['tag_flag'] == 'no_of_bills', 'tag_flag'] = 'Power cons no_of_bills' return power_cons_bills def home_delivery(self, Sales, Customer_returns, Home_delivery_data, Stores, delivery_bill_ids,mis_tag,fofo_tag = 'no' ): sales = Sales.copy(deep=True) customer_returns = Customer_returns.copy(deep=True) home_delivery_data = Home_delivery_data.copy(deep=True) stores = Stores.copy(deep=True) delivery_bill_ids = delivery_bill_ids.copy(deep=True) if fofo_tag == 'yes': home_delivery_data = home_delivery_data[home_delivery_data['franchisee_id'] != 1] sales = sales[sales['franchisee_id'] != 1] customer_returns = customer_returns[customer_returns['franchisee_id'] != 1] # Consumer Count HD_cons = home_delivery_data.groupby(['store_id', 'order_source'])[ ['patient_id']].nunique().reset_index().rename(columns={ "patient_id": "no_of_HD_consumers"}) HD_cons = pd.merge(left=HD_cons, right=stores, how='left', on=['store_id']) HD_cons_count = pd.pivot_table(HD_cons, values='no_of_HD_consumers', index='order_source', columns=['store_name']).reset_index() HD_cons_count['tag_flag'] = 'no_of_HD_consumers' # Deliverd count home_delivery_data['key'] = home_delivery_data['patient_id'].astype(str) + '-' + home_delivery_data[ 'order_number'].astype(str) + '-' + home_delivery_data['bill_id'].astype(str) if mis_tag == 'breakup': home_delivery_data['order_source2'] = np.where(home_delivery_data.order_source_pso.isin(['zeno']), "ecomm", "store") elif mis_tag == 'unified': home_delivery_data['order_source2'] = 'all' HD_count = home_delivery_data.groupby(['store_id', 'order_source2']).agg( {'key': pd.Series.nunique}).reset_index() HD_count = pd.merge(left=HD_count, right=stores, how='left', on=['store_id']) HD_count.rename(columns={'key': 'count_of_HD_delivered'}, inplace=True) HD_count_delivered = pd.pivot_table(HD_count, values='count_of_HD_delivered', index='order_source2', columns=['store_name']).reset_index() HD_count_delivered.rename(columns={'order_source2': 'order_source'}, inplace =True) HD_count_delivered['tag_flag'] = 'count_of_HD_delivered' # HD sales if fofo_tag == 'no': sales = sales[['store_id', 'order_source', 'bill_id', 'rate', 'quantity', 'type1']] customer_returns = customer_returns[ ['store_id', 'order_source', 'bill_id', 'rate', 'returned_quantity', 'type1']] elif fofo_tag=='yes': sales = sales[['store_id', 'order_source', 'fofo_distributor', 'bill_id', 'rate', 'quantity', 'type1']] customer_returns = customer_returns[ ['store_id', 'order_source', 'fofo_distributor', 'bill_id', 'rate', 'returned_quantity', 'type1']] customer_returns['returned_quantity'] = customer_returns['returned_quantity'] * (-1) customer_returns.rename(columns={'returned_quantity': 'quantity'}, inplace=True) sales = pd.concat([sales, customer_returns], sort=True) sales['quantity'] = sales['quantity'].astype(float) sales['rate'] = sales['rate'].astype(float) sales['value'] = sales['rate'] * sales['quantity'] HD_bills = tuple(map(int, list(delivery_bill_ids[~delivery_bill_ids['bill_id'].isnull()]['bill_id'].unique()))) HD_sales = sales[sales['bill_id'].isin(HD_bills)] if fofo_tag=='no': HD_sales_by_type = HD_sales.groupby(['store_id', 'type1', 'order_source'])[["value"]].sum().reset_index() elif fofo_tag == 'yes': HD_sales_by_type = HD_sales.groupby(['store_id', 'type1', 'order_source', 'fofo_distributor'])[["value"]].sum().reset_index() HD_sales_by_type = pd.merge(left=HD_sales_by_type, right=stores, how='left', on=['store_id']) HD_sales_by_type['value'] = HD_sales_by_type['value'].astype(float) if fofo_tag == 'no': HD_sales = pd.pivot_table(HD_sales_by_type, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) elif fofo_tag == 'yes': HD_sales = pd.pivot_table(HD_sales_by_type, values='value', index=['type1', 'order_source', 'fofo_distributor'], columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) HD_sales.rename(columns={'index': 'tag_flag'}, inplace=True) home_delivery = pd.concat([HD_cons_count, HD_count_delivered, HD_sales], sort=True) home_delivery.loc[home_delivery['tag_flag'] == 'ethical', 'tag_flag'] = 'HD ethical sale' home_delivery.loc[home_delivery['tag_flag'] == 'generic', 'tag_flag'] = 'HD generic sale' home_delivery.loc[home_delivery['tag_flag'] == 'others', 'tag_flag'] = 'HD others sale' if mis_tag == 'breakup': home_delivery.loc[home_delivery['tag_flag'] == 'GOODAID', 'tag_flag'] = 'GOODAID sale' return home_delivery def home_delivery_fofo_consumers(self, Workcell_home_delivery_data_fofo, Other_home_delivery_data_fofo, Stores, mis_tag, fofo_tag = 'yes'): workcell_home_delivery_data_fofo = Workcell_home_delivery_data_fofo.copy(deep=True) other_home_delivery_data_fofo = Other_home_delivery_data_fofo.copy(deep=True) stores = Stores.copy(deep=True) # workcell Consumer Count workcell_HD_cons = workcell_home_delivery_data_fofo.groupby(['store_id', 'order_source'])[ ['patient_id']].nunique().reset_index().rename(columns={ "patient_id": "no_of_HD_consumers"}) workcell_HD_cons = pd.merge(left=workcell_HD_cons, right=stores, how='left', on=['store_id']) workcell_HD_cons_count = pd.pivot_table(workcell_HD_cons, values='no_of_HD_consumers', index='order_source', columns=['store_name']).reset_index() workcell_HD_cons_count['tag_flag'] = 'no_of_HD_consumers' workcell_HD_cons_count['fofo_distributor'] = 'workcell' # other Consumer Count other_HD_cons = other_home_delivery_data_fofo.groupby(['store_id', 'order_source'])[ ['patient_id']].nunique().reset_index().rename(columns={ "patient_id": "no_of_HD_consumers"}) other_HD_cons = pd.merge(left=other_HD_cons, right=stores, how='left', on=['store_id']) other_HD_cons_count = pd.pivot_table(other_HD_cons, values='no_of_HD_consumers', index='order_source', columns=['store_name']).reset_index() other_HD_cons_count['tag_flag'] = 'no_of_HD_consumers' other_HD_cons_count['fofo_distributor'] = 'other' # Deliverd count workcell_home_delivery_data_fofo['key'] = workcell_home_delivery_data_fofo['patient_id'].astype(str) + '-' + workcell_home_delivery_data_fofo[ 'order_number'].astype(str) + '-' + workcell_home_delivery_data_fofo['bill_id'].astype(str) other_home_delivery_data_fofo['key'] = other_home_delivery_data_fofo['patient_id'].astype(str) + '-' + other_home_delivery_data_fofo[ 'order_number'].astype(str) + '-' + other_home_delivery_data_fofo['bill_id'].astype(str) if mis_tag == 'breakup': workcell_home_delivery_data_fofo['order_source2'] = np.where( workcell_home_delivery_data_fofo.order_source_pso.isin(['zeno']),"ecomm", "store") other_home_delivery_data_fofo['order_source2'] = np.where( other_home_delivery_data_fofo.order_source_pso.isin(['zeno']), "ecomm", "store") elif mis_tag == 'unified': workcell_home_delivery_data_fofo['order_source2'] = 'all' other_home_delivery_data_fofo['order_source2'] = 'all' workcell_HD_count = workcell_home_delivery_data_fofo.groupby(['store_id', 'order_source2']).agg( {'key': pd.Series.nunique}).reset_index() other_HD_count = other_home_delivery_data_fofo.groupby(['store_id', 'order_source2']).agg( {'key': pd.Series.nunique}).reset_index() workcell_HD_count = pd.merge(left=workcell_HD_count, right=stores, how='left', on=['store_id']) workcell_HD_count.rename(columns={'key': 'count_of_HD_delivered'}, inplace=True) workcell_HD_count_delivered = pd.pivot_table(workcell_HD_count, values='count_of_HD_delivered', index='order_source2', columns=['store_name']).reset_index() workcell_HD_count_delivered.rename(columns={'order_source2': 'order_source'}, inplace=True) workcell_HD_count_delivered['tag_flag'] = 'count_of_HD_delivered' other_HD_count = pd.merge(left=other_HD_count, right=stores, how='left', on=['store_id']) other_HD_count.rename(columns={'key': 'count_of_HD_delivered'}, inplace=True) other_HD_count_delivered = pd.pivot_table(other_HD_count, values='count_of_HD_delivered', index='order_source2', columns=['store_name']).reset_index() other_HD_count_delivered.rename(columns={'order_source2': 'order_source'}, inplace=True) other_HD_count_delivered['tag_flag'] = 'count_of_HD_delivered' workcell_HD_count_delivered['fofo_distributor'] = 'workcell' other_HD_count_delivered['fofo_distributor'] = 'other' home_delivery = pd.concat([workcell_HD_cons_count,other_HD_cons_count,workcell_HD_count_delivered,other_HD_count_delivered], sort=True) return home_delivery def purchase_from_worckell(self, Purchase_from_workcell_data, Stores,mis_tag,fofo_tag = 'no', launch_flag = 'normal'): purchase_from_wc_data = Purchase_from_workcell_data.copy(deep=True) stores = Stores.copy(deep=True) if launch_flag == 'launch_stock': purchase_from_wc_data = purchase_from_wc_data[purchase_from_wc_data['launch_flag'] == 'launch_stock'] purchase_from_wc_data['zp_received_tax'] = np.vectorize(self.taxable_value_vat_based_2)( purchase_from_wc_data['zp_received_net_value'], purchase_from_wc_data['zp_vat']) purchase_from_wc_data['wc_purchased_tax'] = np.vectorize(self.taxable_value_vat_based_2)( purchase_from_wc_data['wc_purchase_net_value'], purchase_from_wc_data['wc_vat']) if fofo_tag=='no': df_yy1 = purchase_from_wc_data.groupby(['store_id', 'type1', 'category'], as_index=False).agg({ 'zp_received_net_value': ['sum'], 'zp_received_tax': ['sum'], 'wc_purchase_net_value': ['sum'], 'wc_purchased_tax': ['sum']}).reset_index(drop=True) df_yy1.columns = ["_".join(x) for x in df_yy1.columns.ravel()] df_yy1.columns = df_yy1.columns.str.rstrip('_x') df_yy2 = df_yy1.groupby(['store_id', 'type1'])[["zp_received_tax_sum"]].sum().reset_index() df_yy2 = pd.merge(left=df_yy2, right=stores, how='left', on=['store_id']) purchase_from_wc = pd.pivot_table(df_yy2, values='zp_received_tax_sum', index='type1', columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) purchase_from_wc.rename(columns={'index': 'tag_flag'}, inplace=True) if launch_flag == 'normal': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'purchase_from_wc_ethical' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'purchase_from_wc_generic' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'purchase_from_wc_others' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'purchase_from_wc_GOODAID' elif launch_flag == 'launch_stock': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_ethical' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_generic' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_others' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_GOODAID' return purchase_from_wc elif fofo_tag =='yes': purchase_from_wc_data = purchase_from_wc_data[purchase_from_wc_data['franchisee_id']!=1] df_yy1 = purchase_from_wc_data.groupby(['store_id', 'type1','fofo_distributor', 'category'], as_index=False).agg({ 'zp_received_net_value': ['sum'], 'zp_received_tax': ['sum'], 'wc_purchase_net_value': ['sum'], 'wc_purchased_tax': ['sum']}).reset_index(drop=True) df_yy1.columns = ["_".join(x) for x in df_yy1.columns.ravel()] df_yy1.columns = df_yy1.columns.str.rstrip('_x') df_yy2 = df_yy1.groupby(['store_id', 'type1','fofo_distributor'])[["zp_received_tax_sum"]].sum().reset_index() df_yy2 = pd.merge(left=df_yy2, right=stores, how='left', on=['store_id']) purchase_from_wc = pd.pivot_table(df_yy2, values='zp_received_tax_sum', index=['type1','fofo_distributor'], columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) purchase_from_wc.rename(columns={'index': 'tag_flag'}, inplace=True) if launch_flag == 'normal': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'purchase_from_wc_ethical' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'purchase_from_wc_generic' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'purchase_from_wc_others' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'purchase_from_wc_GOODAID' elif launch_flag == 'launch_stock': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_ethical' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_generic' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_others' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_GOODAID' return purchase_from_wc def purchase_from_worckell_including_tax(self, Purchase_from_workcell_data, Stores,mis_tag,fofo_tag = 'no', launch_flag = 'normal'): purchase_from_wc_data = Purchase_from_workcell_data.copy(deep=True) stores = Stores.copy(deep=True) if launch_flag == 'launch_stock': purchase_from_wc_data = purchase_from_wc_data[purchase_from_wc_data['launch_flag'] == 'launch_stock'] purchase_from_wc_data['zp_received_tax'] = np.vectorize(self.taxable_value_vat_based_2)( purchase_from_wc_data['zp_received_net_value'], purchase_from_wc_data['zp_vat']) purchase_from_wc_data['wc_purchased_tax'] = np.vectorize(self.taxable_value_vat_based_2)( purchase_from_wc_data['wc_purchase_net_value'], purchase_from_wc_data['wc_vat']) if fofo_tag=='no': df_yy1 = purchase_from_wc_data.groupby(['store_id', 'type1', 'category'], as_index=False).agg({ 'zp_received_net_value': ['sum'], 'zp_received_tax': ['sum'], 'wc_purchase_net_value': ['sum'], 'wc_purchased_tax': ['sum']}).reset_index(drop=True) df_yy1.columns = ["_".join(x) for x in df_yy1.columns.ravel()] df_yy1.columns = df_yy1.columns.str.rstrip('_x') df_yy2 = df_yy1.groupby(['store_id', 'type1'])[["zp_received_net_value_sum"]].sum().reset_index() df_yy2 = pd.merge(left=df_yy2, right=stores, how='left', on=['store_id']) purchase_from_wc = pd.pivot_table(df_yy2, values='zp_received_net_value_sum', index='type1', columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) purchase_from_wc.rename(columns={'index': 'tag_flag'}, inplace=True) if launch_flag == 'normal': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'purchase_from_wc_ethical_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'purchase_from_wc_generic_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'purchase_from_wc_others_inc_tax' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'purchase_from_wc_GOODAID_inc_tax' elif launch_flag == 'launch_stock': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_ethical_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_generic_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_others_inc_tax' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_GOODAID_inc_tax' return purchase_from_wc elif fofo_tag =='yes': purchase_from_wc_data = purchase_from_wc_data[purchase_from_wc_data['franchisee_id']!=1] df_yy1 = purchase_from_wc_data.groupby(['store_id', 'type1','fofo_distributor', 'category'], as_index=False).agg({ 'zp_received_net_value': ['sum'], 'zp_received_tax': ['sum'], 'wc_purchase_net_value': ['sum'], 'wc_purchased_tax': ['sum']}).reset_index(drop=True) df_yy1.columns = ["_".join(x) for x in df_yy1.columns.ravel()] df_yy1.columns = df_yy1.columns.str.rstrip('_x') df_yy2 = df_yy1.groupby(['store_id', 'type1','fofo_distributor'])[["zp_received_net_value_sum"]].sum().reset_index() df_yy2 = pd.merge(left=df_yy2, right=stores, how='left', on=['store_id']) purchase_from_wc = pd.pivot_table(df_yy2, values='zp_received_net_value_sum', index=['type1','fofo_distributor'], columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) purchase_from_wc.rename(columns={'index': 'tag_flag'}, inplace=True) if launch_flag == 'normal': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'purchase_from_wc_ethical_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'purchase_from_wc_generic_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'purchase_from_wc_others_inc_tax' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'purchase_from_wc_GOODAID_inc_tax' elif launch_flag == 'launch_stock': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_ethical_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'generic', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_generic_inc_tax' purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'others', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_others_inc_tax' if mis_tag == 'breakup': purchase_from_wc.loc[purchase_from_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'launch_stock_in_purchase_from_wc_GOODAID_inc_tax' return purchase_from_wc def cogs_for_wc(self, Purchase_from_workcell_data, Stores, mis_tag,fofo_tag = 'no'): purchase_from_wc_data = Purchase_from_workcell_data.copy(deep=True) stores = Stores.copy(deep=True) purchase_from_wc_data['zp_received_tax'] = np.vectorize(self.taxable_value_vat_based_2)( purchase_from_wc_data['zp_received_net_value'], purchase_from_wc_data['zp_vat']) purchase_from_wc_data['wc_purchased_tax'] = np.vectorize(self.taxable_value_vat_based_2)( purchase_from_wc_data['wc_purchase_net_value'], purchase_from_wc_data['wc_vat']) if fofo_tag == 'no': df_yy1 = purchase_from_wc_data.groupby(['store_id', 'type1', 'category'], as_index=False).agg({ 'zp_received_net_value': ['sum'], 'zp_received_tax': ['sum'], 'wc_purchase_net_value': ['sum'], 'wc_purchased_tax': ['sum']}).reset_index(drop=True) df_yy1.columns = ["_".join(x) for x in df_yy1.columns.ravel()] df_yy1.columns = df_yy1.columns.str.rstrip('_x') df_yy2 = df_yy1.groupby(['store_id', 'type1'])[["wc_purchased_tax_sum"]].sum().reset_index() df_yy2 = pd.merge(left=df_yy2, right=stores, how='left', on=['store_id']) cogs_for_wc = pd.pivot_table(df_yy2, values='wc_purchased_tax_sum', index='type1', columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) cogs_for_wc.rename(columns={'index': 'tag_flag'}, inplace=True) cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'cogs_for_wc_ethical' cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'generic', 'tag_flag'] = 'cogs_for_wc_generic' cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'others', 'tag_flag'] = 'cogs_for_wc_others' if mis_tag == 'breakup': cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'cogs_for_wc_GOODAID' return cogs_for_wc elif fofo_tag == 'yes': purchase_from_wc_data = purchase_from_wc_data[purchase_from_wc_data['franchisee_id']!=1] df_yy1 = purchase_from_wc_data.groupby(['store_id', 'type1','fofo_distributor', 'category'], as_index=False).agg({ 'zp_received_net_value': ['sum'], 'zp_received_tax': ['sum'], 'wc_purchase_net_value': ['sum'], 'wc_purchased_tax': ['sum']}).reset_index(drop=True) df_yy1.columns = ["_".join(x) for x in df_yy1.columns.ravel()] df_yy1.columns = df_yy1.columns.str.rstrip('_x') df_yy2 = df_yy1.groupby(['store_id','fofo_distributor', 'type1'])[["wc_purchased_tax_sum"]].sum().reset_index() df_yy2 = pd.merge(left=df_yy2, right=stores, how='left', on=['store_id']) cogs_for_wc = pd.pivot_table(df_yy2, values='wc_purchased_tax_sum', index=['type1','fofo_distributor'], columns=['store_name']).reset_index().rename(columns={ "type1": "index"}) cogs_for_wc.rename(columns={'index': 'tag_flag'}, inplace=True) cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'ethical', 'tag_flag'] = 'cogs_for_wc_ethical' cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'generic', 'tag_flag'] = 'cogs_for_wc_generic' cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'others', 'tag_flag'] = 'cogs_for_wc_others' if mis_tag == 'breakup': cogs_for_wc.loc[cogs_for_wc['tag_flag'] == 'GOODAID', 'tag_flag'] = 'cogs_for_wc_GOODAID' return cogs_for_wc def return_from_zippin(self, Zp_to_wc_return, mis_tag,fofo_tag = 'no'): zp_to_wc_return = Zp_to_wc_return.copy(deep=True) zp_to_wc_return['return_cogs_taxable'] = np.vectorize(self.taxable_value_vat_based_2)( zp_to_wc_return['cogs'], zp_to_wc_return['vat']) if fofo_tag == 'no': zp_to_wc_return1 = zp_to_wc_return.groupby(['cost_centre', 'type1'])[["taxable_value"]].sum().reset_index() zp_to_wc_return1['taxable_value'] = zp_to_wc_return1['taxable_value'].astype(float) return_from_zippin = pd.pivot_table(zp_to_wc_return1, values='taxable_value', index='type1', columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_zippin.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_zippin.loc[return_from_zippin['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_zp_ethical' return_from_zippin.loc[return_from_zippin['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_zp_generic' return_from_zippin.loc[return_from_zippin['tag_flag'] == 'others', 'tag_flag'] = 'return_from_zp_others' if mis_tag == 'breakup': return_from_zippin.loc[return_from_zippin['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_zp_GOODAID' # Return Cogs Taxable zp_to_wc_return2 = zp_to_wc_return.groupby(['cost_centre', 'type1'])[["return_cogs_taxable"]].sum().reset_index() zp_to_wc_return2['return_cogs_taxable'] = zp_to_wc_return2['return_cogs_taxable'].astype(float) return_from_zippin_Cogs_taxable = pd.pivot_table(zp_to_wc_return2, values='return_cogs_taxable', index='type1', columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_zippin_Cogs_taxable.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_zp_COGS_taxable_ethical' return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_zp_COGS_taxable_generic' return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'others', 'tag_flag'] = 'return_from_zp_COGS_taxable_others' if mis_tag == 'breakup': return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_zp_COGS_taxable_GOODAID' # Return net value zp_to_wc_return3 = zp_to_wc_return.groupby(['cost_centre', 'type1'])[["net_value"]].sum().reset_index() zp_to_wc_return3['net_value'] = zp_to_wc_return3['net_value'].astype(float) return_from_zippin_net_value_inc_tax = pd.pivot_table(zp_to_wc_return3, values='net_value', index='type1', columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_zippin_net_value_inc_tax.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_zp_inc_tax_ethical' return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_zp_inc_tax_generic' return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'others', 'tag_flag'] = 'return_from_zp_inc_tax_others' if mis_tag == 'breakup': return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_zp_inc_tax_GOODAID' elif fofo_tag == 'yes': zp_to_wc_return = zp_to_wc_return[zp_to_wc_return['franchisee_id']!=1] zp_to_wc_return1 = zp_to_wc_return.groupby(['cost_centre', 'type1','fofo_distributor'])[["taxable_value"]].sum().reset_index() zp_to_wc_return1['taxable_value'] = zp_to_wc_return1['taxable_value'].astype(float) return_from_zippin = pd.pivot_table(zp_to_wc_return1, values='taxable_value', index=['type1','fofo_distributor'], columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_zippin.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_zippin.loc[return_from_zippin['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_zp_ethical' return_from_zippin.loc[return_from_zippin['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_zp_generic' return_from_zippin.loc[return_from_zippin['tag_flag'] == 'others', 'tag_flag'] = 'return_from_zp_others' if mis_tag == 'breakup': return_from_zippin.loc[return_from_zippin['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_zp_GOODAID' # Return Cogs Taxable zp_to_wc_return2 = zp_to_wc_return.groupby(['cost_centre', 'type1', 'fofo_distributor'])[ ["return_cogs_taxable"]].sum().reset_index() zp_to_wc_return2['return_cogs_taxable'] = zp_to_wc_return2['return_cogs_taxable'].astype(float) return_from_zippin_Cogs_taxable = pd.pivot_table(zp_to_wc_return2, values='return_cogs_taxable', index=['type1', 'fofo_distributor'], columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_zippin_Cogs_taxable.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_zp_COGS_taxable_ethical' return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_zp_COGS_taxable_generic' return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'others', 'tag_flag'] = 'return_from_zp_COGS_taxable_others' if mis_tag == 'breakup': return_from_zippin_Cogs_taxable.loc[return_from_zippin_Cogs_taxable['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_zp_COGS_taxable_GOODAID' # Return net value zp_to_wc_return3 = zp_to_wc_return.groupby(['cost_centre', 'type1', 'fofo_distributor'])[["net_value"]].sum().reset_index() zp_to_wc_return3['net_value'] = zp_to_wc_return3['net_value'].astype(float) return_from_zippin_net_value_inc_tax = pd.pivot_table(zp_to_wc_return3, values='net_value', index=['type1', 'fofo_distributor'], columns=['cost_centre']).reset_index().rename( columns={ "type1": "index"}) return_from_zippin_net_value_inc_tax.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_zp_inc_tax_ethical' return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_zp_inc_tax_generic' return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'others', 'tag_flag'] = 'return_from_zp_inc_tax_others' if mis_tag == 'breakup': return_from_zippin_net_value_inc_tax.loc[return_from_zippin_net_value_inc_tax['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_zp_inc_tax_GOODAID' return_from_zippin = pd.concat([return_from_zippin,return_from_zippin_Cogs_taxable,return_from_zippin_net_value_inc_tax],sort=True) return return_from_zippin def return_from_workcell(self, Wc_return, mis_tag, fofo_tag = 'no'): wc_return = Wc_return.copy(deep=True) wc_return['return_cogs_taxable'] = np.vectorize(self.taxable_value_vat_based_2)( wc_return['cogs'], wc_return['vat']) if fofo_tag == 'no': wc_return1 = wc_return.groupby(['cost_centre', 'type1'])[["taxable_value"]].sum().reset_index() wc_return1['taxable_value'] = wc_return1['taxable_value'].astype(float) return_from_workcell = pd.pivot_table(wc_return1, values='taxable_value', index='type1', columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_workcell.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_workcell.loc[return_from_workcell['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_wc_ethical' return_from_workcell.loc[return_from_workcell['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_wc_generic' return_from_workcell.loc[return_from_workcell['tag_flag'] == 'others', 'tag_flag'] = 'return_from_wc_others' return_from_workcell.loc[return_from_workcell['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_wc_GOODAID' # Return Cogs Taxable wc_return2 = wc_return.groupby(['cost_centre', 'type1'])[["return_cogs_taxable"]].sum().reset_index() wc_return2['return_cogs_taxable'] = wc_return2['return_cogs_taxable'].astype(float) return_from_workcell_Cogs_taxable = pd.pivot_table(wc_return2, values='return_cogs_taxable', index='type1', columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_workcell_Cogs_taxable.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_wc_COGS_taxable_ethical' return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_wc_COGS_taxable_generic' return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'others', 'tag_flag'] = 'return_from_wc_COGS_taxable_others' return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_wc_COGS_taxable_GOODAID' elif fofo_tag == 'yes': wc_return = wc_return[wc_return['franchisee_id']!=1] wc_return1 = wc_return.groupby(['cost_centre', 'type1','fofo_distributor'])[["taxable_value"]].sum().reset_index() wc_return1['taxable_value'] = wc_return1['taxable_value'].astype(float) return_from_workcell = pd.pivot_table(wc_return1, values='taxable_value', index=['type1','fofo_distributor'], columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_workcell.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_workcell.loc[return_from_workcell['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_wc_ethical' return_from_workcell.loc[return_from_workcell['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_wc_generic' return_from_workcell.loc[return_from_workcell['tag_flag'] == 'others', 'tag_flag'] = 'return_from_wc_others' return_from_workcell.loc[return_from_workcell['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_wc_GOODAID' # Return COGS taxable wc_return2 = wc_return.groupby(['cost_centre', 'type1', 'fofo_distributor'])[["return_cogs_taxable"]].sum().reset_index() wc_return2['return_cogs_taxable'] = wc_return2['return_cogs_taxable'].astype(float) return_from_workcell_Cogs_taxable = pd.pivot_table(wc_return2, values='return_cogs_taxable', index=['type1', 'fofo_distributor'], columns=['cost_centre']).reset_index().rename(columns={ "type1": "index"}) return_from_workcell_Cogs_taxable.rename(columns={'index': 'tag_flag'}, inplace=True) return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'ethical', 'tag_flag'] = 'return_from_wc_COGS_taxable_ethical' return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'generic', 'tag_flag'] = 'return_from_wc_COGS_taxable_generic' return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'others', 'tag_flag'] = 'return_from_wc_COGS_taxable_others' return_from_workcell_Cogs_taxable.loc[return_from_workcell_Cogs_taxable['tag_flag'] == 'GOODAID', 'tag_flag'] = 'return_from_wc_COGS_taxable_GOODAID' # return_from_workcell = pd.concat([return_from_workcell,return_from_workcell_Cogs_taxable],sort=True) return return_from_workcell def total_sku_instock(self, Inventory, mis_tag,fofo_tag = 'no'): inventory = Inventory.copy(deep=True) if fofo_tag == 'no': total_sku_instock = inventory.groupby(['type1'])[["drug_id"]].nunique().reset_index() elif fofo_tag == 'yes': inventory = inventory[inventory['franchisee_id']!=1] total_sku_instock = inventory.groupby(['type1','fofo_distributor'])[["drug_id"]].nunique().reset_index() total_sku_instock.rename(columns={'type1': 'tag_flag'}, inplace=True) total_sku_instock.loc[total_sku_instock['tag_flag'] == 'ethical', 'tag_flag'] = 'total_sku_instock_ethical' total_sku_instock.loc[total_sku_instock['tag_flag'] == 'generic', 'tag_flag'] = 'total_sku_instock_generic' total_sku_instock.loc[total_sku_instock['tag_flag'] == 'others', 'tag_flag'] = 'total_sku_instock_others' if mis_tag == 'breakup': total_sku_instock.loc[total_sku_instock['tag_flag'] == 'GOODAID', 'tag_flag'] = 'total_sku_instock_GOODAID' total_sku_instock.rename(columns={'drug_id': 'count'}, inplace=True) return total_sku_instock def chronic_acute_qty(self, Inventory, Stores): total_SKU = Inventory.copy(deep=True) stores = Stores.copy(deep=True) total_SKU['value'] = total_SKU['quantity'] * total_SKU['final_ptr'] total_SKU_amount_qty = total_SKU.groupby(['store_id', 'type1', 'category'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'quantity': ['sum'], 'value': ['sum']}).reset_index(drop=True) total_SKU_amount_qty.columns = ["_".join(x) for x in total_SKU_amount_qty.columns.ravel()] total_SKU_amount_qty = pd.merge(left=total_SKU_amount_qty, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) total_SKU_amount_qty2 = total_SKU_amount_qty[total_SKU_amount_qty['category_'] == 'chronic'] chronic_qty = pd.pivot_table(total_SKU_amount_qty2, values='quantity_sum', index='type1_', columns=['store_name']).reset_index() chronic_qty['tag_flag'] = 'chronic_qty' total_SKU_amount_qty3 = total_SKU_amount_qty[total_SKU_amount_qty['category_'] == 'acute'] acute_qty = pd.pivot_table(total_SKU_amount_qty3, values='quantity_sum', index='type1_', columns=['store_name']).reset_index() acute_qty['tag_flag'] = 'acute_qty' chronic_acute_qty = pd.concat([chronic_qty, acute_qty], sort=True) chronic_acute_qty.rename(columns={'type1_': 'type1'}, inplace=True) return chronic_acute_qty def lp_chronic_acute(self, Local_purchase_data, Sales, fofo_tag = 'no'): local_purchase_data = Local_purchase_data.copy(deep=True) sales = Sales.copy(deep=True) if fofo_tag == 'yes': local_purchase_data = local_purchase_data[local_purchase_data['franchisee_id']!=1] sales = sales[sales['franchisee_id']!=1] sold_local_purchase = sales.merge(local_purchase_data, on='inventory_id', how='inner') sold_local_purchase['sold_quantity'] = sold_local_purchase['quantity'] sold_local_purchase['revenue'] = sold_local_purchase['quantity'] * sold_local_purchase['rate'] df1 = sold_local_purchase.groupby(['inventory_id'], as_index=False).agg({ 'sold_quantity': ['sum'], 'revenue': ['sum']}).reset_index(drop=True) df1.columns = ["_".join(x) for x in df1.columns.ravel()] df1.columns = df1.columns.str.rstrip('_') df2 = pd.merge(left=local_purchase_data, right=df1, how='left', on=['inventory_id']) if fofo_tag == 'no': lp1 = df2.groupby(['store_id', 'store_name', 'category', 'type1'])[['net_value']].sum().reset_index() elif fofo_tag == 'yes': lp1 = df2.groupby(['store_id', 'store_name', 'category', 'type1', 'fofo_distributor'])[['net_value']].sum().reset_index() lp_chronic = lp1[lp1['category'] == 'chronic'] lp_chronic['net_value'] = lp_chronic['net_value'].astype(float) if fofo_tag == 'no': lp_chronic = pd.pivot_table(lp_chronic, values='net_value', index='type1', columns=['store_name']).reset_index() lp_chronic['tag_flag'] = 'local_purchase_chronic' lp_acute = lp1[lp1['category'] == 'acute'] lp_acute['net_value'] = lp_acute['net_value'].astype(float) lp_acute = pd.pivot_table(lp_acute, values='net_value', index='type1', columns=['store_name']).reset_index() lp_acute['tag_flag'] = 'local_purchase_acute' elif fofo_tag == 'yes': lp_chronic = pd.pivot_table(lp_chronic, values='net_value', index=['type1','fofo_distributor'], columns=['store_name']).reset_index() lp_chronic['tag_flag'] = 'local_purchase_chronic' lp_acute = lp1[lp1['category'] == 'acute'] lp_acute['net_value'] = lp_acute['net_value'].astype(float) lp_acute = pd.pivot_table(lp_acute, values='net_value', index=['type1','fofo_distributor'], columns=['store_name']).reset_index() lp_acute['tag_flag'] = 'local_purchase_acute' lp_chronic_acute = pd.concat([lp_chronic, lp_acute], sort=True) return lp_chronic_acute def repeat_consumer_chronic_acute(self, Sales, All_cons_initial_bill_date, Stores,choose_year,choose_month): sales = Sales.copy(deep=True) df1 = All_cons_initial_bill_date.copy(deep=True) stores = Stores.copy(deep=True) df1['year'] = df1['created_at'].dt.year df1["month"] = df1['created_at'].dt.month df1 = df1[(df1['year'] == int(choose_year)) & (df1['month'] == int(choose_month))] sales['flag'] = np.where(sales['category'] == "chronic", 1, 0) df2 = sales.groupby(['store_id', 'order_source', 'patient_id'])[['flag']].sum().reset_index() df2['check'] = np.where(df2['flag'] > 0, "chronic", "acute") df5 = pd.merge(left=df2, right=df1, how='left', on=['store_id', 'patient_id']) df6 = df5[df5['year'].isnull()] df6['repeat'] = "yes" zx = pd.merge(left=sales, right=df6[['store_id', 'order_source', 'patient_id', 'repeat']], how='left', on=['store_id', 'order_source', 'patient_id']) zx1 = zx[zx['repeat'] == 'yes'] zx1['value'] = zx1['rate'] * zx1['quantity'] zx2 = zx1.groupby(['store_id', 'order_source', 'category', 'type1'])["value"].sum().reset_index() zx2 = pd.merge(left=zx2, right=stores, how='left', on=['store_id']) repeat_chronic_sale = zx2[zx2['category'] == 'chronic'] repeat_chronic_sale['value'] = repeat_chronic_sale['value'].astype(float) repeat_chronic_sale = pd.pivot_table(repeat_chronic_sale, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index() repeat_chronic_sale['tag_flag'] = 'repeat_consumer_chronic_sale' repeat_acute_sale = zx2[zx2['category'] == 'acute'] repeat_acute_sale['value'] = repeat_acute_sale['value'].astype(float) repeat_acute_sale = pd.pivot_table(repeat_acute_sale, values='value', index=['type1', 'order_source'], columns=['store_name']).reset_index() repeat_acute_sale['tag_flag'] = 'repeat_consumer_acute_sale' repeat_consumer_chronic_acute = pd.concat([repeat_chronic_sale, repeat_acute_sale],sort=True) return repeat_consumer_chronic_acute def inventory_6to12months(self, Inventory, Stores, mis_tag = 'breakup'): df_dd = Inventory.copy(deep=True) stores = Stores.copy(deep=True) df_dd['value'] = df_dd['quantity'] * df_dd['final_ptr'] df_dd['days'] = (pd.to_datetime(self.analysis_end_time) - df_dd['created_at']).dt.days conditions = [ (df_dd['days'] >= 180) & (df_dd['days'] <= 365), (df_dd['days'] >= 365)] choices = ['6_12', '12+'] df_dd['age_bracket'] = np.select(conditions, choices) df_dd['type1'] = np.where(df_dd['type'].isin(['ethical', 'high-value-ethical']), "ethical", df_dd['type']) df_dd['type1'] = np.where(df_dd['type'].isin(['generic', 'high-value-generic']), "generic", df_dd['type']) df_dd['type1'] = np.where(~df_dd['type1'].isin(['ethical', 'generic']), "others", df_dd['type1']) if mis_tag == 'breakup': df_dd['type1'] = np.where(df_dd['company'].isin(['GOODAID']), "GOODAID", df_dd['type1']) df_dd['taxable'] = (df_dd['quantity'] * df_dd['final_ptr']) / (1 + ((df_dd['vat']) / 100)) df_ageing = df_dd.groupby(['store_id', 'category', 'type1', 'age_bracket'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'value': ['sum'], 'taxable': ['sum']}).reset_index(drop=True) df_ageing.columns = ["_".join(x) for x in df_ageing.columns.ravel()] df_ageing1 = df_ageing[df_ageing['age_bracket_'].isin(['6_12', '12+'])] df_ageing1 = pd.merge(left=df_ageing1, right=stores, how='left', left_on=['store_id_'], right_on=['store_id']) df_ageing1['taxable_sum'] = df_ageing1['taxable_sum'].astype(float) inventory_6to12months = pd.pivot_table(df_ageing1, values='taxable_sum', index=['age_bracket_', 'category_', 'type1_'], columns=['store_name']).reset_index() inventory_6to12months.rename(columns={'type1_': 'type1'}, inplace=True) inventory_6to12months.rename(columns={'category_': 'category'}, inplace=True) inventory_6to12months['tag_flag'] = 'inventory_6to12months' return inventory_6to12months def zippin_pl_cogs(self, Sales, Customer_returns, Stores,fofo_tag = 'no'): df_aa = Sales.copy(deep=True) df_bb = Customer_returns.copy(deep=True) stores = Stores.copy(deep=True) df_aa['GMV'] = df_aa['quantity'] * df_aa['mrp'] df_aa['GMV_tax'] = np.vectorize(self.taxable_value)(df_aa['quantity'], df_aa['mrp'], df_aa['cgst_rate'], df_aa['sgst_rate'], df_aa['igst_rate']) df_aa['REVENUE'] = df_aa['quantity'] * df_aa['rate'] df_aa['REVENUE_tax'] = np.vectorize(self.taxable_value)(df_aa['quantity'], df_aa['rate'], df_aa['cgst_rate'], df_aa['sgst_rate'], df_aa['igst_rate']) df_aa['COGS'] = df_aa['quantity'] * df_aa['final_ptr'] df_aa['COGS_tax'] = np.vectorize(self.taxable_value)(df_aa['quantity'], df_aa['final_ptr'], df_aa['cgst_rate'], df_aa['sgst_rate'], df_aa['igst_rate']) # df_aa['TAX'] = (df_aa['quantity'] * df_aa['final_ptr']) / (1 + ((df_aa['cgst_rate'] + df_aa['sgst_rate']) / 100)) df_aa[['GMV', 'GMV_tax', 'REVENUE', 'REVENUE_tax', 'COGS', 'COGS_tax']] = df_aa[ ['GMV', 'GMV_tax', 'REVENUE', 'REVENUE_tax', 'COGS', 'COGS_tax']].astype(float) if fofo_tag == 'no': df_gross = df_aa.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'quantity': ['sum'], 'GMV': ['sum'], 'GMV_tax': ['sum'], 'REVENUE': ['sum'], 'REVENUE_tax': ['sum'], 'COGS': ['sum'], 'COGS_tax': ['sum'], 'bill_id': pd.Series.nunique # 'net_payable': ['mean'] }).reset_index(drop=True) df_gross.columns = ["_".join(x) for x in df_gross.columns.ravel()] df_gross.rename(columns={'store_id_': 'store_id', 'type1_': 'type1', 'category_': 'category', 'payment_method_': 'payment_method', 'order_source_': 'order_source'}, inplace=True) ABV_temp = df_aa.drop_duplicates(subset=['store_id', 'type1', 'category', 'payment_method', 'bill_id', 'net_payable', 'order_source']) ABV_temp['net_payable'] = ABV_temp['net_payable'].astype(float) ABV = ABV_temp.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source'])["net_payable"].mean().reset_index() df_gross_all = pd.merge(left=df_gross, right=ABV, how='left', on=['store_id', 'type1', 'category', 'payment_method', 'order_source']) df_bb['GMV'] = df_bb['returned_quantity'] * df_bb['mrp'] df_bb['GMV_tax'] = np.vectorize(self.taxable_value)(df_bb['returned_quantity'], df_bb['mrp'], df_bb['cgst_rate'], df_bb['sgst_rate'], df_bb['igst_rate']) df_bb['REVENUE'] = df_bb['returned_quantity'] * df_bb['rate'] df_bb['REVENUE_tax'] = np.vectorize(self.taxable_value)(df_bb['returned_quantity'], df_bb['rate'], df_bb['cgst_rate'], df_bb['sgst_rate'], df_bb['igst_rate']) df_bb['COGS'] = df_bb['returned_quantity'] * df_bb['final_ptr'] df_bb['COGS_tax'] = np.vectorize(self.taxable_value)(df_bb['returned_quantity'], df_bb['final_ptr'], df_bb['cgst_rate'], df_bb['sgst_rate'], df_bb['igst_rate']) # df_bb['TAX'] = (df_bb['returned_quantity'] * df_bb['final_ptr']) / (1 + ((df_bb['cgst_rate'] + df_bb['sgst_rate']) / 100)) df_bb[['GMV', 'GMV_tax', 'REVENUE', 'REVENUE_tax', 'COGS', 'COGS_tax']] = df_bb[ ['GMV', 'GMV_tax', 'REVENUE', 'REVENUE_tax', 'COGS', 'COGS_tax']].astype(float) df_returns = df_bb.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'returned_quantity': ['sum'], 'GMV': ['sum'], 'GMV_tax': ['sum'], 'REVENUE': ['sum'], 'REVENUE_tax': ['sum'], 'COGS': ['sum'], 'COGS_tax': ['sum']}).reset_index(drop=True) df_returns.columns = ["_".join(x) for x in df_returns.columns.ravel()] df_returns.rename(columns={'store_id_': 'store_id', 'type1_': 'type1', 'category_': 'category', 'payment_method_': 'payment_method', 'order_source_': 'order_source'}, inplace=True) df_gross_returns = pd.merge(left=df_gross_all, right=df_returns, how='outer', on=['store_id', 'type1', 'category', 'payment_method', 'order_source']) df_gross_returns.rename(columns={'store_id_': 'store_id', 'type1_': 'type', 'category_': 'category', 'drug_id_nunique_x': 'no_of_drugs_sales', 'GMV_sum_x': 'GMV_sales', 'GMV_tax_sum_x': 'GMV_sales_tax', 'REVENUE_sum_x': 'REVENUE_sales', 'REVENUE_tax_sum_x': 'REVENUE_sales_tax', 'COGS_sum_x': 'COGS_sales', 'COGS_tax_sum_x': 'COGS_sales_tax', 'drug_id_nunique_y': 'no_of_drugs_returns', 'GMV_sum_y': 'GMV_returns', 'GMV_tax_sum_y': 'GMV_returns_tax', 'REVENUE_sum_y': 'REVENUE_returns', 'REVENUE_tax_sum_y': 'REVENUE_returns_tax', 'COGS_sum_y': 'COGS_returns', 'COGS_tax_sum_y': 'COGS_returns_tax'}, inplace=True) df_gross_returns.fillna(0, inplace=True) df_gross_returns['net_cogs'] = df_gross_returns['COGS_sales_tax'] - df_gross_returns['COGS_returns_tax'] df_gross_returns = pd.merge(left=df_gross_returns, right=stores, how='left', on=['store_id']) zp_pl_cogs = df_gross_returns.groupby(['store_id', 'store_name', 'type1', 'order_source'])[['net_cogs']].sum().reset_index() zp_pl_cogs['net_cogs'] = zp_pl_cogs['net_cogs'].astype(float) zp_pl_cogs1 = pd.pivot_table(zp_pl_cogs, values='net_cogs', index=['type1', 'order_source'], columns=['store_name']).reset_index() zp_pl_cogs1['tag_flag'] = 'zp_pl_cogs' return zp_pl_cogs1 elif fofo_tag == 'yes': df_aa = df_aa[df_aa['franchisee_id']!=1] df_bb = df_bb[df_bb['franchisee_id'] != 1] df_gross = df_aa.groupby(['store_id', 'type1', 'category', 'fofo_distributor' ,'payment_method', 'order_source'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'quantity': ['sum'], 'GMV': ['sum'], 'GMV_tax': ['sum'], 'REVENUE': ['sum'], 'REVENUE_tax': ['sum'], 'COGS': ['sum'], 'COGS_tax': ['sum'], 'bill_id': pd.Series.nunique # 'net_payable': ['mean'] }).reset_index(drop=True) df_gross.columns = ["_".join(x) for x in df_gross.columns.ravel()] df_gross.rename(columns={'store_id_': 'store_id', 'type1_': 'type1', 'category_': 'category', 'payment_method_': 'payment_method', 'order_source_': 'order_source', 'fofo_distributor_':'fofo_distributor'}, inplace=True) ABV_temp = df_aa.drop_duplicates(subset=['store_id', 'type1', 'category', 'payment_method', 'bill_id', 'net_payable', 'order_source','fofo_distributor']) ABV_temp['net_payable'] = ABV_temp['net_payable'].astype(float) ABV = ABV_temp.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source','fofo_distributor'])["net_payable"].mean().reset_index() df_gross_all = pd.merge(left=df_gross, right=ABV, how='left', on=['store_id', 'type1', 'category', 'payment_method', 'order_source','fofo_distributor']) df_bb['GMV'] = df_bb['returned_quantity'] * df_bb['mrp'] df_bb['GMV_tax'] = np.vectorize(self.taxable_value)(df_bb['returned_quantity'], df_bb['mrp'], df_bb['cgst_rate'], df_bb['sgst_rate'], df_bb['igst_rate']) df_bb['REVENUE'] = df_bb['returned_quantity'] * df_bb['rate'] df_bb['REVENUE_tax'] = np.vectorize(self.taxable_value)(df_bb['returned_quantity'], df_bb['rate'], df_bb['cgst_rate'], df_bb['sgst_rate'], df_bb['igst_rate']) df_bb['COGS'] = df_bb['returned_quantity'] * df_bb['final_ptr'] df_bb['COGS_tax'] = np.vectorize(self.taxable_value)(df_bb['returned_quantity'], df_bb['final_ptr'], df_bb['cgst_rate'], df_bb['sgst_rate'], df_bb['igst_rate']) # df_bb['TAX'] = (df_bb['returned_quantity'] * df_bb['final_ptr']) / (1 + ((df_bb['cgst_rate'] + df_bb['sgst_rate']) / 100)) df_bb[['GMV', 'GMV_tax', 'REVENUE', 'REVENUE_tax', 'COGS', 'COGS_tax']] = df_bb[ ['GMV', 'GMV_tax', 'REVENUE', 'REVENUE_tax', 'COGS', 'COGS_tax']].astype(float) df_returns = df_bb.groupby(['store_id', 'type1', 'category', 'payment_method', 'order_source','fofo_distributor'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'returned_quantity': ['sum'], 'GMV': ['sum'], 'GMV_tax': ['sum'], 'REVENUE': ['sum'], 'REVENUE_tax': ['sum'], 'COGS': ['sum'], 'COGS_tax': ['sum']}).reset_index(drop=True) df_returns.columns = ["_".join(x) for x in df_returns.columns.ravel()] df_returns.rename(columns={'store_id_': 'store_id', 'type1_': 'type1', 'category_': 'category', 'payment_method_': 'payment_method', 'order_source_': 'order_source', 'fofo_distributor_':'fofo_distributor'}, inplace=True) df_gross_returns = pd.merge(left=df_gross_all, right=df_returns, how='outer', on=['store_id', 'type1', 'category', 'payment_method', 'order_source','fofo_distributor']) df_gross_returns.rename(columns={'store_id_': 'store_id', 'type1_': 'type', 'category_': 'category', 'fofo_distributor_':'fofo_distributor', 'drug_id_nunique_x': 'no_of_drugs_sales', 'GMV_sum_x': 'GMV_sales', 'GMV_tax_sum_x': 'GMV_sales_tax', 'REVENUE_sum_x': 'REVENUE_sales', 'REVENUE_tax_sum_x': 'REVENUE_sales_tax', 'COGS_sum_x': 'COGS_sales', 'COGS_tax_sum_x': 'COGS_sales_tax', 'drug_id_nunique_y': 'no_of_drugs_returns', 'GMV_sum_y': 'GMV_returns', 'GMV_tax_sum_y': 'GMV_returns_tax', 'REVENUE_sum_y': 'REVENUE_returns', 'REVENUE_tax_sum_y': 'REVENUE_returns_tax', 'COGS_sum_y': 'COGS_returns', 'COGS_tax_sum_y': 'COGS_returns_tax'}, inplace=True) df_gross_returns.fillna(0, inplace=True) df_gross_returns['net_cogs'] = df_gross_returns['COGS_sales_tax'] - df_gross_returns['COGS_returns_tax'] df_gross_returns = pd.merge(left=df_gross_returns, right=stores, how='left', on=['store_id']) zp_pl_cogs = df_gross_returns.groupby(['store_id', 'store_name', 'type1', 'order_source','fofo_distributor'])[['net_cogs']].sum().reset_index() zp_pl_cogs['net_cogs'] = zp_pl_cogs['net_cogs'].astype(float) zp_pl_cogs1 = pd.pivot_table(zp_pl_cogs, values='net_cogs', index=['type1', 'order_source','fofo_distributor'], columns=['store_name']).reset_index() zp_pl_cogs1['tag_flag'] = 'zp_pl_cogs' return zp_pl_cogs1 def comp_count(self, Inventory, mis_tag): inventory = Inventory.copy(deep=True) if mis_tag == 'breakup': conditions = [inventory['type1'] == 'GOODAID', inventory['type1'] != 'GOODAID'] choices = ['GOODAID', inventory['type']] inventory['type'] = np.select(conditions, choices) comp_count = inventory.groupby(['type'])['drug_id'].nunique().reset_index() comp_count['tag_flag'] = 'drug_count_by_type' comp_count.rename(columns = {'drug_id':'count', 'type':'type1'}, inplace = True) return comp_count def generic_composition_count(self): generic_composition_count_query = self.mis_queries.generic_composition_count_query.format( schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) generic_composition_count = self.rs_db.get_df(generic_composition_count_query) generic_composition_count.columns = [c.replace('-', '_') for c in generic_composition_count.columns] generic_composition_count['tag_flag'] = 'generic_composition_count' return generic_composition_count def ethical_margin(self): ethical_margin_query = self.mis_queries.ethical_margin_query.format( schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time) ethical_margin = self.rs_db.get_df(ethical_margin_query) ethical_margin.columns = [c.replace('-', '_') for c in ethical_margin.columns] ethical_margin['margin'] = 1 - (ethical_margin['net_value']/ethical_margin['value1']) ethical_margin = ethical_margin[['margin']] ethical_margin['tag_flag'] = 'ethical_margin' return ethical_margin def ethical_margin_fofo(self): ethical_margin_query = self.mis_queries.ethical_margin_fofo_query.format( schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time, equality_symbol = '=' ) ethical_margin = self.rs_db.get_df(ethical_margin_query) ethical_margin.columns = [c.replace('-', '_') for c in ethical_margin.columns] ethical_margin['margin'] = 1 - (ethical_margin['net_value'] / ethical_margin['value1']) ethical_margin = ethical_margin[['margin']] ethical_margin['tag_flag'] = 'ethical_margin' ethical_margin['fofo_distributor'] = 'workcell' other_ethical_margin_query = self.mis_queries.ethical_margin_fofo_query.format( schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=self.analysis_start_time, analysis_end_time=self.analysis_end_time, equality_symbol='!=') other_ethical_margin = self.rs_db.get_df(other_ethical_margin_query) other_ethical_margin.columns = [c.replace('-', '_') for c in other_ethical_margin.columns] other_ethical_margin['margin'] = 1 - (other_ethical_margin['net_value'] / other_ethical_margin['value1']) other_ethical_margin = other_ethical_margin[['margin']] other_ethical_margin['tag_flag'] = 'ethical_margin' other_ethical_margin['fofo_distributor'] = 'other' ethical_margin = pd.concat([ethical_margin,other_ethical_margin],sort = True) return ethical_margin def chronic_generic_count(self, Sales, fofo_tag='no'): df_r = Sales.copy(deep=True) if fofo_tag == 'yes': df_r = df_r[df_r['franchisee_id'] != 1] df_r['flag'] = np.where((df_r['category'] == 'chronic'), 1, 0) df_r['flag2'] = np.where(((df_r['category'] == 'chronic') & (df_r['type'] == 'generic')), 1, 0) if fofo_tag == 'no': df_r3 = df_r.groupby(['store_id', 'patient_id'])[['flag2']].sum().reset_index() chronic_generic = df_r3[df_r3['flag2'] > 0].count()["flag2"] total = df_r3['flag2'].count() chronic_generic_percentage = chronic_generic / total chronic_generic_count = pd.DataFrame({'tag_flag': pd.Series("Chronic customers buying generics", dtype='str'), 'count': pd.Series(chronic_generic, dtype='float')}) elif fofo_tag == 'yes': df_r3 = df_r.groupby(['store_id', 'patient_id', 'fofo_distributor'])[['flag2']].sum().reset_index() chronic_generic = df_r3[df_r3['flag2'] > 0].count()["flag2"] chronic_generic_workcell = df_r3[(df_r3['flag2'] > 0) & (df_r3['fofo_distributor'] == 'workcell')].count()[ "flag2"] chronic_generic_other = df_r3[(df_r3['flag2'] > 0) & (df_r3['fofo_distributor'] == 'other')].count()[ "flag2"] chronic_generic_count_combined = pd.DataFrame({'tag_flag': pd.Series("Chronic customers buying generics", dtype='str'), 'count': pd.Series(chronic_generic, dtype='float'), 'fofo_distributor': pd.Series("combined", dtype='str')}) chronic_generic_count_workcell = pd.DataFrame({'tag_flag': pd.Series("Chronic customers buying generics", dtype='str'), 'count': pd.Series(chronic_generic_workcell, dtype='float'), 'fofo_distributor': pd.Series("workcell", dtype='str')}) chronic_generic_count_other = pd.DataFrame({'tag_flag': pd.Series("Chronic customers buying generics", dtype='str'), 'count': pd.Series(chronic_generic_other, dtype='float'), 'fofo_distributor': pd.Series("other", dtype='str')}) chronic_generic_count = pd.concat([chronic_generic_count_workcell, chronic_generic_count_other], sort=True) chronic_generic_count = self.fofo_distributor_bifurcation_next_calculation_steps( chronic_generic_count_combined, chronic_generic_count, ['tag_flag']) return chronic_generic_count def sales_data_for_repeat_customer(self,date1,date2): sales_query = self.mis_queries.sales_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, analysis_start_time=date1, analysis_end_time=date2) df = self.rs_db.get_df(sales_query) df.columns = [c.replace('-', '_') for c in df.columns] return df def repeat_cons_other_def_curr_month(self, sales_data_for_repeat_customer, stores,choose_month): choose_month = int(choose_month) df_aa = sales_data_for_repeat_customer.copy(deep=True) df_aa['year'] = df_aa['created_at'].dt.year df_aa['month'] = df_aa['created_at'].dt.month df_aa['type1'] = np.where(df_aa['type'].isin(['ethical', 'high-value-ethical']), "ethical", df_aa['type']) df_aa['type1'] = np.where(df_aa['type'].isin(['generic', 'high-value-generic']), "generic", df_aa['type']) df_aa['type1'] = np.where(~df_aa['type1'].isin(['ethical', 'generic']), "others", df_aa['type1']) df1 = df_aa.groupby(['store_id', 'patient_id', 'type1', 'category', 'bill_id', 'year', 'month'])[["value"]].sum().reset_index() df2 = df1.groupby(['store_id', 'patient_id', 'type1', 'category'])[["month"]].nunique().reset_index().rename(columns={ 'month': 'unique_months_billed'}) # ============================================================================= # Total repeat consumers # ============================================================================= df3 = df2[df2['unique_months_billed'] >= 2] df4 = df3.groupby(['store_id', 'type1', 'category'])[["patient_id"]].count().reset_index().rename(columns={ 'patient_id': 'repeat_consumers_count'}) df4['tag_flag'] = 'repeat_cons_other_def_curr_month_count' df4 = pd.merge(left=df4, right=stores, how='left', on=['store_id']) df4 = pd.pivot_table(df4, values='repeat_consumers_count', index=['tag_flag', 'type1', 'category'], columns=['store_name']).reset_index() # ============================================================================= # Repeat consumers lost # ============================================================================= # df5 = df1[df1['month'].isin([9, 10, 11])] def previous_months(month): month = int(month) if month<=0: return month + 12 else: return month df5 = df1[df1['month'].isin([previous_months(choose_month-5), previous_months(choose_month-4), previous_months(choose_month-3)])] df6 = df5.groupby(['store_id', 'patient_id', 'type1', 'category'])[["month"]].nunique().reset_index().rename(columns={ 'month': 'unique_months_billed_till_July'}) # df7 = df1[df1['month'].isin([12, 1, 2])] df7 = df1[df1['month'].isin([previous_months(choose_month-2), previous_months(choose_month-1), previous_months(choose_month)])] df8 = df7.groupby(['store_id', 'patient_id', 'type1', 'category'])[["month"]].nunique().reset_index().rename(columns={ 'month': 'unique_months_billed_after_July'}) df9 = pd.merge(left=df6, right=df8, how='left', on=['store_id', 'patient_id', 'type1', 'category']) df10 = df9[df9['unique_months_billed_after_July'].isnull()] df11 = df10.groupby(['store_id', 'type1', 'category'])[["patient_id"]].count().reset_index() df11['tag_flag'] = 'repeat_cons_other_def_curr_month_lost' df11 = pd.merge(left=df11, right=stores, how='left', on=['store_id']) df11 = pd.pivot_table(df11, values='patient_id', index=['tag_flag', 'type1', 'category'], columns=['store_name']).reset_index() repeat_cons_other_def_curr_month = pd.concat([df4, df11]) return repeat_cons_other_def_curr_month def repeat_cons_other_def_past3_month(self, sales_data_for_repeat_customer, stores,choose_month): choose_month = int(choose_month) df_aa = sales_data_for_repeat_customer.copy(deep=True) df_aa['year'] = df_aa['created_at'].dt.year df_aa['month'] = df_aa['created_at'].dt.month df_aa['type1'] = np.where(df_aa['type'].isin(['ethical', 'high-value-ethical']), "ethical", df_aa['type']) df_aa['type1'] = np.where(df_aa['type'].isin(['generic', 'high-value-generic']), "generic", df_aa['type']) df_aa['type1'] = np.where(~df_aa['type1'].isin(['ethical', 'generic']), "others", df_aa['type1']) df1 = df_aa.groupby(['store_id', 'patient_id', 'type1', 'category', 'bill_id', 'year', 'month'])[["value"]].sum().reset_index() df2 = df1.groupby(['store_id', 'patient_id', 'type1', 'category'])[["month"]].nunique().reset_index().rename( columns={ 'month': 'unique_months_billed'}) # ============================================================================= # Total repeat consumers # ============================================================================= df3 = df2[df2['unique_months_billed'] >= 2] df4 = df3.groupby(['store_id', 'type1', 'category'])[["patient_id"]].count().reset_index().rename(columns={ 'patient_id': 'repeat_consumers_count'}) df4['tag_flag'] = 'repeat_cons_other_def_past3_month_count' df4 = pd.merge(left=df4, right=stores, how='left', on=['store_id']) df4 = pd.pivot_table(df4, values='repeat_consumers_count', index=['tag_flag', 'type1', 'category'], columns=['store_name']).reset_index() # ============================================================================= # Repeat consumers lost # ============================================================================= # df5 = df1[df1['month'].isin([6, 7, 8])] def previous_months(month): month = int(month) if month <= 0: return month + 12 else: return month df5 = df1[df1['month'].isin( [previous_months(choose_month - 8), previous_months(choose_month - 7), previous_months(choose_month - 6)])] df6 = df5.groupby(['store_id', 'patient_id', 'type1', 'category'])[["month"]].nunique().reset_index().rename( columns={ 'month': 'unique_months_billed_till_July'}) # df7 = df1[df1['month'].isin([9, 10, 11])] df7 = df1[df1['month'].isin( [previous_months(choose_month - 5), previous_months(choose_month - 4), previous_months(choose_month-3)])] df8 = df7.groupby(['store_id', 'patient_id', 'type1', 'category'])[["month"]].nunique().reset_index().rename( columns={ 'month': 'unique_months_billed_after_July'}) df9 = pd.merge(left=df6, right=df8, how='left', on=['store_id', 'patient_id', 'type1', 'category']) df10 = df9[df9['unique_months_billed_after_July'].isnull()] df11 = df10.groupby(['store_id', 'type1', 'category'])[["patient_id"]].count().reset_index() df11['tag_flag'] = 'repeat_cons_other_def_past3_month_lost' df11 = pd.merge(left=df11, right=stores, how='left', on=['store_id']) df11 = pd.pivot_table(df11, values='patient_id', index=['tag_flag', 'type1', 'category'], columns=['store_name']).reset_index() repeat_cons_other_def_past3_month = pd.concat([df4, df11]) return repeat_cons_other_def_past3_month def other_files_ethical_margin(self): other_files_ethical_margin_query = self.mis_queries.other_files_ethical_margin_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) other_files_ethical_margin = self.rs_db.get_df(other_files_ethical_margin_query) other_files_ethical_margin.columns = [c.replace('-', '_') for c in other_files_ethical_margin.columns] return other_files_ethical_margin def other_files_distributor_margin(self): other_files_distributor_margin_query = self.mis_queries.other_files_distributor_margin_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, choose_year=self.choose_year, choose_month = self.choose_month) other_files_distributor_margin = self.rs_db.get_df(other_files_distributor_margin_query) other_files_distributor_margin.columns = [c.replace('-', '_') for c in other_files_distributor_margin.columns] return other_files_distributor_margin def other_files_inventory_at_dc_near_expiry(self): other_files_inventory_at_dc_near_expiry_data_query = self.mis_queries.other_files_inventory_at_dc_near_expiry_data_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) df_yy = self.rs_db.get_df(other_files_inventory_at_dc_near_expiry_data_query) df_yy.columns = [c.replace('-', '_') for c in df_yy.columns] df_yy['type1'] = np.where(df_yy['type'].isin(['ethical', 'high-value-ethical']), "ethical", df_yy['type']) df_yy['type1'] = np.where(df_yy['type'].isin(['generic', 'high-value-generic']), "generic", df_yy['type']) df_yy['type1'] = np.where(~df_yy['type1'].isin(['ethical', 'generic']), "others", df_yy['type1']) df_yy['taxable'] = (df_yy['actual_quantity'] * df_yy['final_ptr']) / (1 + ((df_yy['vat']) / 100)) df_yy['days'] = (pd.to_datetime('today') - df_yy['created_at']).dt.days conditions = [ (df_yy['days'] >= 0) & (df_yy['days'] <= 30), (df_yy['days'] >= 31) & (df_yy['days'] <= 60), (df_yy['days'] >= 61) & (df_yy['days'] <= 90), (df_yy['days'] >= 91)] choices = ['0_30', '31_60', '61_90', '90+'] df_yy['age_bracket'] = np.select(conditions, choices) df_yy['expiry_date'] = pd.to_datetime(df_yy['expiry'], format='%Y-%m-%d %H:%M:%S', errors='coerce') df_yy['days_to_expiry'] = (pd.to_datetime('today') - df_yy['expiry_date']).dt.days df_yy2 = df_yy[(df_yy['days_to_expiry'] < 0) & (df_yy['days_to_expiry'] > -90)] DC_near_expiry = df_yy2.groupby(['store_id', 'type1', 'category', 'age_bracket'], as_index=False).agg({ 'drug_id': pd.Series.nunique, 'net_value': ['sum'], 'taxable': ['sum']}).reset_index(drop=True) DC_near_expiry.columns = ["_".join(x) for x in DC_near_expiry.columns.ravel()] return DC_near_expiry def goodaid_gross_return(self): goodaid_store_sales_query = self.mis_queries.goodaid_store_sales_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, choose_year=self.choose_year, choose_month = self.choose_month) goodaid_store_sales = self.rs_db.get_df(goodaid_store_sales_query) goodaid_store_sales.columns = [c.replace('-', '_') for c in goodaid_store_sales.columns] goodaid_store_returns_query = self.mis_queries.goodaid_store_returns_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table, choose_year=self.choose_year, choose_month = self.choose_month) goodaid_store_returns = self.rs_db.get_df(goodaid_store_returns_query) goodaid_store_returns.columns = [c.replace('-', '_') for c in goodaid_store_returns.columns] gross_and_returns = pd.merge(left=goodaid_store_sales, right=goodaid_store_returns, how='left', on=['year', 'month', 'store_id', 'store_name']) return gross_and_returns def goodaid_zippin_inventory(self): goodaid_zippin_inventory_query = self.mis_queries.goodaid_zippin_inventory_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) df_gg = self.rs_db.get_df(goodaid_zippin_inventory_query) df_gg.columns = [c.replace('-', '_') for c in df_gg.columns] df_gg['days'] = (pd.to_datetime('today') - df_gg['created_at']).dt.days conditions = [ (df_gg['days'] >= 0) & (df_gg['days'] <= 30), (df_gg['days'] >= 31) & (df_gg['days'] <= 60), (df_gg['days'] >= 61) & (df_gg['days'] <= 90), (df_gg['days'] >= 91)] choices = ['0_30', '31_60', '61_90', '90+'] df_gg['ageing'] = np.select(conditions, choices) df_gg['expiry_date'] = pd.to_datetime(df_gg['expiry'], format='%Y-%m-%d %H:%M:%S', errors='coerce') df_gg['days_to_expiry'] = (df_gg['expiry_date'] - pd.to_datetime('today')).dt.days del df_gg['days'] del df_gg['expiry'] return df_gg def goodaid_dc_inventory(self): goodaid_dc_inventory_query = self.mis_queries.goodaid_dc_inventory_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) df_jk1 = self.rs_db.get_df(goodaid_dc_inventory_query) df_jk1.columns = [c.replace('-', '_') for c in df_jk1.columns] return df_jk1 def goodaid_wh_inventory(self): date = datetime.datetime(int(self.choose_year), int(self.choose_month)+1, 1, 0, 0, 0).strftime('%Y-%m-%d') goodaid_wh_inventory_query = self.mis_queries.goodaid_wh_inventory_query.format(date = date) wh_inv = self.rs_db.get_df(goodaid_wh_inventory_query) wh_inv.columns = [c.replace('-', '_') for c in wh_inv.columns] goodaid_drugs_query = self.mis_queries.goodaid_drugs_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) goodaid_drugs = self.rs_db.get_df(goodaid_drugs_query) goodaid_drugs.columns = [c.replace('-', '_') for c in goodaid_drugs.columns] wh_inventory = pd.merge(left=wh_inv, right=goodaid_drugs, how='inner', on=['drug_id']) return wh_inventory return df_jk1 def store_info(self): store_info_query = self.mis_queries.store_info_query.format(schema=self.schema_to_select, suffix_to_table=self.suffix_to_table) store_info = self.rs_db.get_df(store_info_query) store_info.columns = [c.replace('-', '_') for c in store_info.columns] return store_info
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/mis/mis_class.py
mis_class.py
max_store_id = """ select max("id") as "store-id-max" from "prod2-generico"."{}" """ insert_stores_query = """insert into "prod2-generico"."{}" ( "id", "etl-created-by", "created-at", "updated-by", "updated-at", "store", "line-manager", "abo", "store-manager", "store-type", "opened-at", "date-diff", "month-diff", "latitude", "longitude", "store-contact-1", "store-contact-2", "store-address", "city", "store-b2b", "line", "landmark", "store-group-id", "franchisee-id", "franchisee-name", "cluster-id", "cluster-name", "acquired", "old-new-static", "line-manager-email", "abo-email", "store-manager-email", "store-email" ) select st.id as id, 'etl-automation' as "etl-created-by", max(st."created-at") as "created-at", 'etl-automation' as "updated-by", convert_timezone('Asia/Calcutta', GETDATE()) as "updated-at", max(st.name) as "store", max(case when b.type = 'line-manager' then b.name end) as "line-manager", max(case when b.type = 'area-business-owner' then b.name end) as "abo", max(case when b.type = 'store-manager' then b.name end) as "store-manager", max(st.category) as "store-type", DATE("opened-at") as "opened-at", datediff(day, DATE("opened-at"), current_date) as "date-diff", datediff(month, DATE("opened-at"), current_date) as "month-diff", max(st."lat") as "latitude", max(st."lon") as "longitude", max(st."contact-number-1") as "store-contact-1", max(st."contact-number-2") as "store-contact-2", max(st."address") as "store-address", max(sg.name) as "city", case when lower(SUBSTRING(st.name, 1, 3))= 'b2b' then 'B2B' else 'Store' end as "store-b2b", '' as line, '' as landmark, max(st."store-group-id") as "store-group-id", st."franchisee-id", f."name", s."cluster-id", s."cluster-name", st."acquired", (case when date(st."opened-at")>= '2022-04-01' then 'new' when date(st."opened-at")= '0101-01-01' then 'not-opened' else 'old' end) as "old-new-static", max(case when b.type = 'line-manager' then b.email end) as "line-manager-email", max(case when b.type = 'area-business-owner' then b.email end) as "abo-email", max(case when b.type = 'store-manager' then b.email end) as "store-manager-email", max(st.email) as "store-email" from "prod2-generico".stores st inner join "prod2-generico".franchisees f on st."franchisee-id" = f.id left join ( select us."store-id", u."name" as name, u."created-at" as "date", u.type, u.email , row_number() over( partition by us."store-id", u.type order by u."created-at" desc) as t_rank from "prod2-generico"."users-stores" as us inner join "prod2-generico"."users" as u on u."id" = us."user-id" where u.type in ('line-manager', 'store-manager', 'area-business-owner')) as b on st.id = b."store-id" and b.t_rank = 1 inner join "prod2-generico"."store-groups" sg on st."store-group-id" = sg.id left join ( select sf."store-id" as "store-id", sf."is-active" as "sf-is-active", sc."cluster-id" as "cluster-id", c.name as "cluster-name", sc."is-active" as "sc-is-active" from "prod2-generico".features f join "prod2-generico"."store-features" sf on f.id = sf."feature-id" join "prod2-generico"."store-clusters" sc on sc."store-id" = sf."store-id" join "prod2-generico".clusters c on c.id = sc."cluster-id" where sf."feature-id" = 69 and sf."is-active" = 1 and sc."is-active" = 1 ) as s on st.id = s."store-id" where st.id > {} group by st.id, st.name, st."opened-at", st."franchisee-id", f."name", s."cluster-id", s."cluster-name", st."acquired", "old-new-static";""" update_stores_query = """update "prod2-generico"."{}" as sm set "updated-at" = convert_timezone('Asia/Calcutta', GETDATE()), "line-manager" = b."line-manager", "abo" = b."abo", "store" = b."store", "franchisee-name" = b."franchisee-name", "cluster-id" = b."cluster-id", "cluster-name" = b."cluster-name", "acquired"=b."acquired", "opened-at"=b."opened-at", "old-new-static"=b."old-new-static", "line-manager-email" = b."line-manager-email", "abo-email" = b."abo-email", "store-manager-email" = b."store-manager-email", "store-email" = b."store-email" from ( select st.id as id, st."acquired", st.name as "store", st."opened-at", (case when date(st."opened-at")>='2022-04-01' then 'new' when date(st."opened-at")='0101-01-01' then 'not-opened' else 'old' end) as "old-new-static", f."name" as "franchisee-name", s."cluster-id" as "cluster-id", s."cluster-name" as "cluster-name", max(case when b.type = 'line-manager' then b.name end) as "line-manager", max(case when b.type = 'area-business-owner' then b.name end) as "abo", max(case when b.type = 'line-manager' then b.email end) as "line-manager-email", max(case when b.type = 'area-business-owner' then b.email end) as "abo-email", max(case when b.type = 'store-manager' then b.email end) as "store-manager-email", max(st.email) as "store-email" from "prod2-generico"."{}" sm inner join "prod2-generico".stores st on st.id = sm.id inner join "prod2-generico".franchisees f on st."franchisee-id" = f.id left join ( select us."store-id", u."name" as name, u."created-at" as "date", u.type, u.email, row_number() over( partition by us."store-id", u.type order by u."created-at" desc) as t_rank from "prod2-generico"."users-stores" as us inner join "prod2-generico"."users" as u on u."id" = us."user-id" where u.type in ('line-manager', 'store-manager', 'area-business-owner')) as b on st.id = b."store-id" and b.t_rank = 1 inner join "prod2-generico"."store-groups" sg on st."store-group-id" = sg.id left join ( select sf."store-id" as "store-id", sf."is-active" as "sf-is-active", sc."cluster-id" as "cluster-id", c.name as "cluster-name", sc."is-active" as "sc-is-active" from "prod2-generico".features f join "prod2-generico"."store-features" sf on f.id = sf."feature-id" join "prod2-generico"."store-clusters" sc on sc."store-id" = sf."store-id" join "prod2-generico".clusters c on c.id = sc."cluster-id" where sf."feature-id" = 69 and sf."is-active" = 1 and sc."is-active" = 1 ) as s on st.id = s."store-id" group by st.id, st."acquired", st.name, st."opened-at", "old-new-static", f."name", s."cluster-id", s."cluster-name") as b where sm.id = b.id and (sm.abo != b.abo or sm."line-manager" != b."line-manager" or sm."acquired" != b."acquired" or b."cluster-id" != sm."cluster-id" or b."cluster-name" != sm."cluster-name" or b."franchisee-name" != sm."franchisee-name" or b."opened-at" != sm."opened-at" or b."line-manager-email" != sm."line-manager-email" or b."abo-email" != sm."abo-email" or b."store-manager-email" != sm."store-manager-email" or b."store-email" != sm."store-email"); """
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/storesmaster/storesmaster_config.py
storesmaster_config.py
update_query = """update "prod2-generico"."{}" SET "cohort-quarter" = stg."cohort-quarter", "cohort-quarter-number" = stg."cohort-quarter-number", "year-cohort" = stg."year-cohort", "store-id" = stg."store-id", "bill-quarter" = stg."bill-quarter", "bill-quarter-number" = stg."bill-quarter-number", "year-bill" = stg."year-bill", "bill-date" = stg."bill-date", "day-zero-in-cohort-quarter" = stg."day-zero-in-cohort-quarter", "day-zero-in-bill-quarter" = stg."day-zero-in-bill-quarter", "day-index" = stg."day-index", "quarter-diff" = stg."quarter-diff", "resurrection-candidate" = stg."resurrection-candidate", "cohort-quarter-patients" = stg."cohort-quarter-patients", "cohort-resurrection-candidates" = stg."cohort-resurrection-candidates" from "prod2-generico"."{}" retention inner join "{}-stg" stg on stg."patient-id" = retention."patient-id" """ insert_query = """insert into "prod2-generico"."{}" select stg.* from "{}-stg" stg left join "prod2-generico"."{}" retention on stg."patient-id" = retention."patient-id" where retention."patient-id" IS NULL """ temp_create = """create temp table "{}-stg" ( "created-at" TIMESTAMP WITHOUT TIME ZONE ENCODE az64 ,"created-by" VARCHAR(765) ENCODE lzo ,"updated-by" VARCHAR(765) ENCODE lzo ,"updated-at" TIMESTAMP WITHOUT TIME ZONE ENCODE az64 ,"patient-id" BIGINT NOT NULL ENCODE az64 ,"store-id" INTEGER NOT NULL ENCODE az64 ,"bill-date" DATE NOT NULL ENCODE az64 ,"last-bill-created-at" TIMESTAMP WITHOUT TIME ZONE ENCODE az64 ,"year-bill" INTEGER NOT NULL ENCODE az64 ,"bill-quarter" VARCHAR(255) ENCODE lzo ,"bill-quarter-number" INTEGER NOT NULL ENCODE az64 ,"year-cohort" INTEGER NOT NULL ENCODE az64 ,"cohort-quarter" VARCHAR(255) ENCODE lzo ,"cohort-quarter-number" INTEGER NOT NULL ENCODE az64 ,"day-zero-in-cohort-quarter" TIMESTAMP WITHOUT TIME ZONE ENCODE az64 ,"day-zero-in-bill-quarter" TIMESTAMP WITHOUT TIME ZONE ENCODE az64 ,"day-index" INTEGER ENCODE az64 ,"quarter-diff" INTEGER ENCODE az64 ,"resurrection-candidate" INTEGER ENCODE az64 ,"cohort-quarter-patients" BIGINT ENCODE az64 ,"cohort-resurrection-candidates" BIGINT ENCODE az64 );"""
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/queries/retention_day_quarter/retention_day_quarter_config.py
retention_day_quarter_config.py
import os import time from io import BytesIO, StringIO import boto3 import pandas as pd import pg8000 import pymongo import redshift_connector as rc from sqlalchemy import create_engine from zeno_etl_libs.config.common import Config class Athena: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.athena_connection = None self.db_secrets = secrets self.region = 'ap-south-1' self.aws_access_key_id = secrets['AWS_ACCESS_KEY_ID'] self.aws_secret_access_key = secrets['AWS_SECRET_ACCESS_KEY_ID'] self.bucket_name = 'aws-glue-temporary-921939243643-ap-south-1' self.s3_resource = boto3.resource('s3', self.region, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key) self.s3_client = boto3.client('s3', self.region, aws_access_key_id=self.aws_access_key_id, aws_secret_access_key=self.aws_secret_access_key) self.athena_client = boto3.client(service_name='athena', region_name=self.region) def connection(self, query=None, schema_name=None): """ This function creates an Athena connection based on the schema name input: schema_name: STRING (This variable connects with the specified database in Athena, default value: None) output: Athena Connection object """ if schema_name: database = schema_name else: database = self.db_secrets['DATALAKE_DATABASE'] response = self.athena_client.start_query_execution( QueryString=query, QueryExecutionContext={'Database': database}, ResultConfiguration={'OutputLocation': 's3://{}/athena/'.format(self.bucket_name)}, ) return response # s3_staging_dir = 's3://aws-glue-temporary-921939243643-ap-south-1/athena/' # if schema_name: # conn_str = f"awsathena+rest://{self.db_secrets['AWS_ACCESS_KEY_ID']}:{self.db_secrets['AWS_SECRET_ACCESS_KEY_ID']}@athena.{self.db_secrets['AWS_REGION']}.amazonaws.com:443/{schema_name}?s3_staging_dir{s3_staging_dir}&work_group=primary" # else: # schema_name = self.db_secrets['DATALAKE_DATABASE'] # conn_str = f"awsathena+rest://{self.db_secrets['AWS_ACCESS_KEY_ID']}:{self.db_secrets['AWS_SECRET_ACCESS_KEY_ID']}@athena.{self.db_secrets['AWS_REGION']}.amazonaws.com:443/{schema_name}?s3_staging_dir{s3_staging_dir}&work_group=primary" # # Create the SQLAlchemy connection. Note that you need to have pyathena installed for this. # engine = create_engine( # conn_str.format( # aws_access_key_id=self.db_secrets['AWS_ACCESS_KEY_ID'], # aws_secret_access_key=self.db_secrets['AWS_SECRET_ACCESS_KEY_ID'], # region_name=self.db_secrets['AWS_REGION'], # schema_name=schema_name, # s3_staging_dir=s3_staging_dir, # ) # ) # athena_connection = engine.connect() # self.athena_connection = athena_connection # return athena_connection def validate_query(self, query_id): resp = ["FAILED", "SUCCEEDED", "CANCELLED"] response = self.athena_client.get_query_execution(QueryExecutionId=query_id) # wait until query finishes while response["QueryExecution"]["Status"]["State"] not in resp: response = self.athena_client.get_query_execution(QueryExecutionId=query_id) return response["QueryExecution"]["Status"]["State"] def get_df(self, query=None, schema_name=None): print('start query: {}\n'.format(query)) qe = self.connection(query, schema_name) qstate = self.validate_query(qe["QueryExecutionId"]) print('query state: {}\n'.format(qstate)) file_name = "athena/{}.csv".format(qe["QueryExecutionId"]) obj = self.s3_client.get_object(Bucket=self.bucket_name, Key=file_name) return pd.read_csv(obj['Body']) def ingest_df_to_datalake(self, df, table_name=None, index=False, is_mis=False, file_format='csv'): if file_format == 'parquet': file_name = self.db_secrets[ 'DATALAKE_DATABASE'] + '/' + table_name + f"/LOAD{int(time.time() * 1000)}.parquet" else: file_name = self.db_secrets[ 'DATALAKE_DATABASE'] + '/' + table_name + f"/LOAD{int(time.time() * 1000)}.csv" path = "/".join(os.getcwd().split("/")[:-2]) + "/tmp/" if not os.path.exists(path): os.mkdir(path, 0o777) if file_format == 'parquet': buffer = BytesIO() # df.to_parquet(parquet_buffer, index=index, engine='fastparquet') df.to_parquet(buffer, index=index, engine='auto') # df.to_csv(local_file_path_csv) else: buffer = StringIO() df.to_csv(buffer, index_label=False, index=index, header=True) if is_mis: bucket_name = 'prod-mis-datalake' else: bucket_name = 'zeno-data-lake' self.s3_resource.Object(bucket_name, file_name).put(Body=buffer.getvalue()) s3_uri = f"s3://{bucket_name}/{file_name}" return s3_uri class DB: def __init__(self, read_only=True): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets self.cursor = None self.connection = None self.read_only = read_only def open_connection(self): """ :return DB cursor """ """function returns the redshift connection or cursor""" if self.read_only: self.connection = rc.connect( host=self.db_secrets['REDSHIFT_HOST'], database=self.db_secrets['REDSHIFT_DB'], user=self.db_secrets['REDSHIFT_USER'], password=self.db_secrets['REDSHIFT_PASSWORD'], port=int(self.db_secrets['REDSHIFT_PORT']), ssl=bool(int(self.db_secrets['REDSHIFT_SSL'])) ) else: self.connection = rc.connect( host=self.db_secrets['REDSHIFT_WRITE_HOST'], database=self.db_secrets['REDSHIFT_WRITE_DB'], user=self.db_secrets['REDSHIFT_WRITE_USER'], password=self.db_secrets['REDSHIFT_WRITE_PASSWORD'], port=int(self.db_secrets['REDSHIFT_WRITE_PORT']), ssl=bool(int(self.db_secrets['REDSHIFT_WRITE_SSL'])) ) self.connection.autocommit = True cursor: rc.Cursor = self.connection.cursor() self.cursor = cursor return cursor def execute(self, query, params=None): """ query: "select * from table where col = '%s' and col2 = '%s' " params: (x, y) """ try: self.cursor.execute(query, params) except Exception as e: print(f"e: {e}") if not self.connection.autocommit: self.cursor.execute("rollback") raise Exception(e) def get_df(self, query) -> pd.DataFrame: self.execute(query, params=None) df: pd.DataFrame = self.cursor.fetch_dataframe() if isinstance(df, type(None)): return pd.DataFrame( columns=[desc[0].decode("utf-8") for desc in self.cursor.description]) else: return df def close_connection(self): """ make sure to close the connection, after all the DB operation are over """ print("Redshift DB connection closed successfully.") self.cursor.close() class RedShiftDB(DB): pass class RedShiftPG8000: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets self.cursor = None self.connection = None def open_connection(self): """ :return DB cursor """ """function returns the redshift connection or cursor""" self.connection = pg8000.connect( host=self.db_secrets['REDSHIFT_HOST'], database=self.db_secrets['REDSHIFT_DB'], user=self.db_secrets['REDSHIFT_USER'], password=self.db_secrets['REDSHIFT_PASSWORD'], port=int(self.db_secrets['REDSHIFT_PORT']) ) self.connection.autocommit = True cursor: rc.Cursor = self.connection.cursor() self.cursor = cursor return cursor def execute(self, query): """ query: "select * from table where col = '%s' and col2 = '%s' " params: (x, y) """ try: self.cursor.execute(query) except Exception as e: print(f"e: {e}") self.cursor.execute("rollback") # self.cursor.execute(query) # self.cursor.execute(query) raise Exception(e) def close_connection(self): """ make sure to close the connection, after all the DB operation are over """ print("Redshift DB connection closed successfully.") self.cursor.close() class RedShift: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets self.connection = None self.engine = None # dispose the engine after user self.url = f"postgresql+psycopg2://{self.db_secrets['REDSHIFT_USER']}:{self.db_secrets['REDSHIFT_PASSWORD']}@" \ f"{self.db_secrets['REDSHIFT_HOST']}:{self.db_secrets['REDSHIFT_PORT']}/" \ f"{self.db_secrets['REDSHIFT_DB']}" def open_connection(self): """ :return DB cursor """ """function returns the redshift connection or cursor""" self.engine = create_engine(self.url) self.connection = self.engine.connect() self.connection.autocommit = True return self.connection def execute(self, query): try: self.engine.execute(query) except Exception as e: print(f"e: {e}") self.engine.execute("rollback") raise Exception(e) def close_connection(self): """ make sure to close the connection, after all the DB operation are over """ self.connection.close() self.engine.dispose() print("Redshift DB connection closed successfully.") class MySQL: """ MySQL DB Connection """ """ implementing singleton design pattern for DB Class """ def __init__(self, read_only=True): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets if read_only: self.user = self.db_secrets['MS_USER'] self.password = self.db_secrets['MS_PASSWORD'] self.host = self.db_secrets['MS_HOST'] self.port = self.db_secrets['MS_PORT'] self.db = self.db_secrets['MS_DB'] else: self.user = self.db_secrets['MYSQL_WRITE_USER'] self.password = self.db_secrets['MYSQL_WRITE_PASSWORD'] self.host = self.db_secrets['MYSQL_WRITE_HOST'] self.port = self.db_secrets['MYSQL_WRITE_PORT'] self.db = self.db_secrets['MYSQL_WRITE_DATABASE'] self.url = f"mysql+pymysql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}" self.engine = None self.connection = None self.cursor = None # Not calling open_connection() function, to avoid DB connection at class instantiation # self.connection = self.open_connection() def open_connection(self): """ :return: connection to mysql DB using pymysql lib """ # self.connection = pymysql.connect(host=self.host, user=self.user, password=self.password, db=self.db, # port=int(self.port)) self.engine = create_engine(self.url, connect_args={'connect_timeout': 3600}) self.connection = self.engine.raw_connection() self.cursor = self.connection.cursor() return self.cursor def close(self): """ closes the DB connection :return None """ print("MySQL DB connection closed successfully!") self.connection.close() self.engine.dispose() class PostGre: """ MySQL DB Connection """ """ implementing singleton design pattern for DB Class """ def __init__(self, is_internal=False): """ @param is_internal: True means DB is owned by tech team, we want a connection with that """ configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets if is_internal: self.user = self.db_secrets[f'INTERNAL_PG_USER'] self.password = self.db_secrets['INTERNAL_PG_PASSWORD'] self.host = self.db_secrets['INTERNAL_PG_HOST'] self.port = self.db_secrets['INTERNAL_PG_PORT'] self.db = self.db_secrets['INTERNAL_PG_DB'] else: self.user = self.db_secrets['PG_USER'] self.password = self.db_secrets['PG_PASSWORD'] self.host = self.db_secrets['PG_HOST'] self.port = self.db_secrets['PG_PORT'] self.db = self.db_secrets['PG_DB'] self.url = f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}" self.connection = None self.cursor = None def open_connection(self): # import psycopg2 # conn_string = f"dbname='{self.db}' port='{self.port}' user='{self.user}' password='{self.password}' " \ # f"host='{self.host}'" # self.connection = psycopg2.connect(conn_string) self.connection = pg8000.connect( database=self.db, user=self.user, password=self.password, host=self.host, port=self.port ) self.cursor = self.connection.cursor() return self.connection def connection(self): """ :return: connection to mysql DB using pymysql lib """ return self.open_connection() def execute(self, query, params=None): """ query: "select * from table where col = '%s' and col2 = '%s' " params: (x, y) """ try: self.connection.execute(query, params) except Exception as e: print(f"e: {e}") self.connection.execute("rollback") def close(self): """ closes the DB connection :return None """ self.connection.close() print("PostGre DB connection closed successfully!") def close_connection(self): self.close() class PostGreWrite: """ implementing singleton design pattern for DB Class """ def __init__(self, is_internal=False): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets if is_internal: self.user = self.db_secrets[f'INTERNAL_PG_USER'] self.password = self.db_secrets['INTERNAL_PG_PASSWORD'] self.host = self.db_secrets['INTERNAL_PG_HOST'] self.port = self.db_secrets['INTERNAL_PG_PORT'] self.db = self.db_secrets['INTERNAL_PG_DB'] else: self.user = self.db_secrets['PG_USER'] self.password = self.db_secrets['PG_PASSWORD'] self.host = self.db_secrets['PG_HOST'] self.port = self.db_secrets['PG_PORT'] self.db = self.db_secrets['PG_DB'] self.url = f"postgresql+pg8000://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}" # self.url = f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}" self.engine = None self.connection = None self.cursor = None def open_connection(self): """ :return: connection to mysql DB using pymysql lib """ self.engine = create_engine(self.url) self.connection = self.engine.raw_connection() self.cursor = self.connection.cursor() return self.connection def close(self): """ closes the DB connection :return None """ print("PostGre DB connection closed successfully!") self.engine.dispose() def close_connection(self): self.close() class RedshiftEngine: def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets self.connection = None def open_connection(self): """ :return DB cursor """ """function returns the redshift connection or cursor""" host = self.db_secrets['REDSHIFT_HOST'] database = self.db_secrets['REDSHIFT_DB'] user = self.db_secrets['REDSHIFT_USER'] password = self.db_secrets['REDSHIFT_PASSWORD'] port = int(self.db_secrets['REDSHIFT_PORT']) ssl = bool(int(self.db_secrets['REDSHIFT_SSL'])) uri = f"postgresql://{user}:{password}@{host}:{port}/{database}" self.connection = pg8000.connect(uri) def execute(self, query, params=None): """ query: "select * from table where col = '%s' and col2 = '%s' " params: (x, y) """ try: self.connection.execute(query, params) except Exception as e: print(f"e: {e}") self.connection.execute("rollback") def create_report_table_using_df(self, df, table_name, schema): try: df.head(5).to_sql( name=table_name, con=self.connection, index=False, if_exists='fail', schema=schema) query = f"""truncate table "{schema}"."{table_name}"; """ self.connection.execute(query) print(f"Created table: {table_name}, successfully.") except Exception as e: print(f"Error creating table: {e}") def close_connection(self): """ make sure to close the connection, after all the DB operation are over """ print("Redshift DB connection closed successfully.") self.connection.close() class MongoDB: """ Mongo DB Connection """ """ implementing singleton design pattern for DB Class """ def __init__(self): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets self.user = self.db_secrets['MONGO_USER'] self.password = self.db_secrets['MONGO_PASSWORD'] self.host = self.db_secrets['MONGO_HOST'] self.port = self.db_secrets['MONGO_PORT'] self.connection = None self.cursor = None def open_connection(self, auth_source): self.connection = pymongo.MongoClient(self.host, self.port, username=self.user, password=self.password, authSource=auth_source) return self.connection def connection(self, auth_source): return self.open_connection(auth_source) def close(self): """ closes the DB connection :return None """ self.connection.close() print("Mongo DB connection closed successfully!") def close_connection(self): self.close() def download_private_key_from_s3(): # s3 = boto3.resource('s3') # file = "id_rsa" # ssh_pkey_full_path = '/tmp/' + file # bucket_name = "aws-prod-glue-assets-921939243643-ap-south-1" # logger.info(f"bucket_name: {bucket_name}") # logger.info(f"ssh_pkey_full_path: {ssh_pkey_full_path}") # s3.Bucket(bucket_name).download_file("private/" + file, file) # logger.info(f"ssh_pkey_full_path downloaded successfully") # return ssh_pkey_full_path pass class MSSql: """ MSSQL DB Connection """ """ implementing singleton design pattern for DB Class """ def __init__(self, connect_via_tunnel=True, db=None, one_beat_type=None): configobj = Config.get_instance() secrets = configobj.get_secrets() self.db_secrets = secrets if str(one_beat_type).lower() == 'in' : self.user = self.db_secrets['IN_OB_MSSQL_USER'] self.password = self.db_secrets['IN_OB_MSSQL_PASSWORD'] self.host = self.db_secrets['IN_OB_MSSQL_HOST'] self.port = self.db_secrets['IN_OB_MSSQL_PORT'] if db is None: self.db = self.db_secrets['IN_OB_MSSQL_DATABASE'] else: self.db = db self.connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + self.host + ";DATABASE=" + \ self.db + ";UID=" + self.user + ";PWD=" + self.password + ";TrustServerCertificate=yes" elif str(one_beat_type).lower() == 'out' : self.user = self.db_secrets['OUT_OB_MSSQL_USER'] self.password = self.db_secrets['OUT_OB_MSSQL_PASSWORD'] self.host = self.db_secrets['OUT_OB_MSSQL_HOST'] self.port = self.db_secrets['OUT_OB_MSSQL_PORT'] if db is None: self.db = self.db_secrets['OUT_OB_MSSQL_DATABASE'] else: self.db = db self.connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + self.host + ";DATABASE=" + \ self.db + ";UID=" + self.user + ";PWD=" + self.password + ";TrustServerCertificate=yes" else: self.user = self.db_secrets['WH_MSSQL_USER'] self.password = self.db_secrets['WH_MSSQL_PASSWORD'] self.host = self.db_secrets['WH_MSSQL_HOST'] self.port = self.db_secrets['WH_MSSQL_PORT'] if db is None: self.db = self.db_secrets['WH_MSSQL_DATABASE'] else: self.db = db self.connection_string = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=" + self.host + ";DATABASE=" + \ self.db + ";UID=" + self.user + ";PWD=" + self.password + ";TrustServerCertificate=yes" # self.url = f"mssql+pymssql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}" # TODO: switch on tunnel connect self.connect_via_tunnel = False self.connection = None self.cursor = None self.tunnel = None def __start_tunnel(self): from sshtunnel import SSHTunnelForwarder try: self.tunnel = SSHTunnelForwarder( ('workstation.generico.in', 22), ssh_username='glue', ssh_pkey=download_private_key_from_s3(), ssh_private_key_password='', remote_bind_address=('wh1.zeno.health', 1433) ) # logger.info("Tunnel class ok") self.tunnel.start() # logger.info("Tunnel started") except Exception as error: raise Exception("MSSQL error while starting tunnel is: {}".format(error)) def open_connection(self): try: if self.connect_via_tunnel: self.__start_tunnel() import pymssql self.connection = pymssql.connect(server=self.host, user=self.user, password=self.password, database=self.db, port=self.port) self.cursor = self.connection.cursor() return self.connection except Exception as error: raise Exception("MSSQL error while establishing connection is: {}".format(error)) def connection(self): """ :return: connection to mysql DB using pymysql lib """ return self.open_connection() def get_table_info(self, table_name): table_info = pd.read_sql( f"SELECT TOP 0 * FROM {self.db}.dbo.[{table_name}];", self.connection) return table_info def close(self): """ closes the DB connection :return None """ self.connection.close() if self.connect_via_tunnel: self.tunnel.close() print("MSSQL DB connection closed successfully!") def close_connection(self): self.close()
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/db/db.py
db.py
from math import sin, cos, sqrt, atan2, radians ################################## # month diff calculation ################################## def month_diff(date_a, date_b): """ This function returns month difference between calendar dates 'date_a' and 'date_b' """ return 12 * (date_a.dt.year - date_b.dt.year) + (date_a.dt.month - date_b.dt.month) # Pass cleaner tuple (string format) into SQL query # Handles single item tuple def sql_tuple(a_tuple): """ This function converts a tuple into a parasable string format for SQL query purpose For example - (1,) a single item tuple will be converted to (1), But if tuple is (1,2) then will remain as (1,2) """ if len(a_tuple) == 1: return '({})'.format(a_tuple[0]) return str(a_tuple) def nearest_store(store_id, data, lat_lon_col_name=['latitude', 'longitude'], from_distance=5, ): """ helper function to get the nearby stores """ """ filtering out the nan values """ R = 6378.1 # radius of earth in KM print(f"nearest_store calculation started for store id: {store_id}") data['lat_r'] = data[lat_lon_col_name[0]].apply(lambda x: radians(float(x))) data['lon_r'] = data[lat_lon_col_name[1]].apply(lambda x: radians(float(x))) lat1 = data[data['store_id'] == store_id]['lat_r'].values[0] lon1 = data[data['store_id'] == store_id]['lon_r'].values[0] data['lat1'] = lat1 data['lon1'] = lon1 data['dlon'] = data['lon_r'] - data['lon1'] data['dlat'] = data['lat_r'] - data['lat1'] data['a'] = data.apply( lambda x: sin(x['dlat'] / 2) ** 2 + cos(x['lat1']) * cos(x['lat_r']) * sin(x['dlon'] / 2) ** 2, 1) data['c'] = data.apply(lambda x: 2 * atan2(sqrt(x['a']), sqrt(1 - x['a'])), 1) data['distance'] = R * data['c'] near_stores = data[data['distance'] <= from_distance].sort_values('distance')['store_id'].values data.drop(columns=['lat_r', 'lon_r', 'lat1', 'lon1', 'dlon', 'dlat', 'a', 'c', 'distance'], inplace=True) return near_stores # Change HH:MM:SS to seconds def hms_to_seconds(t): """ This function converts HH:MM:SS (for example 02:23:18 (2hrs, 23minutes, 18seconds) # into total seconds value, which is 8598 in this) """ split_t = [int(i) for i in t.split(':')] if len(split_t) == 3: h, m, s = split_t return 3600 * h + 60 * m + s elif len(split_t) == 2: m, s = split_t return 60 * m + s else: s = split_t[0] return s
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/general_funcs.py
general_funcs.py
import pandas as pd import numpy as np import time from zeno_etl_libs.django.api import Sql from zeno_etl_libs.db.db import MySQL def doid_custom_write(data, logger, ss_col=None, rop_col=None, oup_col=None): """ data : (pd.DataFrame) can contains columns ["store_id", "drug_id", ss_col, rop_col, oup_col] if only "store_id" and "drug_id" present then ss,rop,oup is set to zero and updated into DOID """ data = data.drop_duplicates() if None in [ss_col, rop_col, oup_col]: data["min"] = 0 data["safe_stock"] = 0 data["max"] = 0 else: data.rename({ss_col: 'min', rop_col: 'safe_stock', oup_col: 'max'}, axis=1, inplace=True) mysql = MySQL(read_only=False) mysql.open_connection() sql = Sql() missed_entries = pd.DataFrame() logger.info("MySQL DOID write starts") for store_id in data['store_id'].unique(): logger.info('Mysql upload for store ' + str(store_id)) current_ss_query = f""" SELECT doid.id, doid.`store-id` , doid.`drug-id` , doid.min, doid.`safe-stock` , doid.max FROM `drug-order-info-data` doid where doid.`store-id` = {store_id} """ current_ss = pd.read_sql(current_ss_query, mysql.connection) current_ss.columns = [c.replace('-', '_') for c in current_ss.columns] data_store = data.loc[ data['store_id'] == store_id, ['store_id', 'drug_id', 'min', 'safe_stock', 'max']] ss_joined = current_ss.merge( data_store, on=['store_id', 'drug_id'], how='right', suffixes=('_old', '')) ss_joined['flag'] = np.where( (ss_joined['min_old'] == ss_joined['min']) & (ss_joined['safe_stock_old'] == ss_joined['safe_stock']) & (ss_joined['max_old'] == ss_joined['max']), 'values same', 'values changed') ss_to_upload = ss_joined.loc[ ss_joined['flag'] == 'values changed', ['id', 'min', 'safe_stock', 'max']] logger.info('SS to update only for ' + str( ss_joined[ss_joined['flag'] != 'values same'].shape[0])) ss_to_upload["id"] = ss_to_upload["id"].astype(float) data_to_be_updated_list = list(ss_to_upload.apply(dict, axis=1)) if len(data_to_be_updated_list) > 0: chunk_size = 1000 for i in range(0, len(data_to_be_updated_list), chunk_size): status, msg = sql.update( {'table': 'DrugOrderInfoData', 'data_to_be_updated': data_to_be_updated_list[ i:i + chunk_size]}, logger) logger.info(f"DrugOrderInfoData update API " f"count: {min(i + chunk_size, len(data_to_be_updated_list))}, " f"status: {status}, msg: {msg}") drug_list = str(list(ss_joined.loc[ ss_joined[ 'flag'] == 'values changed', 'drug_id'].unique()) ).replace('[', '(').replace(']', ')') update_test_query = f""" SELECT `store-id` , `drug-id` , min , `safe-stock` , max from `drug-order-info-data` doid where `store-id` = {store_id} and `drug-id` in {drug_list} """ # time.sleep(15) update_test = pd.read_sql(update_test_query, mysql.connection) update_test.columns = [c.replace('-', '_') for c in update_test.columns] update_test = ss_joined.loc[ ss_joined['flag'] == 'values changed', ['store_id', 'drug_id', 'min', 'safe_stock', 'max']].merge( update_test, on=['store_id', 'drug_id'], suffixes=('_new', '_prod')) update_test['mismatch_flag'] = np.where( (update_test['min_new'] == update_test['min_prod']) & (update_test['safe_stock_new'] == update_test[ 'safe_stock_prod']) & (update_test['max_new'] == update_test['max_prod']), 'updated', 'not updated') missed_entries = missed_entries.append( update_test[update_test['mismatch_flag'] == 'not updated']) logger.info( 'Entries updated successfully: ' + str(update_test[ update_test['mismatch_flag'] == 'updated'].shape[0])) logger.info( 'Entries not updated successfully: ' + str(update_test[ update_test['mismatch_flag'] == 'not updated'].shape[ 0])) mysql.close() return missed_entries
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/doid_write.py
doid_write.py
import datetime import numpy as np import pandas as pd # from memory_profiler import profile def remove_duplicates(df: pd.DataFrame, f): """ #remove duplicates from dataframe of the form id, f, ptr #f corresponds to quantity which is added up for duplicate ids #ptr remains same for all """ print(f"Removed duplicates on column: {f}") df1 = df.groupby('id', as_index=False)[f].sum() df2 = df.drop_duplicates(subset='id').drop(columns=f) df = pd.merge(left=df1, right=df2, on='id', how='left') return df def getids(df: pd.DataFrame): """ utility function to generate string to be used in "in" query """ return ",".join(str(i) for i in df['id'].unique()) def combin_xin(recon_l: pd.DataFrame, xin_l: pd.DataFrame): """ this will take care of stores own inventory coming back """ return pd.concat([recon_l, xin_l], axis=0).drop_duplicates(subset='id') class Data: """ class to get the inventory related data Example: 'o', 'cr', 'xin', 'xout', 'ret', 'sold', 'del', 'ar', 'rr', 'c' """ def __init__(self, db, csv_store_ids, start_date, end_date): """ :param db: database connection :param csv_store_ids: multiple store ids in csv :param start_date: start date in IST :param end_date: end date in IST """ self.db = db self.csv_store_ids = csv_store_ids self.start_ts = f"{start_date} 02:00:00" # in IST self.end_ts = f"{end_date} 03:00:00" # in IST """ since snapshots names are in UTC so tables alias is one day back""" start_date_utc = datetime.datetime.strptime(start_date, '%Y-%m-%d') + datetime.timedelta( days=-1) start_date_utc = start_date_utc.strftime("%Y-%m-%d") end_date_utc = datetime.datetime.strptime(end_date, '%Y-%m-%d') + datetime.timedelta( days=-1) end_date_utc = end_date_utc.strftime("%Y-%m-%d") self.s_alias = f"-mis-{start_date_utc}" if start_date == "2022-06-01": # Only for 2022-06-01 manual snapshot, since snapshot name and date are same self.s_alias = f"-mis-{start_date}" self.e_alias = f"-mis-{end_date_utc}" self.s_schema = "public" self.e_schema = "public" """ Data frames """ self.recon_l = pd.DataFrame() # Final reconciled data frame self.p_l = pd.DataFrame() # purchased / received self.o_l = pd.DataFrame() # opening / initial self.cr_l = pd.DataFrame() # self.xin_l = pd.DataFrame() # self.cin_l = pd.DataFrame() # cluster stock transfer in self.xout_l = pd.DataFrame() # self.cout_l = pd.DataFrame() # cluster stock transfer out self.sold_l = pd.DataFrame() # self.ret_l = pd.DataFrame() # self.ar_l = pd.DataFrame() # self.rr_l = pd.DataFrame() # self.del_l = pd.DataFrame() # self.c_l = pd.DataFrame() # def take_union(self): """ select one value of barcode from left or right data frame """ for col in ['barcode', 'ptr']: self.recon_l[col] = np.where(self.recon_l[f'{col}_x'].isna(), self.recon_l[f'{col}_y'], self.recon_l[f'{col}_x']) self.recon_l.drop(columns=[f'{col}_x', f'{col}_y'], axis=1, inplace=True) def opening(self): """ opening inventory calculation """ q = f""" select id, nvl("barcode-reference", 0) barcode, quantity o, ptr from "{self.s_schema}"."inventory-1{self.s_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is null and quantity != 0 order by id """ o_l_1 = self.db.get_df(query=q) q = f""" select id, nvl("barcode-reference", 0) barcode, quantity o, ptr from "{self.s_schema}"."inventory-1{self.s_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is not null and quantity != 0 order by id """ o_l_2 = self.db.get_df(query=q) self.o_l = pd.concat([o_l_1, o_l_2], ignore_index=True) return self.o_l def purchased(self): """ purchased inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."actual-quantity" p, c.ptr from "{self.e_schema}"."invoice-items-1{self.e_alias}" a join "{self.e_schema}"."invoices-1{self.e_alias}" b on a."franchisee-invoice-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (c."invoice-item-id" = a.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a."actual-quantity" !=0 """ self.p_l = self.db.get_df(query=q) return self.p_l def customer_returns(self): """ customer return inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" cr, c.ptr from "{self.e_schema}"."customer-return-items-1{self.e_alias}" a join "{self.e_schema}"."customer-returns-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."returned-at" >= '{self.start_ts}' and b."returned-at" <= '{self.end_ts}' and a."returned-quantity" !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" cr, c.ptr from "{self.e_schema}"."customer-return-items-1{self.e_alias}" a join "{self.e_schema}"."customer-returns-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."returned-at" >= '{self.start_ts}' and b."returned-at" <= '{self.end_ts}' and a."returned-quantity" !=0 """ self.cr_l = self.db.get_df(query=q) self.cr_l = remove_duplicates(df=self.cr_l, f="cr") return self.cr_l def xin(self): """ Stock transfer in - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a.quantity xin, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."destination-store") where b."destination-store" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a.quantity xin, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."destination-store") where b."destination-store" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a.quantity !=0 """ self.xin_l = self.db.get_df(query=q) self.xin_l = remove_duplicates(df=self.xin_l, f="xin") return self.xin_l def cin(self): """ Cluster Stock Transfer in - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a.quantity cin, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."destination-store") join "{self.e_schema}"."cluster-tasks{self.e_alias}" ct on (a."transfer-id" = ct."stock-transfer-id" and ct."task-type" = 'stock-transfer') where b."destination-store" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a.quantity cin, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."destination-store") join "{self.e_schema}"."cluster-tasks{self.e_alias}" ct on (a."transfer-id" = ct."stock-transfer-id" and ct."task-type" = 'stock-transfer') where b."destination-store" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a.quantity !=0 """ self.cin_l = self.db.get_df(query=q) self.cin_l = remove_duplicates(df=self.cin_l, f="cin") return self.cin_l def xout(self): """ Stock transfer out inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."quantity" xout, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."source-store") where b."source-store" in ({self.csv_store_ids}) and a."transferred-at" >= '{self.start_ts}' and a."transferred-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."quantity" xout, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."source-store") where b."source-store" in ({self.csv_store_ids}) and a."transferred-at" >= '{self.start_ts}' and a."transferred-at" <= '{self.end_ts}' and a.quantity !=0 """ self.xout_l = self.db.get_df(query=q) self.xout_l = remove_duplicates(self.xout_l, "xout") return self.xout_l def cout(self): """ Cluster Stock Transfer out inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."quantity" cout, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."source-store") join "{self.e_schema}"."cluster-tasks{self.e_alias}" ct on (a."transfer-id" = ct."stock-transfer-id" and ct."task-type" = 'stock-transfer') where b."source-store" in ({self.csv_store_ids}) and a."transferred-at" >= '{self.start_ts}' and a."transferred-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."quantity" cout, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."source-store") join "{self.e_schema}"."cluster-tasks{self.e_alias}" ct on (a."transfer-id" = ct."stock-transfer-id" and ct."task-type" = 'stock-transfer') where b."source-store" in ({self.csv_store_ids}) and a."transferred-at" >= '{self.start_ts}' and a."transferred-at" <= '{self.end_ts}' and a.quantity !=0 """ self.cout_l = self.db.get_df(query=q) self.cout_l = remove_duplicates(self.cout_l, "cout") return self.cout_l def sold(self): """ Sold inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."quantity" sold, c.ptr from "{self.e_schema}"."bill-items-1{self.e_alias}" a join "{self.e_schema}"."bills-1{self.e_alias}" b on a."bill-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."quantity" sold, c.ptr from "{self.e_schema}"."bill-items-1{self.e_alias}" a join "{self.e_schema}"."bills-1{self.e_alias}" b on a."bill-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a.quantity !=0 """ self.sold_l = self.db.get_df(query=q) self.sold_l = remove_duplicates(self.sold_l, "sold") return self.sold_l def returned_to_dc(self): """ Return to dc - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" ret, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a."returned-quantity" !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" ret, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a."returned-quantity" !=0 """ self.ret_l = self.db.get_df(query=q) self.ret_l = remove_duplicates(self.ret_l, "ret") return self.ret_l def deleted(self): """ Deleted - inventory calculation """ q = f""" select a.id, nvl("barcode-reference", 0) barcode, a.quantity del, a.ptr from "{self.s_schema}"."inventory-1{self.s_alias}" a join "{self.e_schema}"."deleted-invoices{self.e_alias}" c on a."invoice-id" = c.id where a."store-id" in ({self.csv_store_ids}) and c."deleted-at" >= '{self.start_ts}' and c."deleted-at" <= '{self.end_ts}' and a.quantity !=0 union select a.id, nvl("barcode-reference", 0) barcode, a.quantity del, a.ptr from "{self.s_schema}"."inventory-1{self.s_alias}" a join "{self.e_schema}"."deleted-invoices-1{self.e_alias}" c on a."franchisee-invoice-id" = c.id where a."store-id" in ({self.csv_store_ids}) and c."deleted-at" >= '{self.start_ts}' and c."deleted-at" <= '{self.end_ts}' and a.quantity !=0 """ self.del_l = self.db.get_df(query=q) self.del_l = remove_duplicates(self.del_l, "del") return self.del_l def closing(self): """ Closing inventory calculation """ q = f""" select id, nvl("barcode-reference", 0) barcode, quantity c, ptr from "{self.e_schema}"."inventory-1{self.e_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is null and quantity !=0 order by id """ c_l_1 = self.db.get_df(query=q) q = f""" select id, nvl("barcode-reference", 0) barcode, quantity c, ptr from "{self.e_schema}"."inventory-1{self.e_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is not null and quantity !=0 order by id """ c_l_2 = self.db.get_df(query=q) self.c_l = pd.concat([c_l_1, c_l_2], ignore_index=True) return self.c_l def audit_recon(self): """ Audit recon - inventory calculation """ q = f""" select b.id, nvl("barcode-reference", 0) barcode, a.change ar, b.ptr from "{self.e_schema}"."inventory-changes-1{self.e_alias}" a join "{self.e_schema}"."inventory-1{self.e_alias}" b on (a."inventory-id" = b.id and b."store-id" = a."store-id") where a."store-id" in ({self.csv_store_ids}) and a."created-at" >= '{self.start_ts}' and a."created-at" <= '{self.end_ts}' and a.change !=0 union all select b.id, nvl("barcode-reference", 0) barcode, a.change ar, b.ptr from "{self.e_schema}"."inventory-changes-1{self.e_alias}" a join "{self.e_schema}"."inventory-1{self.e_alias}" b on (a."inventory-id" = b."barcode-reference" and b."store-id" = a."store-id") where a."store-id" in ({self.csv_store_ids}) and a."created-at" >= '{self.start_ts}' and a."created-at" <= '{self.end_ts}' and a.change !=0 """ self.ar_l = self.db.get_df(query=q) self.ar_l = remove_duplicates(self.ar_l, "ar") return self.ar_l def reverted_returns(self): """ Reverted returns - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" rr, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and a."reverted-at" >= '{self.start_ts}' and a."reverted-at" <= '{self.end_ts}' and a."returned-quantity" !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" rr, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and a."reverted-at" >= '{self.start_ts}' and a."reverted-at" <= '{self.end_ts}' and a."returned-quantity" !=0 """ self.rr_l = self.db.get_df(query=q) self.rr_l = remove_duplicates(self.rr_l, "rr") return self.rr_l def get_meta_data(self): """ extra data needed for inventory """ q = f""" select i.id, i."purchase-rate" , d."drug-name" from "prod2-generico"."prod2-generico"."inventory-1" i left join "prod2-generico"."prod2-generico".drugs d on i."drug-id" = d.id where i."store-id" in ({self.csv_store_ids}) """ return self.db.get_df(query=q) # @profile def start_data_fetch(self): """ calls all the function which fetch the data from database """ print("Starting data fetch.") self.opening() print("opening: Successfully fetched.") self.purchased() print("purchased: Successfully fetched.") self.customer_returns() print("customer_returns: Successfully fetched.") self.xin() print("xin: Successfully fetched.") self.cin() print("cin: Successfully fetched.") self.xout() print("xout: Successfully fetched.") self.cout() print("cout: Successfully fetched.") self.sold() print("sold: Successfully fetched.") self.returned_to_dc() print("returned_to_dc: Successfully fetched.") self.deleted() print("deleted: Successfully fetched.") self.closing() print("closing: Successfully fetched.") self.audit_recon() print("audit_recon: Successfully fetched.") self.reverted_returns() print("reverted_returns: Successfully fetched.") # @profile def concat(self): """ data fetching from database """ self.start_data_fetch() """ ## combine initial and received temp_l = select(p_l, :id, :barcode, :p => :o, :ptr) recon_l = vcat(o_l, temp_l) ## following handles inventory lying in inventory-1 but received later recon_l = remove_duplicates(recon_l, "o") recon_l = combine_cr(recon_l, cr_l) recon_l = combine_xin(recon_l, xin_l) recon_l = combine_xout(recon_l, xout_l) recon_l = combine_sold(recon_l, sold_l) recon_l = combine_ret(recon_l, ret_l) recon_l = combine_ar(recon_l, ar_l) recon_l = combine_rr(recon_l, rr_l) recon_l = leftjoin(recon_l, select(del_l, :id, :del), on = :id) recon_l = leftjoin(recon_l, select(c_l, :id, :c), on = :id) """ """ combine initial and received and call it opening(o) """ self.p_l.rename(columns={'p': 'o'}, inplace=True) """ following handles inventory lying in inventory-1 but received later """ self.recon_l = pd.concat([self.p_l, self.o_l], ignore_index=True) self.recon_l = remove_duplicates(self.recon_l, "o") """combine_cr: following handles the case where inventory was stock transferred, after the start time and returned before end time """ self.recon_l = pd.merge(self.recon_l, self.cr_l, on='id', how='outer') self.take_union() """combine_xin: this will take care of stores own inventory coming back""" self.recon_l = pd.merge(self.recon_l, self.xin_l, on='id', how='outer') self.take_union() """combine_cin: this will take care of stores own inventory coming back from cluster """ self.recon_l = pd.merge(self.recon_l, self.cin_l, on='id', how='outer') self.take_union() """combine_xout: this will take care of stores own inventory transferred out""" self.recon_l = pd.merge(self.recon_l, self.xout_l, on='id', how='outer') self.take_union() """combine_cout: this will take care of stores own inventory transferred out from cluster""" self.recon_l = pd.merge(self.recon_l, self.cout_l, on='id', how='outer') self.take_union() """combine_sold: this will take care of stores inventory sold """ self.recon_l = pd.merge(self.recon_l, self.sold_l, on='id', how='outer') self.take_union() """combine_ret: this will take care of stores inventory returned """ self.recon_l = pd.merge(self.recon_l, self.ret_l, on='id', how='outer') self.take_union() """combine_ar: """ self.recon_l = pd.merge(self.recon_l, self.ar_l, on='id', how='outer') self.take_union() """combine_rr: """ self.recon_l = pd.merge(self.recon_l, self.rr_l, on='id', how='outer') self.take_union() self.recon_l = pd.merge(self.recon_l, self.del_l, on='id', how='left') self.take_union() self.recon_l = pd.merge(self.recon_l, self.c_l, on='id', how='left') self.take_union() """ calculate the error """ self.recon_l = self.recon_l.fillna(0) for col in ['id', 'o', 'cr', 'xin', 'cin', 'xout', 'cout', 'ret', 'sold', 'del', 'ar', 'rr', 'c', 'barcode']: self.recon_l[col] = pd.to_numeric(self.recon_l[col]) self.recon_l[col] = self.recon_l[col].astype('int', errors='raise') """ since cin and cout are sub set of xin xout so will not be part of error calculation """ self.recon_l['e'] = self.recon_l['o'] + self.recon_l['cr'] + self.recon_l['xin'] - \ self.recon_l['xout'] - \ self.recon_l['ret'] - self.recon_l['sold'] - self.recon_l['del'] + \ self.recon_l['ar'] + \ self.recon_l['rr'] - self.recon_l['c'] return self.recon_l
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/inventory/inventory.py
inventory.py
import datetime import numpy as np # from memory_profiler import profile import pandas as pd def remove_duplicates(df: pd.DataFrame, f): """ #remove duplicates from dataframe of the form id, f, ptr #f corresponds to quantity which is added up for duplicate ids #ptr remains same for all """ print(f"Removed duplicates on column: {f}") df1 = df.groupby('id', as_index=False)[f].sum() df2 = df.drop_duplicates(subset='id').drop(columns=f) df = pd.merge(left=df1, right=df2, on='id', how='left') return df def getids(df: pd.DataFrame): """ utility function to generate string to be used in "in" query """ return ",".join(str(i) for i in df['id'].unique()) def combin_xin(recon_l: pd.DataFrame, xin_l: pd.DataFrame): """ this will take care of stores own inventory coming back """ return pd.concat([recon_l, xin_l], axis=0).drop_duplicates(subset='id') class Data: """ class to get the inventory related data Example: 'o', 'cr', 'xin', 'xout', 'ret', 'sold', 'del', 'ar', 'rr', 'c' """ def __init__(self, db, csv_store_ids, start_date, end_date, snapshot_ist_time_delta=0): """ :param db: database connection :param csv_store_ids: multiple store ids in csv :param start_date: start date in IST :param end_date: end date in IST """ self.db = db self.csv_store_ids = csv_store_ids self.start_ts = f"{start_date} 02:00:00" # in IST self.end_ts = f"{end_date} 03:00:00" # in IST """ since snapshots names are in UTC so tables alias is one day back""" start_date_utc = datetime.datetime.strptime(start_date, '%Y-%m-%d') - datetime.timedelta( days=snapshot_ist_time_delta) start_date_utc = start_date_utc.strftime("%Y-%m-%d") end_date_utc = datetime.datetime.strptime(end_date, '%Y-%m-%d') - datetime.timedelta( days=snapshot_ist_time_delta) end_date_utc = end_date_utc.strftime("%Y-%m-%d") self.s_alias = f"-mis-{start_date_utc}" if start_date == "2022-06-01": # Only for 2022-06-01 manual snapshot, since snapshot name and date are same self.s_alias = f"-mis-{start_date}" self.e_alias = f"-mis-{end_date_utc}" self.s_schema = "public" self.e_schema = "public" """ Data frames """ self.recon_l = pd.DataFrame() # Final reconciled data frame self.p_l = pd.DataFrame() # purchased / received self.prd_l = pd.DataFrame() # purchased return dispatched self.prs_l = pd.DataFrame() # purchased return settled self.o_l = pd.DataFrame() # opening / initial self.cr_l = pd.DataFrame() # self.xin_l = pd.DataFrame() # self.xout_l = pd.DataFrame() # self.sold_l = pd.DataFrame() # self.ret_l = pd.DataFrame() # self.ar_l = pd.DataFrame() # self.rr_l = pd.DataFrame() # self.del_l = pd.DataFrame() # self.c_l = pd.DataFrame() # def take_union(self): """ select one value of barcode from left or right data frame """ for col in ['barcode', 'ptr']: self.recon_l[col] = np.where(self.recon_l[f'{col}_x'].isna(), self.recon_l[f'{col}_y'], self.recon_l[f'{col}_x']) self.recon_l.drop(columns=[f'{col}_x', f'{col}_y'], axis=1, inplace=True) def opening(self): """ opening inventory calculation """ q = f""" select id, nvl("barcode-reference", 0) barcode, quantity o, ptr from "{self.s_schema}"."inventory-1{self.s_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is null and quantity != 0 order by id """ o_l_1 = self.db.get_df(query=q) q = f""" select id, nvl("barcode-reference", 0) barcode, quantity o, ptr from "{self.s_schema}"."inventory-1{self.s_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is not null and quantity != 0 order by id """ o_l_2 = self.db.get_df(query=q) self.o_l = pd.concat([o_l_1, o_l_2], ignore_index=True) return self.o_l def purchased(self): """ purchased inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."actual-quantity" p, c.ptr from "{self.e_schema}"."invoice-items-1{self.e_alias}" a join "{self.e_schema}"."invoices-1{self.e_alias}" b on a."franchisee-invoice-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (c."invoice-item-id" = a.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a."actual-quantity" !=0 """ self.p_l = self.db.get_df(query=q) return self.p_l def purchased_return_dispatched(self): """ purchased return dispatched inventory calculation """ q = f""" select d.id , nvl("barcode-reference", 0) barcode, c."returned-quantity" prd, d."ptr" from "{self.e_schema}"."debit-notes-1{self.e_alias}" a join "{self.e_schema}"."debit-note-items-1{self.e_alias}" b on b."debit-note-id" = a.id join "{self.e_schema}"."return-items-1{self.e_alias}" c on b."item-id" = c.id join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" e on c."return-id" = e.id join "{self.e_schema}"."inventory-1{self.e_alias}" d on c."inventory-id" = d.id where e."store-id" in ({self.csv_store_ids}) and a."dispatched-at" >= '{self.start_ts}' and a."dispatched-at" <= '{self.end_ts}' """ self.prd_l = self.db.get_df(query=q) return self.prd_l def purchased_return_settled(self): """ purchased return settled inventory calculation """ q = f""" select d.id , nvl("barcode-reference", 0) barcode, c."returned-quantity" prs, d."ptr" from "{self.e_schema}"."debit-notes-1{self.e_alias}" a join "{self.e_schema}"."debit-note-items-1{self.e_alias}" b on b."debit-note-id" = a.id join "{self.e_schema}"."return-items-1{self.e_alias}" c on b."item-id" = c.id join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" e on c."return-id" = e.id join "{self.e_schema}"."inventory-1{self.e_alias}" d on c."inventory-id" = d.id where e."store-id" in ({self.csv_store_ids}) and a."settled-at" >= '{self.start_ts}' and a."settled-at" <= '{self.end_ts}' """ self.prs_l = self.db.get_df(query=q) return self.prs_l def customer_returns(self): """ customer return inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" cr, c.ptr from "{self.e_schema}"."customer-return-items-1{self.e_alias}" a join "{self.e_schema}"."customer-returns-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."returned-at" >= '{self.start_ts}' and b."returned-at" <= '{self.end_ts}' and a."returned-quantity" !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" cr, c.ptr from "{self.e_schema}"."customer-return-items-1{self.e_alias}" a join "{self.e_schema}"."customer-returns-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."returned-at" >= '{self.start_ts}' and b."returned-at" <= '{self.end_ts}' and a."returned-quantity" !=0 """ self.cr_l = self.db.get_df(query=q) self.cr_l = remove_duplicates(df=self.cr_l, f="cr") return self.cr_l def xin(self): """ Stock transfer in - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a.quantity xin, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."destination-store") where b."destination-store" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a.quantity xin, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."destination-store") where b."destination-store" in ({self.csv_store_ids}) and b."received-at" >= '{self.start_ts}' and b."received-at" <= '{self.end_ts}' and a.quantity !=0 """ self.xin_l = self.db.get_df(query=q) self.xin_l = remove_duplicates(df=self.xin_l, f="xin") return self.xin_l def xout(self): """ Stock transfer out inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."quantity" xout, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."source-store") where b."source-store" in ({self.csv_store_ids}) and a."transferred-at" >= '{self.start_ts}' and a."transferred-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."quantity" xout, c.ptr from "{self.e_schema}"."stock-transfer-items-1{self.e_alias}" a join "{self.e_schema}"."stock-transfers-1{self.e_alias}" b on a."transfer-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."source-store") where b."source-store" in ({self.csv_store_ids}) and a."transferred-at" >= '{self.start_ts}' and a."transferred-at" <= '{self.end_ts}' and a.quantity !=0 """ self.xout_l = self.db.get_df(query=q) self.xout_l = remove_duplicates(self.xout_l, "xout") return self.xout_l def sold(self): """ Sold inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."quantity" sold, c.ptr from "{self.e_schema}"."bill-items-1{self.e_alias}" a join "{self.e_schema}"."bills-1{self.e_alias}" b on a."bill-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a.quantity !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."quantity" sold, c.ptr from "{self.e_schema}"."bill-items-1{self.e_alias}" a join "{self.e_schema}"."bills-1{self.e_alias}" b on a."bill-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a.quantity !=0 """ self.sold_l = self.db.get_df(query=q) self.sold_l = remove_duplicates(self.sold_l, "sold") return self.sold_l def returned_to_dc(self): """ Return to dc - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" ret, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a."returned-quantity" !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" ret, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and b."created-at" >= '{self.start_ts}' and b."created-at" <= '{self.end_ts}' and a."returned-quantity" !=0 """ self.ret_l = self.db.get_df(query=q) self.ret_l = remove_duplicates(self.ret_l, "ret") return self.ret_l def deleted(self): """ Deleted - inventory calculation """ q = f""" select a.id, nvl("barcode-reference", 0) barcode, a.quantity del, a.ptr from "{self.s_schema}"."inventory-1{self.s_alias}" a join "{self.e_schema}"."deleted-invoices{self.e_alias}" c on a."invoice-id" = c.id where a."store-id" in ({self.csv_store_ids}) and c."deleted-at" >= '{self.start_ts}' and c."deleted-at" <= '{self.end_ts}' and a.quantity !=0 union select a.id, nvl("barcode-reference", 0) barcode, a.quantity del, a.ptr from "{self.s_schema}"."inventory-1{self.s_alias}" a join "{self.e_schema}"."deleted-invoices-1{self.e_alias}" c on a."franchisee-invoice-id" = c.id where a."store-id" in ({self.csv_store_ids}) and c."deleted-at" >= '{self.start_ts}' and c."deleted-at" <= '{self.end_ts}' and a.quantity !=0 """ self.del_l = self.db.get_df(query=q) self.del_l = remove_duplicates(self.del_l, "del") return self.del_l def closing(self): """ Closing inventory calculation """ q = f""" select id, nvl("barcode-reference", 0) barcode, quantity c, ptr from "{self.e_schema}"."inventory-1{self.e_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is null and quantity !=0 order by id """ c_l_1 = self.db.get_df(query=q) q = f""" select id, nvl("barcode-reference", 0) barcode, quantity c, ptr from "{self.e_schema}"."inventory-1{self.e_alias}" where "store-id" in ({self.csv_store_ids}) and "barcode-reference" is not null and quantity !=0 order by id """ c_l_2 = self.db.get_df(query=q) self.c_l = pd.concat([c_l_1, c_l_2], ignore_index=True) return self.c_l def audit_recon(self): """ Audit recon - inventory calculation """ q = f""" select b.id, nvl("barcode-reference", 0) barcode, a.change ar, b.ptr from "{self.e_schema}"."inventory-changes-1{self.e_alias}" a join "{self.e_schema}"."inventory-1{self.e_alias}" b on (a."inventory-id" = b.id and b."store-id" = a."store-id") where a."store-id" in ({self.csv_store_ids}) and a."created-at" >= '{self.start_ts}' and a."created-at" <= '{self.end_ts}' and a.change !=0 union all select b.id, nvl("barcode-reference", 0) barcode, a.change ar, b.ptr from "{self.e_schema}"."inventory-changes-1{self.e_alias}" a join "{self.e_schema}"."inventory-1{self.e_alias}" b on (a."inventory-id" = b."barcode-reference" and b."store-id" = a."store-id") where a."store-id" in ({self.csv_store_ids}) and a."created-at" >= '{self.start_ts}' and a."created-at" <= '{self.end_ts}' and a.change !=0 """ self.ar_l = self.db.get_df(query=q) self.ar_l = remove_duplicates(self.ar_l, "ar") return self.ar_l def reverted_returns(self): """ Reverted returns - inventory calculation """ q = f""" select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" rr, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c.id and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and a."reverted-at" >= '{self.start_ts}' and a."reverted-at" <= '{self.end_ts}' and a."returned-quantity" !=0 union all select c.id, nvl("barcode-reference", 0) barcode, a."returned-quantity" rr, c.ptr from "{self.e_schema}"."return-items-1{self.e_alias}" a join "{self.e_schema}"."returns-to-dc-1{self.e_alias}" b on a."return-id" = b.id join "{self.e_schema}"."inventory-1{self.e_alias}" c on (a."inventory-id" = c."barcode-reference" and c."store-id" = b."store-id") where b."store-id" in ({self.csv_store_ids}) and a."reverted-at" >= '{self.start_ts}' and a."reverted-at" <= '{self.end_ts}' and a."returned-quantity" !=0 """ self.rr_l = self.db.get_df(query=q) self.rr_l = remove_duplicates(self.rr_l, "rr") return self.rr_l def get_meta_data(self): """ extra data needed for inventory """ q = f""" select i.id, i."purchase-rate" , d."drug-name" from "prod2-generico"."prod2-generico"."inventory-1" i left join "prod2-generico"."prod2-generico".drugs d on i."drug-id" = d.id where i."store-id" in ({self.csv_store_ids}) """ return self.db.get_df(query=q) # @profile def start_data_fetch(self): """ calls all the function which fetch the data from database """ print("Starting data fetch.") self.opening() print("opening: Successfully fetched.") self.purchased() print("purchased: Successfully fetched.") self.purchased_return_dispatched() print("purchased_return_dispatched : Successfully fetched.") self.purchased_return_settled() print("purchased_return_settled : Successfully fetched.") self.customer_returns() print("customer_returns: Successfully fetched.") self.xin() print("xin: Successfully fetched.") self.xout() print("xout: Successfully fetched.") self.sold() print("sold: Successfully fetched.") self.returned_to_dc() print("returned_to_dc: Successfully fetched.") self.deleted() print("deleted: Successfully fetched.") self.closing() print("closing: Successfully fetched.") self.audit_recon() print("audit_recon: Successfully fetched.") self.reverted_returns() print("reverted_returns: Successfully fetched.") # @profile def concat(self): """ data fetching from database """ self.start_data_fetch() """ ## combine initial and received temp_l = select(p_l, :id, :barcode, :p => :o, :ptr) recon_l = vcat(o_l, temp_l) ## following handles inventory lying in inventory-1 but received later recon_l = remove_duplicates(recon_l, "o") recon_l = combine_cr(recon_l, cr_l) recon_l = combine_xin(recon_l, xin_l) recon_l = combine_xout(recon_l, xout_l) recon_l = combine_sold(recon_l, sold_l) recon_l = combine_ret(recon_l, ret_l) recon_l = combine_ar(recon_l, ar_l) recon_l = combine_rr(recon_l, rr_l) recon_l = leftjoin(recon_l, select(del_l, :id, :del), on = :id) recon_l = leftjoin(recon_l, select(c_l, :id, :c), on = :id) """ # """ combine initial and received and call it opening(o) """ # self.p_l.rename(columns={'p': 'o'}, inplace=True) # self.recon_l = remove_duplicates(self.o_l, f="o") self.recon_l = self.o_l # """ following handles inventory lying in inventory-1 but received later """ # self.recon_l = pd.concat([self.p_l, self.o_l], ignore_index=True) # self.recon_l = remove_duplicates(self.recon_l, "o") """ purchase """ self.recon_l = pd.merge(self.recon_l, self.p_l, on='id', how='outer') self.take_union() # """ purchase_return_deleted """ # self.recon_l = pd.merge(self.recon_l, self.prd_l, on='id', how='outer') # self.take_union() # # """ purchase_return_settled """ # self.recon_l = pd.merge(self.recon_l, self.prs_l, on='id', how='outer') # self.take_union() self.recon_l['prd'] = 0 self.recon_l['prs'] = 0 """combine_cr: following handles the case where inventory was stock transferred, after the start time and returned before end time """ self.recon_l = pd.merge(self.recon_l, self.cr_l, on='id', how='outer') self.take_union() """combine_xin: this will take care of stores own inventory coming back""" self.recon_l = pd.merge(self.recon_l, self.xin_l, on='id', how='outer') self.take_union() """combine_xout: this will take care of stores own inventory transferred out""" self.recon_l = pd.merge(self.recon_l, self.xout_l, on='id', how='outer') self.take_union() """combine_sold: this will take care of stores inventory sold """ self.recon_l = pd.merge(self.recon_l, self.sold_l, on='id', how='outer') self.take_union() """combine_ret: this will take care of stores inventory returned """ self.recon_l = pd.merge(self.recon_l, self.ret_l, on='id', how='outer') self.take_union() """combine_ar: """ self.recon_l = pd.merge(self.recon_l, self.ar_l, on='id', how='outer') self.take_union() """combine_rr: """ self.recon_l = pd.merge(self.recon_l, self.rr_l, on='id', how='outer') self.take_union() """ deleted """ self.recon_l = pd.merge(self.recon_l, self.del_l, on='id', how='left') self.take_union() """ closing """ self.recon_l = pd.merge(self.recon_l, self.c_l, on='id', how='left') self.take_union() """ calculate the error """ self.recon_l = self.recon_l.fillna(0) for col in ['id', 'o', 'p', 'prd', 'prs', 'cr', 'xin', 'xout', 'ret', 'sold', 'del', 'ar', 'rr', 'c', 'barcode']: self.recon_l[col] = pd.to_numeric(self.recon_l[col]) self.recon_l[col] = self.recon_l[col].astype('int', errors='raise') self.recon_l['e'] = self.recon_l['o'] + self.recon_l['p'] + self.recon_l['cr'] + \ self.recon_l['xin'] - \ self.recon_l['xout'] - \ self.recon_l['ret'] - self.recon_l['sold'] - self.recon_l['del'] + \ self.recon_l['ar'] + \ self.recon_l['rr'] - self.recon_l['c'] return self.recon_l
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/inventory/inventory_2.py
inventory_2.py
import numpy as np import datetime as dt """ Steps - 1. Get Auto short total time -> from creation to received at store 2. If marked as lost make it 7 days 3. Max LT capped at 7 days """ def lead_time(store_id, reset_date, db, schema, logger=None): end_date = str(( dt.datetime.strptime(reset_date, '%Y-%m-%d') - dt.timedelta(7)).date()) begin_date = str(( dt.datetime.strptime(reset_date, '%Y-%m-%d') - dt.timedelta(97)).date()) logger.info("Lead Time Calculation Starts") logger.info(f"SB Begin Date: {begin_date}, SB End Date: {end_date}") lead_time_query = f""" select "store-id" , "drug-id" , "type" , status , "created-to-delivery-hour" as "lt-hrs" from "{schema}"."as-ms" am where "as-ms" = 'AS' and "store-id" = {store_id} and date("created-at") <= '{end_date}' and date("created-at") >= '{begin_date}' and status not in ('failed', 'deleted') """ lead_time = db.get_df(lead_time_query) lead_time.columns = [c.replace('-', '_') for c in lead_time.columns] # classify all types into generic, ethical & others lead_time["type"] = np.where( lead_time["type"].isin(['ethical', 'high-value-ethical']), 'ethical', lead_time["type"]) lead_time["type"] = np.where(lead_time["type"].isin(['ethical', 'generic']), lead_time["type"], 'others') lead_time["lt_days"] = lead_time["lt_hrs"] / 24 lead_time["lt_days"] = lead_time["lt_days"].fillna(7) lead_time["lt_days"] = np.where(lead_time["lt_days"] > 7, 7, lead_time["lt_days"]) lt_store_mean = np.ceil(lead_time.lt_days.mean()) lt_store_std = round(lead_time.lt_days.std(ddof=0), 2) lt_drug = lead_time.groupby('drug_id'). \ agg({'lt_days': [np.mean, np.std]}).reset_index() lt_drug.columns = ['drug_id', 'lead_time_mean', 'lead_time_std'] lt_drug['lead_time_std'] = np.where( lt_drug['lead_time_std'].isin([0, np.nan]), lt_store_std, lt_drug['lead_time_std']) lt_drug["lead_time_mean"] = np.ceil(lt_drug["lead_time_mean"]) lt_drug['lead_time_std'] = round(lt_drug['lead_time_std'], 2) logger.info("Lead Time Calculation Completed") return lt_drug, lt_store_mean, lt_store_std
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/lead_time.py
lead_time.py
import numpy as np def compare_df(df_pre, df_post, logger, cols_to_compare=None): num_drugs = len(df_pre["drug_id"].unique()) if num_drugs != df_pre.shape[0]: logger.info("WARNING: Duplicate drug entries present!") if cols_to_compare is None: cols_to_compare = ["safety_stock", "reorder_point", "order_upto_point"] df_pre = df_pre[["drug_id"] + cols_to_compare] df_post = df_post[["drug_id"] + cols_to_compare] df_comb = df_pre.merge(df_post, on="drug_id", how="outer", suffixes=('_pre', '_post')) df_comb["changed"] = 'N' for col in cols_to_compare: df_comb["changed"] = np.where( df_comb[col+str('_pre')] != df_comb[col+str('_post')], 'Y', df_comb["changed"]) drugs_corrected = list(df_comb.loc[df_comb["changed"] == 'Y']["drug_id"].unique()) return drugs_corrected def add_correction_flag(df, corr_drug_list, corr_flag): if "correction_flags" not in df.columns: df["correction_flags"] = "" df["correction_flags"] = df["correction_flags"].fillna("") df["correction_flags"] = np.where( (df["drug_id"].isin(corr_drug_list)) & (df["correction_flags"] != ""), df["correction_flags"] + '-' + corr_flag, df["correction_flags"]) df["correction_flags"] = np.where( (df["drug_id"].isin(corr_drug_list)) & (df["correction_flags"] == ""), corr_flag, df["correction_flags"]) return df def compare_df_comb(df_pre, df_post, logger, cols_to_compare=None): num_drugs = len(df_pre["comb_id"].unique()) if num_drugs != df_pre.shape[0]: logger.info("WARNING: Duplicate comb entries present!") if cols_to_compare is None: cols_to_compare = ["safety_stock", "reorder_point", "order_upto_point"] df_pre = df_pre[["comb_id"] + cols_to_compare] df_post = df_post[["comb_id"] + cols_to_compare] df_comb = df_pre.merge(df_post, on="comb_id", how="outer", suffixes=('_pre', '_post')) df_comb["changed"] = 'N' for col in cols_to_compare: df_comb["changed"] = np.where( df_comb[col+str('_pre')] != df_comb[col+str('_post')], 'Y', df_comb["changed"]) comb_corrected = list(df_comb.loc[df_comb["changed"] == 'Y']["comb_id"].unique()) return comb_corrected def add_correction_flag_comb(df, corr_comb_list, corr_flag): if "correction_flags" not in df.columns: df["correction_flags"] = "" df["correction_flags"] = df["correction_flags"].fillna("") df["correction_flags"] = np.where( (df["comb_id"].isin(corr_comb_list)) & (df["correction_flags"] != ""), df["correction_flags"] + '-' + corr_flag, df["correction_flags"]) df["correction_flags"] = np.where( (df["comb_id"].isin(corr_comb_list)) & (df["correction_flags"] == ""), corr_flag, df["correction_flags"]) return df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/correction_flag.py
correction_flag.py
platform_prod = 0 store_col = 'store_id' drug_col = 'drug_id' comb_col = 'comb_id' date_col = 'date' target_col = 'actual_demand' key_col = 'ts_id' eol_cutoff = 13 mature_cutoff = 52 forecast_horizon = 4 flag_weather = 0 flag_seasonality_index = { 'ctb': 0, 'lgbm': 0, 'xgb': 0 } flag_sample_weights = { 'ctb': 0, 'lgbm': 0, 'xgb': 0 } num_shift_lag = 3 lags = [1] add_lags_diff_flag = 1 lags_diff = [(1, 2)] add_monthly_lags_flag = 1 # monthly_lags = [1, 2, 3, 6, 12] monthly_lags = [1, 2, 3, 6] rolling_time_feat = { 'lags': [5, 13, 26, 53], 'agg_func_dict': {'min', 'max', 'mean', 'median', 'std'} } ewma_lags = [4, 8] # trend_lags = [13, 26, 53] trend_lags = [13, 26] perc_noise = [0.2, 0.5, 0.1] # fc_cols = ['preds_xgb_rf_target','preds_cb_target','preds_lgb','AE', 'croston_fcst'] # models = ['croston', 'prophet', 'ETS', 'MA', 'AE_ts', 'lgbm'] run_ml_flag = 1 runs_ts_flag = 1 run_ts_4w_agg_flag = 1 run_ml_4w_agg_flag = 0 models_un_agg = ['ETS_Auto', 'ETS_12w', 'MA', 'LGBM'] models_agg = ['ETS_4w_agg', 'LGBM_4w_agg'] local_testing = 0 models = ['LGBM'] default_model = 'LGBM' # lgbm_params = { # 'objective': 'regression_l1', # 'learning_rate': 0.01, # 'max_bin': 404, # 'num_leaves': 1000, # 'lambda_l1': 0.003657033571790936, # 'lambda_l2': 1.203092568431234, # 'cat_l2': 5.192935907692467, # 'cat_smooth': 9.67794952387374, # 'feature_fraction': 0.997997647335764, # 'bagging_fraction': 0.9162909273820165, # 'bagging_freq': 7, # 'min_data_in_leaf': 33, # 'min_child_samples': 5, # 'metric': 'rmse', # 'boosting_type': 'gbdt', # 'max_depth': -1, # 'random_state': 42, # 'force_row_wise': True, # 'verbose': -1, # 'num_iterations': 1500 # } # fc_cols = ['croston_fcst', 'ETS_fcst', 'ma_fcst','prophet_fcst', 'AE_ts_fcst'] # cols_rename = { # 'preds_xgb_rf_target': 'fcst_1', # 'preds_cb_target': 'fcst_2', # 'preds_lgb':'fcst_3', # 'AE':'fcst_4', # 'croston_fcst':'fcst_5' # } # cols_rename = { # 'croston_fcst': 'fcst_1', # 'ETS_fcst': 'fcst_2', # 'ma_fcst':'fcst_3', # 'prophet_fcst':'fcst_4', # 'AE_ts_fcst':'fcst_5' # }
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/config_ipc_combination.py
config_ipc_combination.py
import pandas as pd import numpy as np def post_processing(store_id, safety_stock_df, seg_df_comb_lvl, seg_df_drug_lvl, schema, db, logger): seg_df_comb_lvl[['store_id', 'comb_id']] = seg_df_comb_lvl['ts_id'].str.split('_', expand=True) seg_df_drug_lvl[['store_id', 'drug_id']] = seg_df_drug_lvl['ts_id'].str.split('_', expand=True) seg_df_drug_lvl['store_id'] = seg_df_drug_lvl['store_id'].astype(int) seg_df_drug_lvl['drug_id'] = seg_df_drug_lvl['drug_id'].astype(int) list_drugs = safety_stock_df['drug_id'].tolist() str_drugs = str(list_drugs).replace('[', '(').replace(']', ')') q_drug_info = f""" select d.id as drug_id, "drug-name" as drug_name, type, composition from "{schema}".drugs d where d.id in {str_drugs} """ df_drug_info = db.get_df(q_drug_info) # get store name q_store_name = f""" select name from "{schema}".stores where id = {store_id} """ store_name = db.get_df(q_store_name)['name'][0] # get current inventory and avg_ptr info q_inv = f""" select "drug-id" as drug_id, sum("locked-quantity"+quantity+ "locked-for-audit"+"locked-for-transfer"+"locked-for-check"+ "locked-for-return") as curr_inventory from "{schema}"."inventory-1" i where "store-id" = {store_id} and "drug-id" in {str_drugs} group by "drug-id" """ df_inv = db.get_df(q_inv) q_avg_ptr_store = f""" select "drug-id" as drug_id, avg(ptr) as avg_ptr from "{schema}"."inventory-1" i where "store-id" = {store_id} and "drug-id" in {str_drugs} and DATEDIFF(day, date("created-at"), current_date) < 365 group by "drug-id" """ df_avg_ptr_store = db.get_df(q_avg_ptr_store) q_avg_ptr_sys = f""" select "drug-id" as drug_id, "avg-ptr" as avg_ptr_sys from "{schema}"."drug-std-info" dsi """ df_avg_ptr_sys = db.get_df(q_avg_ptr_sys) # add all to ss table safety_stock_df['store_id'] = store_id safety_stock_df['store_name'] = store_name safety_stock_df = safety_stock_df.merge( df_drug_info, on='drug_id', how='left') safety_stock_df = safety_stock_df.merge( df_inv, on='drug_id', how='left') safety_stock_df = safety_stock_df.merge( df_avg_ptr_store, on='drug_id', how='left') safety_stock_df = safety_stock_df.merge( df_avg_ptr_sys, on='drug_id', how='left') # replace NA in avg_ptr with system-avg_ptr safety_stock_df["avg_ptr"] = np.where(safety_stock_df["avg_ptr"].isna(), safety_stock_df["avg_ptr_sys"], safety_stock_df["avg_ptr"]) safety_stock_df.drop("avg_ptr_sys", axis=1, inplace=True) safety_stock_df["avg_ptr"] = safety_stock_df["avg_ptr"].astype(float) # calculate DOH safety_stock_df['fcst'] = safety_stock_df['fcst'].fillna(0) safety_stock_df['safety_stock_days'] = np.where( (safety_stock_df['fcst'] == 0) | (safety_stock_df['safety_stock'] == 0), 0, safety_stock_df['safety_stock'] / (safety_stock_df['fcst'] / 28)) safety_stock_df['reorder_days'] = np.where( (safety_stock_df['fcst'] == 0) | (safety_stock_df['reorder_point'] == 0), 0, safety_stock_df['reorder_point'] / (safety_stock_df['fcst'] / 28)) safety_stock_df['order_upto_days'] = np.where( (safety_stock_df['fcst'] == 0) | (safety_stock_df['order_upto_point'] == 0), 0, safety_stock_df['order_upto_point'] / (safety_stock_df['fcst'] / 28)) # calculate max-value, to-order-qty and to-order-val safety_stock_df["max_value"] = safety_stock_df['order_upto_point'] * \ safety_stock_df['avg_ptr'] safety_stock_df['curr_inventory'] = safety_stock_df['curr_inventory'].fillna(0) safety_stock_df['to_order_quantity'] = np.where( safety_stock_df['curr_inventory'] <= safety_stock_df['reorder_point'], safety_stock_df['order_upto_point'] - safety_stock_df['curr_inventory'], 0) safety_stock_df['to_order_value'] = safety_stock_df['to_order_quantity'] * \ safety_stock_df['avg_ptr'] safety_stock_df = safety_stock_df[[ 'store_id', 'store_name', 'comb_id', 'fcst_source', 'map_type', 'fcst_wt', 'bucket', 'model', 'drug_id', 'drug_name', 'type', 'composition', 'fcst', 'std', 'lead_time_mean', 'lead_time_std', 'safety_stock', 'reorder_point', 'order_upto_point', 'correction_flags', 'curr_inventory', 'avg_ptr', 'safety_stock_days', 'reorder_days', 'order_upto_days', 'max_value', 'to_order_quantity', 'to_order_value']] # formatting segmentation table seg_df_comb_lvl.rename(columns={'std': 'sales_std', 'cov': 'sales_cov', 'Mixed': 'bucket', 'Group': 'group', 'PLC Status L1': 'plc_status', 'ADI': 'adi', 'total_LY_sales': 'total_ly_sales', 'start_date': 'sale_start_date'}, inplace=True) seg_df_comb_lvl['sale_start_date'] = seg_df_comb_lvl['sale_start_date'].dt.date seg_df_comb_lvl['store_name'] = store_name seg_df_drug_lvl.rename(columns={'std': 'sales_std', 'cov': 'sales_cov', 'Mixed': 'bucket', 'Group': 'group', 'PLC Status L1': 'plc_status', 'ADI': 'adi', 'total_LY_sales': 'total_ly_sales', 'start_date': 'sale_start_date'}, inplace=True) seg_df_drug_lvl['sale_start_date'] = seg_df_drug_lvl[ 'sale_start_date'].dt.date seg_df_drug_lvl['store_name'] = store_name return safety_stock_df, seg_df_comb_lvl, seg_df_drug_lvl
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/post_processing.py
post_processing.py
import pandas as pd import numpy as np from zeno_etl_libs.utils.ipc_pmf.lead_time import lead_time from zeno_etl_libs.utils.ipc_pmf.heuristics.sl_heuristics import calculate_ss from zeno_etl_libs.utils.ipc_pmf.heuristics.ss_doh_heuristics import ss_doh_min_cap, ss_doh_max_cap from zeno_etl_libs.utils.ipc2.helpers.correction_flag import compare_df, \ add_correction_flag from zeno_etl_libs.utils.ipc2.heuristics.ipcv4_heuristics import v4_corrections def safety_stock_calc(df_fcst_drug, store_id, reset_date, v4_active_flag, drug_type_list_v4, drug_sales_latest_12w, schema, db, logger): fcst_weeks = 4 order_freq = 2 # ========================= LEAD TIME CALCULATIONS ========================= lt_drug, lt_store_mean, lt_store_std = lead_time( store_id, reset_date, db, schema, logger) safety_stock_df = df_fcst_drug.merge( lt_drug[['drug_id', 'lead_time_mean', 'lead_time_std']], how='left', on='drug_id') safety_stock_df['lead_time_mean'].fillna(lt_store_mean, inplace=True) safety_stock_df['lead_time_std'].fillna(lt_store_std, inplace=True) # ==================== SS, ROP, OUP CALCULATION BEGINS ===================== # impute store_std for cases where store-drug std<1 safety_stock_df['lead_time_std'] = np.where( safety_stock_df['lead_time_std'] < 1, lt_store_std, safety_stock_df['lead_time_std']) # calculate SS safety_stock_df = calculate_ss(safety_stock_df, fcst_weeks, logger) # MIN-SS-DOH CAPPING logger.info(f"DOH1 Correction starts") df_pre_corr = safety_stock_df.copy() safety_stock_df = ss_doh_min_cap(safety_stock_df) df_post_corr = safety_stock_df.copy() logger.info(f"Sum SS before: {df_pre_corr['safety_stock'].sum()}") logger.info(f"Sum SS after: {df_post_corr['safety_stock'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['safety_stock']) safety_stock_df = add_correction_flag(safety_stock_df, corr_drug_lst, 'DOH1') # MAX-SS-DOH CAPPING logger.info(f"DOH2 Correction starts") df_pre_corr = safety_stock_df.copy() safety_stock_df = ss_doh_max_cap(safety_stock_df) df_post_corr = safety_stock_df.copy() logger.info(f"Sum SS before: {df_pre_corr['safety_stock'].sum()}") logger.info(f"Sum SS after: {df_post_corr['safety_stock'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['safety_stock']) safety_stock_df = add_correction_flag(safety_stock_df, corr_drug_lst, 'DOH2') # calculate ROP - add lead time demand to SS safety_stock_df['reorder_point'] = safety_stock_df.apply( lambda row: np.round( row['lead_time_mean'] * row['fcst'] / fcst_weeks / 7), axis=1) + safety_stock_df['safety_stock'] # calculate OUP - add order_freq demand to ROP safety_stock_df['order_upto_point'] = ( safety_stock_df['reorder_point'] + np.round( np.where( # if rounding off give 0, increase it to 4-week forecast (safety_stock_df['reorder_point'] + safety_stock_df[ 'fcst'] * order_freq / fcst_weeks / 7 < 0.5) & (safety_stock_df['fcst'] > 0), safety_stock_df['fcst'], safety_stock_df['fcst'] * order_freq / fcst_weeks / 7)) ) # ========== CORRECTION PLUGINS (REWORK SS,ROP,OUP BASED ON REQ) =========== final_ss_df = safety_stock_df.copy() if v4_active_flag == 'Y': logger.info("IPC V4 Correction starts") df_pre_corr = final_ss_df.copy() final_ss_df = v4_corrections(final_ss_df, drug_type_list_v4, db, schema) df_post_corr = final_ss_df.copy() logger.info(f"Sum OUP before: {df_pre_corr['order_upto_point'].sum()}") logger.info(f"Sum OUP after: {df_post_corr['order_upto_point'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'V4') # correct cases where ROP=OUP df_pre_corr = final_ss_df.copy() final_ss_df['order_upto_point'] = np.where( ((final_ss_df['order_upto_point'] > 0) & (final_ss_df['reorder_point'] == final_ss_df['order_upto_point'])), final_ss_df['reorder_point'] + 1, final_ss_df['order_upto_point']) df_post_corr = final_ss_df.copy() corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'OUP_CORR1') # OUP < STD Qty, STD Qty correction df_pre_corr = final_ss_df.copy() final_ss_df = std_qty_oup(final_ss_df, schema, db) df_post_corr = final_ss_df.copy() corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'STD_CORR') # Min OUP=2 for recently sold df_pre_corr = final_ss_df.copy() final_ss_df = min_oup_recency(final_ss_df, drug_sales_latest_12w) df_post_corr = final_ss_df.copy() corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'OUP_CORR2') return final_ss_df def std_qty_oup(df, schema, db): list_drugs = df['drug_id'].tolist() if list_drugs: str_drugs = str(list_drugs).replace('[', '(').replace(']', ')') else: str_drugs = '(0)' q_std_qty = f""" select "drug-id" , "std-qty" from "{schema}"."drug-std-info" dsi where "drug-id" in {str_drugs} """ df_std_qty = db.get_df(q_std_qty) df_std_qty.columns = [c.replace('-', '_') for c in df_std_qty.columns] df = df.merge(df_std_qty, on='drug_id', how='left') df['std_qty'] = df['std_qty'].fillna(1) df_to_corr = df.loc[df['order_upto_point'] < df['std_qty']] df_not_to_corr = df.loc[~(df['order_upto_point'] < df['std_qty'])] df_to_corr['order_upto_point'] = df_to_corr['std_qty'] df_to_corr['reorder_point'] = np.where(df_to_corr['order_upto_point'] > 1, round(df_to_corr['std_qty'] / 2), 0) df = df_not_to_corr.append(df_to_corr) df.drop('std_qty', axis=1, inplace=True) return df def min_oup_recency(df, drug_sales_latest_12w): drug_sales_latest_12w['latest_28d_demand'] = np.round( drug_sales_latest_12w['actual_demand'] / 3) df = df.merge(drug_sales_latest_12w[['drug_id', 'latest_28d_demand']], on='drug_id', how='left') df_to_corr = df.loc[(df['order_upto_point'] == 1) & (df['fcst'] > 0) & (df['latest_28d_demand'] > 0)] df_not_to_corr = df.loc[~((df['order_upto_point'] == 1) & (df['fcst'] > 0) & (df['latest_28d_demand'] > 0))] df_to_corr['order_upto_point'] = 2 df_to_corr['reorder_point'] = 1 df = df_not_to_corr.append(df_to_corr) df.drop('latest_28d_demand', axis=1, inplace=True) return df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/safety_stock.py
safety_stock.py
import pandas as pd import numpy as np import datetime as dt import time from dateutil.relativedelta import relativedelta from zeno_etl_libs.utils.ipc2.engine.data_load import LoadData from zeno_etl_libs.utils.ipc2.engine.forecast import Forecast from zeno_etl_libs.utils.ipc2.engine.feat_engg import Feature_Engg from zeno_etl_libs.utils.ipc_pmf.ipc_combination_fcst.engine.data_pre_process import PreprocessData from zeno_etl_libs.utils.ipc_pmf.ipc_combination_fcst.engine.segmentation import Segmentation from zeno_etl_libs.utils.ipc_pmf.ipc_combination_fcst.engine.ts_fcst import TS_forecast from zeno_etl_libs.utils.ipc_pmf.config_ipc_combination import * def ipc_comb_forecast(store_id, reset_date, type_list, schema, db, logger): store_id_list = ("({})").format(store_id) # for sql pass last_date = dt.date(day=1, month=4, year=2019) # max history load_max_date = pd.to_datetime(reset_date).date() - dt.timedelta( days=pd.to_datetime(reset_date).dayofweek + 1) # define empty variables in case of fail weekly_fcst = pd.DataFrame() ts_fcst = pd.DataFrame() ts_fcst_cols = [] logger.info("Data Loading Started...") data_load_obj = LoadData() ( drug_list, sales_history, cfr_pr, calendar, first_bill_date, drug_sp ) = data_load_obj.load_all_input( type_list=type_list, store_id_list=store_id_list, last_date=last_date, reset_date=reset_date, load_max_date=load_max_date, schema=schema, db=db ) # consider only drugs in specified type drug_list = drug_list["drug_id"].unique().tolist() sales_history = sales_history.loc[sales_history[drug_col].isin(drug_list)] cfr_pr = cfr_pr.loc[cfr_pr[drug_col].isin(drug_list)] # ======================================================================== # AGGREGATE DRUG-LEVEL DEMAND TO COMBINATION LEVEL DEMAND # ======================================================================== sales_history, cfr_pr, comb_list = drugs_to_comb_gps(sales_history, cfr_pr, schema, db) # ======================================================================== logger.info("Data Pre Processing Started...") data_prep_obj = PreprocessData() ( comb_sales_4w_wtd, comb_sales_latest_12w, train_4w_agg_vald_max_date, sales_pred_4w_agg_vald, train_vald_max_date, sales_pred_vald, sales_4w_agg, sales_pred_4w_agg, sales, sales_pred, cal_sales, sales_daily ) = data_prep_obj.preprocess_all( sales=sales_history, comb_list=comb_list, cfr_pr=cfr_pr, calendar=calendar, first_bill_date=first_bill_date, last_date=last_date ) train_max_date = sales[date_col].max() end_date = sales_pred[date_col].max() logger.info("Segmentation Started...") seg_obj = Segmentation() seg_df = seg_obj.get_weekly_segmentation( df=sales.copy(deep=True), df_sales_daily=sales_daily.copy(deep=True), train_max_date=train_max_date, end_date=end_date ) seg_df['reset_date'] = str(reset_date) # ======================================================================== # VALIDATION AND BEST MODEL FOR BUCKET SELECTION # ======================================================================== # Find validation period actual demand valid_start_date = train_max_date - relativedelta(weeks=4) valid_period_demand = sales.loc[sales[date_col] > valid_start_date] valid_period_demand = valid_period_demand.groupby(key_col, as_index=False).agg({target_col: "sum"}) min_history_date_validation = valid_start_date - relativedelta(weeks=4) df_min_date = sales_pred_vald.groupby(key_col, as_index=False).agg({date_col: 'min'}) df_min_date['min_allowed_date'] = min_history_date_validation ts_ids_to_drop = df_min_date.loc[df_min_date['min_allowed_date']<df_min_date[date_col]][key_col].tolist() # Perform Un-Aggregated TS Forecast merged_df1 = pd.merge(sales_pred_vald, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] merged_df1 = merged_df1[~merged_df1['ts_id'].isin(ts_ids_to_drop)] # calculate bucket wise wmape valid_wmape = {'Model': []} for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: valid_wmape[bucket] = [] if runs_ts_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast( df=merged_df1.copy(), train_max_date=train_vald_max_date, forecast_start=train_vald_max_date + relativedelta(weeks=1)) for model in ts_fcst_cols: df_model = ts_fcst[[key_col, 'Mixed', model]] df_model = df_model.groupby(key_col, as_index=False).agg({'Mixed': 'first', model: 'sum'}) df_model = df_model.merge(valid_period_demand, on=key_col, how='left') df_model['error'] = df_model[model] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg({'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error']/df_bucket_wmape[target_col] valid_wmape['Model'].append(model) for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket]['wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) if run_ml_flag == 1: forecast_start = train_vald_max_date + relativedelta(weeks=1) weekly_fcst = run_LGBM(merged_df1, train_vald_max_date, forecast_start, logger, is_validation=True) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg({'preds_lgb': 'sum'}) df_model = lgbm_fcst.merge(valid_period_demand, on=key_col, how='left') df_model = df_model.merge(seg_df[[key_col, 'Mixed']], how='left', on='ts_id') df_model['error'] = df_model['preds_lgb'] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / df_bucket_wmape[target_col] valid_wmape['Model'].append('LGBM') for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket]['wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) # Perform Aggregated TS Forecast merged_df1 = pd.merge(sales_pred_4w_agg_vald, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] merged_df1 = merged_df1[~merged_df1['ts_id'].isin(ts_ids_to_drop)] if run_ts_4w_agg_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast_agg( df=merged_df1.copy(), train_max_date=train_4w_agg_vald_max_date, forecast_start=train_4w_agg_vald_max_date + relativedelta(weeks=1)) for model in ts_fcst_cols: df_model = ts_fcst[[key_col, 'Mixed', model]] df_model = df_model.groupby(key_col, as_index=False).agg( {'Mixed': 'first', model: 'sum'}) df_model = df_model.merge(valid_period_demand, on=key_col, how='left') df_model['error'] = df_model[model] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / df_bucket_wmape[target_col] valid_wmape['Model'].append(model) for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket]['wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) if run_ml_4w_agg_flag == 1: forecast_start = train_4w_agg_vald_max_date + relativedelta(weeks=4) weekly_fcst = run_LGBM(merged_df1, train_4w_agg_vald_max_date, forecast_start, logger, is_validation=True, agg_4w=True) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg( {'preds_lgb': 'sum'}) df_model = lgbm_fcst.merge(valid_period_demand, on=key_col, how='left') df_model = df_model.merge(seg_df[[key_col, 'Mixed']], how='left', on='ts_id') df_model['error'] = df_model['preds_lgb'] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / \ df_bucket_wmape[target_col] valid_wmape['Model'].append('LGBM_4w_agg') for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket][ 'wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) # ======================================================================== # Choose best model based on lowest wmape # ======================================================================== best_bucket_model = {} for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: min_wmape = min(valid_wmape[bucket]) if min_wmape != np.inf: best_bucket_model[bucket] = valid_wmape['Model'][valid_wmape[bucket].index(min_wmape)] else: best_bucket_model[bucket] = default_model # default # ======================================================================== # TRAINING AND FINAL FORECAST # ======================================================================== # Perform Un-Aggregated TS Forecast merged_df1 = pd.merge(sales_pred, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] if runs_ts_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast( df=merged_df1.copy(), train_max_date=train_max_date, forecast_start=train_max_date + relativedelta(weeks=2)) final_fcst = pd.DataFrame() for model_fcst in ts_fcst_cols: df_model_fcst = ts_fcst.groupby(key_col, as_index=False).agg({model_fcst: 'sum'}) df_model_fcst.rename({model_fcst: 'fcst'}, axis=1, inplace=True) df_model_fcst['model'] = model_fcst final_fcst = final_fcst.append(df_model_fcst) if run_ml_flag == 1: forecast_start = train_max_date + relativedelta(weeks=2) weekly_fcst = run_LGBM(merged_df1, train_max_date, forecast_start, logger, is_validation=False) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg({'preds_lgb': 'sum'}) lgbm_fcst.rename({'preds_lgb': 'fcst'}, axis=1, inplace=True) lgbm_fcst['model'] = 'LGBM' final_fcst = final_fcst.append(lgbm_fcst) # Perform Aggregated TS Forecast merged_df1 = pd.merge(sales_pred_4w_agg, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] if run_ts_4w_agg_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast_agg( df=merged_df1.copy(), train_max_date=train_max_date, forecast_start=train_max_date + relativedelta(weeks=2)) for model_fcst in ts_fcst_cols: df_model_fcst = ts_fcst.groupby(key_col, as_index=False).agg( {model_fcst: 'sum'}) df_model_fcst.rename({model_fcst: 'fcst'}, axis=1, inplace=True) df_model_fcst['model'] = model_fcst final_fcst = final_fcst.append(df_model_fcst) if run_ml_4w_agg_flag == 1: forecast_start = train_max_date + relativedelta(weeks=2) weekly_fcst = run_LGBM(merged_df1, train_max_date, forecast_start, logger, is_validation=False, agg_4w=True) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg( {'preds_lgb': 'sum'}) lgbm_fcst.rename({'preds_lgb': 'fcst'}, axis=1, inplace=True) lgbm_fcst['model'] = 'LGBM_4w_agg' final_fcst = final_fcst.append(lgbm_fcst) final_fcst = final_fcst.merge(seg_df[[key_col, 'Mixed']], on=key_col, how='left') final_fcst.rename({'Mixed': 'bucket'}, axis=1, inplace=True) # Choose buckets forecast of best models as final forecast final_selected_fcst = pd.DataFrame() for bucket in best_bucket_model.keys(): df_selected = final_fcst.loc[(final_fcst['bucket'] == bucket) & (final_fcst['model'] == best_bucket_model[bucket])] final_selected_fcst = final_selected_fcst.append(df_selected) # add comb rejected due to recent sales list_all_comb = final_fcst[key_col].unique().tolist() list_all_final_comb = final_selected_fcst[key_col].unique().tolist() list_comb_rejects = list(set(list_all_comb)-set(list_all_final_comb)) comb_fcst_to_add = final_fcst.loc[(final_fcst[key_col].isin(list_comb_rejects)) & (final_fcst["model"] == default_model)] final_selected_fcst = final_selected_fcst.append(comb_fcst_to_add) final_selected_fcst = final_selected_fcst.merge(seg_df[[key_col, 'std']], on=key_col, how='left') final_selected_fcst[[store_col, comb_col]] = final_selected_fcst['ts_id'].str.split('_', expand=True) final_selected_fcst[store_col] = final_selected_fcst[store_col].astype(int) model_name_map = {'preds_ETS_12w': 'ETS_12w', 'preds_ma': 'MA', 'preds_ETS_auto': 'ETS_auto', 'preds_ETS_4w_auto': 'ETS_4w_auto'} final_selected_fcst["model"] = final_selected_fcst["model"].map( model_name_map).fillna(final_selected_fcst["model"]) return final_selected_fcst, seg_df, comb_sales_latest_12w, comb_sales_4w_wtd def run_LGBM(merged_df1, train_max_date, forecast_start, logger, is_validation=False, agg_4w=False): start_time = time.time() merged_df1['All'] = 'All' slice_col = 'All' forecast_volume = merged_df1[merged_df1[date_col] > train_max_date][ target_col].sum() assert forecast_volume == 0 logger.info( "forecast start {} total volume: {}".format(forecast_start, forecast_volume) ) forecast_df = pd.DataFrame() validation_df = pd.DataFrame() weekly_fcst = pd.DataFrame() if agg_4w: end_range = 2 else: end_range = 5 for i in range(1, end_range): if is_validation: num_shift_lags = i else: num_shift_lags = i + 1 # for group_name in merged_df1[slice_col].dropna.unique(): # slice_col = 'Mixed' # for groups in [['AW', 'BW', 'CW', 'DW'], ['AX', 'BX', 'CX', 'DX'], ['AY', 'BY', 'CY', 'DY'], ['AZ', 'BZ', 'CZ', 'DZ']]: for groups in ['All']: logger.info('Group: {}'.format(groups)) logger.info("Feature Engineering Started...") feat_df = pd.DataFrame() for one_df in [merged_df1]: feat_engg_obj = Feature_Engg() one_feat_df = feat_engg_obj.feat_agg( one_df[ one_df[slice_col] == groups ].drop(slice_col, axis=1).copy(deep=True), train_max_date=train_max_date, num_shift_lag=num_shift_lags ) feat_df = pd.concat([one_feat_df, feat_df]) if pd.DataFrame(feat_df).empty: continue logger.info( "Forecasting Started for {}...".format(forecast_start)) forecast_obj = Forecast() fcst_df, val_df, Feature_Imp_all = forecast_obj.get_STM_forecast( feat_df.copy(deep=True), forecast_start=forecast_start, num_shift_lags=num_shift_lags ) forecast_df = pd.concat([forecast_df, fcst_df], axis=0) validation_df = pd.concat([validation_df, val_df]) ml_fc_cols = [i for i in forecast_df.columns if i.startswith('preds_')] # forecast_df['AE'] = forecast_df[ml_fc_cols].mean(axis=1) end_time = time.time() logger.info( "total time for {} forecast: {}" .format(forecast_start, end_time - start_time) ) forecast_start = forecast_start + relativedelta(weeks=1) # weekly_fcst = pd.concat([weekly_fcst, forecast_df]) return forecast_df def drugs_to_comb_gps(sales_history, cfr_pr, schema, db): """map all drugs to its combination groups""" q_drug_comp_hash = f""" select "drug-id" as drug_id, "group" as comb_id from "{schema}"."drug-substitution-mapping" dsm """ df_drug_comb = db.get_df(q_drug_comp_hash) sales_history = sales_history.merge(df_drug_comb, on="drug_id", how="left") sales_history = sales_history.groupby(["store_id", "comb_id", "sales_date"], as_index=False).agg({"net_sales_quantity": "sum"}) cfr_pr = cfr_pr.merge(df_drug_comb, on="drug_id", how="left") cfr_pr = cfr_pr.groupby(["store_id", "comb_id", "shortbook_date"], as_index=False).agg({"loss_quantity": "sum"}) comb_list = df_drug_comb[comb_col].unique().tolist() return sales_history, cfr_pr, comb_list
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_combination_fcst/forecast_main.py
forecast_main.py
import pandas as pd import numpy as np import datetime as dt from zeno_etl_libs.helper.google.sheet.sheet import GoogleSheet from zeno_etl_libs.utils.ipc_pmf.config_ipc_combination import * def fcst_comb_drug_map(store_id, reset_date, comb_fcst_df, drug_fcst_df, type_list_comb_lvl, schema, db, logger): # Round off forecast values comb_fcst_df['fcst'] = np.round(comb_fcst_df['fcst']) comb_fcst_df = comb_fcst_df.loc[comb_fcst_df['fcst'] > 0] # Read assortment from GSheet gs = GoogleSheet() spreadsheet_id = "1tFHCTr3CHdb0UOFseK_ntjAUJSHQHcjLmysPPCWRM04" ast_data = gs.download(data={ "spreadsheet_id": spreadsheet_id, "sheet_name": "Sheet1", "listedFields": []}) df_assortment = pd.DataFrame(ast_data) df_assortment.columns = [c.replace('-', '_') for c in df_assortment.columns] df_assortment[drug_col] = df_assortment[drug_col].astype(int) df_assortment['drug_name'] = df_assortment['drug_name'].astype(str) df_assortment['count'] = 1 df_assortment_comb = df_assortment.loc[df_assortment['type'].isin(type_list_comb_lvl)] # Read combinations and corresponding composition list_unq_comb = comb_fcst_df[comb_col].tolist() if list_unq_comb: str_unq_comb = str(list_unq_comb).replace('[', '(').replace(']', ')') else: str_unq_comb = '(0)' q_comb_drug = f""" select dsm."drug-id" as drug_id, dsm."group" as comb_id, d."drug-name" as drug_name, d.type, d.composition from "{schema}"."drug-substitution-mapping" dsm left join "{schema}".drugs d on dsm."drug-id" = d.id where dsm."group" in {str_unq_comb} """ df_comb_drug = db.get_df(q_comb_drug) # Get all mapping cases merge_comb_drug = df_comb_drug[[drug_col, comb_col]].merge( df_assortment_comb[[drug_col, 'drug_name', 'count']], on=drug_col, how="outer") count_comp_drugs = merge_comb_drug.groupby(comb_col, as_index=False).agg({'count': 'sum'}) list_comb_one_one = count_comp_drugs[count_comp_drugs['count'] == 1][comb_col].tolist() list_comb_one_many = count_comp_drugs[count_comp_drugs['count'] > 1][comb_col].tolist() list_comb_one_none = count_comp_drugs[count_comp_drugs['count'] == 0][comb_col].tolist() # Allocate forecast to drugs df_assortment_merge = df_assortment_comb.merge(df_comb_drug, on=drug_col, how='left') list_drug_with_comb_fcst = df_assortment_merge.loc[df_assortment_merge[comb_col].notna()][drug_col].tolist() df_all_comb = comb_fcst_df.merge(df_assortment_merge, on=comb_col, how='left') df_all_comb = df_all_comb[[store_col, comb_col, 'bucket', 'model', drug_col, 'fcst', 'std', 'correction_flags']] df_fcst_final = pd.DataFrame() # Case 1: One-One, direct assign df_temp = df_all_comb.loc[df_all_comb[comb_col].isin(list_comb_one_one)] df_temp['fcst_wt'] = 1 df_temp['map_type'] = 'one-one' df_temp['fcst_source'] = 'combination_fcst' df_fcst_final = df_fcst_final.append(df_temp) df_one_one = df_temp.copy() df_one_one = df_one_one.merge(df_assortment_comb, on=drug_col, how='left') df_one_one.drop('count', axis=1, inplace=True) # Case 2: One-Many, assign based on past month sales contribution df_temp = df_all_comb.loc[df_all_comb[comb_col].isin(list_comb_one_many)] df_temp = drug_sales_multiplier(df_temp, store_id, reset_date, schema, db) df_one_many = df_temp.copy() df_temp.drop(['sales_90d', 'comb_sales_90d'], axis=1, inplace=True) df_temp['fcst'] = df_temp['fcst'] * df_temp['fcst_wt'] df_temp['std'] = df_temp['std'] * df_temp['fcst_wt'] df_temp['map_type'] = 'one-many' df_temp['fcst_source'] = 'combination_fcst' df_fcst_final = df_fcst_final.append(df_temp) df_one_many = df_one_many.merge(df_assortment_comb, on=drug_col, how='left') df_one_many.drop('count', axis=1, inplace=True) # Case 3: One-None, to send df_one_none = df_all_comb.loc[df_all_comb[comb_col].isin(list_comb_one_none)] df_one_none.drop(drug_col, axis=1, inplace=True) df_one_none = df_one_none.merge(df_comb_drug, on=comb_col, how='left') df_one_none = drug_sales_multiplier(df_one_none, store_id, reset_date, schema, db) df_one_none = df_one_none.loc[df_one_none['sales_90d'] > 0] df_one_none.drop('fcst_wt', axis=1, inplace=True) # Case 4: No Comb - Drugs, to map with drug-level-fcst df_none_one = df_assortment.loc[~df_assortment[drug_col].isin(list_drug_with_comb_fcst)] df_none_one.drop('count', axis=1, inplace=True) # get drug-combination groups list_drugs = df_none_one[drug_col].tolist() if list_drugs: str_list_drugs = str(list_drugs).replace('[', '(').replace(']', ')') else: str_list_drugs = '(0)' q_comb_drug = f""" select dsm."drug-id" as drug_id, dsm."group" as comb_id, d."drug-name" as drug_name, d.type, d.composition from "{schema}"."drug-substitution-mapping" dsm left join "{schema}".drugs d on dsm."drug-id" = d.id where dsm."drug-id" in {str_list_drugs} """ df_comb_drug = db.get_df(q_comb_drug) drug_fcst_df.drop(key_col, axis=1, inplace=True) df_fcst_drug_level_merge = drug_fcst_df.merge(df_none_one[[drug_col]], on=drug_col, how='inner') df_fcst_drug_level_merge = df_fcst_drug_level_merge.merge( df_comb_drug, on=drug_col, how='left') df_fcst_drug_level_merge['fcst_source'] = 'drug_fcst' df_fcst_drug_level_merge['fcst_wt'] = np.nan df_fcst_drug_level_merge['map_type'] = np.nan df_fcst_final = df_fcst_final.append(df_fcst_drug_level_merge[df_fcst_final.columns]) # filter only drugs without combination drugs_with_comb = df_comb_drug[drug_col].tolist() df_none_one = df_none_one.loc[~df_none_one[drug_col].isin(drugs_with_comb)] # Append reject cases forecasted_drugs = df_fcst_final[drug_col].tolist() assortment_drugs = df_assortment[drug_col].tolist() reject_drugs = list(set(forecasted_drugs) ^ set(assortment_drugs)) df_reject_cases = df_assortment.loc[df_assortment[drug_col].isin(reject_drugs)][[drug_col]] df_reject_cases[store_col] = store_id df_reject_cases['bucket'] = 'NA' df_reject_cases['model'] = 'NA' df_reject_cases['fcst'] = 0 df_reject_cases['std'] = 0 df_reject_cases['fcst_wt'] = np.nan df_reject_cases['map_type'] = np.nan df_reject_cases['fcst_source'] = np.nan df_reject_cases['correction_flags'] = "" df_reject_cases = df_reject_cases.merge(df_comb_drug, on=drug_col, how='left') df_fcst_final = df_fcst_final.append(df_reject_cases[df_fcst_final.columns]) # Round off forecast values df_fcst_final['fcst'] = df_fcst_final['fcst'].astype(float) df_fcst_final['fcst'] = np.round(df_fcst_final['fcst']) return df_fcst_final, df_one_one, df_one_many, df_one_none, df_none_one def drug_sales_multiplier(df, store_id, reset_date, schema, db): list_drugs = df[drug_col].tolist() if list_drugs: str_drugs = str(list_drugs).replace('[', '(').replace(']', ')') else: str_drugs = '(0)' sales_start = (dt.datetime.strptime(reset_date, '%Y-%m-%d').date() - dt.timedelta(days=90)).strftime('%Y-%m-%d') q_drug_sales = f""" select "drug-id" , sum("net-quantity") as sales_90d from "{schema}".sales s where "store-id" = {store_id} and date("created-at") >= '{sales_start}' and date("created-at") < '{reset_date}' and "drug-id" in {str_drugs} group by "drug-id" """ df_drug_sales = db.get_df(q_drug_sales) df_drug_sales.columns = [c.replace('-', '_') for c in df_drug_sales.columns] df = df.merge(df_drug_sales, on=drug_col, how='left') df['sales_90d'] = df['sales_90d'].fillna(0) df_comb_sales_sum = df.groupby(comb_col, as_index=False).agg({'sales_90d': 'sum'}) df_comb_sales_sum.rename({'sales_90d': 'comb_sales_90d'}, axis=1, inplace=True) df = df.merge(df_comb_sales_sum, on=comb_col, how='left') df['fcst_wt'] = df['sales_90d']/df['comb_sales_90d'] df['fcst_wt'] = df['fcst_wt'].fillna(0) # assign equal split for combination with 0 sales zero_sales_comb = df_comb_sales_sum.loc[ df_comb_sales_sum['comb_sales_90d'] == 0][comb_col].tolist() df_comb_equal_split_cases = df.groupby(comb_col, as_index=False).agg({drug_col: 'count'}) df_comb_equal_split_cases.rename({drug_col: 'count'}, axis=1, inplace=True) df_comb_equal_split_cases = df_comb_equal_split_cases.loc[df_comb_equal_split_cases[comb_col].isin(zero_sales_comb)] df_comb_equal_split_cases['equal_split_wt'] = 1/df_comb_equal_split_cases['count'] df = df.merge(df_comb_equal_split_cases, on=comb_col, how='left') df['fcst_wt'] = np.where(df['equal_split_wt'].isna(), df['fcst_wt'], df['equal_split_wt']) df.drop(['count', 'equal_split_wt'], axis=1, inplace=True) return df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_combination_fcst/fcst_mapping.py
fcst_mapping.py
import numpy as np np.random.seed(0) import pandas as pd # import time # import re # from datetime import date # from dateutil.relativedelta import relativedelta # from statsmodels.tsa.exponential_smoothing.ets import ETSModel from prophet import Prophet from statsmodels.tsa.api import ExponentialSmoothing # import sktime from zeno_etl_libs.utils.ipc2.helpers.helper_functions import \ applyParallel_croston # from boruta import BorutaPy from zeno_etl_libs.utils.ipc_pmf.config_ipc_combination import ( date_col, target_col, models_un_agg, models_agg ) import logging logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') class TS_forecast: def train_test_split(self, df, train_max_date, forecast_start): df.rename(columns={date_col: 'ds', target_col: 'y'}, inplace=True) df.sort_values(by=['ds'], inplace=True) train = df[df['ds'] <= train_max_date] test = df[df['ds'] >= forecast_start] return train, test def Croston_TSB(self, ts, extra_periods=4, alpha=0.4, beta=0.4): d = np.array(ts) # Transform the input into a numpy array cols = len(d) # Historical period length d = np.append(d, [ np.nan] * extra_periods) # Append np.nan into the demand array to cover future periods # level (a), probability(p) and forecast (f) a, p, f = np.full((3, cols + extra_periods), np.nan) # Initialization first_occurence = np.argmax(d[:cols] > 0) a[0] = d[first_occurence] p[0] = 1 / (1 + first_occurence) f[0] = p[0] * a[0] # Create all the t+1 forecasts for t in range(0, cols): if d[t] > 0: a[t + 1] = alpha * d[t] + (1 - alpha) * a[t] p[t + 1] = beta * (1) + (1 - beta) * p[t] else: a[t + 1] = a[t] p[t + 1] = (1 - beta) * p[t] f[t + 1] = p[t + 1] * a[t + 1] # Future Forecast a[cols + 1:cols + extra_periods] = a[cols] p[cols + 1:cols + extra_periods] = p[cols] f[cols + 1:cols + extra_periods] = f[cols] df = pd.DataFrame.from_dict( {"Demand": d, "Forecast": f, "Period": p, "Level": a, "Error": d - f}) return df[-extra_periods:] def ETS_forecast(self, train, test, latest_12w_fit=False): try: train.set_index(['ds'], inplace=True) test.set_index(['ds'], inplace=True) train.index.freq = train.index.inferred_freq test.index.freq = test.index.inferred_freq if latest_12w_fit: train = train[-12:] # use only latest 3 months fit = ExponentialSmoothing(train['y']).fit() preds = fit.forecast(len(test) + 1) preds = preds[-len(test):] except Exception as e: logger.info("error in ETS fcst") logger.info(str(e)) preds = 0 return preds def ma_forecast(self, data): """ Purpose: Compute MA forecast for the for the forecast horizon specified Inputs: time series to create forecast Output: series with forecasted values """ sma_df = data.copy(deep=True) yhat = [] if len(data) >= 8: for i in range(5): sma_val = sma_df.rolling(8).mean().iloc[-1] sma_df.loc[sma_df.index.max() + 1] = sma_val yhat.append(sma_val) else: for i in range(5): sma_val = sma_df.rolling(len(data)).mean().iloc[-1] sma_df.loc[sma_df.index.max() + 1] = sma_val yhat.append(sma_val) logger.info(yhat) return yhat[-4:] def prophet_fcst(self, train, test, params=None): # reg_list = [] try: if params is None: pro = Prophet() else: pro = Prophet(n_changepoints=params) # for j in train.columns: # if j not in col_list: # pro.add_regressor(j) # reg_list.append(j) pro.fit(train[['ds', 'y']]) pred_f = pro.predict(test) test = test[["ds", "y"]] test = pd.merge(test, pred_f, on="ds", how="left") except Exception as e: logger.info("error in prophet fcst") logger.info(str(e)) test['yhat'] = 0 return test def ts_forecast_un_agg(self, df, train_max_date, forecast_start): train, test = self.train_test_split(df, train_max_date=train_max_date, forecast_start=forecast_start) test = test.sort_values(by=['ds']) if 'croston' in models_un_agg: preds_croston = self.Croston_TSB(train['y']) test['preds_croston'] = preds_croston['Forecast'].values if 'ETS_Auto' in models_un_agg: preds_ETS = self.ETS_forecast(train.copy(), test.copy()) try: test['preds_ETS_auto'] = preds_ETS.values except: test['preds_ETS_auto'] = 0 if 'ETS_12w' in models_un_agg: preds_ETS = self.ETS_forecast(train.copy(), test.copy(), latest_12w_fit=True) try: test['preds_ETS_12w'] = preds_ETS.values except: test['preds_ETS_12w'] = 0 if 'MA' in models_un_agg: preds_ma = self.ma_forecast(train['y']) test['preds_ma'] = preds_ma if 'prophet' in models_un_agg: preds_prophet = self.prophet_fcst(train.copy(), test.copy()) test['preds_prophet'] = preds_prophet['yhat'].values return test def ts_forecast_agg(self, df, train_max_date, forecast_start): train, test = self.train_test_split(df, train_max_date=train_max_date, forecast_start=forecast_start) test = test.sort_values(by=['ds']) if 'ETS_4w_agg' in models_agg: preds_ETS = self.ETS_forecast(train.copy(), test.copy(), latest_12w_fit=True) try: test['preds_ETS_4w_auto'] = preds_ETS.values except: test['preds_ETS_4w_auto'] = 0 return test def apply_ts_forecast(self, df, train_max_date, forecast_start): # global train_date # train_date = train_max_date # global forecast_start_date # forecast_start_date = forecast_start preds = applyParallel_croston( df.groupby('ts_id'), func=self.ts_forecast_un_agg, train_max_date=train_max_date, forecast_start=forecast_start ) preds.rename(columns={'ds': date_col, 'y': target_col}, inplace=True) ts_fcst_cols = [i for i in preds.columns if i.startswith('preds_')] for col in ts_fcst_cols: preds[col].fillna(0, inplace=True) preds[col] = np.where(preds[col] < 0, 0, preds[col]) # preds['preds_AE_ts'] = preds[ts_fcst_cols].mean(axis=1) return preds, ts_fcst_cols def apply_ts_forecast_agg(self, df, train_max_date, forecast_start): # global train_date # train_date = train_max_date # global forecast_start_date # forecast_start_date = forecast_start preds = applyParallel_croston( df.groupby('ts_id'), func=self.ts_forecast_agg, train_max_date=train_max_date, forecast_start=forecast_start ) preds.rename(columns={'ds': date_col, 'y': target_col}, inplace=True) ts_fcst_cols = [i for i in preds.columns if i.startswith('preds_')] for col in ts_fcst_cols: preds[col].fillna(0, inplace=True) preds[col] = np.where(preds[col] < 0, 0, preds[col]) # preds['preds_AE_ts'] = preds[ts_fcst_cols].mean(axis=1) return preds, ts_fcst_cols
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_combination_fcst/engine/ts_fcst.py
ts_fcst.py
import pandas as pd import datetime import numpy as np from zeno_etl_libs.utils.ipc_pmf.config_ipc_combination import date_col, store_col, \ comb_col, target_col, key_col, local_testing class PreprocessData: def add_ts_id(self, df): df = df[~df[comb_col].isnull()].reset_index(drop=True) df['ts_id'] = ( df[store_col].astype(int).astype(str) + '_' + df[comb_col].astype(str) ) return df def preprocess_sales(self, df, comb_list): df.rename(columns={ 'net_sales_quantity': target_col }, inplace=True) df.rename(columns={ 'sales_date': date_col }, inplace=True) set_dtypes = { store_col: int, comb_col: str, date_col: str, target_col: float } df = df.astype(set_dtypes) df[target_col] = df[target_col].round() df[date_col] = pd.to_datetime(df[date_col]) df = df.groupby( [store_col, comb_col, key_col, date_col] )[target_col].sum().reset_index() df = df[df[comb_col].isin(comb_list)] return df def get_formatted_data(self, df): df_start = df.groupby([key_col])[date_col].min().reset_index().rename( columns={date_col: 'sales_start'}) df = df[[key_col, date_col, target_col]] min_date = df[date_col].dropna().min() end_date = df[date_col].dropna().max() date_range = [] date_range = pd.date_range( start=min_date, end=end_date, freq='d' ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = np.nan df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = pd.merge(df, df_start, how='left', on=key_col) df = df[df[date_col] >= df['sales_start']] df[[store_col, comb_col]] = df[key_col].str.split('_', expand=True) df[[store_col, comb_col]] = df[[store_col, comb_col]] df[store_col] = df[store_col].astype(int) df[comb_col] = df[comb_col].astype(str) return df def preprocess_cfr_pr(self, df): set_dtypes = { store_col: int, comb_col: str, 'loss_quantity': int } df = df.astype(set_dtypes) df['shortbook_date'] = pd.to_datetime(df['shortbook_date']) return df def merge_cfr_pr(self, sales, cfr_pr): df = sales.merge(cfr_pr, left_on=[store_col, comb_col, date_col], right_on=[store_col, comb_col, 'shortbook_date'], how='left') df[date_col] = df[date_col].combine_first(df['shortbook_date']) df[target_col].fillna(0, inplace=True) df['loss_quantity'].fillna(0, inplace=True) df[target_col] += df['loss_quantity'] df.drop(['shortbook_date', 'loss_quantity'], axis=1, inplace=True) return df def preprocess_calendar(self, df, last_date): df.rename(columns={'date': date_col}, inplace=True) df[date_col] = pd.to_datetime(df[date_col]) cal_sales = df.copy() cal_sales['week_begin_dt'] = cal_sales.apply( lambda x: x[date_col] - datetime.timedelta(x['day_of_week']), axis=1) cal_sales['month_begin_dt'] = cal_sales.apply( lambda x: x['date'] - datetime.timedelta(x['date'].day - 1), axis=1) cal_sales['key'] = 1 ld = pd.to_datetime(last_date) cal_sales = cal_sales[cal_sales[date_col] > ld] return df, cal_sales def merge_calendar(self, sales, calendar): df = sales.merge(calendar, how='left', on=date_col ) # df_week_days_count = df.groupby([key_col, 'year', 'week_of_year'])[date_col].count().reset_index().rename(columns = {date_col:'week_days_count'}) # df['week_days_count'] = 1 df['week_begin_dt'] = df.apply( lambda x: x[date_col] - datetime.timedelta(x['day_of_week']), axis=1) df_week_days_count = df.groupby(['ts_id', 'week_begin_dt'])[ date_col].count().reset_index().rename( columns={date_col: 'week_days_count'}) # df = df.groupby(['ts_id', store_col, drug_col, ]).resample('W-Mon', on =date_col )[target_col].sum().reset_index() df = df.groupby(['ts_id', store_col, comb_col, 'week_begin_dt'])[ target_col].sum().reset_index() df = pd.merge(df, df_week_days_count, how='left', on=[key_col, 'week_begin_dt']) df = df[df['week_days_count'] == 7].reset_index(drop=True) df.drop(columns=['week_days_count'], inplace=True) df.rename(columns={'week_begin_dt': date_col}, inplace=True) return df def preprocess_bill_date(self, df): df.rename(columns={'store-id': store_col}, inplace=True) df['bill_date'] = pd.to_datetime(df['bill_date']) return df def merge_first_bill_date(self, sales, first_bill_date): df = pd.merge(sales, first_bill_date, on=[store_col]) df = df[df[date_col] >= df['bill_date']].reset_index(drop=True) df.drop(columns=['bill_date'], inplace=True) return df def make_future_df(self, df): start_date_df = ( df .groupby(key_col)[date_col] .min() .reset_index() .rename(columns={date_col: 'start_date'}) ) df = df[[key_col, date_col, target_col]] end_date = df[date_col].max() + datetime.timedelta(weeks=5) min_date = df[date_col].min() date_range = pd.date_range( start=min_date, end=end_date, freq="W-MON" ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = 0 df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = df.merge(start_date_df, on=key_col, how='left') df = df[ df[date_col] >= df['start_date'] ] df.drop('start_date', axis=1, inplace=True) df[[store_col, comb_col]] = df[key_col].str.split('_', expand=True) return df def make_future_df_4w_agg(self, df): start_date_df = ( df .groupby(key_col)[date_col] .min() .reset_index() .rename(columns={date_col: 'start_date'}) ) df = df[[key_col, date_col, target_col]] fcst_week_start = df[date_col].max() + datetime.timedelta(weeks=5) date_range = [fcst_week_start] df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = 0 df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = df.merge(start_date_df, on=key_col, how='left') df = df[ df[date_col] >= df['start_date'] ] df.drop('start_date', axis=1, inplace=True) df[[store_col, comb_col]] = df[key_col].str.split('_', expand=True) return df def sales_pred_vald_df( self, df ): vald_max_date = df[date_col].max() - datetime.timedelta(weeks=4) df_vald_train = df.loc[df[date_col] <= vald_max_date] df_vald_future = df.loc[~(df[date_col] <= vald_max_date)] df_vald_future[target_col] = 0 df_final = df_vald_train.append(df_vald_future) train_vald_max_date = df_vald_train[date_col].max() return df_final, train_vald_max_date def sales_4w_agg( self, df ): # ===================================================================== # Combine 4 weeks into an arbitrary group # ===================================================================== unique_ts_ids = df[key_col].unique().tolist() sales_4w_agg = pd.DataFrame() for ts_id in unique_ts_ids: week_gp_size = 4 sales_temp = df.loc[df[key_col] == ts_id] available_week_count = sales_temp.shape[0] if available_week_count >= (3 * week_gp_size): allowable_week_count = int( week_gp_size * np.fix(available_week_count / week_gp_size)) sales_temp = sales_temp.sort_values(by=["date"], ascending=True) sales_temp = sales_temp[-allowable_week_count:] week_gps_count = int(allowable_week_count / week_gp_size) week_gps_list = np.arange(1, week_gps_count + 1, 1) week_gps_id_list = np.repeat(week_gps_list, week_gp_size) sales_temp["week_gps_id"] = week_gps_id_list sales_temp = sales_temp.groupby( [key_col, store_col, comb_col, "week_gps_id"], as_index=False).agg( {"date": "first", "actual_demand": "sum"}) sales_4w_agg = sales_4w_agg.append(sales_temp) sales_4w_agg = sales_4w_agg.drop("week_gps_id", axis=1) sales_pred_4w_agg = self.make_future_df_4w_agg(sales_4w_agg.copy()) return sales_4w_agg, sales_pred_4w_agg def comb_sales_12w( self, df ): date_12w_back = df[date_col].max() - datetime.timedelta(weeks=12) df_12w = df.loc[df[date_col] > date_12w_back] df_12w = df_12w.groupby([store_col, comb_col], as_index=False).agg( {target_col: 'sum'}) return df_12w def comb_sales_4w_wtd( self, df ): date_4w_back = df[date_col].max() - datetime.timedelta(weeks=4) df_4w = df.loc[df[date_col] > date_4w_back] # sales > 0 and all 4 latest week df_4w_1 = df_4w[df_4w[target_col] > 0] df_4w_cnt = df_4w_1.groupby([store_col, comb_col], as_index=False).agg( {target_col: 'count'}) df_4w_cnt.rename({target_col: 'week_count'}, axis=1, inplace=True) list_4w_combs = df_4w_cnt.loc[df_4w_cnt['week_count'] == 4][comb_col].tolist() df_4w_1 = df_4w_1.loc[df_4w_1[comb_col].isin(list_4w_combs)] dates_list = list(df_4w_1.date.unique()) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[3], 0.4, 0) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[2], 0.3, df_4w_1['weights']) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[1], 0.2, df_4w_1['weights']) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[0], 0.1, df_4w_1['weights']) df_4w_1['wtd_demand'] = df_4w_1[target_col] * df_4w_1['weights'] df_4w_1 = df_4w_1.groupby([store_col, comb_col], as_index=False).agg( {'wtd_demand': 'sum'}) # sales > 0 and only 3 latest week df_4w_2 = df_4w[df_4w[target_col] > 0] df_4w_cnt = df_4w_2.groupby([store_col, comb_col], as_index=False).agg( {target_col: 'count'}) df_4w_cnt.rename({target_col: 'week_count'}, axis=1, inplace=True) list_4w_combs = df_4w_cnt.loc[df_4w_cnt['week_count'] == 3][comb_col].tolist() df_4w_2 = df_4w_2.loc[df_4w_2[comb_col].isin(list_4w_combs)] df_4w_2['w_count'] = np.tile(np.arange(1, 4), len(df_4w_2))[:len(df_4w_2)] df_4w_2['weights'] = np.where(df_4w_2['w_count'] == 3, 0.5, 0) df_4w_2['weights'] = np.where(df_4w_2['w_count'] == 2, 0.3, df_4w_2['weights']) df_4w_2['weights'] = np.where(df_4w_2['w_count'] == 1, 0.2, df_4w_2['weights']) df_4w_2['wtd_demand'] = df_4w_2[target_col] * df_4w_2['weights'] df_4w_2 = df_4w_2.groupby([store_col, comb_col], as_index=False).agg( {'wtd_demand': 'sum'}) # sales > 0 and only 2 latest week df_4w_3 = df_4w[df_4w[target_col] > 0] df_4w_cnt = df_4w_3.groupby([store_col, comb_col], as_index=False).agg( {target_col: 'count'}) df_4w_cnt.rename({target_col: 'week_count'}, axis=1, inplace=True) list_4w_combs = df_4w_cnt.loc[df_4w_cnt['week_count'] == 2][ comb_col].tolist() df_4w_3 = df_4w_3.loc[df_4w_3[comb_col].isin(list_4w_combs)] df_4w_3['w_count'] = np.tile(np.arange(1, 3), len(df_4w_3))[:len(df_4w_3)] df_4w_3['weights'] = np.where(df_4w_3['w_count'] == 2, 0.6, 0) df_4w_3['weights'] = np.where(df_4w_3['w_count'] == 1, 0.4, df_4w_3['weights']) df_4w_3['wtd_demand'] = df_4w_3[target_col] * df_4w_3['weights'] df_4w_3 = df_4w_3.groupby([store_col, comb_col], as_index=False).agg( {'wtd_demand': 'sum'}) df_4w = pd.concat([df_4w_1, df_4w_2, df_4w_3], axis=0) df_4w['wtd_demand'] = np.round(df_4w['wtd_demand'] * 4) return df_4w def preprocess_all( self, sales=None, cfr_pr=None, comb_list=None, calendar=None, first_bill_date=None, last_date=None, ): sales = self.add_ts_id(sales) # filter ################################################# if local_testing == 1: tsid_list = \ sales.sort_values(by=['net_sales_quantity'], ascending=False)[ key_col].unique().tolist()[:20] sales = sales[sales[key_col].isin(tsid_list)] ################################################# sales = self.preprocess_sales(sales, comb_list) sales = self.get_formatted_data(sales) cfr_pr = self.preprocess_cfr_pr(cfr_pr) sales_daily = self.merge_cfr_pr(sales, cfr_pr) calendar, cal_sales = self.preprocess_calendar(calendar, last_date) sales = self.merge_calendar(sales_daily, calendar) first_bill_date = self.preprocess_bill_date(first_bill_date) sales = self.merge_first_bill_date(sales, first_bill_date) sales_pred = self.make_future_df(sales.copy()) sales_pred_vald, train_vald_max_date = self.sales_pred_vald_df(sales) sales_4w_agg, sales_pred_4w_agg = self.sales_4w_agg(sales) sales_pred_4w_agg_vald, train_4w_agg_vald_max_date = self.sales_pred_vald_df(sales_4w_agg) comb_sales_latest_12w = self.comb_sales_12w(sales) comb_sales_4w_wtd = self.comb_sales_4w_wtd(sales) return ( comb_sales_4w_wtd, comb_sales_latest_12w, train_4w_agg_vald_max_date, sales_pred_4w_agg_vald, train_vald_max_date, sales_pred_vald, sales_4w_agg, sales_pred_4w_agg, sales, sales_pred, cal_sales, sales_daily )
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_combination_fcst/engine/data_pre_process.py
data_pre_process.py
import pandas as pd import numpy as np from dateutil.relativedelta import relativedelta from tqdm import tqdm import logging import warnings warnings.filterwarnings("ignore") logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') from zeno_etl_libs.utils.ipc_pmf.config_ipc_combination import ( date_col, target_col, store_col, comb_col, eol_cutoff ) class Segmentation: def add_ts_id(self, df): df['ts_id'] = ( df[store_col].astype(int).astype(str) + '_' + df[comb_col].astype(str) ) return df def _calc_abc(self, df52): B_cutoff = 0.5 C_cutoff = 0.80 D_cutoff = 0.95 tot_sales = ( df52.groupby([ 'ts_id' ])[target_col].sum().reset_index() ) tot_sales.rename(columns={target_col: 'total_LY_sales'}, inplace=True) tot_sales.sort_values('total_LY_sales', ascending=False, inplace=True) tot_sales["perc_sales"] = ( tot_sales['total_LY_sales'] / tot_sales['total_LY_sales'].sum() ) tot_sales["cum_perc_sales"] = tot_sales.perc_sales.cumsum() tot_sales["ABC"] = "A" tot_sales.loc[tot_sales.cum_perc_sales > B_cutoff, "ABC"] = "B" tot_sales.loc[tot_sales.cum_perc_sales > C_cutoff, "ABC"] = "C" tot_sales.loc[tot_sales.cum_perc_sales > D_cutoff, "ABC"] = "D" # tot_sales = self.add_ts_id(tot_sales) return tot_sales[['ts_id', 'ABC', 'total_LY_sales', 'perc_sales', 'cum_perc_sales']] # TODO: lower COV cutoffs def get_abc_classification(self, df52): province_abc = df52.groupby( [store_col] ).apply(self._calc_abc) province_abc = province_abc[['ts_id', "ABC"]].reset_index(drop=True) # one tot_sales = ( df52 .groupby(['ts_id'])[target_col] .agg(['sum', 'mean']) .reset_index() ) tot_sales.rename( columns={'sum': 'total_LY_sales', 'mean': 'avg_ly_sales'}, inplace=True) tot_sales = tot_sales.merge( province_abc, on=['ts_id'], how='left' ) tot_sales = tot_sales.drop_duplicates() # tot_sales = self.add_ts_id(tot_sales) tot_sales = tot_sales[['ts_id', 'ABC']] return tot_sales def get_xyzw_classification(self, df1): input_ts_id = df1['ts_id'].unique() df1 = df1[df1[target_col] > 0] cov_df = df1.groupby(['ts_id'])[target_col].agg( ["mean", "std", "count", "sum"]) cov_df.reset_index(drop=False, inplace=True) cov_df['cov'] = np.where( ((cov_df["count"] > 2) & (cov_df["sum"] > 0)), (cov_df["std"]) / (cov_df["mean"]), np.nan ) cov_df['WXYZ'] = 'Z' cov_df.loc[cov_df['cov'] <= 1.2, 'WXYZ'] = 'Y' cov_df.loc[cov_df['cov'] <= 0.8, 'WXYZ'] = 'X' cov_df.loc[cov_df['cov'] <= 0.5, 'WXYZ'] = 'W' # cov_df = self.add_ts_id(cov_df) cov_df = cov_df[['ts_id', 'cov', 'WXYZ']] non_mapped_ts_ids = list( set(input_ts_id) - set(cov_df['ts_id'].unique()) ) non_mapped_cov = pd.DataFrame({ 'ts_id': non_mapped_ts_ids, 'cov': [np.nan] * len(non_mapped_ts_ids), 'WXYZ': ['Z'] * len(non_mapped_ts_ids) }) cov_df = pd.concat([cov_df, non_mapped_cov], axis=0) cov_df = cov_df.reset_index(drop=True) return cov_df def get_std(self, df1): input_ts_id = df1['ts_id'].unique() # df1 = df1[df1[target_col]>0] std_df = df1.groupby(['ts_id'])[target_col].agg(["std"]) return std_df def calc_interval_mean(self, x, key): df = pd.DataFrame({"X": x, "ts_id": key}).reset_index( drop=True).reset_index() df = df[df.X > 0] df["index_shift"] = df["index"].shift(-1) df["interval"] = df["index_shift"] - df["index"] df = df.dropna(subset=["interval"]) df['ADI'] = np.mean(df["interval"]) return df[['ts_id', 'ADI']] def calc_adi(self, df): # df = self.add_ts_id(df) logger.info( 'Combinations entering adi: {}'.format(df['ts_id'].nunique())) dict_of = dict(iter(df.groupby(['ts_id']))) logger.info("Total tsids in df: {}".format(df.ts_id.nunique())) logger.info("Total dictionary length: {}".format(len(dict_of))) list_dict = [ self.calc_interval_mean(dict_of[x][target_col], x) for x in tqdm(dict_of.keys()) ] data = ( pd.concat(list_dict) .reset_index(drop=True) .drop_duplicates() .reset_index(drop=True) ) logger.info('Combinations exiting adi: {}'.format(data.ts_id.nunique())) return data def get_PLC_segmentation(self, df, mature_cutoff_date, eol_cutoff_date): df1 = df[df[target_col] > 0] df1 = df1.groupby(['ts_id']).agg({date_col: [min, max]}) df1.reset_index(drop=False, inplace=True) df1.columns = [' '.join(col).strip() for col in df1.columns.values] df1['PLC Status L1'] = 'Mature' df1.loc[ (df1[date_col + ' min'] > mature_cutoff_date), 'PLC Status L1' ] = 'New Product' df1.loc[ (df1[date_col + ' max'] <= eol_cutoff_date), 'PLC Status L1' ] = 'EOL' # df1 = self.add_ts_id(df1) df1 = df1[['ts_id', 'PLC Status L1']] return df1 def get_group_mapping(self, seg_df): seg_df['Mixed'] = seg_df['ABC'].astype(str) + seg_df['WXYZ'].astype(str) seg_df['Group'] = 'Group3' group1_mask = seg_df['Mixed'].isin(['AW', 'AX', 'BW', 'BX']) seg_df.loc[group1_mask, 'Group'] = 'Group1' group2_mask = seg_df['Mixed'].isin(['AY', 'AZ', 'BY', 'BZ']) seg_df.loc[group2_mask, 'Group'] = 'Group2' return seg_df def calc_dem_pat(self, cov_df, adi_df): logger.info('Combinations entering calc_dem_pat: {}'.format( cov_df.ts_id.nunique())) logger.info('Combinations entering calc_dem_pat: {}'.format( adi_df.ts_id.nunique())) df = pd.merge(cov_df, adi_df, how='left', on='ts_id') df["cov2"] = np.power(df["cov"], 2) df["classification"] = "Lumpy" df.loc[ (df.ADI >= 1.32) & (df.cov2 < 0.49), "classification" ] = "Intermittent" df.loc[ (df.ADI < 1.32) & (df.cov2 >= 0.49), "classification" ] = "Erratic" df.loc[ (df.ADI < 1.32) & (df.cov2 < 0.49), "classification" ] = "Smooth" logger.info( 'Combinations exiting calc_dem_pat: {}'.format(df.ts_id.nunique())) return df[['ts_id', 'classification']] def get_start_end_dates_df(self, df, key_col, date_col, target_col, train_max_date, end_date): start_end_date_df = ( df[df[target_col] > 0] .groupby(key_col)[date_col] .agg({'min', 'max'}) .reset_index() .rename(columns={'min': 'start_date', 'max': 'end_date'}) ) start_end_date_df.loc[ ( start_end_date_df['end_date'] > ( train_max_date - relativedelta(weeks=eol_cutoff) ) ), 'end_date' ] = end_date return start_end_date_df def get_weekly_segmentation(self, df, df_sales_daily, train_max_date, end_date): df = df[df[date_col] <= train_max_date] df1 = df[ df[date_col] > (train_max_date - relativedelta(weeks=52)) ].copy(deep=True) df_std = df_sales_daily[ df_sales_daily[date_col] > (train_max_date - relativedelta(days=90)) ].copy(deep=True) df1 = self.add_ts_id(df1) abc_df = self._calc_abc(df1) xyzw_df = self.get_xyzw_classification(df1) std_df = self.get_std(df_std) adi_df = self.calc_adi(df1) demand_pattern_df = self.calc_dem_pat(xyzw_df[['ts_id', 'cov']], adi_df) mature_cutoff_date = train_max_date - relativedelta(weeks=52) eol_cutoff_date = train_max_date - relativedelta(weeks=13) plc_df = self.get_PLC_segmentation(df, mature_cutoff_date, eol_cutoff_date) start_end_date_df = self.get_start_end_dates_df( df, key_col='ts_id', date_col=date_col, target_col=target_col, train_max_date=train_max_date, end_date=end_date ) seg_df = plc_df.merge(abc_df, on='ts_id', how='outer') seg_df = seg_df.merge(xyzw_df, on='ts_id', how='outer') seg_df = seg_df.merge(adi_df, on='ts_id', how='outer') seg_df = seg_df.merge(demand_pattern_df, on='ts_id', how='outer') seg_df = seg_df.merge(start_end_date_df, on='ts_id', how='outer') seg_df = seg_df.merge(std_df, on='ts_id', how='outer') seg_df = self.get_group_mapping(seg_df) seg_df['Mixed'] = np.where(seg_df['Mixed']=='nannan', np.nan, seg_df['Mixed']) return seg_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_combination_fcst/engine/segmentation.py
segmentation.py
import numpy as np from zeno_etl_libs.utils.ipc_pmf.correction_flag import compare_df, \ add_correction_flag, compare_df_comb, add_correction_flag_comb def fcst_correction(fcst_df_comb_lvl, comb_sales_latest_12w, fcst_df_drug_lvl, drug_sales_latest_12w, drug_sales_latest_4w, comb_sales_4w_wtd, drug_sales_4w_wtd, logger): comb_sales_latest_12w['latest_28d_demand'] = np.round( comb_sales_latest_12w['actual_demand'] / 3) drug_sales_latest_12w['latest_28d_demand'] = np.round( drug_sales_latest_12w['actual_demand'] / 3) fcst_df_comb_lvl['fcst'] = np.round(fcst_df_comb_lvl['fcst']) fcst_df_comb_lvl = fcst_df_comb_lvl.merge( comb_sales_latest_12w[['comb_id', 'latest_28d_demand']], on='comb_id', how='left') fcst_df_drug_lvl['drug_id'] = fcst_df_drug_lvl['drug_id'].astype(int) fcst_df_drug_lvl['fcst'] = np.round(fcst_df_drug_lvl['fcst']) fcst_df_drug_lvl = fcst_df_drug_lvl.merge( drug_sales_latest_12w[['drug_id', 'latest_28d_demand']], on='drug_id', how='left') logger.info(f"Combination Fcst Recency Correction starts") df_pre_corr = fcst_df_comb_lvl.copy() fcst_df_comb_lvl['fcst'] = np.where(fcst_df_comb_lvl['fcst'] == 0, fcst_df_comb_lvl['latest_28d_demand'], fcst_df_comb_lvl['fcst']) df_post_corr = fcst_df_comb_lvl.copy() logger.info(f"Sum Fcst before: {df_pre_corr['fcst'].sum()}") logger.info(f"Sum Fcst after: {df_post_corr['fcst'].sum()}") corr_comb_lst = compare_df_comb(df_pre_corr, df_post_corr, logger, cols_to_compare=['fcst']) fcst_df_comb_lvl = add_correction_flag_comb(fcst_df_comb_lvl, corr_comb_lst, 'REC_CORR1') logger.info(f"Drug Fcst Recency Correction 1 starts") df_pre_corr = fcst_df_drug_lvl.copy() fcst_df_drug_lvl['fcst'] = np.where(fcst_df_drug_lvl['fcst'] == 0, fcst_df_drug_lvl['latest_28d_demand'], fcst_df_drug_lvl['fcst']) df_post_corr = fcst_df_drug_lvl.copy() logger.info(f"Sum Fcst before: {fcst_df_drug_lvl['fcst'].sum()}") logger.info(f"Sum Fcst after: {fcst_df_drug_lvl['fcst'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['fcst']) fcst_df_drug_lvl = add_correction_flag(fcst_df_drug_lvl, corr_drug_lst, 'REC_CORR1') fcst_df_comb_lvl.drop('latest_28d_demand', axis=1, inplace=True) fcst_df_drug_lvl.drop('latest_28d_demand', axis=1, inplace=True) # add drugs with recent sales forecasted_drug_list = fcst_df_drug_lvl['drug_id'].tolist() df_add1 = drug_sales_latest_12w.loc[~drug_sales_latest_12w['drug_id'].isin(forecasted_drug_list)] df_add2 = drug_sales_latest_4w.loc[~drug_sales_latest_4w['drug_id'].isin(forecasted_drug_list)] df_add1.rename({'latest_28d_demand': 'fcst'}, axis=1, inplace=True) df_add1.drop('actual_demand', axis=1, inplace=True) df_add2.rename({'actual_demand': 'fcst'}, axis=1, inplace=True) df_add = df_add1.append(df_add2) df_add = df_add.loc[df_add['fcst'] > 0] if df_add.shape[0] > 0: df_add['model'] = 'NA' df_add['bucket'] = 'NA' df_add['std'] = 0 df_add['correction_flags'] = "" df_add['ts_id'] = ( df_add['store_id'].astype(int).astype(str) + '_' + df_add['drug_id'].astype(int).astype(str) ) logger.info(f"Drug Fcst Recency Correction 2 starts") df_pre_corr = fcst_df_drug_lvl.copy() fcst_df_drug_lvl = fcst_df_drug_lvl.append(df_add[fcst_df_drug_lvl.columns]) df_post_corr = fcst_df_drug_lvl.copy() logger.info(f"Sum Fcst before: {fcst_df_drug_lvl['fcst'].sum()}") logger.info(f"Sum Fcst after: {fcst_df_drug_lvl['fcst'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['fcst']) fcst_df_drug_lvl = add_correction_flag(fcst_df_drug_lvl, corr_drug_lst, 'REC_CORR2') fcst_df_comb_lvl['fcst'] = np.where(fcst_df_comb_lvl['fcst'] < 0, 0, fcst_df_comb_lvl['fcst']) fcst_df_drug_lvl['fcst'] = np.where(fcst_df_drug_lvl['fcst'] < 0, 0, fcst_df_drug_lvl['fcst']) # fcst 4 week weighted replace logger.info(f"Comb Fcst Recency Correction 3 starts") df_pre_corr = fcst_df_comb_lvl.copy() fcst_df_comb_lvl = fcst_df_comb_lvl.merge(comb_sales_4w_wtd, on=['store_id', 'comb_id'], how='left') fcst_df_comb_lvl['fcst'] = np.where(fcst_df_comb_lvl['fcst'] < fcst_df_comb_lvl['wtd_demand'], fcst_df_comb_lvl['wtd_demand'], fcst_df_comb_lvl['fcst']) df_post_corr = fcst_df_comb_lvl.copy() logger.info(f"Sum Fcst before: {df_pre_corr['fcst'].sum()}") logger.info(f"Sum Fcst after: {df_post_corr['fcst'].sum()}") corr_comb_lst = compare_df_comb(df_pre_corr, df_post_corr, logger, cols_to_compare=['fcst']) fcst_df_comb_lvl = add_correction_flag_comb(fcst_df_comb_lvl, corr_comb_lst, 'REC_CORR3') logger.info(f"Drug Fcst Recency Correction 3 starts") df_pre_corr = fcst_df_drug_lvl.copy() fcst_df_drug_lvl = fcst_df_drug_lvl.merge(drug_sales_4w_wtd, on=['store_id', 'drug_id'], how='left') fcst_df_drug_lvl['fcst'] = np.where(fcst_df_drug_lvl['fcst'] < fcst_df_drug_lvl['wtd_demand'], fcst_df_drug_lvl['wtd_demand'], fcst_df_drug_lvl['fcst']) df_post_corr = fcst_df_drug_lvl.copy() logger.info(f"Sum Fcst before: {df_pre_corr['fcst'].sum()}") logger.info(f"Sum Fcst after: {df_post_corr['fcst'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['fcst']) fcst_df_drug_lvl = add_correction_flag(fcst_df_drug_lvl, corr_drug_lst, 'REC_CORR3') fcst_df_comb_lvl.drop('wtd_demand', axis=1, inplace=True) fcst_df_drug_lvl.drop('wtd_demand', axis=1, inplace=True) return fcst_df_comb_lvl, fcst_df_drug_lvl
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/heuristics/recency_corr.py
recency_corr.py
import pandas as pd import numpy as np import datetime as dt import time from dateutil.relativedelta import relativedelta from scipy.stats import norm from zeno_etl_libs.utils.ipc2.engine.data_load import LoadData from zeno_etl_libs.utils.ipc2.engine.forecast import Forecast from zeno_etl_libs.utils.ipc2.engine.feat_engg import Feature_Engg from zeno_etl_libs.utils.ipc_pmf.ipc_drug_fcst.engine.data_pre_process import PreprocessData from zeno_etl_libs.utils.ipc_pmf.ipc_drug_fcst.engine.segmentation import Segmentation from zeno_etl_libs.utils.ipc_pmf.ipc_drug_fcst.engine.ts_fcst import TS_forecast from zeno_etl_libs.utils.ipc_pmf.config_ipc_drug import * def ipc_drug_forecast(store_id, reset_date, type_list, schema, db, logger): store_id_list = ("({})").format(store_id) # for sql pass last_date = dt.date(day=1, month=4, year=2019) # max history load_max_date = pd.to_datetime(reset_date).date() - dt.timedelta( days=pd.to_datetime(reset_date).dayofweek + 1) # define empty variables in case of fail weekly_fcst = pd.DataFrame() ts_fcst = pd.DataFrame() ts_fcst_cols = [] logger.info("Data Loading Started...") data_load_obj = LoadData() ( drug_list, sales_history, cfr_pr, calendar, first_bill_date, drug_sp ) = data_load_obj.load_all_input( type_list=type_list, store_id_list=store_id_list, last_date=last_date, reset_date=reset_date, load_max_date=load_max_date, schema=schema, db=db ) logger.info("Data Pre Processing Started...") data_prep_obj = PreprocessData() ( drug_sales_4w_wtd, drug_sales_latest_4w, drug_sales_latest_12w, train_4w_agg_vald_max_date, sales_pred_4w_agg_vald, train_vald_max_date, sales_pred_vald, sales_4w_agg, sales_pred_4w_agg, sales, sales_pred, cal_sales, sales_daily ) = data_prep_obj.preprocess_all( sales=sales_history, drug_list=drug_list, cfr_pr=cfr_pr, calendar=calendar, first_bill_date=first_bill_date, last_date=last_date ) train_max_date = sales[date_col].max() end_date = sales_pred[date_col].max() logger.info("Segmentation Started...") seg_obj = Segmentation() seg_df = seg_obj.get_weekly_segmentation( df=sales.copy(deep=True), df_sales_daily=sales_daily.copy(deep=True), train_max_date=train_max_date, end_date=end_date ) seg_df['reset_date'] = str(reset_date) # ======================================================================== # VALIDATION AND BEST MODEL FOR BUCKET SELECTION # ======================================================================== # Find validation period actual demand valid_start_date = train_max_date - relativedelta(weeks=4) valid_period_demand = sales.loc[sales[date_col] > valid_start_date] valid_period_demand = valid_period_demand.groupby(key_col, as_index=False).agg( {target_col: "sum"}) min_history_date_validation = valid_start_date - relativedelta(weeks=4) df_min_date = sales_pred_vald.groupby(key_col, as_index=False).agg( {date_col: 'min'}) df_min_date['min_allowed_date'] = min_history_date_validation ts_ids_to_drop = \ df_min_date.loc[df_min_date['min_allowed_date'] < df_min_date[date_col]][ key_col].tolist() # Perform Un-Aggregated TS Forecast merged_df1 = pd.merge(sales_pred_vald, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[ merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] merged_df1 = merged_df1[~merged_df1['ts_id'].isin(ts_ids_to_drop)] # calculate bucket wise wmape valid_wmape = {'Model': []} for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: valid_wmape[bucket] = [] if runs_ts_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast( df=merged_df1.copy(), train_max_date=train_vald_max_date, forecast_start=train_vald_max_date + relativedelta(weeks=1)) for model in ts_fcst_cols: df_model = ts_fcst[[key_col, 'Mixed', model]] df_model = df_model.groupby(key_col, as_index=False).agg( {'Mixed': 'first', model: 'sum'}) df_model = df_model.merge(valid_period_demand, on=key_col, how='left') df_model['error'] = df_model[model] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / \ df_bucket_wmape[target_col] valid_wmape['Model'].append(model) for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = \ df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket][ 'wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) if run_ml_flag == 1: forecast_start = train_vald_max_date + relativedelta(weeks=1) weekly_fcst = run_LGBM(merged_df1, train_vald_max_date, forecast_start, logger, is_validation=True) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg( {'preds_lgb': 'sum'}) df_model = lgbm_fcst.merge(valid_period_demand, on=key_col, how='left') df_model = df_model.merge(seg_df[[key_col, 'Mixed']], how='left', on='ts_id') df_model['error'] = df_model['preds_lgb'] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / \ df_bucket_wmape[target_col] valid_wmape['Model'].append('LGBM') for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket][ 'wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) # Perform Aggregated TS Forecast merged_df1 = pd.merge(sales_pred_4w_agg_vald, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[ merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] merged_df1 = merged_df1[~merged_df1['ts_id'].isin(ts_ids_to_drop)] if run_ts_4w_agg_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast_agg( df=merged_df1.copy(), train_max_date=train_4w_agg_vald_max_date, forecast_start=train_4w_agg_vald_max_date + relativedelta(weeks=1)) for model in ts_fcst_cols: df_model = ts_fcst[[key_col, 'Mixed', model]] df_model = df_model.groupby(key_col, as_index=False).agg( {'Mixed': 'first', model: 'sum'}) df_model = df_model.merge(valid_period_demand, on=key_col, how='left') df_model['error'] = df_model[model] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / \ df_bucket_wmape[target_col] valid_wmape['Model'].append(model) for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = \ df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket][ 'wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) if run_ml_4w_agg_flag == 1: forecast_start = train_4w_agg_vald_max_date + relativedelta(weeks=4) weekly_fcst = run_LGBM(merged_df1, train_4w_agg_vald_max_date, forecast_start, logger, is_validation=True, agg_4w=True) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg( {'preds_lgb': 'sum'}) df_model = lgbm_fcst.merge(valid_period_demand, on=key_col, how='left') df_model = df_model.merge(seg_df[[key_col, 'Mixed']], how='left', on='ts_id') df_model['error'] = df_model['preds_lgb'] - df_model[target_col] df_model['abs_error'] = abs(df_model['error']) df_bucket_wmape = df_model.groupby('Mixed', as_index=False).agg( {'abs_error': 'sum', target_col: 'sum'}) df_bucket_wmape['wmape'] = df_bucket_wmape['abs_error'] / \ df_bucket_wmape[target_col] valid_wmape['Model'].append('LGBM_4w_agg') for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: try: wmape = df_bucket_wmape.loc[df_bucket_wmape['Mixed'] == bucket][ 'wmape'].values[0] except: wmape = np.inf valid_wmape[bucket].append(wmape) # ======================================================================== # Choose best model based on lowest wmape # ======================================================================== best_bucket_model = {} for bucket in ['AW', 'AX', 'AY', 'AZ', 'BW', 'BX', 'BY', 'BZ', 'CW', 'CX', 'CY', 'CZ', 'DW', 'DX', 'DY', 'DZ']: min_wmape = min(valid_wmape[bucket]) if min_wmape != np.inf: best_bucket_model[bucket] = valid_wmape['Model'][ valid_wmape[bucket].index(min_wmape)] else: best_bucket_model[bucket] = default_model # default # FIXED BUCKETS # best_bucket_model['CY'] = 'LGBM' # best_bucket_model['CZ'] = 'LGBM' # best_bucket_model['DY'] = 'LGBM' # best_bucket_model['DZ'] = 'LGBM' # ======================================================================== # TRAINING AND FINAL FORECAST # ======================================================================== # Perform Un-Aggregated TS Forecast merged_df1 = pd.merge(sales_pred, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[ merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] if runs_ts_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast( df=merged_df1.copy(), train_max_date=train_max_date, forecast_start=train_max_date + relativedelta(weeks=2)) final_fcst = pd.DataFrame() for model_fcst in ts_fcst_cols: df_model_fcst = ts_fcst.groupby(key_col, as_index=False).agg( {model_fcst: 'sum'}) df_model_fcst.rename({model_fcst: 'fcst'}, axis=1, inplace=True) df_model_fcst['model'] = model_fcst final_fcst = final_fcst.append(df_model_fcst) if run_ml_flag == 1: forecast_start = train_max_date + relativedelta(weeks=2) weekly_fcst = run_LGBM(merged_df1, train_max_date, forecast_start, logger, is_validation=False) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg( {'preds_lgb': 'sum'}) lgbm_fcst.rename({'preds_lgb': 'fcst'}, axis=1, inplace=True) lgbm_fcst['model'] = 'LGBM' final_fcst = final_fcst.append(lgbm_fcst) # Perform Aggregated TS Forecast merged_df1 = pd.merge(sales_pred_4w_agg, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[ merged_df1['PLC Status L1'].isin(['Mature', 'New Product'])] if run_ts_4w_agg_flag == 1: ts_fcst_obj = TS_forecast() ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast_agg( df=merged_df1.copy(), train_max_date=train_max_date, forecast_start=train_max_date + relativedelta(weeks=2)) for model_fcst in ts_fcst_cols: df_model_fcst = ts_fcst.groupby(key_col, as_index=False).agg( {model_fcst: 'sum'}) df_model_fcst.rename({model_fcst: 'fcst'}, axis=1, inplace=True) df_model_fcst['model'] = model_fcst final_fcst = final_fcst.append(df_model_fcst) if run_ml_4w_agg_flag == 1: forecast_start = train_max_date + relativedelta(weeks=2) weekly_fcst = run_LGBM(merged_df1, train_max_date, forecast_start, logger, is_validation=False, agg_4w=True) lgbm_fcst = weekly_fcst.groupby(key_col, as_index=False).agg( {'preds_lgb': 'sum'}) lgbm_fcst.rename({'preds_lgb': 'fcst'}, axis=1, inplace=True) lgbm_fcst['model'] = 'LGBM_4w_agg' final_fcst = final_fcst.append(lgbm_fcst) final_fcst = final_fcst.merge(seg_df[[key_col, 'Mixed']], on=key_col, how='left') final_fcst.rename({'Mixed': 'bucket'}, axis=1, inplace=True) # Choose buckets forecast of best models as final forecast final_selected_fcst = pd.DataFrame() for bucket in best_bucket_model.keys(): df_selected = final_fcst.loc[(final_fcst['bucket'] == bucket) & (final_fcst['model'] == best_bucket_model[ bucket])] final_selected_fcst = final_selected_fcst.append(df_selected) # add comb rejected due to recent sales list_all_comb = final_fcst[key_col].unique().tolist() list_all_final_comb = final_selected_fcst[key_col].unique().tolist() list_comb_rejects = list(set(list_all_comb) - set(list_all_final_comb)) comb_fcst_to_add = final_fcst.loc[ (final_fcst[key_col].isin(list_comb_rejects)) & (final_fcst["model"] == default_model)] final_selected_fcst = final_selected_fcst.append(comb_fcst_to_add) final_selected_fcst = final_selected_fcst.merge(seg_df[[key_col, 'std']], on=key_col, how='left') final_selected_fcst[[store_col, drug_col]] = final_selected_fcst[ 'ts_id'].str.split('_', expand=True) final_selected_fcst[store_col] = final_selected_fcst[store_col].astype(int) model_name_map = {'preds_ETS_12w': 'ETS_12w', 'preds_ma': 'MA', 'preds_ETS_auto': 'ETS_auto', 'preds_croston': 'Croston', 'preds_ETS_4w_auto': 'ETS_4w_auto', 'preds_AE_ts': 'AvgTS', 'preds_prophet': 'Prophet'} final_selected_fcst["model"] = final_selected_fcst["model"].map( model_name_map).fillna(final_selected_fcst["model"]) return final_selected_fcst, seg_df, drug_sales_latest_12w, drug_sales_latest_4w, drug_sales_4w_wtd def run_LGBM(merged_df1, train_max_date, forecast_start, logger, is_validation=False, agg_4w=False): start_time = time.time() merged_df1['All'] = 'All' slice_col = 'All' forecast_volume = merged_df1[merged_df1[date_col] > train_max_date][ target_col].sum() assert forecast_volume == 0 logger.info( "forecast start {} total volume: {}".format(forecast_start, forecast_volume) ) forecast_df = pd.DataFrame() validation_df = pd.DataFrame() weekly_fcst = pd.DataFrame() if agg_4w: end_range = 2 else: end_range = 5 for i in range(1, end_range): if is_validation: num_shift_lags = i else: num_shift_lags = i + 1 # for group_name in merged_df1[slice_col].dropna.unique(): # slice_col = 'Mixed' # for groups in [['AW', 'BW', 'CW', 'DW'], ['AX', 'BX', 'CX', 'DX'], ['AY', 'BY', 'CY', 'DY'], ['AZ', 'BZ', 'CZ', 'DZ']]: for groups in ['All']: logger.info('Group: {}'.format(groups)) logger.info("Feature Engineering Started...") feat_df = pd.DataFrame() for one_df in [merged_df1]: feat_engg_obj = Feature_Engg() one_feat_df = feat_engg_obj.feat_agg( one_df[ one_df[slice_col] == groups ].drop(slice_col, axis=1).copy(deep=True), train_max_date=train_max_date, num_shift_lag=num_shift_lags ) feat_df = pd.concat([one_feat_df, feat_df]) if pd.DataFrame(feat_df).empty: continue logger.info( "Forecasting Started for {}...".format(forecast_start)) forecast_obj = Forecast() fcst_df, val_df, Feature_Imp_all = forecast_obj.get_STM_forecast( feat_df.copy(deep=True), forecast_start=forecast_start, num_shift_lags=num_shift_lags ) forecast_df = pd.concat([forecast_df, fcst_df], axis=0) validation_df = pd.concat([validation_df, val_df]) ml_fc_cols = [i for i in forecast_df.columns if i.startswith('preds_')] # forecast_df['AE'] = forecast_df[ml_fc_cols].mean(axis=1) end_time = time.time() logger.info( "total time for {} forecast: {}" .format(forecast_start, end_time - start_time) ) forecast_start = forecast_start + relativedelta(weeks=1) # weekly_fcst = pd.concat([weekly_fcst, forecast_df]) return forecast_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_drug_fcst/forecast_main.py
forecast_main.py
import numpy as np np.random.seed(0) import pandas as pd # import time # import re # from datetime import date # from dateutil.relativedelta import relativedelta # from statsmodels.tsa.exponential_smoothing.ets import ETSModel from prophet import Prophet from statsmodels.tsa.api import ExponentialSmoothing # import sktime from zeno_etl_libs.utils.ipc2.helpers.helper_functions import \ applyParallel_croston # from boruta import BorutaPy from zeno_etl_libs.utils.ipc_pmf.config_ipc_drug import ( date_col, target_col, models_un_agg, models_agg ) import logging logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') class TS_forecast: def train_test_split(self, df, train_max_date, forecast_start): df.rename(columns={date_col: 'ds', target_col: 'y'}, inplace=True) df.sort_values(by=['ds'], inplace=True) train = df[df['ds'] <= train_max_date] test = df[df['ds'] >= forecast_start] return train, test def Croston_TSB(self, ts, extra_periods=4, alpha=0.4, beta=0.4): d = np.array(ts) # Transform the input into a numpy array cols = len(d) # Historical period length d = np.append(d, [ np.nan] * extra_periods) # Append np.nan into the demand array to cover future periods # level (a), probability(p) and forecast (f) a, p, f = np.full((3, cols + extra_periods), np.nan) # Initialization first_occurence = np.argmax(d[:cols] > 0) a[0] = d[first_occurence] p[0] = 1 / (1 + first_occurence) f[0] = p[0] * a[0] # Create all the t+1 forecasts for t in range(0, cols): if d[t] > 0: a[t + 1] = alpha * d[t] + (1 - alpha) * a[t] p[t + 1] = beta * (1) + (1 - beta) * p[t] else: a[t + 1] = a[t] p[t + 1] = (1 - beta) * p[t] f[t + 1] = p[t + 1] * a[t + 1] # Future Forecast a[cols + 1:cols + extra_periods] = a[cols] p[cols + 1:cols + extra_periods] = p[cols] f[cols + 1:cols + extra_periods] = f[cols] df = pd.DataFrame.from_dict( {"Demand": d, "Forecast": f, "Period": p, "Level": a, "Error": d - f}) return df[-extra_periods:] def ETS_forecast(self, train, test, latest_12w_fit=False): try: train.set_index(['ds'], inplace=True) test.set_index(['ds'], inplace=True) train.index.freq = train.index.inferred_freq test.index.freq = test.index.inferred_freq if latest_12w_fit: train = train[-12:] # use only latest 3 months fit = ExponentialSmoothing(train['y']).fit() preds = fit.forecast(len(test) + 1) preds = preds[-len(test):] except Exception as e: logger.info("error in ETS fcst") logger.info(str(e)) preds = 0 return preds def ma_forecast(self, data): """ Purpose: Compute MA forecast for the for the forecast horizon specified Inputs: time series to create forecast Output: series with forecasted values """ sma_df = data.copy(deep=True) yhat = [] if len(data) >= 8: for i in range(5): sma_val = sma_df.rolling(8).mean().iloc[-1] sma_df.loc[sma_df.index.max() + 1] = sma_val yhat.append(sma_val) else: for i in range(5): sma_val = sma_df.rolling(len(data)).mean().iloc[-1] sma_df.loc[sma_df.index.max() + 1] = sma_val yhat.append(sma_val) logger.info(yhat) return yhat[-4:] def prophet_fcst(self, train, test, params=None): # reg_list = [] try: if params is None: pro = Prophet() else: pro = Prophet(n_changepoints=params) # for j in train.columns: # if j not in col_list: # pro.add_regressor(j) # reg_list.append(j) pro.fit(train[['ds', 'y']]) pred_f = pro.predict(test) test = test[["ds", "y"]] test = pd.merge(test, pred_f, on="ds", how="left") except Exception as e: logger.info("error in prophet fcst") logger.info(str(e)) test['yhat'] = 0 return test def ts_forecast_un_agg(self, df, train_max_date, forecast_start): train, test = self.train_test_split(df, train_max_date=train_max_date, forecast_start=forecast_start) test = test.sort_values(by=['ds']) if 'croston' in models_un_agg: preds_croston = self.Croston_TSB(train['y']) test['preds_croston'] = preds_croston['Forecast'].values if 'ETS_Auto' in models_un_agg: preds_ETS = self.ETS_forecast(train.copy(), test.copy()) try: test['preds_ETS_auto'] = preds_ETS.values except: test['preds_ETS_auto'] = 0 if 'ETS_12w' in models_un_agg: preds_ETS = self.ETS_forecast(train.copy(), test.copy(), latest_12w_fit=True) try: test['preds_ETS_12w'] = preds_ETS.values except: test['preds_ETS_12w'] = 0 if 'MA' in models_un_agg: preds_ma = self.ma_forecast(train['y']) test['preds_ma'] = preds_ma if 'prophet' in models_un_agg: preds_prophet = self.prophet_fcst(train.copy(), test.copy()) test['preds_prophet'] = preds_prophet['yhat'].values return test def ts_forecast_agg(self, df, train_max_date, forecast_start): train, test = self.train_test_split(df, train_max_date=train_max_date, forecast_start=forecast_start) test = test.sort_values(by=['ds']) if 'ETS_4w_agg' in models_agg: preds_ETS = self.ETS_forecast(train.copy(), test.copy(), latest_12w_fit=True) try: test['preds_ETS_4w_auto'] = preds_ETS.values except: test['preds_ETS_4w_auto'] = 0 return test def apply_ts_forecast(self, df, train_max_date, forecast_start): # global train_date # train_date = train_max_date # global forecast_start_date # forecast_start_date = forecast_start preds = applyParallel_croston( df.groupby('ts_id'), func=self.ts_forecast_un_agg, train_max_date=train_max_date, forecast_start=forecast_start ) preds.rename(columns={'ds': date_col, 'y': target_col}, inplace=True) ts_fcst_cols = [i for i in preds.columns if i.startswith('preds_')] for col in ts_fcst_cols: preds[col].fillna(0, inplace=True) preds[col] = np.where(preds[col] < 0, 0, preds[col]) # preds['preds_AE_ts'] = preds[ts_fcst_cols].mean(axis=1) # ts_fcst_cols = ts_fcst_cols + ['preds_AE_ts'] return preds, ts_fcst_cols def apply_ts_forecast_agg(self, df, train_max_date, forecast_start): # global train_date # train_date = train_max_date # global forecast_start_date # forecast_start_date = forecast_start preds = applyParallel_croston( df.groupby('ts_id'), func=self.ts_forecast_agg, train_max_date=train_max_date, forecast_start=forecast_start ) preds.rename(columns={'ds': date_col, 'y': target_col}, inplace=True) ts_fcst_cols = [i for i in preds.columns if i.startswith('preds_')] for col in ts_fcst_cols: preds[col].fillna(0, inplace=True) preds[col] = np.where(preds[col] < 0, 0, preds[col]) # preds['preds_AE_ts'] = preds[ts_fcst_cols].mean(axis=1) return preds, ts_fcst_cols
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_drug_fcst/engine/ts_fcst.py
ts_fcst.py
import pandas as pd import datetime import numpy as np from zeno_etl_libs.utils.ipc_pmf.config_ipc_drug import date_col, store_col, \ drug_col, target_col, key_col, local_testing class PreprocessData: def add_ts_id(self, df): df = df[~df[drug_col].isnull()].reset_index(drop=True) df['ts_id'] = ( df[store_col].astype(int).astype(str) + '_' + df[drug_col].astype(int).astype(str) ) return df def preprocess_sales(self, df, drug_list): df.rename(columns={ 'net_sales_quantity': target_col }, inplace=True) df.rename(columns={ 'sales_date': date_col }, inplace=True) set_dtypes = { store_col: int, drug_col: int, date_col: str, target_col: float } df = df.astype(set_dtypes) df[target_col] = df[target_col].round() df[date_col] = pd.to_datetime(df[date_col]) df = df.groupby( [store_col, drug_col, key_col, date_col] )[target_col].sum().reset_index() df = df[df[drug_col].isin(drug_list[drug_col].unique().tolist())] return df def get_formatted_data(self, df): df_start = df.groupby([key_col])[date_col].min().reset_index().rename( columns={date_col: 'sales_start'}) df = df[[key_col, date_col, target_col]] min_date = df[date_col].dropna().min() end_date = df[date_col].dropna().max() date_range = [] date_range = pd.date_range( start=min_date, end=end_date, freq='d' ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = np.nan df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = pd.merge(df, df_start, how='left', on=key_col) df = df[df[date_col] >= df['sales_start']] df[[store_col, drug_col]] = df[key_col].str.split('_', expand=True) df[[store_col, drug_col]] = df[[store_col, drug_col]] df[store_col] = df[store_col].astype(int) df[drug_col] = df[drug_col].astype(int) return df def preprocess_cfr_pr(self, df): set_dtypes = { store_col: int, drug_col: int, 'loss_quantity': int } df = df.astype(set_dtypes) df['shortbook_date'] = pd.to_datetime(df['shortbook_date']) return df def merge_cfr_pr(self, sales, cfr_pr): df = sales.merge(cfr_pr, left_on=[store_col, drug_col, date_col], right_on=[store_col, drug_col, 'shortbook_date'], how='left') df[date_col] = df[date_col].combine_first(df['shortbook_date']) df[target_col].fillna(0, inplace=True) df['loss_quantity'].fillna(0, inplace=True) df[target_col] += df['loss_quantity'] df.drop(['shortbook_date', 'loss_quantity'], axis=1, inplace=True) return df def preprocess_calendar(self, df, last_date): df.rename(columns={'date': date_col}, inplace=True) df[date_col] = pd.to_datetime(df[date_col]) cal_sales = df.copy() cal_sales['week_begin_dt'] = cal_sales.apply( lambda x: x[date_col] - datetime.timedelta(x['day_of_week']), axis=1) cal_sales['month_begin_dt'] = cal_sales.apply( lambda x: x['date'] - datetime.timedelta(x['date'].day - 1), axis=1) cal_sales['key'] = 1 ld = pd.to_datetime(last_date) cal_sales = cal_sales[cal_sales[date_col] > ld] return df, cal_sales def merge_calendar(self, sales, calendar): df = sales.merge(calendar, how='left', on=date_col ) # df_week_days_count = df.groupby([key_col, 'year', 'week_of_year'])[date_col].count().reset_index().rename(columns = {date_col:'week_days_count'}) # df['week_days_count'] = 1 df['week_begin_dt'] = df.apply( lambda x: x[date_col] - datetime.timedelta(x['day_of_week']), axis=1) df_week_days_count = df.groupby(['ts_id', 'week_begin_dt'])[ date_col].count().reset_index().rename( columns={date_col: 'week_days_count'}) # df = df.groupby(['ts_id', store_col, drug_col, ]).resample('W-Mon', on =date_col )[target_col].sum().reset_index() df = df.groupby(['ts_id', store_col, drug_col, 'week_begin_dt'])[ target_col].sum().reset_index() df = pd.merge(df, df_week_days_count, how='left', on=[key_col, 'week_begin_dt']) df = df[df['week_days_count'] == 7].reset_index(drop=True) df.drop(columns=['week_days_count'], inplace=True) df.rename(columns={'week_begin_dt': date_col}, inplace=True) return df def preprocess_bill_date(self, df): df.rename(columns={'store-id': store_col}, inplace=True) df['bill_date'] = pd.to_datetime(df['bill_date']) return df def merge_first_bill_date(self, sales, first_bill_date): df = pd.merge(sales, first_bill_date, on=[store_col]) df = df[df[date_col] >= df['bill_date']].reset_index(drop=True) df.drop(columns=['bill_date'], inplace=True) return df def make_future_df(self, df): start_date_df = ( df .groupby(key_col)[date_col] .min() .reset_index() .rename(columns={date_col: 'start_date'}) ) df = df[[key_col, date_col, target_col]] end_date = df[date_col].max() + datetime.timedelta(weeks=5) min_date = df[date_col].min() date_range = pd.date_range( start=min_date, end=end_date, freq="W-MON" ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = 0 df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = df.merge(start_date_df, on=key_col, how='left') df = df[ df[date_col] >= df['start_date'] ] df.drop('start_date', axis=1, inplace=True) df[[store_col, drug_col]] = df[key_col].str.split('_', expand=True) return df def make_future_df_4w_agg(self, df): start_date_df = ( df .groupby(key_col)[date_col] .min() .reset_index() .rename(columns={date_col: 'start_date'}) ) df = df[[key_col, date_col, target_col]] fcst_week_start = df[date_col].max() + datetime.timedelta(weeks=5) date_range = [fcst_week_start] df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = 0 df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = df.merge(start_date_df, on=key_col, how='left') df = df[ df[date_col] >= df['start_date'] ] df.drop('start_date', axis=1, inplace=True) df[[store_col, drug_col]] = df[key_col].str.split('_', expand=True) return df def sales_pred_vald_df( self, df ): vald_max_date = df[date_col].max() - datetime.timedelta(weeks=4) df_vald_train = df.loc[df[date_col] <= vald_max_date] df_vald_future = df.loc[~(df[date_col] <= vald_max_date)] df_vald_future[target_col] = 0 df_final = df_vald_train.append(df_vald_future) train_vald_max_date = df_vald_train[date_col].max() return df_final, train_vald_max_date def sales_4w_agg( self, df ): # ===================================================================== # Combine 4 weeks into an arbitrary group # ===================================================================== unique_ts_ids = df[key_col].unique().tolist() sales_4w_agg = pd.DataFrame() for ts_id in unique_ts_ids: week_gp_size = 4 sales_temp = df.loc[df[key_col] == ts_id] available_week_count = sales_temp.shape[0] if available_week_count >= (3 * week_gp_size): allowable_week_count = int( week_gp_size * np.fix(available_week_count / week_gp_size)) sales_temp = sales_temp.sort_values(by=["date"], ascending=True) sales_temp = sales_temp[-allowable_week_count:] week_gps_count = int(allowable_week_count / week_gp_size) week_gps_list = np.arange(1, week_gps_count + 1, 1) week_gps_id_list = np.repeat(week_gps_list, week_gp_size) sales_temp["week_gps_id"] = week_gps_id_list sales_temp = sales_temp.groupby( [key_col, store_col, drug_col, "week_gps_id"], as_index=False).agg( {"date": "first", "actual_demand": "sum"}) sales_4w_agg = sales_4w_agg.append(sales_temp) sales_4w_agg = sales_4w_agg.drop("week_gps_id", axis=1) sales_pred_4w_agg = self.make_future_df_4w_agg(sales_4w_agg.copy()) return sales_4w_agg, sales_pred_4w_agg def drug_sales_12w_4w( self, df ): date_12w_back = df[date_col].max() - datetime.timedelta(weeks=12) df_12w = df.loc[df[date_col] > date_12w_back] date_4w_back = df[date_col].max() - datetime.timedelta(weeks=4) df_4w = df.loc[df[date_col] > date_4w_back] df_count = df_12w.groupby([store_col, drug_col], as_index=False).agg( {target_col: 'count'}) df_count.rename({target_col: 'week_count'}, axis=1, inplace=True) drugs_with_12w_data = df_count.loc[df_count['week_count'] == 12][drug_col].tolist() drugs_with_4w_data = df_count.loc[(df_count['week_count'] >= 4) & (df_count['week_count'] != 12)][drug_col].tolist() df_12w = df_12w.groupby([store_col, drug_col], as_index=False).agg( {target_col: 'sum'}) df_12w = df_12w.loc[df_12w[drug_col].isin(drugs_with_12w_data)] df_4w = df_4w.groupby([store_col, drug_col], as_index=False).agg( {target_col: 'sum'}) df_4w = df_4w.loc[df_4w[drug_col].isin(drugs_with_4w_data)] return df_12w, df_4w def drug_sales_4w_wtd( self, df ): date_4w_back = df[date_col].max() - datetime.timedelta(weeks=4) df_4w = df.loc[df[date_col] > date_4w_back] # sales > 0 and all 4 latest week df_4w_1 = df_4w[df_4w[target_col] > 0] df_4w_cnt = df_4w_1.groupby([store_col, drug_col], as_index=False).agg( {target_col: 'count'}) df_4w_cnt.rename({target_col: 'week_count'}, axis=1, inplace=True) list_4w_drugs = df_4w_cnt.loc[df_4w_cnt['week_count'] == 4][drug_col].tolist() df_4w_1 = df_4w_1.loc[df_4w_1[drug_col].isin(list_4w_drugs)] dates_list = list(df_4w.date.unique()) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[3], 0.4, 0) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[2], 0.3, df_4w_1['weights']) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[1], 0.2, df_4w_1['weights']) df_4w_1['weights'] = np.where(df_4w_1[date_col] == dates_list[0], 0.1, df_4w_1['weights']) df_4w_1['wtd_demand'] = df_4w_1[target_col] * df_4w_1['weights'] df_4w_1 = df_4w_1.groupby([store_col, drug_col], as_index=False).agg( {'wtd_demand': 'sum'}) # sales > 0 and only 3 latest week df_4w_2 = df_4w[df_4w[target_col] > 0] df_4w_cnt = df_4w_2.groupby([store_col, drug_col], as_index=False).agg( {target_col: 'count'}) df_4w_cnt.rename({target_col: 'week_count'}, axis=1, inplace=True) list_4w_drugs = df_4w_cnt.loc[df_4w_cnt['week_count'] == 3][drug_col].tolist() df_4w_2 = df_4w_2.loc[df_4w_2[drug_col].isin(list_4w_drugs)] df_4w_2['w_count'] = np.tile(np.arange(1, 4), len(df_4w_2))[:len(df_4w_2)] df_4w_2['weights'] = np.where(df_4w_2['w_count'] == 3, 0.5, 0) df_4w_2['weights'] = np.where(df_4w_2['w_count'] == 2, 0.3, df_4w_2['weights']) df_4w_2['weights'] = np.where(df_4w_2['w_count'] == 1, 0.2, df_4w_2['weights']) df_4w_2['wtd_demand'] = df_4w_2[target_col] * df_4w_2['weights'] df_4w_2 = df_4w_2.groupby([store_col, drug_col], as_index=False).agg( {'wtd_demand': 'sum'}) # sales > 0 and only 2 latest week df_4w_3 = df_4w[df_4w[target_col] > 0] df_4w_cnt = df_4w_3.groupby([store_col, drug_col], as_index=False).agg( {target_col: 'count'}) df_4w_cnt.rename({target_col: 'week_count'}, axis=1, inplace=True) list_4w_drugs = df_4w_cnt.loc[df_4w_cnt['week_count'] == 2][drug_col].tolist() df_4w_3 = df_4w_3.loc[df_4w_3[drug_col].isin(list_4w_drugs)] df_4w_3['w_count'] = np.tile(np.arange(1, 3), len(df_4w_3))[:len(df_4w_3)] df_4w_3['weights'] = np.where(df_4w_3['w_count'] == 2, 0.6, 0) df_4w_3['weights'] = np.where(df_4w_3['w_count'] == 1, 0.4, df_4w_3['weights']) df_4w_3['wtd_demand'] = df_4w_3[target_col] * df_4w_3['weights'] df_4w_3 = df_4w_3.groupby([store_col, drug_col], as_index=False).agg( {'wtd_demand': 'sum'}) df_4w = pd.concat([df_4w_1, df_4w_2, df_4w_3], axis=0) df_4w['wtd_demand'] = np.round(df_4w['wtd_demand'] * 4) return df_4w def preprocess_all( self, sales=None, cfr_pr=None, drug_list=None, calendar=None, first_bill_date=None, last_date=None, ): sales = self.add_ts_id(sales) # filter ################################################# if local_testing == 1: tsid_list = \ sales.sort_values(by=['net_sales_quantity'], ascending=False)[ key_col].unique().tolist()[:20] sales = sales[sales[key_col].isin(tsid_list)] ################################################# sales = self.preprocess_sales(sales, drug_list) sales = self.get_formatted_data(sales) cfr_pr = self.preprocess_cfr_pr(cfr_pr) sales_daily = self.merge_cfr_pr(sales, cfr_pr) calendar, cal_sales = self.preprocess_calendar(calendar, last_date) sales = self.merge_calendar(sales_daily, calendar) first_bill_date = self.preprocess_bill_date(first_bill_date) sales = self.merge_first_bill_date(sales, first_bill_date) sales_pred = self.make_future_df(sales.copy()) sales_pred_vald, train_vald_max_date = self.sales_pred_vald_df(sales) sales_4w_agg, sales_pred_4w_agg = self.sales_4w_agg(sales) sales_pred_4w_agg_vald, train_4w_agg_vald_max_date = self.sales_pred_vald_df(sales_4w_agg) drug_sales_latest_12w, drug_sales_latest_4w = self.drug_sales_12w_4w(sales) drug_sales_4w_wtd = self.drug_sales_4w_wtd(sales) return ( drug_sales_4w_wtd, drug_sales_latest_4w, drug_sales_latest_12w, train_4w_agg_vald_max_date, sales_pred_4w_agg_vald, train_vald_max_date, sales_pred_vald, sales_4w_agg, sales_pred_4w_agg, sales, sales_pred, cal_sales, sales_daily )
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_drug_fcst/engine/data_pre_process.py
data_pre_process.py
import pandas as pd import numpy as np from dateutil.relativedelta import relativedelta from tqdm import tqdm import logging import warnings warnings.filterwarnings("ignore") logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') from zeno_etl_libs.utils.ipc_pmf.config_ipc_drug import ( date_col, target_col, store_col, drug_col, eol_cutoff ) class Segmentation: def add_ts_id(self, df): df['ts_id'] = ( df[store_col].astype(int).astype(str) + '_' + df[drug_col].astype(int).astype(str) ) return df def _calc_abc(self, df52): B_cutoff = 0.5 C_cutoff = 0.8 D_cutoff = 0.95 tot_sales = ( df52.groupby([ 'ts_id' ])[target_col].sum().reset_index() ) tot_sales.rename(columns={target_col: 'total_LY_sales'}, inplace=True) tot_sales.sort_values('total_LY_sales', ascending=False, inplace=True) tot_sales["perc_sales"] = ( tot_sales['total_LY_sales'] / tot_sales['total_LY_sales'].sum() ) tot_sales["cum_perc_sales"] = tot_sales.perc_sales.cumsum() tot_sales["ABC"] = "A" tot_sales.loc[tot_sales.cum_perc_sales > B_cutoff, "ABC"] = "B" tot_sales.loc[tot_sales.cum_perc_sales > C_cutoff, "ABC"] = "C" tot_sales.loc[tot_sales.cum_perc_sales > D_cutoff, "ABC"] = "D" # tot_sales = self.add_ts_id(tot_sales) return tot_sales[['ts_id', 'ABC', 'total_LY_sales']] # TODO: lower COV cutoffs def get_abc_classification(self, df52): province_abc = df52.groupby( [store_col] ).apply(self._calc_abc) province_abc = province_abc[['ts_id', "ABC"]].reset_index(drop=True) # one tot_sales = ( df52 .groupby(['ts_id'])[target_col] .agg(['sum', 'mean']) .reset_index() ) tot_sales.rename( columns={'sum': 'total_LY_sales', 'mean': 'avg_ly_sales'}, inplace=True) tot_sales = tot_sales.merge( province_abc, on=['ts_id'], how='left' ) tot_sales = tot_sales.drop_duplicates() # tot_sales = self.add_ts_id(tot_sales) tot_sales = tot_sales[['ts_id', 'ABC']] return tot_sales def get_xyzw_classification(self, df1): input_ts_id = df1['ts_id'].unique() df1 = df1[df1[target_col] > 0] cov_df = df1.groupby(['ts_id'])[target_col].agg( ["mean", "std", "count", "sum"]) cov_df.reset_index(drop=False, inplace=True) cov_df['cov'] = np.where( ((cov_df["count"] > 2) & (cov_df["sum"] > 0)), (cov_df["std"]) / (cov_df["mean"]), np.nan ) cov_df['WXYZ'] = 'Z' cov_df.loc[cov_df['cov'] <= 1.2, 'WXYZ'] = 'Y' cov_df.loc[cov_df['cov'] <= 0.8, 'WXYZ'] = 'X' cov_df.loc[cov_df['cov'] <= 0.5, 'WXYZ'] = 'W' # cov_df = self.add_ts_id(cov_df) cov_df = cov_df[['ts_id', 'cov', 'WXYZ']] non_mapped_ts_ids = list( set(input_ts_id) - set(cov_df['ts_id'].unique()) ) non_mapped_cov = pd.DataFrame({ 'ts_id': non_mapped_ts_ids, 'cov': [np.nan] * len(non_mapped_ts_ids), 'WXYZ': ['Z'] * len(non_mapped_ts_ids) }) cov_df = pd.concat([cov_df, non_mapped_cov], axis=0) cov_df = cov_df.reset_index(drop=True) return cov_df def get_std(self, df1): input_ts_id = df1['ts_id'].unique() # df1 = df1[df1[target_col]>0] std_df = df1.groupby(['ts_id'])[target_col].agg(["std"]) return std_df def calc_interval_mean(self, x, key): df = pd.DataFrame({"X": x, "ts_id": key}).reset_index( drop=True).reset_index() df = df[df.X > 0] df["index_shift"] = df["index"].shift(-1) df["interval"] = df["index_shift"] - df["index"] df = df.dropna(subset=["interval"]) df['ADI'] = np.mean(df["interval"]) return df[['ts_id', 'ADI']] def calc_adi(self, df): # df = self.add_ts_id(df) logger.info( 'Combinations entering adi: {}'.format(df['ts_id'].nunique())) dict_of = dict(iter(df.groupby(['ts_id']))) logger.info("Total tsids in df: {}".format(df.ts_id.nunique())) logger.info("Total dictionary length: {}".format(len(dict_of))) list_dict = [ self.calc_interval_mean(dict_of[x][target_col], x) for x in tqdm(dict_of.keys()) ] data = ( pd.concat(list_dict) .reset_index(drop=True) .drop_duplicates() .reset_index(drop=True) ) logger.info('Combinations exiting adi: {}'.format(data.ts_id.nunique())) return data def get_PLC_segmentation(self, df, mature_cutoff_date, eol_cutoff_date): df1 = df[df[target_col] > 0] df1 = df1.groupby(['ts_id']).agg({date_col: [min, max]}) df1.reset_index(drop=False, inplace=True) df1.columns = [' '.join(col).strip() for col in df1.columns.values] df1['PLC Status L1'] = 'Mature' df1.loc[ (df1[date_col + ' min'] > mature_cutoff_date), 'PLC Status L1' ] = 'New Product' df1.loc[ (df1[date_col + ' max'] <= eol_cutoff_date), 'PLC Status L1' ] = 'EOL' # df1 = self.add_ts_id(df1) df1 = df1[['ts_id', 'PLC Status L1']] return df1 def get_group_mapping(self, seg_df): seg_df['Mixed'] = seg_df['ABC'].astype(str) + seg_df['WXYZ'].astype(str) seg_df['Group'] = 'Group3' group1_mask = seg_df['Mixed'].isin(['AW', 'AX', 'BW', 'BX']) seg_df.loc[group1_mask, 'Group'] = 'Group1' group2_mask = seg_df['Mixed'].isin(['AY', 'AZ', 'BY', 'BZ']) seg_df.loc[group2_mask, 'Group'] = 'Group2' return seg_df def calc_dem_pat(self, cov_df, adi_df): logger.info('Combinations entering calc_dem_pat: {}'.format( cov_df.ts_id.nunique())) logger.info('Combinations entering calc_dem_pat: {}'.format( adi_df.ts_id.nunique())) df = pd.merge(cov_df, adi_df, how='left', on='ts_id') df["cov2"] = np.power(df["cov"], 2) df["classification"] = "Lumpy" df.loc[ (df.ADI >= 1.32) & (df.cov2 < 0.49), "classification" ] = "Intermittent" df.loc[ (df.ADI < 1.32) & (df.cov2 >= 0.49), "classification" ] = "Erratic" df.loc[ (df.ADI < 1.32) & (df.cov2 < 0.49), "classification" ] = "Smooth" logger.info( 'Combinations exiting calc_dem_pat: {}'.format(df.ts_id.nunique())) return df[['ts_id', 'classification']] def get_start_end_dates_df(self, df, key_col, date_col, target_col, train_max_date, end_date): start_end_date_df = ( df[df[target_col] > 0] .groupby(key_col)[date_col] .agg({'min', 'max'}) .reset_index() .rename(columns={'min': 'start_date', 'max': 'end_date'}) ) start_end_date_df.loc[ ( start_end_date_df['end_date'] > ( train_max_date - relativedelta(weeks=eol_cutoff) ) ), 'end_date' ] = end_date return start_end_date_df def get_weekly_segmentation(self, df, df_sales_daily, train_max_date, end_date): df = df[df[date_col] <= train_max_date] df1 = df[ df[date_col] > (train_max_date - relativedelta(weeks=52)) ].copy(deep=True) df_std = df_sales_daily[ df_sales_daily[date_col] > (train_max_date - relativedelta(days=90)) ].copy(deep=True) df1 = self.add_ts_id(df1) abc_df = self._calc_abc(df1) xyzw_df = self.get_xyzw_classification(df1) std_df = self.get_std(df_std) adi_df = self.calc_adi(df1) demand_pattern_df = self.calc_dem_pat(xyzw_df[['ts_id', 'cov']], adi_df) mature_cutoff_date = train_max_date - relativedelta(weeks=52) eol_cutoff_date = train_max_date - relativedelta(weeks=13) plc_df = self.get_PLC_segmentation(df, mature_cutoff_date, eol_cutoff_date) start_end_date_df = self.get_start_end_dates_df( df, key_col='ts_id', date_col=date_col, target_col=target_col, train_max_date=train_max_date, end_date=end_date ) seg_df = plc_df.merge(abc_df, on='ts_id', how='outer') seg_df = seg_df.merge(xyzw_df, on='ts_id', how='outer') seg_df = seg_df.merge(adi_df, on='ts_id', how='outer') seg_df = seg_df.merge(demand_pattern_df, on='ts_id', how='outer') seg_df = seg_df.merge(start_end_date_df, on='ts_id', how='outer') seg_df = seg_df.merge(std_df, on='ts_id', how='outer') seg_df = self.get_group_mapping(seg_df) seg_df['Mixed'] = np.where(seg_df['Mixed']=='nannan', np.nan, seg_df['Mixed']) return seg_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc_pmf/ipc_drug_fcst/engine/segmentation.py
segmentation.py
from zeno_etl_libs.db.db import DB, MySQL from zeno_etl_libs.logger import get_logger from datetime import timedelta import pandas as pd # Common utility functions for crm-campaigns listed in this class # Connections also inititated in the class, to be closed when the script is closed. class CrmCampaigns: """ # Sequence # Data prep # no_bill_in_last_n_days(data, run_date, last_n_days_param = 15) # no_call_in_last_n_days(data, run_date, last_n_days_param = 30) # patient_latest_store(data) # remove_dnd(data) # db_write() """ def __init__(self): self.logger = get_logger() self.ms_connection_read = MySQL() self.ms_connection_read.open_connection() # ALERT: read_only=False, if you want connection which writes self.ms_connection_write = MySQL(read_only=False) self.ms_connection_write.open_connection() self.rs_db = DB() self.rs_db.open_connection() ############################## # Utility functions ############################## def patient_latest_store(self, data_pass): data_base_c_grp = data_pass.copy() patients = tuple(data_base_c_grp['patient_id'].to_list()) self.logger.info("Length of patients tuple is - {}".format(len(patients))) ########################################## # Latest store-id ########################################## store_q = """ select `patient-id`, `store-id`, `created-at` from ( select `patient-id`, `store-id`, `created-at`, ROW_NUMBER() OVER (partition by `patient-id` order by `created-at` desc) as bill_rank_desc from `bills-1` b where `patient-id` in {} ) sub where bill_rank_desc = 1 """.format(patients) data_store = pd.read_sql_query(store_q, self.ms_connection_read.connection) data_store.columns = [c.replace('-', '_') for c in data_store.columns] self.logger.info("Length of data store is {}".format(len(data_store))) # Already unique, but still check data_store['created_at'] = pd.to_datetime(data_store['created_at']) data_store = data_store.sort_values(by=['patient_id', 'created_at'], ascending=[True, False]) # Keep latest store-id data_store = data_store.drop_duplicates(subset='patient_id') data_store = data_store[['patient_id', 'store_id']].copy() self.logger.info("Length of data store after dropping duplicates - is " "{}".format(len(data_store))) return data_store def no_bill_in_last_n_days(self, data_pass, run_date, last_n_days_param=15): ########################################## # No bills in last 15 days ########################################## data_base_c_grp = data_pass.copy() patients = tuple(data_base_c_grp['patient_id'].to_list()) self.logger.info("Length of patients tuple is - {}".format(len(patients))) # Take parameter input, default is 15 last_n_days_cutoff = last_n_days_param self.logger.info("Last n days cutoff is {}".format(last_n_days_cutoff)) run_date_minus_n_days = (pd.to_datetime(run_date) - timedelta(days=last_n_days_cutoff)).strftime("%Y-%m-%d") self.logger.info("Run date minus n days is {}".format(run_date_minus_n_days)) lb_q = """ SELECT `patient-id` FROM `bills-1` WHERE `created-at` >= '{0} 00:00:00' and `patient-id` in {1} GROUP BY `patient-id` """.format(run_date_minus_n_days, patients) already_billed = pd.read_sql_query(lb_q, self.ms_connection_read.connection) already_billed.columns = [c.replace('-', '_') for c in already_billed.columns] already_billed_list = already_billed['patient_id'].to_list() self.logger.info("Length of Already billed last 15 days (List)- " "fetched is {}".format(len(already_billed_list))) data_base_c_grp = data_base_c_grp.query("patient_id not in @already_billed_list") self.logger.info("Length of data base after filtering already billed - " "length is {}".format(len(data_base_c_grp))) return data_base_c_grp def no_call_in_last_n_days(self, data_pass, run_date, last_n_days_param=30): ########################################## # No calls in last 30 days period ########################################## data_base_c_grp = data_pass.copy() patients = tuple(data_base_c_grp['patient_id'].to_list()) self.logger.info("Length of patients tuple is - {}".format(len(patients))) # Take parameter input, default is 15 last_n_days_cutoff = last_n_days_param self.logger.info("Last n days cutoff is {}".format(last_n_days_cutoff)) run_date_minus_n_days = (pd.to_datetime(run_date) - timedelta(days=last_n_days_cutoff)).strftime("%Y-%m-%d") self.logger.info("Run date minus n days is {}".format(run_date_minus_n_days)) calling_q = """ SELECT `patient-id` FROM `calling-dashboard` WHERE (`list-date` >= '{0}' OR `call-date` >= '{0}') and `patient-id` in {1} GROUP BY `patient-id` """.format(run_date_minus_n_days, patients) data_c = pd.read_sql_query(calling_q, self.ms_connection_read.connection) data_c.columns = [c.replace('-', '_') for c in data_c.columns] already_p = data_c['patient_id'].drop_duplicates().to_list() self.logger.info("Length of Calling last {0} days (List)- " "fetched is {1}".format(last_n_days_cutoff, len(already_p))) data_base_c_grp = data_base_c_grp.query("patient_id not in @already_p") self.logger.info("Length of data base after filtering already called - " "length is {}".format(len(data_base_c_grp))) return data_base_c_grp def remove_dnd(self, data_pass): data_base_c_grp = data_pass.copy() read_schema = 'prod2-generico' self.rs_db.execute(f"set search_path to '{read_schema}'", params=None) # Read DND list dnd_q = """ select (case when a."patient-id" is not null then a."patient-id" else b."id" end) as "patient-id" from "dnd-list" a left join "prod2-generico"."patients" b on a."phone" = b."phone" where a."call-dnd" = 1 """ self.logger.info(dnd_q) self.rs_db.execute(dnd_q, params=None) dnd: pd.DataFrame = self.rs_db.cursor.fetch_dataframe() if dnd is None: dnd = pd.DataFrame(columns=['patient_id']) dnd.columns = [c.replace('-', '_') for c in dnd.columns] self.logger.info("dnd data length is : {}".format(len(dnd))) dnd_list = dnd['patient_id'].drop_duplicates().to_list() # Remove those already covered data_base_c_grp = data_base_c_grp.query("patient_id not in @dnd_list") self.logger.info("Net list after removing DND - length is : {}".format(len(data_base_c_grp))) return data_base_c_grp def db_write(self, data_pass, run_date_str, campaign_id_param, callback_reason_str_param, store_daily_limit_param=5, default_sort_needed=True): self.logger.info("Running for run date {}".format(run_date_str)) data_base_c_grp = data_pass.copy() # If default_sort then default sort on ABV descending, for each store if default_sort_needed: data_base_c_grp = data_base_c_grp.sort_values(by=['store_id', 'average_bill_value'], ascending=[True, False]) data_base_c_grp['priority'] = data_base_c_grp.groupby(['store_id']).cumcount() + 1 else: # assumes that sorting is already done, with priority column present pass ########################################## # Filter on Ranking ########################################## # Take parameter input, default is 5 store_daily_limit = store_daily_limit_param self.logger.info("Store level daily call limit is {}".format(store_daily_limit)) read_schema = 'prod2-generico' self.rs_db.execute(f"set search_path to '{read_schema}'", params=None) store_limit_q = f"""select "store-id" as "store_id", "store-daily-limit" from "{read_schema}"."store-calling-exceptions" sce where "campaign-id" = {campaign_id_param} and current_date between "start-date" and "end-date"; """ store_limit = self.rs_db.get_df(query=store_limit_q) data_base_c_grp = pd.merge(data_base_c_grp, store_limit, on='store_id', how='left') data_base_c_grp["store-daily-limit"] = data_base_c_grp["store-daily-limit"].fillna(store_daily_limit_param) data_base_c_grp = data_base_c_grp[data_base_c_grp['priority'] <= data_base_c_grp["store-daily-limit"]] data_base_c_grp = data_base_c_grp.drop(columns=["store-daily-limit"]) self.logger.info("Length of data base after Rank filtering - " "length is {}".format(len(data_base_c_grp))) ########################################## # WRITE to calling dashboard ########################################## data_export = data_base_c_grp[['store_id', 'patient_id', 'priority']].copy() data_export['list_date'] = run_date_str data_export['call_date'] = data_export['list_date'] data_export['campaign_id'] = campaign_id_param # integer data_export['callback_reason'] = callback_reason_str_param # string data_export.columns = [c.replace('_', '-') for c in data_export.columns] ########################################## # DANGER ZONE ########################################## self.logger.info("Insert started for length {}".format(len(data_export))) data_export.to_sql(name='calling-dashboard', con=self.ms_connection_write.engine, if_exists='append', index=False, chunksize=500, method='multi') self.logger.info("Insert done") def close_connections(self): # Closing the DB Connection self.ms_connection_read.close() self.ms_connection_write.close() self.rs_db.close_connection()
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/consumer/crm_campaigns.py
crm_campaigns.py
import pandas as pd import numpy as np import datetime as dt import time from dateutil.relativedelta import relativedelta from scipy.stats import norm from zeno_etl_libs.utils.ipc2.config_ipc import * from zeno_etl_libs.utils.ipc2.engine.data_load import LoadData from zeno_etl_libs.utils.ipc2.engine.data_pre_process import PreprocessData from zeno_etl_libs.utils.ipc2.engine.segmentation import Segmentation from zeno_etl_libs.utils.ipc2.engine.ts_fcst import TS_forecast from zeno_etl_libs.utils.ipc2.engine.forecast import Forecast from zeno_etl_libs.utils.ipc2.engine.feat_engg import Feature_Engg def ipc_forecast(store_id, reset_date, type_list, schema, db, logger): store_id_list = ("({})").format(store_id) # for sql pass last_date = dt.date(day=1, month=4, year=2019) # max history #baseline # last_date = pd.to_datetime(reset_date).date() - dt.timedelta(weeks=26) # capping sales history to 6 months # last_date = pd.to_datetime(reset_date).date() - dt.timedelta(weeks=52) # capping sales history to 12 months load_max_date = pd.to_datetime(reset_date).date() - dt.timedelta(days = pd.to_datetime(reset_date).dayofweek+1) # define empty variables in case of fail weekly_fcst = pd.DataFrame() ts_fcst = pd.DataFrame() ts_fcst_cols = [] logger.info("Data Loading Started...") data_load_obj = LoadData() ( drug_list, sales_history, cfr_pr, calendar, first_bill_date, drug_sp ) = data_load_obj.load_all_input( type_list=type_list, store_id_list=store_id_list, last_date=last_date, reset_date=reset_date, load_max_date=load_max_date, schema=schema, db=db ) logger.info("Data Pre Processing Started...") data_prep_obj = PreprocessData() ( sales, sales_pred, cal_sales, sales_daily ) = data_prep_obj.preprocess_all( sales=sales_history, drug_list=drug_list, cfr_pr=cfr_pr, calendar=calendar, first_bill_date=first_bill_date, last_date=last_date ) train_max_date = sales[date_col].max() end_date = sales_pred[date_col].max() logger.info("Segmentation Started...") seg_obj = Segmentation() sales = pd.merge(sales, drug_sp, how='left', on = ['drug_id']) sales_daily = pd.merge(sales_daily, drug_sp, how='left', on = ['drug_id']) sales_daily['actual_demand_value'] = sales_daily['actual_demand']*sales_daily['avg_sales_value'] sales['actual_demand_value'] = sales['actual_demand']*sales['avg_sales_value'] seg_df, drug_class = seg_obj.get_weekly_segmentation( df=sales.copy(deep=True), df_sales_daily=sales_daily.copy(deep=True), train_max_date=train_max_date, end_date=end_date ) seg_df['reset_date'] = str(reset_date) seg_df['PLC Status L1'].fillna('NPI',inplace=True) #correction for missed combinations in fcst merged_df1 = pd.merge(sales_pred, seg_df, how='left', on=['ts_id']) merged_df1 = merged_df1[merged_df1['PLC Status L1'].isin(['Mature', 'NPI'])] if runs_ts_flag == 1: ts_fcst_obj = TS_forecast() # df_ts_fcst = applyParallel( # merged_df1.groupby('ts_id'), # func=TS_forecast.ts_forecast( # df=merged_df1.copy(), train_max_date = train_max_date, # forecast_start = train_max_date + relativedelta(weeks=2))) ts_fcst, ts_fcst_cols = ts_fcst_obj.apply_ts_forecast( df=merged_df1.copy(), train_max_date=train_max_date, forecast_start=train_max_date + relativedelta(weeks=2)) # ========================= Forecast for 1-4 weeks ========================= if run_ml_flag == 1: start_time = time.time() forecast_start = train_max_date + relativedelta(weeks=2) merged_df1['All'] = 'All' slice_col = 'All' forecast_volume = merged_df1[merged_df1[date_col] > train_max_date][ target_col].sum() assert forecast_volume == 0 logger.info( "forecast start {} total volume: {}".format(forecast_start, forecast_volume) ) forecast_df = pd.DataFrame() validation_df = pd.DataFrame() for i in range(1, 5): num_shift_lags = i + 1 # for group_name in merged_df1[slice_col].dropna.unique(): for group_name in ['All']: logger.info('Group: {}'.format(group_name)) logger.info("Feature Engineering Started...") feat_df = pd.DataFrame() for one_df in [merged_df1]: feat_engg_obj = Feature_Engg() one_feat_df = feat_engg_obj.feat_agg( one_df[ one_df[slice_col] == group_name ].drop(slice_col, axis=1).copy(deep=True), train_max_date=train_max_date, num_shift_lag=num_shift_lags ) feat_df = pd.concat([one_feat_df, feat_df]) if pd.DataFrame(feat_df).empty: continue logger.info( "Forecasting Started for {}...".format(forecast_start)) forecast_obj = Forecast() fcst_df, val_df, Feature_Imp_all = forecast_obj.get_STM_forecast( feat_df.copy(deep=True), forecast_start=forecast_start, num_shift_lags=num_shift_lags ) forecast_df = pd.concat([forecast_df, fcst_df], axis=0) validation_df = pd.concat([validation_df, val_df]) ml_fc_cols = [i for i in forecast_df.columns if i.startswith('preds_')] forecast_df['AE'] = forecast_df[ml_fc_cols].mean(axis=1) end_time = time.time() logger.info( "total time for {} forecast: {}" .format(forecast_start, end_time - start_time) ) forecast_start = forecast_start + relativedelta(weeks=1) weekly_fcst = pd.concat([weekly_fcst, forecast_df]) weekly_fcst['reset_date'] = reset_date if runs_ts_flag == 0: weekly_fcst = weekly_fcst.copy(deep=True) if run_ml_flag == 0: weekly_fcst = ts_fcst.copy(deep=True) weekly_fcst['reset_date'] = reset_date if (run_ml_flag == 1 & runs_ts_flag == 1): weekly_fcst = pd.merge(weekly_fcst, ts_fcst[[key_col, date_col] + ts_fcst_cols], how='left', on=[key_col, date_col]) weekly_fcst.drop_duplicates(inplace=True) weekly_fcst['model'] = 'LGBM' weekly_fcst[[store_col, drug_col]] = weekly_fcst[key_col].str.split('_', expand=True) weekly_fcst.rename(columns={'preds_lgb': 'fcst'}, inplace=True) # weekly_fcst.rename(columns={'preds_xgb_rf_target':'fcst'},inplace=True) weekly_fcst = pd.merge(weekly_fcst, seg_df[['ts_id', 'std', 'Mixed']], how='left', on=['ts_id']) weekly_fcst.rename(columns={'Mixed': 'bucket'}, inplace=True) for key in percentile_bucket_dict.keys(): print(key, percentile_bucket_dict[key]) indexs = weekly_fcst[weekly_fcst.bucket == key].index weekly_fcst.loc[indexs, 'percentile'] = percentile_bucket_dict[key] weekly_fcst.loc[indexs, 'fcst'] = np.round( weekly_fcst.loc[indexs, 'fcst'] + norm.ppf(percentile_bucket_dict[key]) * weekly_fcst.loc[indexs, 'std']) weekly_fcst = weekly_fcst[ ['store_id', 'drug_id', 'model', 'date', 'fcst', 'std', 'bucket', 'percentile']] fc_cols = [i for i in weekly_fcst.columns if i.startswith('preds_')] weekly_fcst['std'].fillna(seg_df['std'].mean(), inplace=True) # agg_fcst = weekly_fcst.groupby( # ['model', 'store_id', 'drug_id', 'bucket', 'percentile']).\ # agg({'fcst': 'sum', 'std': sum_std}).reset_index() agg_fcst = weekly_fcst.groupby( ['model', 'store_id', 'drug_id', 'bucket', 'percentile']). \ agg({'fcst': 'sum', 'std': 'mean'}).reset_index() agg_fcst['store_id'] = agg_fcst['store_id'].astype(int) agg_fcst['drug_id'] = agg_fcst['drug_id'].astype(int) return agg_fcst, cal_sales, weekly_fcst, seg_df, drug_class
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/forecast_main.py
forecast_main.py
import pandas as pd import numpy as np def post_processing(safety_stock_df, weekly_fcst, seg_df, store_id, schema, db, logger): # get drug_name, type and grade seg_df[['store_id', 'drug_id']] = seg_df['ts_id'].str.split('_', expand=True) seg_df['store_id'] = seg_df['store_id'].astype(int) seg_df['drug_id'] = seg_df['drug_id'].astype(int) drug_list1 = list(safety_stock_df["drug_id"].unique()) drug_list2 = list(seg_df["drug_id"].unique()) drug_list = tuple(set(drug_list1+drug_list2)) # get drugs in both tables q_drug_info = f""" select d.id as drug_id, "drug-name" as drug_name, type, coalesce(doi."drug-grade", 'NA') as drug_grade from "{schema}".drugs d left join "{schema}"."drug-order-info" doi on d.id = doi."drug-id" where d.id in {drug_list} and doi."store-id" = {store_id} """ df_drug_info = db.get_df(q_drug_info) # get store name q_store_name = f""" select name from "{schema}".stores where id = {store_id} """ store_name = db.get_df(q_store_name)['name'][0] # get current inventory and avg_ptr info q_inv = f""" select "drug-id" as drug_id, sum("locked-quantity"+quantity+ "locked-for-audit"+"locked-for-transfer"+"locked-for-check"+ "locked-for-return") as curr_inventory from "{schema}"."inventory-1" i where "store-id" = {store_id} and "drug-id" in {drug_list} group by "drug-id" """ df_inv = db.get_df(q_inv) q_avg_ptr_store = f""" select "drug-id" as drug_id, avg(ptr) as avg_ptr from "{schema}"."inventory-1" i where "store-id" = {store_id} and "drug-id" in {drug_list} and DATEDIFF(day, date("created-at"), current_date) < 365 group by "drug-id" """ df_avg_ptr_store = db.get_df(q_avg_ptr_store) q_avg_ptr_sys = f""" select "drug-id" as drug_id, "avg-ptr" as avg_ptr_sys from "{schema}"."drug-std-info" dsi """ df_avg_ptr_sys = db.get_df(q_avg_ptr_sys) # add all to ss table safety_stock_df['store_id'] = store_id safety_stock_df['store_name'] = store_name safety_stock_df = safety_stock_df.merge( df_drug_info, on='drug_id', how='left') safety_stock_df['drug_grade'].fillna('NA', inplace=True) safety_stock_df = safety_stock_df.merge( df_inv, on='drug_id', how='left') safety_stock_df = safety_stock_df.merge( df_avg_ptr_store, on='drug_id', how='left') safety_stock_df = safety_stock_df.merge( df_avg_ptr_sys, on='drug_id', how='left') # replace NA in avg_ptr with system-avg_ptr safety_stock_df["avg_ptr"] = np.where(safety_stock_df["avg_ptr"].isna(), safety_stock_df["avg_ptr_sys"], safety_stock_df["avg_ptr"]) safety_stock_df.drop("avg_ptr_sys", axis=1, inplace=True) safety_stock_df["avg_ptr"] = safety_stock_df["avg_ptr"].astype(float) # calculate DOH safety_stock_df['safety_stock_days'] = np.where( (safety_stock_df['fcst'] == 0) | (safety_stock_df['safety_stock'] == 0), 0, safety_stock_df['safety_stock'] / (safety_stock_df['fcst'] / 28)) safety_stock_df['reorder_days'] = np.where( (safety_stock_df['fcst'] == 0) | (safety_stock_df['reorder_point'] == 0), 0, safety_stock_df['reorder_point'] / (safety_stock_df['fcst'] / 28)) safety_stock_df['order_upto_days'] = np.where( (safety_stock_df['fcst'] == 0) | (safety_stock_df['order_upto_point'] == 0), 0, safety_stock_df['order_upto_point'] / (safety_stock_df['fcst'] / 28)) # calculate max-value, to-order-qty and to-order-val safety_stock_df["max_value"] = safety_stock_df['order_upto_point'] * \ safety_stock_df['avg_ptr'] safety_stock_df['to_order_quantity'] = np.where( safety_stock_df['curr_inventory'] <= safety_stock_df['reorder_point'], safety_stock_df['order_upto_point'] - safety_stock_df['curr_inventory'], 0) safety_stock_df['to_order_value'] = safety_stock_df['to_order_quantity'] * \ safety_stock_df['avg_ptr'] # formatting weekly_fcst table weekly_fcst['store_name'] = store_name weekly_fcst.rename( columns={'date': 'week_begin_dt', 'fcst': 'weekly_fcst', 'std': 'fcst_std'}, inplace=True) # formatting segmentation table seg_df.rename(columns={'std': 'sales_std', 'cov': 'sales_cov', 'Mixed': 'bucket', 'Group': 'group', 'PLC Status L1': 'plc_status', 'ADI': 'adi', 'total_LY_sales': 'total_ly_sales', 'start_date': 'sale_start_date'}, inplace=True) seg_df['plc_status'] = np.where(seg_df['plc_status'] == 'NPI', 'New Product', seg_df['plc_status']) seg_df['sale_start_date'] = seg_df['sale_start_date'].dt.date seg_df['store_name'] = store_name seg_df = seg_df.merge(df_drug_info, on='drug_id', how='left') seg_df['drug_grade'].fillna('NA', inplace=True) # get oder_value_summary for email attachment order_value = safety_stock_df.pivot_table( index=['store_id', 'store_name', 'type'], values=['to_order_quantity', 'to_order_value'], aggfunc='sum', margins=True, margins_name='Total').reset_index() return safety_stock_df, order_value, weekly_fcst, seg_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/post_processing.py
post_processing.py
import pandas as pd import numpy as np def wh_consolidation(safety_stock_df, db, schema, logger): """ replace for drugs which has a substitute available in WH (corrected logic) """ # getting list of SKUs to be rejected and substituted wh_subs_query = f""" select "drug-id" , "drug-id-replaced" , "same-release" from "{schema}"."wh-sku-subs-master" wssm left join "{schema}".drugs d on wssm."drug-id" = d.id where "add-wh" = 'No' and d."type" not in ('ethical') """ df_wh_subs = db.get_df(wh_subs_query) df_wh_subs.columns = [c.replace('-', '_') for c in df_wh_subs.columns] all_assort_drugs = list(safety_stock_df.loc[ safety_stock_df["order_upto_point"] > 0]["drug_id"].unique()) # reject cases reject_cases_1 = df_wh_subs.loc[ df_wh_subs["drug_id"] == df_wh_subs["drug_id_replaced"]] reject_drugs_lst_1 = list(reject_cases_1["drug_id"].unique()) reject_cases_2 = df_wh_subs.loc[ (df_wh_subs["drug_id"] != df_wh_subs["drug_id_replaced"]) & (df_wh_subs["same_release"] == 'NO')] reject_drugs_lst_2 = list(reject_cases_2["drug_id"].unique()) # replace cases replace_cases = df_wh_subs.loc[ (df_wh_subs["drug_id"] != df_wh_subs["drug_id_replaced"]) & (df_wh_subs["same_release"] == 'YES')] reject_drugs_lst_3 = list(replace_cases["drug_id"].unique()) replace_merge_df = safety_stock_df.merge( replace_cases, on="drug_id", how="inner").drop("same_release", axis=1)[ ["drug_id", "drug_id_replaced", "safety_stock", "reorder_point", "order_upto_point"]] # get preferred entry in case of multiple drug_id with same drug_id_replaced # choosing the case with highest OUP replace_merge_df = replace_merge_df.sort_values( by=['drug_id_replaced', 'order_upto_point'], ascending=False) preferred_drug_replace_map = replace_merge_df.groupby( "drug_id_replaced").agg({"drug_id": "first"}) # first will have highest OUP preferred_drug_replace_df = replace_merge_df.merge( preferred_drug_replace_map, on=["drug_id", "drug_id_replaced"], how="inner") substitute_drugs_add_df = preferred_drug_replace_df.copy() substitute_drugs_add_df = substitute_drugs_add_df.drop("drug_id", axis=1) substitute_drugs_add_df.rename(columns={"drug_id_replaced": "drug_id"}, inplace=True) # only need to add the substitute if below condition satisfy substitute_drugs_add_df = substitute_drugs_add_df.loc[ (substitute_drugs_add_df["order_upto_point"] > 0) & (~substitute_drugs_add_df["drug_id"].isin(all_assort_drugs))] # remove previous entry with 0 OUP for substitute drug if present (avoids duplicate) substitute_drugs = list(substitute_drugs_add_df["drug_id"].unique()) safety_stock_df = safety_stock_df.loc[~( (safety_stock_df["order_upto_point"] == 0) & (safety_stock_df["drug_id"].isin(substitute_drugs)))] # filling the relevant columns substitute_drugs_add_df['model'] = 'NA' substitute_drugs_add_df['bucket'] = 'NA' substitute_drugs_add_df['fcst'] = 0 substitute_drugs_add_df['std'] = 0 substitute_drugs_add_df['lead_time_mean'] = 0 substitute_drugs_add_df['lead_time_std'] = 0 reject_drugs_lst = list(set(reject_drugs_lst_1 + reject_drugs_lst_2 + reject_drugs_lst_3)) logger.info(f"Drugs to reject: {len(reject_drugs_lst)}") logger.info(f"Drugs to add as substitute: {substitute_drugs_add_df.shape[0]}") ss_zero_cases = safety_stock_df.loc[safety_stock_df["drug_id"].isin(reject_drugs_lst)] ss_rest_cases = safety_stock_df.loc[~safety_stock_df["drug_id"].isin(reject_drugs_lst)] ss_zero_cases["safety_stock"] = 0 ss_zero_cases["reorder_point"] = 0 ss_zero_cases["order_upto_point"] = 0 safety_stock_df_final = pd.concat( [ss_rest_cases, ss_zero_cases, substitute_drugs_add_df]) return safety_stock_df_final def goodaid_consolidation(safety_stock_df, db, schema, logger, substition_type=None): """ for goodaid compositions, only keep goodaid and those drugs in same composition which are part of WH portfolio. reject all other drugs in that composition """ if substition_type is None: substition_type = ['generic'] # Good Aid SKU list ga_sku_query = """ select wh."drug-id" , d.composition from "{schema}"."wh-sku-subs-master" wh left join "{schema}".drugs d on d.id = wh."drug-id" where wh."add-wh" = 'Yes' and d."company-id" = 6984 and d.type in {0} """.format( str(substition_type).replace('[', '(').replace(']', ')'), schema=schema) ga_sku = db.get_df(ga_sku_query) ga_sku.columns = [c.replace('-', '_') for c in ga_sku.columns] logger.info('GoodAid SKU list ' + str(ga_sku.shape[0])) # Generic Top SKU ga_active_composition = tuple(ga_sku['composition'].values) top_sku_query = """ select wh."drug-id" , d.composition from "{schema}"."wh-sku-subs-master" wh left join "{schema}".drugs d on d.id = wh."drug-id" where wh."add-wh" = 'Yes' and d."company-id" != 6984 and d.type in {0} and d.composition in {1} """.format( str(substition_type).replace('[', '(').replace(']', ')'), str(ga_active_composition), schema=schema) top_sku = db.get_df(top_sku_query) top_sku.columns = [c.replace('-', '_') for c in top_sku.columns] logger.info('GoodAid comp Top SKU list ' + str(top_sku.shape[0])) # SS substition for other drugs rest_sku_query = """ select id as drug_id, composition from "{schema}".drugs where composition in {0} and id not in {1} and "company-id" != 6984 and type in {2} """.format(str(ga_active_composition), str(tuple(top_sku['drug_id'].values)), str(substition_type).replace('[', '(').replace(']', ')'), schema=schema) rest_sku = db.get_df(rest_sku_query) logger.info('GoodAid comp rest SKU list ' + str(rest_sku.shape[0])) # substitution logic starts gaid_drug_list = list(ga_sku["drug_id"].unique()) top_drug_list = list(top_sku["drug_id"].unique()) reject_drug_list = list(rest_sku["drug_id"].unique()) ss_zero_cases = safety_stock_df.loc[safety_stock_df["drug_id"].isin(reject_drug_list)] ss_rest_cases = safety_stock_df.loc[~safety_stock_df["drug_id"].isin(reject_drug_list)] logger.info('Setting rest sku SS, ROP, OUP to zero') ss_zero_cases["safety_stock"] = 0 ss_zero_cases["reorder_point"] = 0 ss_zero_cases["order_upto_point"] = 0 safety_stock_df_final = pd.concat([ss_rest_cases, ss_zero_cases]) return safety_stock_df_final def D_class_consolidation(safety_stock_df, store_id, db, schema, logger): """ for D class drugs, discard drugs from assortment for which same composition is present in A,B or C class """ drugs_list = tuple(safety_stock_df["drug_id"].unique()) comp = f""" select "drug-id" as drug_id, "group" from "{schema}"."drug-unique-composition-mapping" ducm where "drug-id" in {drugs_list} """ df_comp = db.get_df(comp) df = pd.merge(safety_stock_df, df_comp, how='left', on=['drug_id']) df['store_comp'] = str(store_id) + "_" + df['group'].astype(str) df_blank_comp = df[df['group'] == ""] df = df[df['group'] != ""].reset_index(drop=True) df['ABC Class'] = np.where(df['bucket'].isin(['AW', 'AX', 'AY', 'AZ']), 'A', np.nan) df['ABC Class'] = np.where(df['bucket'].isin(['BW', 'BX', 'BY', 'BZ']), 'B', df['ABC Class']) df['ABC Class'] = np.where(df['bucket'].isin(['CW', 'CX', 'CY', 'CZ']), 'C', df['ABC Class']) df['ABC Class'] = np.where(df['bucket'].isin(['DW', 'DX', 'DY', 'DZ']), 'D', df['ABC Class']) df['ABC Class'].fillna('None', inplace=True) list_ABC = df[(df['ABC Class'].isin(['A', 'B', 'C'])) & (df['order_upto_point'] > 0)][ 'store_comp'].unique().tolist() list_D = df[(df['ABC Class'].isin(['D'])) & (df['order_upto_point'] > 0)][ 'store_comp'].unique().tolist() common_comp_D = [value for value in list_D if value in list_ABC] D_exc_comp = [value for value in list_D if value not in list_ABC] df_D = df[df['ABC Class'] == 'D'] df_D_new = df_D.copy() df_D_new['order_upto_point'] = np.where( df_D_new['store_comp'].isin(common_comp_D), 0, df_D_new['order_upto_point']) df_D_new['reorder_point'] = np.where( df_D_new['store_comp'].isin(common_comp_D), 0, df_D_new['reorder_point']) df_D_new['safety_stock'] = np.where( df_D_new['store_comp'].isin(common_comp_D), 0, df_D_new['safety_stock']) df_remove_D = df[df['ABC Class'] != 'D'] df_add_new_D = pd.concat([df_remove_D, df_D_new]) df_add_blank_comp = pd.concat([df_add_new_D, df_blank_comp]) safety_stock_df_final = df_add_blank_comp.drop( columns=['store_comp', 'group', 'ABC Class']) return safety_stock_df_final
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/portfolio_consolidation.py
portfolio_consolidation.py
import numpy as np from zeno_etl_libs.utils.ipc.lead_time import lead_time from zeno_etl_libs.utils.ipc2.helpers.correction_flag import compare_df, \ add_correction_flag from zeno_etl_libs.utils.ipc2.heuristics.sl_heuristics import calculate_ss from zeno_etl_libs.utils.ipc2.heuristics.doh_heuristics import ss_doh_wh_cap, \ ss_doh_non_wh_cap from zeno_etl_libs.utils.ipc2.heuristics.ipcv3_heuristics import v3_corrections from zeno_etl_libs.utils.ipc2.heuristics.ipcv4_heuristics import v4_corrections from zeno_etl_libs.utils.ipc2.heuristics.ipcv5_heuristics import v5_corrections from zeno_etl_libs.utils.ipc2.heuristics.ipcv3N_heuristics import v3N_corrections def safety_stock_calc(agg_fcst, cal_sales, store_id, reset_date, v3_active_flag, corrections_selling_probability_cutoff, corrections_cumulative_probability_cutoff, v4_active_flag, drug_type_list_v4, v5_active_flag, open_po_turbhe_active, schema, db, logger): fcst_weeks = 4 review_time = 4 # order_freq = 4 # ================== TEMPORARY FOR OPEN-PO TURBHE STORES =================== if open_po_turbhe_active == 'Y': q_turbhe_stores = f""" select distinct "store-id" from "{schema}"."store-dc-mapping" sdm left join "{schema}".stores s on s.id =sdm."store-id" where "forward-dc-id" = 169 and s."franchisee-id" = 1 and name <> 'Zippin Central' and "is-active" = 1 and "opened-at" != '0101-01-01 00:00:00' """ df_turbhe_stores = db.get_df(q_turbhe_stores) turbhe_stores = df_turbhe_stores["store-id"].tolist() # add store_id 2 & 264 (mulund east and mulund west sarvodaya) turbhe_stores += [2, 264] if store_id in turbhe_stores: review_time = 2 # order_freq = 3 # ========================= LEAD TIME CALCULATIONS ========================= lt_drug, lt_store_mean, lt_store_std = lead_time( store_id, cal_sales, reset_date, db, schema, logger) safety_stock_df = agg_fcst.merge( lt_drug[['drug_id', 'lead_time_mean', 'lead_time_std']], how='left', on='drug_id') safety_stock_df['lead_time_mean'].fillna(lt_store_mean, inplace=True) safety_stock_df['lead_time_std'].fillna(lt_store_std, inplace=True) # ==================== SS, ROP, OUP CALCULATION BEGINS ===================== # # impute store_std for cases where store-drug std<1 # safety_stock_df['lead_time_std'] = np.where( # safety_stock_df['lead_time_std'] < 1, # lt_store_std, safety_stock_df['lead_time_std']) # calculate SS safety_stock_df = calculate_ss(safety_stock_df, fcst_weeks, logger) # SS-DOH CAPPING #1 logger.info(f"DOH1 (SS-WH-DOH) Correction starts") df_pre_corr = safety_stock_df.copy() safety_stock_df = ss_doh_wh_cap(safety_stock_df, schema, db) df_post_corr = safety_stock_df.copy() logger.info(f"Sum SS before: {df_pre_corr['safety_stock'].sum()}") logger.info(f"Sum SS after: {df_post_corr['safety_stock'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['safety_stock']) safety_stock_df = add_correction_flag(safety_stock_df, corr_drug_lst, 'DOH1') # SS-DOH CAPPING #2 logger.info(f"DOH2 (SS-Non-WH-DOH) Correction starts") df_pre_corr = safety_stock_df.copy() safety_stock_df = ss_doh_non_wh_cap(safety_stock_df, schema, db) df_post_corr = safety_stock_df.copy() logger.info(f"Sum SS before: {df_pre_corr['safety_stock'].sum()}") logger.info(f"Sum SS after: {df_post_corr['safety_stock'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger, cols_to_compare=['safety_stock']) safety_stock_df = add_correction_flag(safety_stock_df, corr_drug_lst, 'DOH2') # new ROP calculation - add LT + RT demand to SS safety_stock_df['reorder_point'] = np.round( (((safety_stock_df['fcst'] / 28) * (safety_stock_df['lead_time_mean'] + review_time)) + safety_stock_df['safety_stock']) ) # calculate ROP - add lead time demand to SS # safety_stock_df['reorder_point'] = safety_stock_df.apply( # lambda row: np.round( # row['lead_time_mean'] * row['fcst'] / fcst_weeks / 7), # axis=1) + safety_stock_df['safety_stock'] # new OUP calculation - add 2days demand on ROP safety_stock_df['order_upto_point'] = np.round( (((safety_stock_df['fcst'] / 28) * 2) + safety_stock_df['reorder_point']) ) # calculate OUP - add order_freq demand to ROP # safety_stock_df['order_upto_point'] = ( # safety_stock_df['reorder_point'] + # np.round( # np.where( # # if rounding off give 0, increase it to 4-week forecast # (safety_stock_df['reorder_point'] + # safety_stock_df[ # 'fcst'] * order_freq / fcst_weeks / 7 < 0.5) & # (safety_stock_df['fcst'] > 0), # safety_stock_df['fcst'], # safety_stock_df['fcst'] * order_freq / fcst_weeks / 7)) # ) # correction for negative forecast safety_stock_df['safety_stock'] = np.where( safety_stock_df['safety_stock'] < 0, 0, safety_stock_df['safety_stock']) safety_stock_df['reorder_point'] = np.where( safety_stock_df['reorder_point'] < 0, 0, safety_stock_df['reorder_point']) safety_stock_df['order_upto_point'] = np.where( safety_stock_df['order_upto_point'] < 0, 0, safety_stock_df['order_upto_point']) # ========== CORRECTION PLUGINS (REWORK SS,ROP,OUP BASED ON REQ) =========== final_ss_df = safety_stock_df.copy() # if v3_active_flag == 'Y': # logger.info("IPC V3 Correction starts") # df_pre_corr = final_ss_df.copy() # final_ss_df = v3_corrections(final_ss_df, store_id, # corrections_selling_probability_cutoff, # corrections_cumulative_probability_cutoff, # schema, db, logger) # df_post_corr = final_ss_df.copy() # logger.info(f"Sum OUP before: {df_pre_corr['order_upto_point'].sum()}") # logger.info(f"Sum OUP after: {df_post_corr['order_upto_point'].sum()}") # corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) # final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'V3') if v3_active_flag == 'Y': logger.info("IPC V3N Correction starts") df_pre_corr = final_ss_df.copy() final_ss_df = v3N_corrections(final_ss_df, store_id, reset_date, schema, db, logger) df_post_corr = final_ss_df.copy() logger.info(f"Sum OUP before: {df_pre_corr['order_upto_point'].sum()}") logger.info(f"Sum OUP after: {df_post_corr['order_upto_point'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'V3N') if v4_active_flag == 'Y': logger.info("IPC V4 Correction starts") df_pre_corr = final_ss_df.copy() final_ss_df = v4_corrections(final_ss_df, drug_type_list_v4, db, schema) df_post_corr = final_ss_df.copy() logger.info(f"Sum OUP before: {df_pre_corr['order_upto_point'].sum()}") logger.info(f"Sum OUP after: {df_post_corr['order_upto_point'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'V4') # currently v5 only active for pilot stores if (v5_active_flag == 'Y') & (store_id in [51, 134, 83]): logger.info("IPC V5 STD-Qty Correction starts") df_pre_corr = final_ss_df.copy() final_ss_df = v5_corrections(final_ss_df, db, schema, logger) df_post_corr = final_ss_df.copy() logger.info(f"Sum OUP before: {df_pre_corr['order_upto_point'].sum()}") logger.info(f"Sum OUP after: {df_post_corr['order_upto_point'].sum()}") corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'V5') # correct cases where ROP=OUP df_pre_corr = final_ss_df.copy() final_ss_df['order_upto_point'] = np.where( ((final_ss_df['order_upto_point'] > 0) & (final_ss_df['reorder_point'] == final_ss_df['order_upto_point'])), final_ss_df['reorder_point'] + 1, final_ss_df['order_upto_point']) df_post_corr = final_ss_df.copy() corr_drug_lst = compare_df(df_pre_corr, df_post_corr, logger) final_ss_df = add_correction_flag(final_ss_df, corr_drug_lst, 'OUP_CORR') return final_ss_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/safety_stock.py
safety_stock.py
import numpy as np import pandas as pd import datetime as dt def check_oup_outlier(safety_stock_df, store_id, reset_date, db, schema): """ Find cases where OUP > Past 90 days net-sales + pr_loss """ safety_stock_df.columns = [c.replace('-', '_') for c in safety_stock_df.columns] # check only for cases where fcst & oup > 0 df_check_cases = safety_stock_df.loc[ (safety_stock_df["fcst"] > 0) & (safety_stock_df["order_upto_point"] > 0)] df_check_cases = df_check_cases[[ "store_id", "store_name", "drug_id", "drug_name", "type", "model", "fcst", "safety_stock", "reorder_point", "order_upto_point", "correction_flags", "curr_inventory", "to_order_quantity", "to_order_value"]] drug_list = list(df_check_cases["drug_id"].unique()) drug_list_str = str(drug_list).replace('[', '(').replace(']', ')') p90d_begin_date = dt.datetime.strptime(reset_date, '%Y-%m-%d').date() - \ dt.timedelta(days=90) p90d_begin_date = p90d_begin_date.strftime("%Y-%m-%d") q_p90d_sales = f""" select "drug-id" as drug_id, sum("net-quantity") as net_sales_p90d from "{schema}".sales s where "store-id" = {store_id} and "drug-id" in {drug_list_str} and date("created-at") >= '{p90d_begin_date}' and date("created-at") < '{reset_date}' group by "drug-id" """ df_p90d_sales = db.get_df(q_p90d_sales) q_p90d_pr_loss = f""" select "drug-id" as drug_id, sum("loss-quantity") as pr_loss_p90d from "{schema}"."cfr-patient-request" where "store-id" = {store_id} and "shortbook-date" >= '{p90d_begin_date}' and "shortbook-date" < '{reset_date}' and "drug-id" in {drug_list_str} and ("drug-category" = 'chronic' or "repeatability-index" >= 40) and "loss-quantity" > 0 group by "drug-id" """ df_p90d_pr_loss = db.get_df(q_p90d_pr_loss) df_check_cases = df_check_cases.merge(df_p90d_sales, on="drug_id", how="left") df_check_cases = df_check_cases.merge(df_p90d_pr_loss, on="drug_id", how="left") df_check_cases["net_sales_p90d"] = df_check_cases["net_sales_p90d"].fillna(0).astype(int) df_check_cases["pr_loss_p90d"] = df_check_cases["pr_loss_p90d"].fillna(0).astype(int) df_check_cases["p90d_demand"] = df_check_cases["net_sales_p90d"] + df_check_cases["pr_loss_p90d"] df_check_cases["outlier"] = 'NA' df_check_cases["outlier"] = np.where( df_check_cases["order_upto_point"] > df_check_cases["p90d_demand"], 'Y', 'N') df_outliers = df_check_cases.loc[df_check_cases["outlier"] == 'Y'] outlier_drugs = list(df_outliers["drug_id"].unique()) manual_doid_upd_df = df_outliers[["store_id", "drug_id", "safety_stock", "reorder_point", "order_upto_point"]] manual_doid_upd_df.rename(columns={"safety_stock": "ss", "reorder_point": "rop", "order_upto_point": "oup"}, inplace=True) return outlier_drugs, df_outliers, manual_doid_upd_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/helpers/outlier_check.py
outlier_check.py
import numpy as np np.random.seed(0) import pandas as pd # import time # import re # from datetime import date # from dateutil.relativedelta import relativedelta # from statsmodels.tsa.exponential_smoothing.ets import ETSModel from prophet import Prophet from statsmodels.tsa.api import ExponentialSmoothing # import sktime from sktime.forecasting.ets import AutoETS from zeno_etl_libs.utils.ipc2.helpers.helper_functions import sum_std,\ applyParallel, applyParallel_croston # from boruta import BorutaPy from zeno_etl_libs.utils.ipc2.config_ipc import ( date_col, target_col, models ) import logging logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') class TS_forecast: def train_test_split(self, df, train_max_date, forecast_start): df.rename(columns={date_col: 'ds', target_col: 'y'}, inplace=True) df.sort_values(by=['ds'], inplace=True) train = df[df['ds'] <= train_max_date] test = df[df['ds'] >= forecast_start] return train, test def Croston_TSB(self, ts, extra_periods=4, alpha=0.4, beta=0.4): d = np.array(ts) # Transform the input into a numpy array cols = len(d) # Historical period length d = np.append(d, [ np.nan] * extra_periods) # Append np.nan into the demand array to cover future periods # level (a), probability(p) and forecast (f) a, p, f = np.full((3, cols + extra_periods), np.nan) # Initialization first_occurence = np.argmax(d[:cols] > 0) a[0] = d[first_occurence] p[0] = 1 / (1 + first_occurence) f[0] = p[0] * a[0] # Create all the t+1 forecasts for t in range(0, cols): if d[t] > 0: a[t + 1] = alpha * d[t] + (1 - alpha) * a[t] p[t + 1] = beta * (1) + (1 - beta) * p[t] else: a[t + 1] = a[t] p[t + 1] = (1 - beta) * p[t] f[t + 1] = p[t + 1] * a[t + 1] # Future Forecast a[cols + 1:cols + extra_periods] = a[cols] p[cols + 1:cols + extra_periods] = p[cols] f[cols + 1:cols + extra_periods] = f[cols] df = pd.DataFrame.from_dict( {"Demand": d, "Forecast": f, "Period": p, "Level": a, "Error": d - f}) return df[-extra_periods:] def ETS_forecast(self, train, test): try: train.set_index(['ds'], inplace=True) test.set_index(['ds'], inplace=True) train.index.freq = train.index.inferred_freq test.index.freq = test.index.inferred_freq # fit in statsmodels # model = AutoETS(sp=52,auto=True,allow_multiplicative_trend = False, additive_only=True) # fit = model.fit(train['y']) try: # fit = ETSModel(np.asarray(train['y']) ,seasonal_periods=52 ,trend='add', seasonal='add').fit() fit = AutoETS(auto=True).fit(train['y']) preds = fit.predict(test.index) except Exception as e: logger.info("error in Auto-ETS") logger.info(str(e)) fit = ExponentialSmoothing(train['y']).fit() preds = fit.forecast(len(test) + 1) preds = preds[-len(test):] except Exception as e: logger.info("error in ETS fcst") logger.info(str(e)) preds = 0 return preds def ma_forecast(self, data): """ Purpose: Compute MA forecast for the for the forecast horizon specified Inputs: time series to create forecast Output: series with forecasted values """ sma_df = data.copy(deep=True) yhat = [] if len(data) >= 8: for i in range(5): sma_val = sma_df.rolling(8).mean().iloc[-1] sma_df.loc[sma_df.index.max() + 1] = sma_val yhat.append(sma_val) else: for i in range(5): sma_val = sma_df.rolling(len(data)).mean().iloc[-1] sma_df.loc[sma_df.index.max() + 1] = sma_val yhat.append(sma_val) logger.info(yhat) return yhat[-4:] def prophet_fcst(self, train, test, params=None): # reg_list = [] try: if params is None: pro = Prophet() else: pro = Prophet(n_changepoints=params) # for j in train.columns: # if j not in col_list: # pro.add_regressor(j) # reg_list.append(j) pro.fit(train[['ds', 'y']]) pred_f = pro.predict(test) test = test[["ds", "y"]] test = pd.merge(test, pred_f, on="ds", how="left") except Exception as e: logger.info("error in prophet fcst") logger.info(str(e)) test['yhat'] = 0 return test def ts_forecast(self, df, train_max_date, forecast_start): train, test = self.train_test_split(df, train_max_date=train_max_date, forecast_start=forecast_start) test = test.sort_values(by=['ds']) if 'croston' in models: preds_croston = self.Croston_TSB(train['y']) test['preds_croston'] = preds_croston['Forecast'].values if 'ETS' in models: preds_ETS = self.ETS_forecast(train.copy(), test.copy()) try: test['preds_ETS'] = preds_ETS.values except: test['preds_ETS'] = 0 if 'MA' in models: preds_ma = self.ma_forecast(train['y']) test['preds_ma'] = preds_ma if 'prophet' in models: preds_prophet = self.prophet_fcst(train.copy(), test.copy()) test['preds_prophet'] = preds_prophet['yhat'].values return test def apply_ts_forecast(self, df, train_max_date, forecast_start): # global train_date # train_date = train_max_date # global forecast_start_date # forecast_start_date = forecast_start preds = applyParallel_croston( df.groupby('ts_id'), func=self.ts_forecast, train_max_date=train_max_date, forecast_start=forecast_start ) preds.rename(columns={'ds': date_col, 'y': target_col}, inplace=True) ts_fcst_cols = [i for i in preds.columns if i.startswith('preds_')] for col in ts_fcst_cols: preds[col].fillna(0, inplace=True) preds[col] = np.where(preds[col] < 0, 0, preds[col]) preds['preds_AE_ts'] = preds[ts_fcst_cols].mean(axis=1) return preds, ts_fcst_cols
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/engine/ts_fcst.py
ts_fcst.py
import pandas as pd import datetime import numpy as np from zeno_etl_libs.utils.ipc2.config_ipc import date_col, store_col, \ drug_col, target_col, key_col, local_testing class PreprocessData: def add_ts_id(self, df): df = df[~df[drug_col].isnull()].reset_index(drop=True) df['ts_id'] = ( df[store_col].astype(int).astype(str) + '_' + df[drug_col].astype(int).astype(str) ) return df def preprocess_sales(self, df, drug_list): df.rename(columns={ 'net_sales_quantity': target_col }, inplace=True) df.rename(columns={ 'sales_date': date_col }, inplace=True) set_dtypes = { store_col: int, drug_col: int, date_col: str, target_col: float } df = df.astype(set_dtypes) df[target_col] = df[target_col].round() df[date_col] = pd.to_datetime(df[date_col]) df = df.groupby( [store_col, drug_col, key_col, date_col] )[target_col].sum().reset_index() df = df[df[drug_col].isin(drug_list[drug_col].unique().tolist())] return df def get_formatted_data(self, df): df_start = df.groupby([key_col])[date_col].min().reset_index().rename( columns={date_col: 'sales_start'}) df = df[[key_col, date_col, target_col]] min_date = df[date_col].dropna().min() end_date = df[date_col].dropna().max() date_range = [] date_range = pd.date_range( start=min_date, end=end_date, freq='d' ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = np.nan df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = pd.merge(df, df_start, how='left', on=key_col) df = df[df[date_col] >= df['sales_start']] df[[store_col, drug_col]] = df[key_col].str.split('_', expand=True) df[[store_col, drug_col]] = df[[store_col, drug_col]].astype(int) return df def preprocess_cfr_pr(self, df): set_dtypes = { store_col: int, drug_col: int, 'loss_quantity': int } df = df.astype(set_dtypes) df['shortbook_date'] = pd.to_datetime(df['shortbook_date']) return df def merge_cfr_pr(self, sales, cfr_pr): df = sales.merge(cfr_pr, left_on=[store_col, drug_col, date_col], right_on=[store_col, drug_col, 'shortbook_date'], how='left') df[date_col] = df[date_col].combine_first(df['shortbook_date']) df[target_col].fillna(0, inplace=True) df['loss_quantity'].fillna(0, inplace=True) df[target_col] += df['loss_quantity'] df.drop(['shortbook_date', 'loss_quantity'], axis=1, inplace=True) return df def preprocess_calendar(self, df, last_date): df.rename(columns={'date': date_col}, inplace=True) df[date_col] = pd.to_datetime(df[date_col]) cal_sales = df.copy() cal_sales['week_begin_dt'] = cal_sales.apply( lambda x: x[date_col] - datetime.timedelta(x['day_of_week']), axis=1) cal_sales['month_begin_dt'] = cal_sales.apply( lambda x: x['date'] - datetime.timedelta(x['date'].day - 1), axis=1) cal_sales['key'] = 1 ld = pd.to_datetime(last_date) cal_sales = cal_sales[cal_sales[date_col] > ld] return df, cal_sales def merge_calendar(self, sales, calendar): df = sales.merge(calendar, how='left', on=date_col ) # df_week_days_count = df.groupby([key_col, 'year', 'week_of_year'])[date_col].count().reset_index().rename(columns = {date_col:'week_days_count'}) # df['week_days_count'] = 1 df['week_begin_dt'] = df.apply( lambda x: x[date_col] - datetime.timedelta(x['day_of_week']), axis=1) df_week_days_count = df.groupby(['ts_id', 'week_begin_dt'])[ date_col].count().reset_index().rename( columns={date_col: 'week_days_count'}) # df = df.groupby(['ts_id', store_col, drug_col, ]).resample('W-Mon', on =date_col )[target_col].sum().reset_index() df = df.groupby(['ts_id', store_col, drug_col, 'week_begin_dt'])[ target_col].sum().reset_index() df = pd.merge(df, df_week_days_count, how='left', on=[key_col, 'week_begin_dt']) df = df[df['week_days_count'] == 7].reset_index(drop=True) df.drop(columns=['week_days_count'], inplace=True) df.rename(columns={'week_begin_dt': date_col}, inplace=True) return df def preprocess_bill_date(self, df): df.rename(columns={'store-id': store_col}, inplace=True) df['bill_date'] = pd.to_datetime(df['bill_date']) return df def merge_first_bill_date(self, sales, first_bill_date): df = pd.merge(sales, first_bill_date, on=[store_col]) df = df[df[date_col] >= df['bill_date']].reset_index(drop=True) df.drop(columns=['bill_date'], inplace=True) return df def make_future_df(self, df): start_date_df = ( df .groupby(key_col)[date_col] .min() .reset_index() .rename(columns={date_col: 'start_date'}) ) df = df[[key_col, date_col, target_col]] end_date = df[date_col].max() + datetime.timedelta(weeks=5) min_date = df[date_col].min() date_range = pd.date_range( start=min_date, end=end_date, freq="W-MON" ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = 0 df = ( df .fillna(0) .stack() .reset_index() .rename(columns={0: target_col}) ) df = df.merge(start_date_df, on=key_col, how='left') df = df[ df[date_col] >= df['start_date'] ] df.drop('start_date', axis=1, inplace=True) df[[store_col, drug_col]] = df[key_col].str.split('_', expand=True) return df def preprocess_all( self, sales=None, cfr_pr=None, drug_list=None, calendar=None, first_bill_date=None, last_date=None, ): sales = self.add_ts_id(sales) # filter ################################################# if local_testing == 1: tsid_list = \ sales.sort_values(by=['net_sales_quantity'], ascending=False)[ key_col].unique().tolist()[:100] sales = sales[sales[key_col].isin(tsid_list)] ################################################# sales = self.preprocess_sales(sales, drug_list) sales = self.get_formatted_data(sales) cfr_pr = self.preprocess_cfr_pr(cfr_pr) sales_daily = self.merge_cfr_pr(sales, cfr_pr) calendar, cal_sales = self.preprocess_calendar(calendar, last_date) sales = self.merge_calendar(sales_daily, calendar) first_bill_date = self.preprocess_bill_date(first_bill_date) sales = self.merge_first_bill_date(sales, first_bill_date) sales_pred = self.make_future_df(sales.copy()) return ( sales, sales_pred, cal_sales, sales_daily )
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/engine/data_pre_process.py
data_pre_process.py
import numpy as np np.random.seed(0) import pandas as pd import time import re from dateutil.relativedelta import relativedelta from category_encoders.target_encoder import TargetEncoder from category_encoders.leave_one_out import LeaveOneOutEncoder from sklearn.ensemble import RandomForestRegressor from sklearn.ensemble import GradientBoostingRegressor from category_encoders.cat_boost import CatBoostEncoder from xgboost import XGBRegressor from xgboost import XGBRFRegressor from lightgbm import LGBMRegressor from catboost import CatBoostRegressor # from boruta import BorutaPy from zeno_etl_libs.utils.ipc2.config_ipc import ( date_col, target_col, flag_sample_weights, flag_seasonality_index, models ) import logging logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') class Forecast: def one_hot_encode(self, df, one_hot_cols): added_one_hot_cols = [] for col in one_hot_cols: one_hot_df = pd.get_dummies(df[col], prefix=col, drop_first=False) df = pd.concat([df.drop(col, axis=1), one_hot_df], axis=1) added_one_hot_cols += one_hot_df.columns.to_list() return df, added_one_hot_cols def encode(self, X_train, y_train, cat_cols, encoder_type='Target'): if len(cat_cols) == 0: return X_train, None cat_cols = list(set(cat_cols) & set(X_train.columns)) logger.debug('Categorical Encoding Started...') if encoder_type == 'Target': encoder = TargetEncoder(cols=cat_cols) elif encoder_type == 'Catboost': encoder = CatBoostEncoder(cols=cat_cols) elif encoder_type == 'LeaveOneOut': encoder = LeaveOneOutEncoder(cols=cat_cols) X_train_enc = encoder.fit_transform( X_train, y_train ) return X_train_enc, encoder # def get_target_encode(X_train, y_train, target_encode_cols): # for col in target_encode_cols: # encoder = TargetEncoder() # encoder.fit_transform(df[[col, province_col]], df[target_col]) # encoder.transform() # return df def get_feature_imp(self, algo, algo_name, feature_cols, forecast_month): importance = algo.feature_importances_.round(4) names = np.array(feature_cols) Feature_Imp = pd.DataFrame(data=np.column_stack((names, importance) ), columns=['names', 'importance']) Feature_Imp.columns = ['Feature', 'Feature_Importance'] Feature_Imp[date_col] = forecast_month Feature_Imp = Feature_Imp[[date_col, 'Feature', 'Feature_Importance']] Feature_Imp = Feature_Imp.rename( columns={'Feature_Importance': algo_name}) return (Feature_Imp) def get_train_test_split( self, df, train_max_date, forecast_start_date, forecast_end_date, num_shift_lags=1 ): # TODO - no need to do this. Lags are offset enough. we can use latest week data train = df[df[date_col] <= train_max_date] # train train = train[ (train[date_col] < forecast_start_date) ] # train.dropna(inplace = True) train.set_index(['ts_id', date_col], inplace=True) X_train = train.drop(target_col, axis=1) y_train = train[target_col].fillna(0) # test test = df[ (df[date_col] >= forecast_start_date) & (df[date_col] <= forecast_end_date) ] test.set_index(['ts_id', date_col], inplace=True) X_test = test.drop(target_col, axis=1) y_test = test[target_col] return X_train, y_train, X_test, y_test def get_regex_filtered_data(self, l): regex = re.compile(r"\[|\]|<", re.IGNORECASE) filtered_l = [ regex.sub("_", col) if any( x in str(col) for x in set(("[", "]", "<", " "))) else col for col in l ] filtered_l = [i.replace(" ", "_") for i in filtered_l] return filtered_l def add_sample_weights(self, df, max_date, forecast_start): df['days_till_last_day'] = (df[date_col] - max_date).dt.days df['sample_weights'] = np.exp(df['days_till_last_day'] / 2) df.loc[ df[date_col] == (forecast_start - relativedelta(months=12)), 'sample_weights' ] = 1 df.loc[ df[date_col] == (forecast_start - relativedelta(months=24)), 'sample_weights' ] = 1 df = df.set_index(['ts_id', date_col]) return df['sample_weights'] def get_model_predictions( self, model, model_name, X_test, y_test, Feature_Imp_all, feature_cols, forecast_start ): if Feature_Imp_all.empty: Feature_Imp_all = self.get_feature_imp( model, model_name, feature_cols, forecast_start ) else: Feature_Imp_all = pd.merge( Feature_Imp_all, self.get_feature_imp(model, model_name, feature_cols, forecast_start), how='outer', on=[date_col, 'Feature'] ) if pd.DataFrame(X_test).empty: return y_test, Feature_Imp_all y_pred = model.predict(X_test) pred_col = 'preds_{}'.format(model_name) y_test[pred_col] = y_pred y_test.loc[y_test[pred_col] < 0, pred_col] = 0 y_test[pred_col] = y_test[pred_col].fillna(0) lgb_acc = 1 - self.wmape(y_test[target_col], y_test[pred_col]) logger.info( "{} Accuracy {}: {}" .format(model_name, forecast_start.strftime("%b"), lgb_acc) ) return y_test, Feature_Imp_all def wmape(self, actuals, forecast): error = abs(actuals - forecast) wmape_val = error.sum() / actuals.sum() return wmape_val def get_STM_forecast(self, df, forecast_start, num_shift_lags=1): global num_cols, cat_cols ts_features = [ i for i in df.columns if ( ('lag' in i) | ('_Sin' in i) | ('_Cos' in i) | ('_mean' in i) | ('_trend' in i) | ('ewm' in i) | ('seasonality_index' in i) ) ] flag_cols = [i for i in df.columns if i.endswith('_flag')] num_cols = ( ts_features ) target_encode_cols = [ ] one_hot_cols = [ 'classification', 'Group', 'ABC', 'WXYZ', 'PLC Status L1' ] df, added_one_hot_cols = self.one_hot_encode(df, one_hot_cols) # df = self.get_target_encode(df, target_encode_cols) train_max_date = (forecast_start - relativedelta(weeks=num_shift_lags)) X_train, y_train, X_test, y_test = self.get_train_test_split( df, train_max_date, forecast_start, forecast_start, num_shift_lags=num_shift_lags ) # TODO: # Add more cat features from item heirarchy and province, planning item, # rolling features # ewma # promo # Nan --> keep it # ETS as a feature cat_features = [ # 'covid_flag' ] + target_encode_cols feature_cols = num_cols + flag_cols + added_one_hot_cols + [ 'LOL', 'Year_Num', 'Quarter_Num', 'Quarter_Sin', 'Quarter_Cos', 'Month_Num', 'Month_Sin', 'Month_Cos', 'cov', 'ADI' ] feature_cols = list(set(df.columns) & set(feature_cols)) # cat_features = [] X_train = X_train[feature_cols + cat_features] X_test = X_test[feature_cols + cat_features] X_train.columns = self.get_regex_filtered_data(X_train.columns) X_test.columns = self.get_regex_filtered_data(X_test.columns) y_test = y_test.reset_index() val = ( X_train .reset_index() .merge(y_train.reset_index(), on=[date_col, 'ts_id'], how='left') ) _, _, X_val, y_val = self.get_train_test_split( val, train_max_date=train_max_date - relativedelta(weeks=8), forecast_start_date=train_max_date - relativedelta(weeks=7), forecast_end_date=train_max_date, num_shift_lags=num_shift_lags ) y_val = y_val.reset_index() filtered_cat_features = self.get_regex_filtered_data(cat_features) # X_train.dropna() Feature_Imp_all = pd.DataFrame() # X_train[filtered_cat_features] = X_train[filtered_cat_features].astype(str) # Encoding X_train_target, encoder = self.encode(X_train, y_train, filtered_cat_features, 'Target') if encoder != None: X_test_target = encoder.transform( X_test[encoder.get_feature_names()]) X_val_target = encoder.transform(X_val[encoder.get_feature_names()]) else: X_test_target = X_test.copy(deep=True) X_val_target = X_val.copy(deep=True) smaple_weights = self.add_sample_weights( X_train_target.reset_index()[['ts_id', date_col]], max_date=train_max_date, forecast_start=forecast_start ) if 'XGB' in models: ###XGBRF## logger.info("Forecasting XGRF...") xgb_rf = XGBRFRegressor(n_estimators=750, random_state=42) xgb_sample_weights = None if flag_sample_weights['xgb']: xgb_sample_weights = smaple_weights X_train_xgb = X_train_target X_test_xgb = X_test_target X_val_xgb = X_val_target if flag_seasonality_index['xgb']: seas_cols = [i for i in X_train_target.columns if 'seasonality_index' in i] X_train_xgb = X_train_target.drop(seas_cols, axis=1).copy() X_test_xgb = X_test_target.drop(seas_cols, axis=1).copy() X_val_xgb = X_val_target.drop(seas_cols, axis=1).copy() xgb_rf.fit(X_train_xgb, y_train, sample_weight=xgb_sample_weights) y_test, Feature_Imp_all = self.get_model_predictions( model=xgb_rf, model_name='xgb_rf_target', X_test=X_test_xgb, y_test=y_test, Feature_Imp_all=Feature_Imp_all, feature_cols=X_train_xgb.columns, forecast_start=forecast_start ) y_val, Feature_Imp_all = self.get_model_predictions( model=xgb_rf, model_name='xgb_rf_target', X_test=X_val_xgb, y_test=y_val, Feature_Imp_all=Feature_Imp_all, feature_cols=X_train_xgb.columns, forecast_start=forecast_start ) if 'CTB' in models: ###Catboost Target## logger.info("Forecasting CB...") cb = CatBoostRegressor( n_estimators=1000, learning_rate=0.01, cat_features=filtered_cat_features, # one_hot_max_size = 16, random_state=42, verbose=0 ) ctb_sample_weights = None if flag_sample_weights['ctb']: ctb_sample_weights = smaple_weights X_train_ctb = X_train X_test_ctb = X_test X_val_ctb = X_val if flag_seasonality_index['ctb']: seas_cols = [i for i in X_train.columns if 'seasonality_index' in i] X_train_ctb = X_train.drop(seas_cols, axis=1).copy() X_test_ctb = X_test.drop(seas_cols, axis=1).copy() X_val_ctb = X_val.drop(seas_cols, axis=1).copy() cb.fit(X_train_ctb, y_train, sample_weight=ctb_sample_weights) y_test, Feature_Imp_all = self.get_model_predictions( model=cb, model_name='cb_target', X_test=X_test_ctb, y_test=y_test, Feature_Imp_all=Feature_Imp_all, feature_cols=X_train_ctb.columns, forecast_start=forecast_start ) y_val, Feature_Imp_all = self.get_model_predictions( model=cb, model_name='cb_target', X_test=X_val_ctb, y_test=y_val, Feature_Imp_all=Feature_Imp_all, feature_cols=X_train_ctb.columns, forecast_start=forecast_start ) if 'LGBM' in models: ###LGBM## logger.info("Forecasting LGBM...") lgb = LGBMRegressor( n_estimators=2000, learning_rate=0.005, max_depth=10, num_leaves=int((2 ** 10) / 2), max_bin=1000, random_state=42, verbose=-1, categorical_feature=filtered_cat_features, ) X_train1 = X_train.copy(deep=True) X_test1 = X_test.copy(deep=True) X_val1 = X_val.copy(deep=True) logger.info("LGBM train: {}".format(X_train1.head(2))) logger.info( 'Filtered cat features:{}'.format(filtered_cat_features)) logger.info('Filtered cat features columns:{}'.format( X_train1[filtered_cat_features].columns)) logger.info('Filtered cat features dtypes:{}'.format( X_train1[filtered_cat_features].dtypes)) # X_train1[filtered_cat_features] = X_train1[filtered_cat_features].astype('category') # X_test1[filtered_cat_features] = X_test1[filtered_cat_features].astype('category') # X_val1[filtered_cat_features] = X_val1[filtered_cat_features].astype('category') lgb_sample_weights = None if flag_sample_weights['lgbm']: lgb_sample_weights = smaple_weights X_train_lgbm = X_train1 X_test_lgbm = X_test1 X_val_lgbm = X_val1 if flag_seasonality_index['lgbm']: seas_cols = [i for i in X_train1.columns if 'seasonality_index' in i] X_train_lgbm = X_train1.drop(seas_cols, axis=1).copy() X_test_lgbm = X_test1.drop(seas_cols, axis=1).copy() X_val_lgbm = X_val1.drop(seas_cols, axis=1).copy() lgb.fit(X_train_lgbm, y_train, sample_weight=lgb_sample_weights) y_test, Feature_Imp_all = self.get_model_predictions( model=lgb, model_name='lgb', X_test=X_test_lgbm, y_test=y_test, Feature_Imp_all=Feature_Imp_all, feature_cols=X_test_lgbm.columns, forecast_start=forecast_start ) y_val, Feature_Imp_all = self.get_model_predictions( model=lgb, model_name='lgb', X_test=X_val_lgbm, y_test=y_val, Feature_Imp_all=Feature_Imp_all, feature_cols=X_test_lgbm.columns, forecast_start=forecast_start ) return y_test, y_val, Feature_Imp_all
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/engine/forecast.py
forecast.py
class LoadData: def load_file(self, db, query): df = db.get_df(query) df.columns = [c.replace('-', '_') for c in df.columns] return df def load_all_input( self, type_list=None, store_id_list=None, last_date=None, reset_date=None, load_max_date=None, schema=None, db=None ): drug_list = self.load_file( query=""" select id as drug_id from "{schema}".drugs where type in {0} """.format(type_list, schema=schema), db=db ) sales_history = self.load_file( query=""" select date("created-at") as "sales-date","store-id", "drug-id" , sum("net-quantity") as "net-sales-quantity" from "{schema}".sales s where "store-id" in {store_id_list} and date("created-at") >= '{last_date}' and date("created-at") <= '{load_max_date}' group by "store-id", "drug-id", "sales-date" """.format( store_id_list=store_id_list, last_date=last_date, load_max_date=load_max_date, schema=schema), db=db ) cfr_pr = self.load_file( query=f""" select "store-id", "drug-id","shortbook-date", sum("loss-quantity") as "loss-quantity" from "{schema}"."cfr-patient-request" where "shortbook-date" >= '{last_date}' and "shortbook-date" <= '{load_max_date}' and "drug-id" <> -1 and ("drug-category" = 'chronic' or "repeatability-index" >= 40) and "loss-quantity" > 0 and "drug-type" in {type_list} and "store-id" in {store_id_list} group by "store-id","drug-id", "shortbook-date" """, db=db ) calendar = self.load_file( query=""" select date, year, month, "week-of-year", "day-of-week" from "{schema}".calendar where date < '{reset_date}' """.format(schema=schema, reset_date=reset_date), db=db ) first_bill_date = self.load_file( query=""" select "store-id" , min(date("created-at")) as bill_date from "{schema}".sales where "store-id" in {store_id_list} group by "store-id" """.format(schema=schema, store_id_list=store_id_list), db=db ) drug_sp = self.load_file( query = """select "drug-id", "avg-selling-rate" as avg_sales_value from "{schema}"."drug-std-info" dsi """.format(schema=schema), db=db ) return ( drug_list, sales_history, cfr_pr, calendar, first_bill_date, drug_sp )
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/engine/data_load.py
data_load.py
import pandas as pd import numpy as np from dateutil.relativedelta import relativedelta from tqdm import tqdm import logging import warnings warnings.filterwarnings("ignore") logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') from zeno_etl_libs.utils.ipc2.config_ipc import ( date_col, target_col, store_col, drug_col, eol_cutoff ) class Segmentation: def add_ts_id(self, df): df['ts_id'] = ( df[store_col].astype(int).astype(str) + '_' + df[drug_col].astype(int).astype(str) ) return df def _calc_abc(self, df52): B_cutoff = 0.5 C_cutoff = 0.8 D_cutoff = 0.95 tot_sales = ( df52.groupby([ 'ts_id' ])['actual_demand_value'].sum().reset_index() ) tot_sales.rename(columns={'actual_demand_value': 'total_LY_sales'}, inplace=True) tot_sales.sort_values('total_LY_sales', ascending=False, inplace=True) tot_sales["perc_sales"] = ( tot_sales['total_LY_sales'] / tot_sales['total_LY_sales'].sum() ) tot_sales["cum_perc_sales"] = tot_sales.perc_sales.cumsum() tot_sales["ABC"] = "A" tot_sales.loc[tot_sales.cum_perc_sales > B_cutoff, "ABC"] = "B" tot_sales.loc[tot_sales.cum_perc_sales > C_cutoff, "ABC"] = "C" tot_sales.loc[tot_sales.cum_perc_sales > D_cutoff, "ABC"] = "D" # tot_sales = self.add_ts_id(tot_sales) return tot_sales[['ts_id', 'ABC', 'total_LY_sales']] # TODO: lower COV cutoffs def get_abc_classification(self, df52): province_abc = df52.groupby( [store_col] ).apply(self._calc_abc) province_abc = province_abc[['ts_id', "ABC"]].reset_index(drop=True) # one tot_sales = ( df52 .groupby(['ts_id'])[target_col] .agg(['sum', 'mean']) .reset_index() ) tot_sales.rename( columns={'sum': 'total_LY_sales', 'mean': 'avg_ly_sales'}, inplace=True) tot_sales = tot_sales.merge( province_abc, on=['ts_id'], how='left' ) tot_sales = tot_sales.drop_duplicates() # tot_sales = self.add_ts_id(tot_sales) tot_sales = tot_sales[['ts_id', 'ABC']] return tot_sales def get_xyzw_classification(self, df1): input_ts_id = df1['ts_id'].unique() df1 = df1[df1[target_col] > 0] cov_df = df1.groupby(['ts_id'])[target_col].agg( ["mean", "std", "count", "sum"]) cov_df.reset_index(drop=False, inplace=True) cov_df['cov'] = np.where( ((cov_df["count"] > 2) & (cov_df["sum"] > 0)), (cov_df["std"]) / (cov_df["mean"]), np.nan ) cov_df['WXYZ'] = 'Z' cov_df.loc[cov_df['cov'] <= 1.2, 'WXYZ'] = 'Y' cov_df.loc[cov_df['cov'] <= 0.8, 'WXYZ'] = 'X' cov_df.loc[cov_df['cov'] <= 0.5, 'WXYZ'] = 'W' # cov_df = self.add_ts_id(cov_df) cov_df = cov_df[['ts_id', 'cov', 'WXYZ']] non_mapped_ts_ids = list( set(input_ts_id) - set(cov_df['ts_id'].unique()) ) non_mapped_cov = pd.DataFrame({ 'ts_id': non_mapped_ts_ids, 'cov': [np.nan] * len(non_mapped_ts_ids), 'WXYZ': ['Z'] * len(non_mapped_ts_ids) }) cov_df = pd.concat([cov_df, non_mapped_cov], axis=0) cov_df = cov_df.reset_index(drop=True) return cov_df def get_std(self, df1): input_ts_id = df1['ts_id'].unique() # df1 = df1[df1[target_col]>0] std_df = df1.groupby(['ts_id'])[target_col].agg(["std"]) return std_df def calc_interval_mean(self, x, key): df = pd.DataFrame({"X": x, "ts_id": key}).reset_index( drop=True).reset_index() df = df[df.X > 0] df["index_shift"] = df["index"].shift(-1) df["interval"] = df["index_shift"] - df["index"] df = df.dropna(subset=["interval"]) df['ADI'] = np.mean(df["interval"]) return df[['ts_id', 'ADI']] def calc_adi(self, df): # df = self.add_ts_id(df) logger.info( 'Combinations entering adi: {}'.format(df['ts_id'].nunique())) dict_of = dict(iter(df.groupby(['ts_id']))) logger.info("Total tsids in df: {}".format(df.ts_id.nunique())) logger.info("Total dictionary length: {}".format(len(dict_of))) list_dict = [ self.calc_interval_mean(dict_of[x][target_col], x) for x in tqdm(dict_of.keys()) ] data = ( pd.concat(list_dict) .reset_index(drop=True) .drop_duplicates() .reset_index(drop=True) ) logger.info('Combinations exiting adi: {}'.format(data.ts_id.nunique())) return data def get_PLC_segmentation(self, df, mature_cutoff_date, eol_cutoff_date): df1 = df[df[target_col] > 0] df1 = df1.groupby(['ts_id']).agg({date_col: [min, max]}) df1.reset_index(drop=False, inplace=True) df1.columns = [' '.join(col).strip() for col in df1.columns.values] df1['PLC Status L1'] = 'Mature' df1.loc[ (df1[date_col + ' min'] > mature_cutoff_date), 'PLC Status L1' ] = 'NPI' df1.loc[ (df1[date_col + ' max'] <= eol_cutoff_date), 'PLC Status L1' ] = 'EOL' # df1 = self.add_ts_id(df1) df1 = df1[['ts_id', 'PLC Status L1']] return df1 def get_group_mapping(self, seg_df): seg_df['Mixed'] = seg_df['ABC'].astype(str) + seg_df['WXYZ'].astype(str) seg_df['Group'] = 'Group3' group1_mask = seg_df['Mixed'].isin(['AW', 'AX', 'BW', 'BX']) seg_df.loc[group1_mask, 'Group'] = 'Group1' group2_mask = seg_df['Mixed'].isin(['AY', 'AZ', 'BY', 'BZ']) seg_df.loc[group2_mask, 'Group'] = 'Group2' return seg_df def calc_dem_pat(self, cov_df, adi_df): logger.info('Combinations entering calc_dem_pat: {}'.format( cov_df.ts_id.nunique())) logger.info('Combinations entering calc_dem_pat: {}'.format( adi_df.ts_id.nunique())) df = pd.merge(cov_df, adi_df, how='left', on='ts_id') df["cov2"] = np.power(df["cov"], 2) df["classification"] = "Lumpy" df.loc[ (df.ADI >= 1.32) & (df.cov2 < 0.49), "classification" ] = "Intermittent" df.loc[ (df.ADI < 1.32) & (df.cov2 >= 0.49), "classification" ] = "Erratic" df.loc[ (df.ADI < 1.32) & (df.cov2 < 0.49), "classification" ] = "Smooth" logger.info( 'Combinations exiting calc_dem_pat: {}'.format(df.ts_id.nunique())) return df[['ts_id', 'classification']] def get_start_end_dates_df(self, df, key_col, date_col, target_col, train_max_date, end_date): start_end_date_df = ( df[df[target_col] > 0] .groupby(key_col)[date_col] .agg({'min', 'max'}) .reset_index() .rename(columns={'min': 'start_date', 'max': 'end_date'}) ) start_end_date_df.loc[ ( start_end_date_df['end_date'] > ( train_max_date - relativedelta(weeks=eol_cutoff) ) ), 'end_date' ] = end_date return start_end_date_df def get_weekly_segmentation(self, df, df_sales_daily, train_max_date, end_date): df = df[df[date_col] <= train_max_date] df1 = df[ df[date_col] > (train_max_date - relativedelta(weeks=52)) ].copy(deep=True) df_std = df_sales_daily[ df_sales_daily[date_col] > (train_max_date - relativedelta(days=90)) ].copy(deep=True) df1 = self.add_ts_id(df1) abc_df = self._calc_abc(df1) xyzw_df = self.get_xyzw_classification(df1) std_df = self.get_std(df_std) adi_df = self.calc_adi(df1) demand_pattern_df = self.calc_dem_pat(xyzw_df[['ts_id', 'cov']], adi_df) mature_cutoff_date = train_max_date - relativedelta(weeks=52) eol_cutoff_date = train_max_date - relativedelta(weeks=13) plc_df = self.get_PLC_segmentation(df, mature_cutoff_date, eol_cutoff_date) start_end_date_df = self.get_start_end_dates_df( df, key_col='ts_id', date_col=date_col, target_col=target_col, train_max_date=train_max_date, end_date=end_date ) seg_df = plc_df.merge(abc_df, on='ts_id', how='outer') seg_df = seg_df.merge(xyzw_df, on='ts_id', how='outer') seg_df = seg_df.merge(adi_df, on='ts_id', how='outer') seg_df = seg_df.merge(demand_pattern_df, on='ts_id', how='outer') seg_df = seg_df.merge(start_end_date_df, on='ts_id', how='outer') seg_df = seg_df.merge(std_df, on='ts_id', how='outer') seg_df = self.get_group_mapping(seg_df) seg_df['Mixed'] = np.where(seg_df['Mixed']=='nannan', np.nan, seg_df['Mixed']) drug_class = seg_df[ ['ts_id', 'total_LY_sales', 'std', 'cov', 'ABC', 'WXYZ']] drug_class[[store_col, drug_col]] = drug_class['ts_id'].str.split('_', expand=True) drug_class.rename( columns={'total_LY_sales': 'net_sales', 'std': 'sales_std_dev', 'cov': 'sales_cov', 'ABC': 'bucket_abc', 'WXYZ': 'bucket_xyz'}, inplace=True) drug_class.drop(columns=['ts_id'], inplace=True) # seg_df[[store_col, drug_col]] = seg_df['ts_id'].str.split('_', expand = True) # seg_df.drop(columns=['ts_id'],inplace=True) # seg_df.rename(columns={'std':'sales_std_dev', 'cov':'sales_cov', 'ABC':'bucket_abcd', 'WXYZ':'bucket_wxyz', 'Mixed':'bucket'}, inplace=True) # seg_df['PLC Status L1'] = np.where(seg_df['PLC Status L1']=='NPI', 'New_Product', seg_df['PLC Status L1']) # seg_df['start_date'] = seg_df['start_date'].astype(str) # seg_df = seg_df[[store_col, drug_col,'PLC Status L1', 'total_LY_sales', 'bucket_abcd', 'bucket_wxyz', 'bucket', 'classification', 'Group', 'sales_std_dev', 'sales_cov', 'ADI', 'start_date' ]] # seg_df = pd.merge(seg_df, drug_class[[store_col, 'store_name', drug_col, ]]) return seg_df, drug_class
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/engine/segmentation.py
segmentation.py
from datetime import date import pandas as pd import numpy as np from dateutil.relativedelta import relativedelta from tqdm import tqdm import time from statsmodels.tsa.arima_model import ARMA from sklearn.linear_model import LinearRegression import logging import warnings warnings.filterwarnings("ignore") logger = logging.getLogger("_logger") logging.basicConfig(level=logging.DEBUG, format='%(message)s') from zeno_etl_libs.utils.ipc2.config_ipc import ( date_col, target_col, store_col, drug_col, eol_cutoff, add_lags_diff_flag, add_monthly_lags_flag, rolling_time_feat, ewma_lags, trend_lags, lags, lags_diff, monthly_lags, ) class Feature_Engg: def add_week_of_month(self, df): df["week_of_month"] = df[date_col].apply(lambda d: (d.day - 1) // 7 + 1) return df def add_month(self, df): df['Month'] = df[date_col].dt.month return df def calc_si(self, df, num_shift_lag=1): max_date = df[date_col].max() time_lag = (len(df) // cutoff_dic["si_week_freq"]) * cutoff_dic[ "si_week_freq"] min_date = max_date + relativedelta(months=-time_lag) df = df[(df[date_col] > min_date)] try: tsid_mean = np.mean(df[target_col].shift(num_shift_lag)) except BaseException as e: logger.info(df.ts_id.iloc[0]) logger.info(e) # continue df = df.groupby(["ts_id", "Week_Number"])[ target_col].mean().reset_index() df["Seas_index"] = df[target_col] / tsid_mean df = df[["ts_id", "Week_Number", "Seas_index"]] return df def ratio_based_si(self, df, train_max_date, num_shift_lag): if 'ts_id' not in df.columns: df = self.add_ts_id(df) train = df[df[date_col] <= train_max_date] dict_of = dict(iter(train.groupby(["ts_id"]))) si_list = [self.calc_si(dict_of[x], num_shift_lag) for x in dict_of.keys()] si_df = pd.concat(si_list) if 'Seas_index' in df.columns: df.drop(['Seas_index'], axis=1, inplace=True) df = pd.merge(df, si_df, how="left", on=["ts_id", "Week_Number"]) df['Seas_index'].fillna(1, inplace=True) return df def add_ARMA_forecast(self, data): model = ARMA(data[target_col], order=(0, 1)) model_fit = model.fit(disp=False) # forecast for required time priods yhat = model_fit.predict( len(data) - cutoff_dic["forecast_horizon_period"], len(data) - 1 ) data["ARMA_Forecast"] = yhat data["Actuals + ARMA"] = np.where( data["ARMA_Forecast"].isnull(), data[target_col], data["ARMA_Forecast"] ) return data[["ts_id", date_col, "Actuals + ARMA"]] def trend_sim_lin_reg(self, df): df.sort_values(by=date_col, inplace=True) df.reset_index() df["index"] = df.index x = df["index"].values.reshape(-1, 1) y = df[target_col].values.reshape(-1, 1) # fit linear regression regressor = LinearRegression() regressor.fit(x, y) return regressor.coef_ def add_trend_lags(self, actuals, fcst): # for i in lags: # fcst['Lag_' + str(i)] = fcst['Actuals + ARMA'].shift(i) # fcst = fcst.fillna(0) # fcst["rolling_ly_lag"] = (0.1 * fcst["Lag_104"]) + (0.9 * fcst["Lag_52"]) # fcst.drop(['Lag_' + str(i) for i in lags], axis = 1, inplace = True) actuals_trend = actuals.groupby(["ts_id"])[ target_col].sum().reset_index() actuals_trend["trend_value"] = self.trend_sim_lin_reg(actuals)[0, 0] return actuals_trend, fcst def add_lags(self, df, lag_list, num_shift_lag=1): is_drop_ts_id = False if 'ts_id' not in df.columns: df = self.add_ts_id(df) is_drop_ts_id = True df_grp_sum = (df.groupby([date_col, "ts_id"])[target_col] .sum() .unstack()) lag_l = [] lag_l_diff = [] for lag in lag_list: lag_df = ( df_grp_sum .shift(lag + num_shift_lag - 1) .fillna(method="bfill") .stack() .reset_index() ) lag_df_diff = ( df_grp_sum .shift(lag + num_shift_lag - 1) .diff(1) .fillna(method="bfill") .stack() .reset_index() ) lag_df.rename(columns={0: "lag_" + str(lag)}, inplace=True) lag_df_diff.rename(columns={0: "lag_" + str(lag) + '_diff'}, inplace=True) if "lag_" + str(lag) in df.columns: df.drop("lag_" + str(lag), axis=1, inplace=True) if "lag_" + str(lag) + '_diff' in df.columns: df.drop("lag_" + str(lag) + '_diff', axis=1, inplace=True) lag_l.append(lag_df.set_index(["ts_id", date_col])) lag_l.append(lag_df_diff.set_index(["ts_id", date_col])) lag_df = None for l in lag_l: if lag_df is None: lag_df = l else: lag_df = lag_df.join(l) df = df.merge(lag_df.reset_index(), on=["ts_id", date_col], how="left") df.drop("ts_id", axis=1, inplace=is_drop_ts_id) return df def add_lag_diff(self, df, lags_diff, num_shift_lag=1): drop_cols = [] for i, j in lags_diff: col_name = 'lag_diff_{}_{}'.format(i, j) if col_name in df.columns: df.drop(col_name, axis=1, inplace=True) if 'lag_' + str(i) not in df.columns: df = self.add_lags(df, [i], num_shift_lag=num_shift_lag) drop_cols.append('lag_' + str(i)) if 'lag_' + str(j) not in df.columns: df = self.add_lags(df, [j], num_shift_lag=num_shift_lag) drop_cols.append('lag_' + str(j)) drop_cols.append('lag_' + str(j) + '_diff') df[col_name] = df['lag_' + str(i)] - df['lag_' + str(j)] if len(drop_cols) > 0: df.drop(drop_cols, axis=1, inplace=True) return df def add_montly_lags(self, df, monthly_lags, num_shift_lag=1): start = time.time() mo_lag_dict = {} for lag in monthly_lags: mo_lag_dict[lag] = [] for week_num in df['week_of_month'].unique(): one_week_df = df[df['week_of_month'] == week_num][ ['ts_id', date_col, target_col]] df_grp = one_week_df.groupby([date_col, 'ts_id'])[ target_col].sum().unstack() df_grp.sort_index(axis=1, inplace=True) for lag in monthly_lags: lag1 = lag mo_lag = ( df_grp .shift(lag1) .bfill() .unstack() .reset_index() .rename(columns={0: str(lag) + '_mo_lag'}) ) # for diff_num in range(1,5): # diff_col = str(lag)+'_mo_lag_diff'+ str(diff_num) # mo_lag_diff = ( # df_grp # .shift(lag1) # .diff(diff_num) # .bfill() # .unstack() # .reset_index() # .rename(columns = {0: str(lag) +'_mo_lag_diff' + str(diff_num)}) # ) # mo_lag = mo_lag.merge(mo_lag_diff, on = ['ts_id', date_col], how = 'left') mo_lag_dict[lag].append(mo_lag) for lag in monthly_lags: col_name = str(lag) + '_mo_lag' if col_name in df.columns: df.drop(col_name, axis=1, inplace=True) for diff_num in range(1, 5): diff_col = str(lag) + '_mo_lag_diff' + str(diff_num) if diff_col in df.columns: df.drop(diff_col, axis=1, inplace=True) mo_lag = pd.concat(mo_lag_dict[lag]) for diff_num in range(1, 5): diff_col = str(lag) + '_mo_lag_diff' + str(diff_num) mo_lag_diff = ( mo_lag .groupby([date_col, 'ts_id']) [str(lag) + '_mo_lag'] .sum() .unstack() .sort_index() .diff(diff_num) .bfill() .stack() .reset_index() .rename(columns={0: diff_col}) ) mo_lag = mo_lag.merge(mo_lag_diff, on=['ts_id', date_col], how='left') df = df.merge(mo_lag, on=['ts_id', date_col], how='left') end = time.time() logger.debug( "Time for updated monthly lags: {} mins".format((end - start) / 60)) return df def add_rolling_lag(self, df, num_shift_lag=1): df["rolling lag"] = ( (0.4 * df[target_col].shift(num_shift_lag)) + (0.3 * df[target_col].shift(num_shift_lag + 1)) + (0.2 * df[target_col].shift(num_shift_lag + 2)) + (0.1 * df[target_col].shift(num_shift_lag + 3)) ) return df def add_start_end_dates(self, df): # Look for monthly format 4-4-5 and then make logic is_drop_week_of_month = False if 'week_of_month' not in df.columns: df = self.add_week_of_month(df) is_drop_week_of_month = True df["MonthStart"] = 0 df.loc[df['week_of_month'] == 1, 'MonthStart'] = 1 df.drop('week_of_month', axis=1, inplace=is_drop_week_of_month) # df[date_col]= pd.to_datetime(df[date_col]) month_end_list = ( df[df['MonthStart'] == 1][date_col].dt.date - relativedelta( weeks=1) ).values df['MonthEnd'] = 0 df.loc[ df[date_col].isin(month_end_list), 'MonthEnd' ] = 1 # df["MonthEnd"] = df[date_col].dt.is_month_end.astype(int) df["QuarterStart"] = ( df[date_col].dt.month.isin([1, 4, 7, 10]).astype(int) & df['MonthStart'] ) df["QuarterEnd"] = ( df[date_col].dt.month.isin([3, 6, 9, 12]).astype(int) & df['MonthEnd'] ) return df def add_holiday_ratio(self, df): df["Holiday_Ratio"] = df["holiday_count"].fillna(0) / 7 return df def add_rolling_time_features(self, df, week_list, agg_dict, num_shift_lag=1): roll_l = [] roll_l_diff = [] drop_cols = set() df_grp_sum = ( df.groupby([date_col, "ts_id"])[target_col] .sum() .unstack() .shift(num_shift_lag) ) for num in week_list: week_df = ( df_grp_sum .rolling(num) .agg(agg_dict) .bfill() .ffill() .unstack() .unstack(level=1) .reset_index() ).rename( columns={ "level_0": "ts_id", "mean": "avg_week" + str(num), "median": "median_week" + str(num), "std": "std_week" + str(num), "max": "max_week" + str(num), "min": "min_week" + str(num), } ) week_df_diff = ( df_grp_sum .rolling(num) .agg('mean') .diff(1) .bfill() .ffill() .unstack() .reset_index() ).rename(columns={0: "avg_week" + str(num) + "_diff"}) drop_cols = drop_cols.union( set(week_df.drop(["ts_id", date_col], axis=1).columns) | set(week_df_diff.drop(["ts_id", date_col], axis=1).columns) ) roll_l.append(week_df.set_index(["ts_id", date_col])) roll_l.append(week_df_diff.set_index(["ts_id", date_col])) drop_cols = list(drop_cols & set(df.columns)) df.drop(drop_cols, axis=1, inplace=True) week_df = None for l in roll_l: if week_df is None: week_df = l else: week_df = week_df.join(l) df = df.merge(week_df.reset_index(), on=["ts_id", date_col], how="left") # for i in [13, 25, 52]: # roll_df = df_grp_sum.shift(num_shift_lag).bfill().rolling(i, min_periods = 1) # roll_df = ( # roll_df.quantile(0.75) # - roll_df.quantile(0.25) # ).unstack().reset_index().rename(columns = {0: 'Quantile_diff_'+str(i)}) # if 'Quantile_diff_'+str(i) in df.columns: # df.drop('Quantile_diff_'+str(i), axis = 1, inplace = True) # df = df.merge(roll_df, on = ['ts_id', date_col], how = 'left') return df def add_ewma(self, df, week_list, agg_dict, num_shift_lag=1): ewma_l = [] df_grp_sum = (df.groupby([date_col, "ts_id"])[target_col] .sum() .unstack() .shift(num_shift_lag) .bfill() .ffill()) for num in week_list: week_df = ( df_grp_sum .ewm(span=num, ignore_na=True, adjust=True, min_periods=1) .agg(agg_dict) .bfill() .ffill() .unstack() .unstack(level=1) .reset_index() ) week_df.rename(columns={ "level_0": "ts_id", "mean": "ewma_" + str(num) }, inplace=True) if "ewma_" + str(num) in df.columns: df.drop("ewma_" + str(num), axis=1, inplace=True) ewma_l.append(week_df.set_index(["ts_id", date_col])) week_df = None for l in ewma_l: if week_df is None: week_df = l else: week_df = week_df.join(l) df = df.merge(week_df.reset_index(), on=["ts_id", date_col], how="left") return df def add_trend(self, df, week_list, num_shift_lag=1): # is_drop = True if 'ts_id' not in df.columns: df = self.add_ts_id(df) df_grp = ( df.groupby([date_col, "ts_id"])[target_col] .sum() .unstack() ) df_grp = df_grp.shift(num_shift_lag).bfill() numerator = df_grp.rolling(5, min_periods=1).mean() # all_trends_df = None df.set_index([date_col, "ts_id"], inplace=True) for num in week_list: denominator = df_grp.rolling(num, min_periods=1).mean() one_trend_df = (numerator / ( denominator + 1e-8)).bfill().ffill().stack().reset_index() one_trend_df.rename(columns={0: "trend_week" + str(num)}, inplace=True) if "trend_week" + str(num) in df.columns: df.drop(columns="trend_week" + str(num), inplace=True) df = df.join(one_trend_df.set_index([date_col, "ts_id"]), how='left') # all_trends_df.append(one_trend_df) return df.reset_index() def create_one_hot_holiday(self, df, col, name, on="holiday"): df[name] = 0 df.loc[df[on] == col, name] = 1 return df def add_week_of_month(self, df): df["week_of_month"] = df[date_col].apply(lambda d: (d.day - 1) // 7 + 1) return df def add_year_month(self, df): year_df = df[date_col].dt.year month_df = df[date_col].dt.month df['YearMonth'] = year_df.astype(str) + '_' + month_df.astype(str) return df def add_month(self, df): df['Month'] = df[date_col].dt.month return df def feat_agg(self, df, train_max_date, num_shift_lag): if pd.DataFrame(df).empty: return df if target_col not in df.columns: raise ValueError( "{} col not in dataframe passed".format(target_col)) if date_col not in df.columns: raise ValueError("{} col not in dataframe passed".format(date_col)) df = self.add_week_of_month(df) df = self.add_month(df) df.loc[df[date_col] > train_max_date, target_col] = np.nan logger.debug("Adding TS Features...") logger.debug("Adding lags...") logger.debug("Lags: {}".format(lags)) df = self.add_lags(df, lags, num_shift_lag=num_shift_lag) if add_lags_diff_flag: logger.debug("Adding Lag Diff") logger.debug( "lags_diff: {}".format(lags_diff, num_shift_lag=num_shift_lag)) df = self.add_lag_diff(df, lags_diff) if add_monthly_lags_flag: logger.debug("Adding Monthly lags..") df = self.add_montly_lags(df, monthly_lags, num_shift_lag=num_shift_lag) logger.debug("Adding start end dates...") df = self.add_start_end_dates(df) logger.debug("Adding rolling time features...") df = self.add_rolling_time_features( df, rolling_time_feat["lags"], rolling_time_feat["agg_func_dict"], num_shift_lag=num_shift_lag ) logger.debug("Adding ewma...") df = self.add_ewma(df, ewma_lags, {"mean"}, num_shift_lag=num_shift_lag) logger.debug("Adding trend...") df = self.add_trend(df, trend_lags, num_shift_lag=num_shift_lag) logger.debug("TS Features added successfully...") logger.info("maxdate after TS: {}".format(df[date_col].max())) return df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/engine/feat_engg.py
feat_engg.py
import pandas as pd import numpy as np import datetime as dt def v5_corrections(store_id, safety_stock_df, db, schema, logger): """ Main function to perform V5 corrections """ # Get Drug STD Qty and list of repeatable drug_ids df_3m_drugs, unique_drugs_3m = get_3m_drug_std_qty(store_id, db, schema, logger) # Locate drugs to perform correction check df_std_check = safety_stock_df.loc[safety_stock_df["drug_id"].isin( unique_drugs_3m)][["drug_id", "fcst", "safety_stock", "reorder_point", "order_upto_point"]] # Drugs not forecasted by IPC drugs_3m_not_set = list(set(unique_drugs_3m) ^ set(df_std_check["drug_id"].unique())) logger.info(f"Number of drugs not forecasted: {len(drugs_3m_not_set)}") # Merge STD Qty with SS table and find drugs correction areas df_std_check = df_3m_drugs.merge(df_std_check, on="drug_id", how="left") df_std_check = df_std_check.dropna() df_std_check["rop>=std_qty"] = np.where( df_std_check["reorder_point"] >= df_std_check["std_qty"], "Y", "N") tot_rep_drugs = df_std_check.shape[0] corr_req = df_std_check.loc[df_std_check['rop>=std_qty'] == 'N'].shape[0] corr_not_req = df_std_check.loc[df_std_check['rop>=std_qty'] == 'Y'].shape[0] logger.info(f"Number of repeatable drugs: {tot_rep_drugs}") logger.info(f"Number of repeatable drugs corrections required: {corr_req}") logger.info(f"Number of repeatable drugs corrections not required: {corr_not_req}") # CORRECTION STARTS order_freq = 4 column_order = list(df_std_check.columns) column_order += ["corr_ss", "corr_rop", "corr_oup"] # CASE1: No changes required df_no_change = df_std_check.loc[df_std_check["rop>=std_qty"] == "Y"].copy() df_no_change["corr_ss"] = df_no_change["safety_stock"].astype(int) df_no_change["corr_rop"] = df_no_change["reorder_point"].astype(int) df_no_change["corr_oup"] = df_no_change["order_upto_point"].astype(int) # CASE2: SS & ROP & OUP is Non Zero df_change1 = df_std_check.loc[(df_std_check["rop>=std_qty"] == "N") & (df_std_check["safety_stock"] != 0) & (df_std_check["reorder_point"] != 0) & (df_std_check["order_upto_point"] != 0)].copy() df_change1["mul_1"] = df_change1["reorder_point"] / df_change1["safety_stock"] df_change1["mul_2"] = df_change1["order_upto_point"] / df_change1["reorder_point"] df_change1["corr_rop"] = df_change1["std_qty"] df_change1["corr_ss"] = np.ceil(df_change1["corr_rop"] / df_change1["mul_1"]).astype(int) # If ROP >= OUP, then in those cases, increase OUP. df_change11 = df_change1.loc[ df_change1["corr_rop"] >= df_change1["order_upto_point"]].copy() df_change12 = df_change1.loc[ df_change1["corr_rop"] < df_change1["order_upto_point"]].copy() df_change11["corr_oup"] = np.ceil(df_change11["corr_rop"] + ( df_change11["fcst"] * order_freq / 28)).astype(int) df_change12["corr_oup"] = np.ceil(df_change12["corr_rop"] + ( df_change12["fcst"] * order_freq / 28)).astype(int) df_change1 = df_change11.append(df_change12) df_change1 = df_change1[column_order] # CASE3: Any of SS & ROP & OUP is Zero df_change2 = df_std_check.loc[(df_std_check["rop>=std_qty"] == "N")].copy() df_change2 = df_change2.loc[~((df_change2["safety_stock"] != 0) & (df_change2["reorder_point"] != 0) & (df_change2["order_upto_point"] != 0))].copy() df_change2["corr_rop"] = df_change2["std_qty"].astype(int) df_change2["corr_ss"] = np.floor(df_change2["corr_rop"] / 2).astype(int) # If ROP >= OUP, then in those cases, increase OUP. df_change21 = df_change2.loc[ df_change2["corr_rop"] >= df_change2["order_upto_point"]].copy() df_change22 = df_change2.loc[ df_change2["corr_rop"] < df_change2["order_upto_point"]].copy() df_change21["corr_oup"] = np.ceil(df_change21["corr_rop"] + ( df_change21["fcst"] * order_freq / 28)).astype(int) df_change22["corr_oup"] = np.ceil(df_change22["corr_rop"] + ( df_change22["fcst"] * order_freq / 28)).astype(int) df_change2 = df_change21.append(df_change22) df_change2 = df_change2[column_order] # Combine all 3 cases df_corrected = df_no_change.append(df_change1) df_corrected = df_corrected.append(df_change2) df_corrected = df_corrected.sort_index(ascending=True) # Get DF of corrected drugs and merge with input DF df_corrected_to_merge = df_corrected.loc[df_corrected["rop>=std_qty"] == "N"][ ["drug_id", "corr_ss", "corr_rop", "corr_oup"]] corr_safety_stock_df = safety_stock_df.merge(df_corrected_to_merge, on="drug_id", how="left") # Make corrections for required drugs corr_safety_stock_df["safety_stock"] = np.where( corr_safety_stock_df["corr_ss"] >= 0, corr_safety_stock_df["corr_ss"], corr_safety_stock_df["safety_stock"]) corr_safety_stock_df["reorder_point"] = np.where( corr_safety_stock_df["corr_rop"] >= 0, corr_safety_stock_df["corr_rop"], corr_safety_stock_df["reorder_point"]) corr_safety_stock_df["order_upto_point"] = np.where( corr_safety_stock_df["corr_oup"] >= 0, corr_safety_stock_df["corr_oup"], corr_safety_stock_df["order_upto_point"]) corr_safety_stock_df.drop(["corr_ss", "corr_rop", "corr_oup"], axis=1, inplace=True) corr_safety_stock_df["max_value"] = corr_safety_stock_df["order_upto_point"] * \ corr_safety_stock_df["fptr"] assert safety_stock_df.shape == corr_safety_stock_df.shape # Evaluate PRE and POST correction pre_post_metrics = { "metric": ["pre_corr", "post_corr"], "ss_qty": [safety_stock_df["safety_stock"].sum(), corr_safety_stock_df["safety_stock"].sum()], "ss_val": [round((safety_stock_df["safety_stock"] * safety_stock_df["fptr"]).sum(), 2), round((corr_safety_stock_df["safety_stock"] * corr_safety_stock_df["fptr"]).sum(), 2)], "rop_qty": [safety_stock_df["reorder_point"].sum(), corr_safety_stock_df["reorder_point"].sum()], "rop_val": [round((safety_stock_df["reorder_point"] * safety_stock_df["fptr"]).sum(), 2), round((corr_safety_stock_df["reorder_point"] * corr_safety_stock_df["fptr"]).sum(), 2)], "oup_qty": [safety_stock_df["order_upto_point"].sum(), corr_safety_stock_df["order_upto_point"].sum()], "oup_val": [round((safety_stock_df["order_upto_point"] * safety_stock_df["fptr"]).sum(), 2), round((corr_safety_stock_df["order_upto_point"] * corr_safety_stock_df["fptr"]).sum(), 2)] } pre_post_metics_df = pd.DataFrame.from_dict(pre_post_metrics).set_index('metric').T pre_post_metics_df["delta"] = pre_post_metics_df["post_corr"] - pre_post_metics_df["pre_corr"] pre_post_metics_df["change%"] = round((pre_post_metics_df["delta"] / pre_post_metics_df["pre_corr"]) * 100, 2) logger.info(f"\n{str(pre_post_metics_df)}") return corr_safety_stock_df def max_mode(pd_series): return int(max(pd_series.mode())) def get_3m_drug_std_qty(store_id, db, schema, logger): """ To fetch repeatable patient-drug qty from past 90days and calculate standard drug qty. """ start_date = (dt.date.today() - dt.timedelta(days=90)).strftime("%Y-%m-%d") end_date = dt.date.today().strftime("%Y-%m-%d") q_3m = """ select "patient-id" , "old-new" , "drug-id" , date("created-at") as "on-date", quantity as "tot-qty" from "{schema}".sales where "store-id" = {0} and "is-repeatable" = 1 and "bill-flag" = 'gross' and "created-at" > '{1} 00:00:00' and "created-at" < '{2} 00:00:00' """.format(store_id, start_date, end_date, schema=schema) df_3m = db.get_df(q_3m) df_3m.columns = [c.replace('-', '_') for c in df_3m.columns] # Get patient-drug-level STD Qty df_3m["3m_bills"] = 1 df_3m["std_qty"] = df_3m["tot_qty"] df_3m_patient = df_3m.groupby(["patient_id", "drug_id"], as_index=False).agg( {"3m_bills": "sum", "tot_qty": "sum", "std_qty": max_mode}) logger.info(f"Total repeatable patients: {len(df_3m_patient.patient_id.unique())}") # Get drug-level STD Qty df_3m_drugs = df_3m_patient.groupby("drug_id", as_index=False).agg( {"std_qty": "max"}) # STD Qty > 10 is considered outliers, to drop. drug_count_before = df_3m_drugs.shape[0] df_3m_drugs = df_3m_drugs.loc[df_3m_drugs["std_qty"] <= 10] drug_count_after = df_3m_drugs.shape[0] logger.info(f"Number of outlier drugs STD Qty: {drug_count_before-drug_count_after}") # Repeatable drugs STD Qty to check against IPC set ROP unique_drugs_3m = list(df_3m_drugs["drug_id"].unique()) return df_3m_drugs, unique_drugs_3m
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/heuristics/ipcv5_heuristics_old.py
ipcv5_heuristics_old.py
import pandas as pd import numpy as np import datetime as dt def v3_corrections(final_ss_df, store_id, corrections_selling_probability_cutoff, corrections_cumulative_probability_cutoff, schema, db, logger): final_ss_df['store_id'] = store_id q_prob = f""" select * from "{schema}"."ipc-corrections-rest-cases" where "store-id" = {store_id} """ q_prob_111 = f""" select * from "{schema}"."ipc-corrections-111-cases" where "store-id" = {store_id} """ prob_matrix = db.get_df(q_prob) df_111 = db.get_df(q_prob_111) prob_matrix.columns = [c.replace('-', '_') for c in prob_matrix.columns] df_111.columns = [c.replace('-', '_') for c in df_111.columns] # list of drugs for which corrections is required. i.e. max value 0. df_corrections_list = final_ss_df[ final_ss_df['order_upto_point'] == 0][['store_id', 'drug_id']] df_corrections = pd.merge( df_corrections_list, prob_matrix, how='inner', on=['store_id', 'drug_id'], validate='one_to_one') df_corrections = df_corrections.drop(columns={'corrected_max'}) df_corrections['order_upto_point'] = np.round( df_corrections['current_ma_3_months']) df_corrections_1 = df_corrections[ (df_corrections['cumm_prob'] >= corrections_cumulative_probability_cutoff['ma_less_than_2']) & (df_corrections['current_flag_ma_less_than_2'] == 1)] df_corrections_2 = df_corrections[ (df_corrections['cumm_prob'] >= corrections_cumulative_probability_cutoff['ma_more_than_2']) & (df_corrections['current_flag_ma_less_than_2'] == 0)] df_corrections_1 = df_corrections_1[ (df_corrections_1['selling_probability'] >= corrections_selling_probability_cutoff['ma_less_than_2']) & (df_corrections_1['current_flag_ma_less_than_2'] == 1)] df_corrections_2 = df_corrections_2[ (df_corrections_2['selling_probability'] >= corrections_selling_probability_cutoff['ma_more_than_2']) & (df_corrections_2['current_flag_ma_less_than_2'] == 0)] df_corrections = pd.concat( [df_corrections_1, df_corrections_2]).reset_index(drop=True) df_corrections_final = df_corrections.copy()[ ['store_id', 'drug_id', 'current_bucket', 'selling_probability', 'cumm_prob', 'current_flag_ma_less_than_2', 'avg_ptr', 'current_ma_3_months']] df_corrections = df_corrections[ ['store_id', 'drug_id', 'order_upto_point']] df_corrections['reorder_point'] = np.floor( df_corrections['order_upto_point'] / 2) df_corrections['safety_stock'] = np.floor( df_corrections['order_upto_point'] / 4) df_corrections = df_corrections.set_index(['store_id', 'drug_id']) final_ss_df = final_ss_df.set_index(['store_id', 'drug_id']) final_ss_df.update(df_corrections) final_ss_df = final_ss_df.reset_index() df_corrections = df_corrections.reset_index() df_corrections = pd.merge( df_corrections, df_corrections_final, on=['store_id', 'drug_id'], how='left', validate='one_to_one') # update 111 cases here. df_corrections_111 = pd.merge( df_corrections_list, df_111, how='inner', on=['store_id', 'drug_id'], validate='one_to_one') df_corrections_111 = df_corrections_111.drop( columns={'original_max', 'corrected_max', 'inv_impact', 'max_impact'}, axis=1) df_corrections_111['order_upto_point'] = np.round( df_corrections_111['ma_3_months']) df_corrections_111['reorder_point'] = np.floor( df_corrections_111['order_upto_point'] / 2) df_corrections_111['safety_stock'] = np.floor( df_corrections_111['order_upto_point'] / 4) df_corrections_111 = df_corrections_111.set_index( ['store_id', 'drug_id']) final_ss_df = final_ss_df.set_index(['store_id', 'drug_id']) final_ss_df.update(df_corrections_111) final_ss_df = final_ss_df.reset_index() df_corrections_111 = df_corrections_111.reset_index() # set reset date curr_date = str(dt.date.today()) df_corrections['reset_date'] = curr_date df_corrections_111['reset_date'] = curr_date final_ss_df.drop('store_id', axis=1, inplace=True) return final_ss_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/heuristics/ipcv3_heuristics.py
ipcv3_heuristics.py
from zeno_etl_libs.helper.aws.s3 import S3 import pandas as pd import numpy as np def v5_corrections(safety_stock_df, db, schema, logger): # ======================= DEFINE BUCKETS TO CORRECT ======================== corr_buckets = ['AW', 'AX', 'AY', 'BX', 'BY'] # ========================================================================== logger.info("Reading STD-Qty for all drugs") # get drug-std_qty data (TO CHANGE READ IF LIVE FOR ALL STORES) s3 = S3() file_path = s3.download_file_from_s3('STD_QTY_IPC/df_std_qty.csv') df_std_qty = pd.read_csv(file_path) logger.info("Define Max-STD-Qty for all drugs based on confidence") for index, row in df_std_qty.iterrows(): if row["std_qty_3_cf"] == 'H': max_std_qty = row["std_qty_3"] elif row["std_qty_2_cf"] in ['H', 'M']: max_std_qty = row["std_qty_2"] else: max_std_qty = row["std_qty"] df_std_qty.loc[index, "max_std_qty"] = max_std_qty # merge std and max.std to base df safety_stock_df = safety_stock_df.merge( df_std_qty[["drug_id", "std_qty", "max_std_qty"]], on="drug_id", how="left") safety_stock_df["std_qty"] = safety_stock_df["std_qty"].fillna(1) safety_stock_df["max_std_qty"] = safety_stock_df["max_std_qty"].fillna(1) # drugs to correct and not correct df_to_corr = safety_stock_df.loc[ safety_stock_df["bucket"].isin(corr_buckets)] df_not_to_corr = safety_stock_df.loc[ ~safety_stock_df["bucket"].isin(corr_buckets)] logger.info(f"Num drugs considered for correction {df_to_corr.shape[0]}") logger.info(f"Num drugs not considered for correction {df_not_to_corr.shape[0]}") logger.info("Correction logic starts") for index, row in df_to_corr.iterrows(): fcst = row["fcst"] rop = row["reorder_point"] oup = row["order_upto_point"] std = row["std_qty"] max_std = row["max_std_qty"] new_rop = std_round(rop, std=max_std) if (new_rop != 0) & (new_rop / rop >= 2): new_rop = std_round(rop, std=std) if (new_rop / rop >= 2) & (new_rop > fcst): new_rop = rop # no correction if (new_rop == 0) & (oup > 0): new_oup = std else: new_oup = std_round(oup, std=max_std) if (new_oup <= new_rop) & (new_oup != 0): new_oup = std_round(new_rop + 1, std=max_std) df_to_corr.loc[index, "reorder_point"] = new_rop df_to_corr.loc[index, "order_upto_point"] = new_oup corr_safety_stock_df = pd.concat([df_to_corr, df_not_to_corr]) corr_safety_stock_df.drop(columns=["std_qty", "max_std_qty"], axis=1, inplace=True) return corr_safety_stock_df def std_round(x, std): """ round x to the closest higher multiple of std-qty """ return std * np.ceil(x/std)
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/heuristics/ipcv5_heuristics.py
ipcv5_heuristics.py
import pandas as pd import numpy as np import datetime as datetime from datetime import timedelta from zeno_etl_libs.utils.ipc2.config_ipc import key_col, date_col, store_col, drug_col, target_col def load_data(store_id,start_d, end_d,db,schema): mos = f''' select * from "{schema}".sales where date("created-at") >= '{start_d}' and date("created-at") < '{end_d}' and "store-id" in ({store_id}) ''' cfr = f'''select cpr."store-id" , cpr."drug-id" , cpr."shortbook-date", sum("loss-quantity") as "loss-quantity" from "{schema}"."cfr-patient-request" cpr where "shortbook-date" >= '{start_d}' and "shortbook-date" < '{end_d}' and cpr."drug-id" <> -1 and ("drug-category" = 'chronic' or "repeatability-index" >= 40) and "loss-quantity" > 0 and "drug-type" in ('ethical', 'ayurvedic', 'generic', 'discontinued-products', 'banned', 'general', 'high-value-ethical', 'baby-product', 'surgical', 'otc', 'glucose-test-kit', 'category-2', 'category-1', 'category-4', 'baby-food', '', 'category-3') and "store-id" in ({store_id}) group by cpr."store-id" , cpr."drug-id" , "shortbook-date" ''' df_mos = db.get_df(mos) df_cfr = db.get_df(cfr) df_mos.columns = [c.replace('-', '_') for c in df_mos.columns] df_cfr.columns = [c.replace('-', '_') for c in df_cfr.columns] return df_mos,df_cfr def pre_process_data(df_mos,df_cfr): set_dtypes = { store_col: int, drug_col: int, 'loss_quantity': int } df_cfr = df_cfr.astype(set_dtypes) df_cfr['shortbook_date'] = pd.to_datetime(df_cfr['shortbook_date']) df_mos['created_at'] = pd.to_datetime(df_mos['created_at']) df_mos['sales_date'] = pd.to_datetime(df_mos['created_at'].dt.date) df_mos['drug_id'].fillna(0,inplace=True) df_mos['ts_id'] = df_mos['store_id'].astype(int).astype(str) + "_" + df_mos['drug_id'].astype(int).astype(str) df_mos = df_mos.groupby(['ts_id', 'sales_date', 'store_id', 'drug_id' ])['net_quantity'].sum().reset_index() df_mos.rename(columns={'sales_date':date_col},inplace=True) df_mos.rename(columns={'net_quantity':target_col},inplace=True) return df_mos, df_cfr def get_formatted_data(df, key_col, date_col, store_col, drug_col, target_col): df = df[[key_col, date_col, target_col]] min_date = df[date_col].dropna().min() end_date = df[date_col].dropna().max() date_range = [] date_range = pd.date_range( start= min_date, end= end_date, freq= 'd' ) date_range = list(set(date_range) - set(df[date_col])) df = ( df .groupby([date_col] + [key_col])[target_col] .sum() .unstack() ) for date in date_range: df.loc[date, :] = np.nan df = ( df .fillna(0) .stack() .reset_index() .rename(columns = {0: target_col}) ) df[[store_col, drug_col]] = df[key_col].str.split('_', expand = True) df[[store_col, drug_col]] = df[[store_col, drug_col]].astype(float).astype(int) return df def aggreagte_data(df_sales, df_cfr): df = df_sales.merge(df_cfr, left_on=[store_col, drug_col, date_col], right_on=[store_col, drug_col, 'shortbook_date'], how='left') df[date_col] = df[date_col].combine_first(df['shortbook_date']) df[target_col].fillna(0, inplace=True) df['loss_quantity'].fillna(0, inplace=True) df[target_col] += df['loss_quantity'] df.drop(['shortbook_date', 'loss_quantity'], axis=1, inplace=True) df_l3m_sales = df.groupby([store_col,drug_col])[target_col].sum().reset_index() df_l3m_sales.rename(columns={target_col:'l3m_sales_qty'},inplace=True) return df_l3m_sales def implement_corrections(final_ss_df,df_l3m_sales): cols = final_ss_df.columns df = pd.merge(final_ss_df,df_l3m_sales,how='left', on=[store_col,drug_col] ) df['fcst_zero_w_sales'] = np.where(((df['fcst']==0)&(df['l3m_sales_qty']>0)&(df['order_upto_point']==0)),1, 0) df['order_upto_point'] = np.where(df['fcst_zero_w_sales']==1,np.round((df['l3m_sales_qty']/3)*(18/30)), df['order_upto_point']) df['order_upto_point'] = np.where(((df['fcst_zero_w_sales']==1)&(df['l3m_sales_qty']==2)&(df['order_upto_point']==0)),1, df['order_upto_point']) df['reorder_point'] = np.where(df['fcst_zero_w_sales']==1,np.floor((df['l3m_sales_qty']/3)*(13/30)), df['reorder_point']) df['reorder_point'] = np.where((df['fcst_zero_w_sales']==1)&(df['reorder_point']==df['order_upto_point'])&(df['reorder_point']>0),df['order_upto_point']-1, df['reorder_point']) df['safety_stock'] = np.where(df['fcst_zero_w_sales']==1,np.floor((df['l3m_sales_qty']/3)*(7/30)), df['safety_stock']) df = df[cols] return df def v3N_corrections(final_ss_df, store_id, reset_date, schema, db, logger): end_d = pd.to_datetime(reset_date) start_d = end_d - timedelta(days= 90) start_d = str(start_d.date()) end_d = str(end_d.date()) df_mos,df_cfr = load_data(store_id=store_id,start_d=start_d, end_d=end_d,db=db,schema=schema) df_mos, df_cfr = pre_process_data(df_mos=df_mos, df_cfr=df_cfr) df_sales = get_formatted_data(df=df_mos, key_col=key_col, date_col = date_col, target_col=target_col, store_col=store_col, drug_col=drug_col) df_l3m_sales = aggreagte_data(df_sales=df_sales, df_cfr = df_cfr) final_ss_df = implement_corrections(final_ss_df=final_ss_df, df_l3m_sales=df_l3m_sales) return final_ss_df
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/ipc2/heuristics/ipcv3N_heuristics.py
ipcv3N_heuristics.py
import numpy as np import pandas as pd from datetime import datetime from scipy.stats import norm from zeno_etl_libs.utils.ipc.lead_time import lead_time from zeno_etl_libs.utils.ipc.heuristics.ipcv4_heuristics import ipcv4_heuristics from zeno_etl_libs.utils.ipc.heuristics.ipcv5_heuristics import v5_corrections # from zeno_etl_libs.utils.ipc.heuristics.ipcv6_heuristics import v6_corrections def non_ipc_safety_stock_calc( store_id, cal_sales, reset_date, final_predict, drug_class, corrections_flag, corrections_selling_probability_cutoff, corrections_cumulative_probability_cutoff, chronic_max_flag, train_flag, drug_type_list_v4, v5_active_flag, v6_active_flag, v6_type_list, v6_ptr_cut_off, db, schema, logger): '''LEAD TIME CALCULATION''' lt_drug, lt_store_mean, lt_store_std = lead_time( store_id, cal_sales, reset_date, db, schema, logger) service_level = 0.95 num_days = 4 * 7 order_freq = 4 z = norm.ppf(service_level) print(lt_store_mean, lt_store_std) drug_class = drug_class.copy() drug_class['bucket'] = drug_class['bucket_abc'] + drug_class['bucket_xyz'] safety_stock_df = final_predict.merge( lt_drug[['drug_id', 'lead_time_mean', 'lead_time_std']], how='left', on='drug_id') safety_stock_df['lead_time_mean'].fillna(lt_store_mean, inplace=True) safety_stock_df['lead_time_std'].fillna(lt_store_std, inplace=True) safety_stock_df = safety_stock_df.merge( drug_class[['drug_id', 'bucket']], on='drug_id', how='left') safety_stock_df['bucket'].fillna('NA', inplace=True) safety_stock_df['demand_daily'] = safety_stock_df['fcst']/num_days safety_stock_df['demand_daily_deviation'] = ( safety_stock_df['std']/np.sqrt(num_days)) # heuristics #1 safety_stock_df['lead_time_std'] = np.where( safety_stock_df['lead_time_std'] < 1, lt_store_std, safety_stock_df['lead_time_std']) # non ipc store safety stock safety_stock_df['safety_stock'] = np.round( z * np.sqrt( ( safety_stock_df['lead_time_mean'] * safety_stock_df['demand_daily_deviation'] * safety_stock_df['demand_daily_deviation'] ) + ( safety_stock_df['lead_time_std'] * safety_stock_df['lead_time_std'] * safety_stock_df['demand_daily'] * safety_stock_df['demand_daily'] ))) safety_stock_df['reorder_point'] = np.round( safety_stock_df['safety_stock'] + safety_stock_df['demand_daily'] * safety_stock_df['lead_time_mean']) safety_stock_df['order_upto_point'] = np.round( safety_stock_df['reorder_point'] + safety_stock_df['demand_daily'] * order_freq) safety_stock_df['safety_stock_days'] = np.round( num_days * safety_stock_df['safety_stock'] / safety_stock_df['fcst']) safety_stock_df['reorder_days'] = np.round( num_days * safety_stock_df['reorder_point'] / safety_stock_df['fcst']) safety_stock_df['order_upto_days'] = np.round( num_days * safety_stock_df['order_upto_point'] / safety_stock_df['fcst']) # getting order value drug_list = list(safety_stock_df['drug_id'].unique()) print(len(drug_list)) drug_str = str(drug_list).replace('[', '(').replace(']', ')') fptr_query = """ select "drug-id" , avg(ptr) as fptr, sum(quantity) as curr_inventory from "{schema}"."inventory-1" i where "store-id" = {store_id} and "drug-id" in {drug_str} group by "drug-id" """.format(store_id=store_id, drug_str=drug_str, schema=schema) fptr = db.get_df(fptr_query) fptr.columns = [c.replace('-', '_') for c in fptr.columns] fptr["fptr"] = fptr["fptr"].astype(float) safety_stock_df = safety_stock_df.merge(fptr, on='drug_id', how='left') safety_stock_df['fptr'].fillna(100, inplace=True) safety_stock_df['max_value'] = ( safety_stock_df['fptr'] * safety_stock_df['order_upto_point']) # correction plugin - Start if corrections_flag & train_flag: safety_stock_df['correction_flag'] = 'N' safety_stock_df['store_id'] = store_id print("corrections code is running now:") q_prob = f"""select * from "{schema}"."ipc-corrections-rest-cases" """ q_prob_111 = f"""select * from "{schema}"."ipc-corrections-111-cases" """ prob_matrix = db.get_df(q_prob) df_111 = db.get_df(q_prob_111) prob_matrix.columns = [c.replace('-', '_') for c in prob_matrix.columns] df_111.columns = [c.replace('-', '_') for c in df_111.columns] # list of drugs for which corrections is required. i.e. max value 0. df_corrections_list = safety_stock_df[ safety_stock_df['order_upto_point'] == 0][['store_id', 'drug_id']] df_corrections = pd.merge( df_corrections_list, prob_matrix, how='inner', on=['store_id', 'drug_id'], validate='one_to_one') df_corrections = df_corrections.drop(columns={'corrected_max'}) df_corrections['order_upto_point'] = np.round( df_corrections['current_ma_3_months']) df_corrections_1 = df_corrections[ (df_corrections['cumm_prob'] >= corrections_cumulative_probability_cutoff['ma_less_than_2']) & (df_corrections['current_flag_ma_less_than_2'] == 1)] df_corrections_2 = df_corrections[ (df_corrections['cumm_prob'] >= corrections_cumulative_probability_cutoff['ma_more_than_2']) & (df_corrections['current_flag_ma_less_than_2'] == 0)] df_corrections_1 = df_corrections_1[ (df_corrections_1['selling_probability'] >= corrections_selling_probability_cutoff['ma_less_than_2']) & (df_corrections_1['current_flag_ma_less_than_2'] == 1)] df_corrections_2 = df_corrections_2[ (df_corrections_2['selling_probability'] >= corrections_selling_probability_cutoff['ma_more_than_2']) & (df_corrections_2['current_flag_ma_less_than_2'] == 0)] df_corrections = pd.concat( [df_corrections_1, df_corrections_2]).reset_index(drop=True) df_corrections_final = df_corrections.copy()[ ['store_id', 'drug_id', 'current_bucket', 'selling_probability', 'cumm_prob', 'current_flag_ma_less_than_2', 'avg_ptr', 'current_ma_3_months']] # adding run time current inventory df_corrections_final = pd.merge( df_corrections_final, safety_stock_df[['store_id', 'drug_id', 'curr_inventory']], on=['store_id', 'drug_id'], how='left', validate='one_to_one') df_corrections = df_corrections[ ['store_id', 'drug_id', 'order_upto_point']] df_corrections['reorder_point'] = np.floor( df_corrections['order_upto_point'] / 2) df_corrections['safety_stock'] = np.floor( df_corrections['order_upto_point'] / 4) df_corrections['correction_flag'] = 'Y' df_corrections['is_ipc'] = 'Y' df_corrections = df_corrections.set_index(['store_id', 'drug_id']) safety_stock_df = safety_stock_df.set_index(['store_id', 'drug_id']) safety_stock_df.update(df_corrections) safety_stock_df = safety_stock_df.reset_index() df_corrections = df_corrections.reset_index() df_corrections = pd.merge( df_corrections, df_corrections_final, on=['store_id', 'drug_id'], how='left', validate='one_to_one') # update 111 cases here. df_corrections_111 = pd.merge( df_corrections_list, df_111, how='inner', on=['store_id', 'drug_id'], validate='one_to_one') df_corrections_111 = df_corrections_111.drop( columns={'current_inventory', 'original_max', 'corrected_max', 'inv_impact', 'max_impact'}, axis=1) df_corrections_111['order_upto_point'] = np.round( df_corrections_111['ma_3_months']) df_corrections_111['reorder_point'] = np.floor( df_corrections_111['order_upto_point'] / 2) df_corrections_111['safety_stock'] = np.floor( df_corrections_111['order_upto_point'] / 4) df_corrections_111['correction_flag'] = 'Y' df_corrections_111['is_ipc'] = 'Y' # adding run time current inventory df_corrections_111 = pd.merge( df_corrections_111, safety_stock_df[['store_id', 'drug_id', 'curr_inventory']], on=['store_id', 'drug_id'], how='left', validate='one_to_one') df_corrections_111 = df_corrections_111.set_index( ['store_id', 'drug_id']) safety_stock_df = safety_stock_df.set_index(['store_id', 'drug_id']) safety_stock_df.update(df_corrections_111) safety_stock_df = safety_stock_df.reset_index() df_corrections_111 = df_corrections_111.reset_index() # set reset date curr_date = str(datetime.now()) df_corrections['reset_date'] = curr_date df_corrections_111['reset_date'] = curr_date safety_stock_df = safety_stock_df.drop(['store_id'], axis=1) else: print('corrections block skipped :') df_corrections = pd.DataFrame() df_corrections_111 = pd.DataFrame() safety_stock_df['correction_flag'] = 'N' # Correction plugin - End # # Chronic drug changes if chronic_max_flag == 'Y': # based on ME OOS feedback - keep chronic drugs drug_max_zero = tuple( safety_stock_df.query('order_upto_point == 0')['drug_id']) # reading chronic drug list drug_chronic_max_zero_query = ''' select id as drug_id from "{schema}".drugs where category = 'chronic' and id in {0} '''.format(str(drug_max_zero), schema=schema) drug_chronic_max_zero = db.get_df(drug_chronic_max_zero_query)[ 'drug_id'] # setting non zero max for such drugs safety_stock_df.loc[ (safety_stock_df['drug_id'].isin(drug_chronic_max_zero)) & (safety_stock_df['order_upto_point'] == 0), 'order_upto_point'] = 1 safety_stock_df.loc[ (safety_stock_df['drug_id'].isin(drug_chronic_max_zero)) & (safety_stock_df['order_upto_point'] == 0), 'correction_flag'] = 'Y_chronic' safety_stock_df = ipcv4_heuristics(safety_stock_df, drug_type_list_v4, db, schema) if v5_active_flag == "Y": logger.info("IPC V5 Correction Starts") safety_stock_df = v5_corrections(store_id, safety_stock_df, db, schema, logger) logger.info("IPC V5 Correction Successful") # if v6_active_flag == "Y": # logger.info("IPC V6 Correction Starts") # safety_stock_df, drugs_max_to_lock_ipcv6, drug_rejects_ipcv6 = \ # v6_corrections(store_id, safety_stock_df, reset_date, v6_type_list, # v6_ptr_cut_off, logger) # # # add algo name to v6 write table # drugs_max_to_lock_ipcv6["algo"] = 'non-ipc' # drug_rejects_ipcv6["algo"] = 'non-ipc' # logger.info("IPC V6 Correction Successful") # else: drugs_max_to_lock_ipcv6 = pd.DataFrame() drug_rejects_ipcv6 = pd.DataFrame() return safety_stock_df, df_corrections, df_corrections_111, \ drugs_max_to_lock_ipcv6, drug_rejects_ipcv6
zeno-etl-libs-v3
/zeno_etl_libs_v3-1.0.17-py3-none-any.whl/zeno_etl_libs/utils/non_ipc/safety_stock/safety_stock.py
safety_stock.py