import tensorflow as tf
import tensorflow_probability as tfp

tfe = tf.contrib.eager
tfd = tfp.distributions

sess = tf.InteractiveSession()

"""
Explanation: <a href="https://colab.research.google.com/github/nbortolotti/tensorflow-experiences/blob/develop/colaboratory/TensorFlow_Probability_Hospital.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
Introduction
End of explanation
"""


n = 6.
count = tf.range(
    start=0.,
    limit= n +1)

p = tf.constant([0.25])
dist = tfd.Binomial(total_count=n, probs=p).prob(count)

dist.eval() #support line to analyze tensor

"""
Explanation: Hospital records show that of patients suffering from a certain disease, 75% die of it. What is the probability that of 6 randomly selected patients, 4 will recover?
*This is a binomial distribution because there are only 2 outcomes (the patient dies, or does not).
Conditions: 
*   die or not [die 75%]
*   n=6
*   p=0.25
https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Binomial
Modeling
End of explanation
"""


# routine to convert tensors
def tensorConvertor(tensors):
  if tf.executing_eagerly():
    return tf.contrib.framework.nest.pack_sequence_as(
        tensors,
        [t.numpy() if tf.contrib.framework.is_tensor(t) else t
         for t in tf.contrib.framework.nest.flatten(tensors)])
  return sess.run(tensors)

dist_np= tensorConvertor(dist) #support line to conver tensor

dist_np #support line to analyze  array

"""
Explanation: Support operations
End of explanation
"""


import matplotlib.pyplot as plt;
import numpy as np;

for i, v in enumerate(dist_np):
  objects = (0, 1, 2, 3, 4, 5, 6)
  y_pos = np.arange(len(objects))

  plt.barh(y_pos, dist_np, align='center', alpha=0.5)
  plt.ylabel('Patients')
  plt.xlabel('Probability')
  plt.title('Recorver probability patients Hospital')
  plt.text(v, i , str(v), color='green', fontweight='bold')

"""
Explanation: Visualization
End of explanation
"""


