1 ```#!/usr/bin/python ``` 2 ```# -*- coding: utf-8 -*- ``` 3 4 3 ```""" ``` 5 ``` ``` 6 ``` The persistent entropy has been defined in . A precursor of this definition was given in  ``` 7 ``` to measure how different bars of the barcode are in length. ``` 8 ``` ``` 9 ```  M. Rucco, F. Castiglione, E. Merelli, M. Pettini, Characterisation of the ``` 10 ``` idiotypic immune network through persistent entropy, in: Proc. Complex, 2015. ``` 11 ```  H. Chintakunta, T. Gentimis, R. Gonzalez-Diaz, M.-J. Jimenez, ``` 12 ``` H. Krim, An entropy-based persistence barcode, Pattern Recognition ``` 13 ``` 48 (2) (2015) 391–401. ``` 14 ``` ``` 15 ``` Implementation of persistent entropy ``` 16 17 ``` Author: Eduardo Paluzo Hidalgo (cimagroup, University of Seville) ``` 18 ``` contact: epaluzo@us.es ``` 19 20 ```""" ``` 21 22 3 ```from __future__ import division ``` 23 3 ```import numpy as np ``` 24 25 26 3 ```__all__ = ["persistent_entropy"] ``` 27 28 29 3 ```def persistent_entropy( ``` 30 ``` dgms, keep_inf=False, val_inf=None, normalize=False ``` 31 ```): ``` 32 ``` """ ``` 33 ``` Perform the persistent entropy values of a family of persistence barcodes (or persistence diagrams). ``` 34 ``` Assumes that the input diagrams are from a determined dimension. If the infinity bars have any meaning ``` 35 ``` in your experiment and you want to keep them, remember to give the value you desire to val_Inf. ``` 36 37 ``` Parameters ``` 38 ``` ----------- ``` 39 ``` dgms: ndarray (n_pairs, 2) or list of diagrams ``` 40 ``` array or list of arrays of birth/death pairs of a persistence barcode of a determined dimension. ``` 41 ``` keep_inf: bool, default False ``` 42 ``` if False, the infinity bars are removed. ``` 43 ``` if True, the infinity bars remain. ``` 44 ``` val_inf: float, default None ``` 45 ``` substitution value to infinity. ``` 46 ``` normalize: bool, default False ``` 47 ``` if False, the persistent entropy values are not normalized. ``` 48 ``` if True, the persistent entropy values are normalized. ``` 49 ``` ``` 50 ``` Returns ``` 51 ``` -------- ``` 52 53 ``` ps: ndarray (n_pairs,) ``` 54 ``` array of persistent entropy values corresponding to each persistence barcode. ``` 55 56 ``` """ ``` 57 58 3 ``` if isinstance(dgms, list) == False: ``` 59 3 ``` dgms = [dgms] ``` 60 61 ``` # Step 1: Remove infinity bars if keep_inf = False. If keep_inf = True, infinity value is substituted by val_inf. ``` 62 63 3 ``` if keep_inf == False: ``` 64 3 ``` dgms = [(dgm[dgm[:, 1] != np.inf]) for dgm in dgms] ``` 65 3 ``` if keep_inf == True: ``` 66 3 ``` if val_inf != None: ``` 67 3 ``` dgms = [ ``` 68 ``` np.where(dgm == np.inf, val_inf, dgm) ``` 69 ``` for dgm in dgms ``` 70 ``` ] ``` 71 ``` else: ``` 72 0 ``` raise Exception( ``` 73 ``` "Remember: You need to provide a value to infinity bars if you want to keep them." ``` 74 ``` ) ``` 75 76 ``` # Step 2: Persistent entropy computation. ``` 77 3 ``` ps = [] ``` 78 3 ``` for dgm in dgms: ``` 79 3 ``` l = dgm[:, 1] - dgm[:, 0] ``` 80 3 ``` if all(l > 0): ``` 81 3 ``` L = np.sum(l) ``` 82 3 ``` p = l / L ``` 83 3 ``` E = -np.sum(p * np.log(p)) ``` 84 3 ``` if normalize == True: ``` 85 0 ``` E = E / np.log(len(l)) ``` 86 3 ``` ps.append(E) ``` 87 ``` else: ``` 88 0 ``` raise Exception("A bar is born after dying") ``` 89 90 3 ``` return np.array(ps) ```

Read our documentation on viewing source code .