text:
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""Tests for sorting operators."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import unittest
from absl.testing import parameterized
import numpy as np

from tensorflow.compiler.tests import xla_test
from tensorflow.compiler.tf2xla.python import xla
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import function
from tensorflow.python.framework import ops
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn_ops
from tensorflow.python.platform import test

class XlaSortOpTest(xla_test.XLATestCase, parameterized.TestCase):

  def _assertOpOutputMatchesExpected(self, op, args, expected):
    """Tests that op(*args) == expected."""
    with self.session() as session:
      with self.test_scope():
        placeholders = [
            array_ops.placeholder(dtypes.as_dtype(arg.dtype), arg.shape)
            for arg in args
        ]
        feeds = {placeholders[i]: args[i] for i in range(0, len(args))}
        output = op(*placeholders)
        if isinstance(output, ops.Tensor):
          output = [output]

      results = session.run(output, feeds)
      for result, v in zip(results, expected):
        self.assertAllClose(v, result, rtol=1e-3)

  def _shuffled_arange(self, shape, dtype):
    x = np.arange(np.prod(shape), dtype=dtype)
    np.random.shuffle(x)
    return x.reshape(shape)

  def _supported_key_types(self):
    supported_key_types = set([
        dtypes.bfloat16.as_numpy_dtype, np.float16, np.float32, np.float64,
        np.int32, np.uint32, np.int16, np.uint16, np.int8, np.uint8
    ])
    res = supported_key_types.intersection(self.numeric_types)
    assert res
    return res

  def testSort(self):
    for dtype in self._supported_key_types():
      x = self._shuffled_arange((101,), dtype)
      self._assertOpOutputMatchesExpected(
          xla.sort, [x], expected=[np.arange(101, dtype=dtype)])

  def testKeyValueSort(self):
    for key_type in self._supported_key_types():
      for value_type in self._supported_key_types():
        if key_type == np.uint8 or value_type == np.uint8:
          # I do not understand why the test fails on uint8. We plan to
          # deprecate xla.key_value_sort in favor of xla.variadic_sort anyway.
          continue
        x = self._shuffled_arange((101,), key_type)
        y = (-x).astype(value_type)
        self._assertOpOutputMatchesExpected(
            xla.key_value_sort, [x, y],
            expected=[
                np.arange(101, dtype=key_type),
                -np.arange(101, dtype=value_type)
            ])

  @parameterized.parameters(0, 1, 2)
  @test_util.disable_mlir_bridge("Not supported yet")
  def testVariadicSortDimension(self, dimension):
    shape = (2, 3, 4)
    for key_type in self._supported_key_types():
      x = self._shuffled_arange(shape, key_type)
      expected = np.sort(x, axis=dimension)

      @function.Defun(key_type, key_type)
      def compare_lt(x1, x2):
        return x1 < x2

      def wrap_sort(x):
        return xla.variadic_sort([x],
                                 dimension=dimension,
                                 is_stable=False,
                                 comparator=compare_lt)

      self._assertOpOutputMatchesExpected(wrap_sort, [x], expected=[expected])

  @test_util.disable_mlir_bridge("Not supported yet")
  def testVariadicSortReverse(self):
    shape = (100,)
    for key_type in self._supported_key_types():
      x = self._shuffled_arange(shape, key_type)
      expected = np.sort(x, axis=0)[::-1]

      @function.Defun(key_type, key_type)
      def compare_gt(x1, x2):
        return x1 > x2

      def wrap_sort(x):
        return xla.variadic_sort([x],
                                 dimension=0,
                                 is_stable=False,
                                 comparator=compare_gt)

      self._assertOpOutputMatchesExpected(wrap_sort, [x], expected=[expected])

  @parameterized.parameters(0, 1, 2)
  @test_util.disable_mlir_bridge("Not supported yet")
  def testVariadicSortSeveral(self, dimension):
    if np.__version__ < "1.15":
      raise unittest.SkipTest("np.take_along_axis was added in 1.15")
    shape = (2, 3, 4)
    for key_type in self._supported_key_types():
      for value_type_1 in self._supported_key_types():
        for value_type_2 in self._supported_key_types():
          inputs = [
              self._shuffled_arange(shape, key_type),
              self._shuffled_arange(shape, value_type_1),
              self._shuffled_arange(shape, value_type_2)
          ]

          # The first array is sorted, and the others are shuffled the same way
          sorted_indices = np.argsort(inputs[0], axis=dimension)
          expected = [
              np.take_along_axis(inp, sorted_indices, axis=dimension)
              for inp in inputs
          ]
          self.assertAllEqual(np.sort(inputs[0], axis=dimension), expected[0])

          @function.Defun(key_type, key_type, value_type_1, value_type_1,
                          value_type_2, value_type_2)
          def compare_lt(x1, x2, y1, y2, z1, z2):
            del y1, y2, z1, z2
            return x1 < x2

          def wrap_sort(*args):
            return xla.variadic_sort(
                args,  # Pass the arguments as a tuple
                comparator=compare_lt,
                dimension=dimension,
                is_stable=False)

          self._assertOpOutputMatchesExpected(
              wrap_sort, inputs, expected=expected)

  @test_util.disable_mlir_bridge("Not supported yet")
  def testVariadicSortLexicographic(self):
    # Three inputs: the first two are used for lexicographic sort, and the
    # third is just swapped accordingly.
    # The first array will contain only 0 and 1, to test lexicographic order
    if np.__version__ < "1.15":
      raise unittest.SkipTest("np.take_along_axis was added in 1.15")
    shape = (20,)
    for key_type_1 in set([np.int16, np.uint16, np.int32, np.uint32]):
      for key_type_2 in self._supported_key_types():
        for value_type in self._supported_key_types():
          inputs = [
              # Ensure that some keys in the first input are equal
              np.random.uniform(0, 2, shape).astype(key_type_1),
              self._shuffled_arange(shape, key_type_2),
              self._shuffled_arange(shape, value_type)
          ]
          # The first two arrays are sorted lexicographically, and the third
          # is shuffled the same way
          sorted_indices = np.argsort(100 * inputs[0] + inputs[1])
          expected = [
              np.take_along_axis(inp, sorted_indices, axis=0) for inp in inputs
          ]

          @function.Defun(key_type_1, key_type_1, key_type_2, key_type_2,
                          value_type, value_type)
          def compare_lexicographic(x1, x2, y1, y2, z1, z2):
            del z1, z2
            return math_ops.logical_or(
                x1 < x2, math_ops.logical_and(math_ops.equal(x1, x2), y1 < y2))

          def wrap_sort(*args):
            return xla.variadic_sort(
                args,  # Pass the arguments as a tuple
                comparator=compare_lexicographic,
                dimension=0,
                is_stable=False)

          self._assertOpOutputMatchesExpected(
              wrap_sort, inputs, expected=expected)

  @parameterized.parameters(0, 1, 2)
  @test_util.disable_mlir_bridge("Not supported yet")
  def testVariadicSortSeveralStable(self, dimension):
    shape = (2, 3, 4)
    for key_type in self._supported_key_types():
      for value_type_1 in self._supported_key_types():
        for value_type_2 in self._supported_key_types():
          # The first input is all 0s, there should be no changes for
          # stable sort.
          inputs = [
              np.zeros(shape, key_type),
              self._shuffled_arange(shape, value_type_1),
              self._shuffled_arange(shape, value_type_2)
          ]

          @function.Defun(key_type, key_type, value_type_1, value_type_1,
                          value_type_2, value_type_2)
          def compare_lt(x1, x2, y1, y2, z1, z2):
            del y1, y2, z1, z2
            return x1 < x2

          def wrap_sort(*args):
            return xla.variadic_sort(
                args,  # Pass the arguments as a tuple
                comparator=compare_lt,
                dimension=dimension,
                is_stable=True)

          self._assertOpOutputMatchesExpected(
              wrap_sort, inputs, expected=inputs)

  def testTopK(self):
    supported_types = set([
        dtypes.bfloat16.as_numpy_dtype, np.float16, np.float32, np.float64,
        np.int32, np.uint32, np.int64, np.uint64
    ])
    for dtype in supported_types.intersection(self.numeric_types):
      # Use small input size for bfloat16. Otherwise, we'll get duplicate values
      # after conversion to bfloat16, so the possible resulting index array is
      # no longer unique.
      if dtype in (dtypes.bfloat16.as_numpy_dtype, np.float16):
        array_size = 20
        k_options = [0, 1, 2, 10, 20]
      else:
        array_size = 200 * 1000
        k_options = [0, 1, 2, 10, 20, 100, 1000, 200 * 1000]
      for x in [np.arange(array_size)]:
        np.random.shuffle(x)
        for k in k_options:
          indices = x.argsort()[::-1][:k]

          def topk(v, k=k):
            return nn_ops.top_k(v, k=k, sorted=True)

          self._assertOpOutputMatchesExpected(
              topk, [x.astype(dtype)],
              expected=[x[indices].astype(dtype), indices])

  @parameterized.named_parameters(
      ("HalfPrecision", dtypes.bfloat16.as_numpy_dtype),
      ("HalfFloatPrecision", np.float16),
      ("SinglePrecision", np.float32),
      ("DoublePrecision", np.float64),
      ("Int32", np.int32),
      ("UnsignedInt32", np.uint32),
      ("Int64", np.int64),
      ("UnsignedInt64", np.uint64),
  )
  def testTopK2D(self, dtype):
    if dtype in self.numeric_types:
      # Use small input size for bfloat16. Otherwise, we'll get duplicate values
      # after conversion to bfloat16, so the possible resulting index array is
      # no longer unique.
      if dtype in (dtypes.bfloat16.as_numpy_dtype, np.float16):
        array_size = 10
        k_options = [0, 1, 2, 10]
      else:
        array_size = 200 * 1000
        k_options = [0, 1, 2, 10, 20, 100, 1000, 200 * 1000]
      batch = 16
      for x in [np.arange(batch * array_size)]:
        np.random.shuffle(x)
        x = np.reshape(x, [batch, array_size])
        for k in k_options:
          indices = x.argsort(axis=1)[::, -1:-k - 1:-1]
          expected = np.sort(x, axis=1)[::, -1:-k - 1:-1]

          def topk(v, k=k):
            return nn_ops.top_k(v, k=k, sorted=True)

          self._assertOpOutputMatchesExpected(
              topk, [x.astype(dtype)],
              expected=[expected.astype(dtype), indices])

  def testTopKZeros(self):
    """Tests that positive and negative zeros sort correctly."""
    supported_types = set(
        [dtypes.bfloat16.as_numpy_dtype, np.float16, np.float32, np.float64])
    for dtype in supported_types.intersection(self.numeric_types):
      with self.session() as sess:
        p = array_ops.placeholder(dtype)
        with self.test_scope():
          topk = nn_ops.top_k(p, k=4)
        results = sess.run(
            topk,
            {p: np.array([0., -0., 0., 3., -0., -4., 0., -0.], dtype=dtype)})
        self.assertAllEqual(np.array([3., 0., 0., 0.], dtype=dtype), results[0])
        self.assertEqual(list([3, 0, 2, 6]), list(results[1]))

  def testTopKInfinities(self):
    """Tests that positive and negative infinity sort correctly."""
    supported_types = set(
        [dtypes.bfloat16.as_numpy_dtype, np.float16, np.float32, np.float64])
    for dtype in supported_types.intersection(self.numeric_types):
      with self.session() as sess:
        p = array_ops.placeholder(dtype)
        with self.test_scope():
          topk = nn_ops.top_k(p, k=6)
        results = sess.run(topk, {
            p:
                np.array([1, 2, float("inf"), -float("inf"), -1, -2],
                         dtype=dtype)
        })
        self.assertAllEqual(
            np.array([float("inf"), 2.0, 1.0, -1.0, -2.0, -float("inf")],
                     dtype=dtype), results[0])
        self.assertEqual(list([2, 1, 0, 4, 5, 3]), list(results[1]))

  @parameterized.named_parameters(
      ("Int32", np.int32),
      ("Int64", np.uint64),
  )
  def testInTopK(self, dtype):
    if dtype in self.numeric_types:
      array_size = 200 * 1000
      k_options = [0, 1, 2, 10, 20, 100, 1000, 200 * 1000]
      batch = 16
      for x in [np.arange(batch * array_size)]:
        np.random.shuffle(x)
        x = np.reshape(x, [batch, array_size])
        y = np.random.randint(0, array_size, size=batch)
        for k in k_options:
          indices = x.argsort(axis=1)[::, -1:-k - 1:-1]
          expected = [y[i] in indices[i] for i in range(batch)]

          def in_topk(predictions, targets, k=k):
            return nn_ops.in_top_k(predictions, targets, k)

          self._assertOpOutputMatchesExpected(
              in_topk,
              [x.astype(np.float32), y.astype(dtype)],
              expected=[expected])

if __name__ == "__main__":
  test.main()

max_stars_repo_name: kalosisz/tensorflow

max_issues_repo_name: kalosisz/tensorflow

max_forks_repo_name: kalosisz/tensorflow


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Helper functions for analysis of the Cell Painting Assay data.
"""

import functools
from glob import glob
import os.path as op
from typing import Iterable, List, Optional, Union

# import sys

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt

import scipy.spatial.distance as dist

OUTPUT_DIR = "../output"

ACT_PROF_FEATURES = [
    "Median_Cells_AreaShape_Area",
    "Median_Cells_AreaShape_MaximumRadius",
    "Median_Cells_AreaShape_MeanRadius",
    "Median_Cells_AreaShape_MinFeretDiameter",
    "Median_Cells_AreaShape_MinorAxisLength",
    "Median_Cells_AreaShape_Perimeter",
    "Median_Cells_Correlation_Correlation_ER_Ph_golgi",
    "Median_Cells_Correlation_Correlation_ER_Syto",
    "Median_Cells_Correlation_Correlation_Hoechst_ER",
    "Median_Cells_Correlation_Correlation_Hoechst_Mito",
    "Median_Cells_Correlation_Correlation_Hoechst_Ph_golgi",
    "Median_Cells_Correlation_Correlation_Hoechst_Syto",
    "Median_Cells_Correlation_Correlation_Mito_ER",
    "Median_Cells_Correlation_Correlation_Mito_Ph_golgi",
    "Median_Cells_Correlation_Correlation_Mito_Syto",
    "Median_Cells_Correlation_Correlation_Syto_Ph_golgi",
    "Median_Cells_Correlation_K_ER_Syto",
    "Median_Cells_Correlation_K_Hoechst_Syto",
    "Median_Cells_Correlation_K_Mito_Hoechst",
    "Median_Cells_Correlation_K_Ph_golgi_Syto",
    "Median_Cells_Correlation_K_Syto_ER",
    "Median_Cells_Correlation_K_Syto_Hoechst",
    "Median_Cells_Correlation_K_Syto_Ph_golgi",
    "Median_Cells_Correlation_Manders_ER_Hoechst",
    "Median_Cells_Correlation_Manders_ER_Syto",
    "Median_Cells_Correlation_Manders_Mito_Hoechst",
    "Median_Cells_Correlation_Manders_Ph_golgi_Hoechst",
    "Median_Cells_Correlation_Manders_Syto_Hoechst",
    "Median_Cells_Correlation_Overlap_Hoechst_ER",
    "Median_Cells_Correlation_Overlap_Hoechst_Mito",
    "Median_Cells_Correlation_Overlap_Hoechst_Ph_golgi",
    "Median_Cells_Correlation_Overlap_Hoechst_Syto",
    "Median_Cells_Correlation_Overlap_Mito_ER",
    "Median_Cells_Correlation_Overlap_Mito_Ph_golgi",
    "Median_Cells_Correlation_Overlap_Mito_Syto",
    "Median_Cells_Correlation_Overlap_Syto_Ph_golgi",
    "Median_Cells_Correlation_RWC_ER_Mito",
    "Median_Cells_Correlation_RWC_Hoechst_ER",
    "Median_Cells_Correlation_RWC_Hoechst_Mito",
    "Median_Cells_Correlation_RWC_Hoechst_Ph_golgi",
    "Median_Cells_Correlation_RWC_Hoechst_Syto",
    "Median_Cells_Correlation_RWC_Mito_Hoechst",
    "Median_Cells_Correlation_RWC_Mito_Syto",
    "Median_Cells_Correlation_RWC_Ph_golgi_Hoechst",
    "Median_Cells_Correlation_RWC_Ph_golgi_Mito",
    "Median_Cells_Correlation_RWC_Ph_golgi_Syto",
    "Median_Cells_Correlation_RWC_Syto_Hoechst",
    "Median_Cells_Correlation_RWC_Syto_Mito",
    "Median_Cells_Granularity_1_Mito",
    "Median_Cells_Granularity_1_Ph_golgi",
    "Median_Cells_Granularity_1_Syto",
    "Median_Cells_Granularity_2_Mito",
    "Median_Cells_Granularity_2_Ph_golgi",
    "Median_Cells_Granularity_2_Syto",
    "Median_Cells_Granularity_3_ER",
    "Median_Cells_Granularity_3_Mito",
    "Median_Cells_Granularity_3_Ph_golgi",
    "Median_Cells_Granularity_3_Syto",
    "Median_Cells_Granularity_4_Mito",
    "Median_Cells_Granularity_5_Mito",
    "Median_Cells_Intensity_IntegratedIntensityEdge_Hoechst",
    "Median_Cells_Intensity_IntegratedIntensity_Syto",
    "Median_Cells_Intensity_LowerQuartileIntensity_Mito",
    "Median_Cells_Intensity_MADIntensity_ER",
    "Median_Cells_Intensity_MADIntensity_Hoechst",
    "Median_Cells_Intensity_MADIntensity_Mito",
    "Median_Cells_Intensity_MADIntensity_Ph_golgi",
    "Median_Cells_Intensity_MADIntensity_Syto",
    "Median_Cells_Intensity_MaxIntensityEdge_Mito",
    "Median_Cells_Intensity_MaxIntensityEdge_Syto",
    "Median_Cells_Intensity_MaxIntensity_Hoechst",
    "Median_Cells_Intensity_MaxIntensity_Mito",
    "Median_Cells_Intensity_MaxIntensity_Ph_golgi",
    "Median_Cells_Intensity_MeanIntensityEdge_Hoechst",
    "Median_Cells_Intensity_MeanIntensity_Hoechst",
    "Median_Cells_Intensity_MeanIntensity_Mito",
    "Median_Cells_Intensity_MeanIntensity_Syto",
    "Median_Cells_Intensity_MedianIntensity_ER",
    "Median_Cells_Intensity_MedianIntensity_Mito",
    "Median_Cells_Intensity_MedianIntensity_Syto",
    "Median_Cells_Intensity_MinIntensityEdge_Mito",
    "Median_Cells_Intensity_MinIntensityEdge_Syto",
    "Median_Cells_Intensity_MinIntensity_Mito",
    "Median_Cells_Intensity_MinIntensity_Syto",
    "Median_Cells_Intensity_StdIntensityEdge_ER",
    "Median_Cells_Intensity_StdIntensityEdge_Mito",
    "Median_Cells_Intensity_StdIntensityEdge_Syto",
    "Median_Cells_Intensity_StdIntensity_Hoechst",
    "Median_Cells_Intensity_StdIntensity_Mito",
    "Median_Cells_Intensity_StdIntensity_Ph_golgi",
    "Median_Cells_Intensity_StdIntensity_Syto",
    "Median_Cells_Intensity_UpperQuartileIntensity_Hoechst",
    "Median_Cells_Intensity_UpperQuartileIntensity_Mito",
    "Median_Cells_Intensity_UpperQuartileIntensity_Syto",
    "Median_Cells_RadialDistribution_FracAtD_Mito_3of4",
    "Median_Cells_RadialDistribution_FracAtD_Mito_4of4",
    "Median_Cells_RadialDistribution_FracAtD_Ph_golgi_1of4",
    "Median_Cells_RadialDistribution_FracAtD_Ph_golgi_2of4",
    "Median_Cells_RadialDistribution_FracAtD_Ph_golgi_4of4",
    "Median_Cells_RadialDistribution_MeanFrac_Mito_1of4",
    "Median_Cells_RadialDistribution_MeanFrac_Mito_2of4",
    "Median_Cells_RadialDistribution_MeanFrac_Mito_3of4",
    "Median_Cells_RadialDistribution_MeanFrac_Mito_4of4",
    "Median_Cells_RadialDistribution_MeanFrac_Ph_golgi_1of4",
    "Median_Cells_RadialDistribution_MeanFrac_Ph_golgi_2of4",
    "Median_Cells_RadialDistribution_MeanFrac_Ph_golgi_4of4",
    "Median_Cells_RadialDistribution_RadialCV_Mito_3of4",
    "Median_Cells_RadialDistribution_RadialCV_Mito_4of4",
    "Median_Cells_RadialDistribution_RadialCV_Ph_golgi_1of4",
    "Median_Cells_RadialDistribution_RadialCV_Ph_golgi_2of4",
    "Median_Cells_RadialDistribution_RadialCV_Ph_golgi_3of4",
    "Median_Cells_Texture_AngularSecondMoment_Mito_10_00",
    "Median_Cells_Texture_AngularSecondMoment_Mito_3_00",
    "Median_Cells_Texture_AngularSecondMoment_Mito_5_00",
    "Median_Cells_Texture_AngularSecondMoment_Ph_golgi_10_00",
    "Median_Cells_Texture_AngularSecondMoment_Ph_golgi_3_00",
    "Median_Cells_Texture_AngularSecondMoment_Ph_golgi_5_00",
    "Median_Cells_Texture_AngularSecondMoment_Syto_10_00",
    "Median_Cells_Texture_AngularSecondMoment_Syto_3_00",
    "Median_Cells_Texture_AngularSecondMoment_Syto_5_00",
    "Median_Cells_Texture_Contrast_ER_3_00",
    "Median_Cells_Texture_Contrast_ER_5_00",
    "Median_Cells_Texture_Contrast_Hoechst_10_00",
    "Median_Cells_Texture_Contrast_Hoechst_3_00",
    "Median_Cells_Texture_Contrast_Hoechst_5_00",
    "Median_Cells_Texture_Contrast_Mito_10_00",
    "Median_Cells_Texture_Contrast_Mito_3_00",
    "Median_Cells_Texture_Contrast_Mito_5_00",
    "Median_Cells_Texture_Contrast_Ph_golgi_10_00",
    "Median_Cells_Texture_Contrast_Ph_golgi_3_00",
    "Median_Cells_Texture_Contrast_Ph_golgi_5_00",
    "Median_Cells_Texture_Contrast_Syto_10_00",
    "Median_Cells_Texture_Contrast_Syto_3_00",
    "Median_Cells_Texture_Contrast_Syto_5_00",
    "Median_Cells_Texture_Correlation_ER_10_00",
    "Median_Cells_Texture_Correlation_ER_3_00",
    "Median_Cells_Texture_Correlation_ER_5_00",
    "Median_Cells_Texture_Correlation_Mito_10_00",
    "Median_Cells_Texture_Correlation_Mito_3_00",
    "Median_Cells_Texture_Correlation_Mito_5_00",
    "Median_Cells_Texture_Correlation_Ph_golgi_10_00",
    "Median_Cells_Texture_Correlation_Ph_golgi_3_00",
    "Median_Cells_Texture_Correlation_Ph_golgi_5_00",
    "Median_Cells_Texture_Correlation_Syto_10_00",
    "Median_Cells_Texture_Correlation_Syto_5_00",
    "Median_Cells_Texture_DifferenceEntropy_Hoechst_10_00",
    "Median_Cells_Texture_DifferenceEntropy_Mito_10_00",
    "Median_Cells_Texture_DifferenceEntropy_Mito_3_00",
    "Median_Cells_Texture_DifferenceEntropy_Mito_5_00",
    "Median_Cells_Texture_DifferenceEntropy_Ph_golgi_10_00",
    "Median_Cells_Texture_DifferenceEntropy_Ph_golgi_3_00",
    "Median_Cells_Texture_DifferenceEntropy_Ph_golgi_5_00",
    "Median_Cells_Texture_DifferenceEntropy_Syto_10_00",
    "Median_Cells_Texture_DifferenceEntropy_Syto_3_00",
    "Median_Cells_Texture_DifferenceEntropy_Syto_5_00",
    "Median_Cells_Texture_DifferenceVariance_Mito_10_00",
    "Median_Cells_Texture_DifferenceVariance_Mito_3_00",
    "Median_Cells_Texture_DifferenceVariance_Mito_5_00",
    "Median_Cells_Texture_DifferenceVariance_Ph_golgi_10_00",
    "Median_Cells_Texture_DifferenceVariance_Ph_golgi_3_00",
    "Median_Cells_Texture_DifferenceVariance_Ph_golgi_5_00",
    "Median_Cells_Texture_DifferenceVariance_Syto_10_00",
    "Median_Cells_Texture_DifferenceVariance_Syto_3_00",
    "Median_Cells_Texture_DifferenceVariance_Syto_5_00",
    "Median_Cells_Texture_Entropy_Mito_10_00",
    "Median_Cells_Texture_Entropy_Mito_3_00",
    "Median_Cells_Texture_Entropy_Mito_5_00",
    "Median_Cells_Texture_Entropy_Ph_golgi_10_00",
    "Median_Cells_Texture_Entropy_Ph_golgi_3_00",
    "Median_Cells_Texture_Entropy_Ph_golgi_5_00",
    "Median_Cells_Texture_Entropy_Syto_10_00",
    "Median_Cells_Texture_Entropy_Syto_3_00",
    "Median_Cells_Texture_Entropy_Syto_5_00",
    "Median_Cells_Texture_InfoMeas2_Ph_golgi_10_00",
    "Median_Cells_Texture_InfoMeas2_Ph_golgi_3_00",
    "Median_Cells_Texture_InfoMeas2_Ph_golgi_5_00",
    "Median_Cells_Texture_InverseDifferenceMoment_ER_10_00",
    "Median_Cells_Texture_InverseDifferenceMoment_ER_3_00",
    "Median_Cells_Texture_InverseDifferenceMoment_ER_5_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Mito_10_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Mito_3_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Mito_5_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Ph_golgi_10_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Ph_golgi_3_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Ph_golgi_5_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Syto_10_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Syto_3_00",
    "Median_Cells_Texture_InverseDifferenceMoment_Syto_5_00",
    "Median_Cells_Texture_SumAverage_Hoechst_10_00",
    "Median_Cells_Texture_SumAverage_Hoechst_3_00",
    "Median_Cells_Texture_SumAverage_Hoechst_5_00",
    "Median_Cells_Texture_SumAverage_Mito_10_00",
    "Median_Cells_Texture_SumAverage_Mito_3_00",
    "Median_Cells_Texture_SumAverage_Mito_5_00",
    "Median_Cells_Texture_SumAverage_Syto_10_00",
    "Median_Cells_Texture_SumAverage_Syto_3_00",
    "Median_Cells_Texture_SumAverage_Syto_5_00",
    "Median_Cells_Texture_SumEntropy_Mito_10_00",
    "Median_Cells_Texture_SumEntropy_Mito_3_00",
    "Median_Cells_Texture_SumEntropy_Mito_5_00",
    "Median_Cells_Texture_SumEntropy_Ph_golgi_10_00",
    "Median_Cells_Texture_SumEntropy_Ph_golgi_3_00",
    "Median_Cells_Texture_SumEntropy_Ph_golgi_5_00",
    "Median_Cells_Texture_SumEntropy_Syto_10_00",
    "Median_Cells_Texture_SumEntropy_Syto_3_00",
    "Median_Cells_Texture_SumEntropy_Syto_5_00",
    "Median_Cells_Texture_SumVariance_Hoechst_10_00",
    "Median_Cells_Texture_SumVariance_Hoechst_3_00",
    "Median_Cells_Texture_SumVariance_Hoechst_5_00",
    "Median_Cells_Texture_SumVariance_Mito_10_00",
    "Median_Cells_Texture_SumVariance_Mito_3_00",
    "Median_Cells_Texture_SumVariance_Mito_5_00",
    "Median_Cells_Texture_SumVariance_Ph_golgi_10_00",
    "Median_Cells_Texture_SumVariance_Ph_golgi_3_00",
    "Median_Cells_Texture_SumVariance_Ph_golgi_5_00",
    "Median_Cells_Texture_SumVariance_Syto_3_00",
    "Median_Cells_Texture_SumVariance_Syto_5_00",
    "Median_Cells_Texture_Variance_Hoechst_10_00",
    "Median_Cells_Texture_Variance_Hoechst_3_00",
    "Median_Cells_Texture_Variance_Hoechst_5_00",
    "Median_Cells_Texture_Variance_Mito_10_00",
    "Median_Cells_Texture_Variance_Mito_3_00",
    "Median_Cells_Texture_Variance_Mito_5_00",
    "Median_Cells_Texture_Variance_Ph_golgi_10_00",
    "Median_Cells_Texture_Variance_Ph_golgi_3_00",
    "Median_Cells_Texture_Variance_Ph_golgi_5_00",
    "Median_Cells_Texture_Variance_Syto_10_00",
    "Median_Cells_Texture_Variance_Syto_3_00",
    "Median_Cells_Texture_Variance_Syto_5_00",
    "Median_Cytoplasm_AreaShape_Area",
    "Median_Cytoplasm_AreaShape_MinFeretDiameter",
    "Median_Cytoplasm_AreaShape_MinorAxisLength",
    "Median_Cytoplasm_AreaShape_Perimeter",
    "Median_Cytoplasm_Correlation_Correlation_ER_Ph_golgi",
    "Median_Cytoplasm_Correlation_Correlation_ER_Syto",
    "Median_Cytoplasm_Correlation_Correlation_Hoechst_ER",
    "Median_Cytoplasm_Correlation_Correlation_Hoechst_Mito",
    "Median_Cytoplasm_Correlation_Correlation_Hoechst_Ph_golgi",
    "Median_Cytoplasm_Correlation_Correlation_Hoechst_Syto",
    "Median_Cytoplasm_Correlation_Correlation_Mito_ER",
    "Median_Cytoplasm_Correlation_Correlation_Mito_Ph_golgi",
    "Median_Cytoplasm_Correlation_Correlation_Mito_Syto",
    "Median_Cytoplasm_Correlation_Correlation_Syto_Ph_golgi",
    "Median_Cytoplasm_Correlation_K_ER_Syto",
    "Median_Cytoplasm_Correlation_K_Hoechst_ER",
    "Median_Cytoplasm_Correlation_K_Hoechst_Mito",
    "Median_Cytoplasm_Correlation_K_Hoechst_Ph_golgi",
    "Median_Cytoplasm_Correlation_K_Hoechst_Syto",
    "Median_Cytoplasm_Correlation_K_Mito_Hoechst",
    "Median_Cytoplasm_Correlation_K_Mito_Syto",
    "Median_Cytoplasm_Correlation_K_Ph_golgi_Syto",
    "Median_Cytoplasm_Correlation_K_Syto_ER",
    "Median_Cytoplasm_Correlation_K_Syto_Hoechst",
    "Median_Cytoplasm_Correlation_K_Syto_Mito",
    "Median_Cytoplasm_Correlation_K_Syto_Ph_golgi",
    "Median_Cytoplasm_Correlation_Manders_ER_Hoechst",
    "Median_Cytoplasm_Correlation_Manders_ER_Syto",
    "Median_Cytoplasm_Correlation_Manders_Hoechst_Syto",
    "Median_Cytoplasm_Correlation_Manders_Mito_Hoechst",
    "Median_Cytoplasm_Correlation_Manders_Mito_Syto",
    "Median_Cytoplasm_Correlation_Manders_Ph_golgi_Hoechst",
    "Median_Cytoplasm_Correlation_Manders_Ph_golgi_Syto",
    "Median_Cytoplasm_Correlation_Manders_Syto_Hoechst",
    "Median_Cytoplasm_Correlation_Overlap_ER_Syto",
    "Median_Cytoplasm_Correlation_Overlap_Hoechst_ER",
    "Median_Cytoplasm_Correlation_Overlap_Hoechst_Mito",
    "Median_Cytoplasm_Correlation_Overlap_Hoechst_Ph_golgi",
    "Median_Cytoplasm_Correlation_Overlap_Mito_Ph_golgi",
    "Median_Cytoplasm_Correlation_Overlap_Mito_Syto",
    "Median_Cytoplasm_Correlation_Overlap_Syto_Ph_golgi",
    "Median_Cytoplasm_Correlation_RWC_ER_Hoechst",
    "Median_Cytoplasm_Correlation_RWC_ER_Mito",
    "Median_Cytoplasm_Correlation_RWC_Hoechst_Mito",
    "Median_Cytoplasm_Correlation_RWC_Hoechst_Ph_golgi",
    "Median_Cytoplasm_Correlation_RWC_Hoechst_Syto",
    "Median_Cytoplasm_Correlation_RWC_Mito_Hoechst",
    "Median_Cytoplasm_Correlation_RWC_Mito_Syto",
    "Median_Cytoplasm_Correlation_RWC_Ph_golgi_Hoechst",
    "Median_Cytoplasm_Correlation_RWC_Ph_golgi_Mito",
    "Median_Cytoplasm_Correlation_RWC_Ph_golgi_Syto",
    "Median_Cytoplasm_Correlation_RWC_Syto_Hoechst",
    "Median_Cytoplasm_Correlation_RWC_Syto_Mito",
    "Median_Cytoplasm_Granularity_1_Mito",
    "Median_Cytoplasm_Granularity_1_Ph_golgi",
    "Median_Cytoplasm_Granularity_1_Syto",
    "Median_Cytoplasm_Granularity_2_ER",
    "Median_Cytoplasm_Granularity_2_Mito",
    "Median_Cytoplasm_Granularity_2_Ph_golgi",
    "Median_Cytoplasm_Granularity_3_ER",
    "Median_Cytoplasm_Granularity_3_Mito",
    "Median_Cytoplasm_Granularity_3_Ph_golgi",
    "Median_Cytoplasm_Granularity_3_Syto",
    "Median_Cytoplasm_Granularity_4_Mito",
    "Median_Cytoplasm_Granularity_4_Ph_golgi",
    "Median_Cytoplasm_Granularity_5_Mito",
    "Median_Cytoplasm_Granularity_5_Ph_golgi",
    "Median_Cytoplasm_Intensity_IntegratedIntensity_Syto",
    "Median_Cytoplasm_Intensity_LowerQuartileIntensity_Mito",
    "Median_Cytoplasm_Intensity_MADIntensity_ER",
    "Median_Cytoplasm_Intensity_MADIntensity_Hoechst",
    "Median_Cytoplasm_Intensity_MADIntensity_Mito",
    "Median_Cytoplasm_Intensity_MADIntensity_Ph_golgi",
    "Median_Cytoplasm_Intensity_MADIntensity_Syto",
    "Median_Cytoplasm_Intensity_MaxIntensityEdge_Hoechst",
    "Median_Cytoplasm_Intensity_MaxIntensityEdge_Mito",
    "Median_Cytoplasm_Intensity_MaxIntensityEdge_Ph_golgi",
    "Median_Cytoplasm_Intensity_MaxIntensityEdge_Syto",
    "Median_Cytoplasm_Intensity_MaxIntensity_Hoechst",
    "Median_Cytoplasm_Intensity_MaxIntensity_Mito",
    "Median_Cytoplasm_Intensity_MaxIntensity_Ph_golgi",
    "Median_Cytoplasm_Intensity_MaxIntensity_Syto",
    "Median_Cytoplasm_Intensity_MeanIntensityEdge_Hoechst",
    "Median_Cytoplasm_Intensity_MeanIntensity_Mito",
    "Median_Cytoplasm_Intensity_MeanIntensity_Syto",
    "Median_Cytoplasm_Intensity_MedianIntensity_ER",
    "Median_Cytoplasm_Intensity_MedianIntensity_Mito",
    "Median_Cytoplasm_Intensity_MedianIntensity_Syto",
    "Median_Cytoplasm_Intensity_MinIntensityEdge_Mito",
    "Median_Cytoplasm_Intensity_MinIntensityEdge_Syto",
    "Median_Cytoplasm_Intensity_MinIntensity_Mito",
    "Median_Cytoplasm_Intensity_MinIntensity_Syto",
    "Median_Cytoplasm_Intensity_StdIntensityEdge_Hoechst",
    "Median_Cytoplasm_Intensity_StdIntensityEdge_Mito",
    "Median_Cytoplasm_Intensity_StdIntensityEdge_Ph_golgi",
    "Median_Cytoplasm_Intensity_StdIntensityEdge_Syto",
    "Median_Cytoplasm_Intensity_StdIntensity_Hoechst",
    "Median_Cytoplasm_Intensity_StdIntensity_Mito",
    "Median_Cytoplasm_Intensity_StdIntensity_Ph_golgi",
    "Median_Cytoplasm_Intensity_StdIntensity_Syto",
    "Median_Cytoplasm_Intensity_UpperQuartileIntensity_ER",
    "Median_Cytoplasm_Intensity_UpperQuartileIntensity_Mito",
    "Median_Cytoplasm_Intensity_UpperQuartileIntensity_Syto",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_ER_1of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_ER_2of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Mito_1of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Mito_2of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Mito_3of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Mito_4of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Ph_golgi_1of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Ph_golgi_2of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Ph_golgi_3of4",
    "Median_Cytoplasm_RadialDistribution_MeanFrac_Ph_golgi_4of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Mito_1of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Mito_2of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Mito_3of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Mito_4of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Ph_golgi_1of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Ph_golgi_2of4",
    "Median_Cytoplasm_RadialDistribution_RadialCV_Ph_golgi_3of4",
    "Median_Cytoplasm_Texture_AngularSecondMoment_ER_3_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_ER_5_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Mito_10_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Mito_3_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Mito_5_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Syto_10_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Syto_3_00",
    "Median_Cytoplasm_Texture_AngularSecondMoment_Syto_5_00",
    "Median_Cytoplasm_Texture_Contrast_ER_10_00",
    "Median_Cytoplasm_Texture_Contrast_ER_3_00",
    "Median_Cytoplasm_Texture_Contrast_ER_5_00",
    "Median_Cytoplasm_Texture_Contrast_Hoechst_10_00",
    "Median_Cytoplasm_Texture_Contrast_Hoechst_3_00",
    "Median_Cytoplasm_Texture_Contrast_Hoechst_5_00",
    "Median_Cytoplasm_Texture_Contrast_Mito_10_00",
    "Median_Cytoplasm_Texture_Contrast_Mito_3_00",
    "Median_Cytoplasm_Texture_Contrast_Mito_5_00",
    "Median_Cytoplasm_Texture_Contrast_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_Contrast_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_Contrast_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_Contrast_Syto_10_00",
    "Median_Cytoplasm_Texture_Contrast_Syto_5_00",
    "Median_Cytoplasm_Texture_Correlation_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_Correlation_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_Correlation_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_ER_3_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_ER_5_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Mito_10_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Mito_3_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Mito_5_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Syto_10_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Syto_3_00",
    "Median_Cytoplasm_Texture_DifferenceEntropy_Syto_5_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Mito_10_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Mito_3_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Mito_5_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Syto_10_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Syto_3_00",
    "Median_Cytoplasm_Texture_DifferenceVariance_Syto_5_00",
    "Median_Cytoplasm_Texture_Entropy_ER_3_00",
    "Median_Cytoplasm_Texture_Entropy_ER_5_00",
    "Median_Cytoplasm_Texture_Entropy_Mito_10_00",
    "Median_Cytoplasm_Texture_Entropy_Mito_3_00",
    "Median_Cytoplasm_Texture_Entropy_Mito_5_00",
    "Median_Cytoplasm_Texture_Entropy_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_Entropy_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_Entropy_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_Entropy_Syto_10_00",
    "Median_Cytoplasm_Texture_Entropy_Syto_3_00",
    "Median_Cytoplasm_Texture_Entropy_Syto_5_00",
    "Median_Cytoplasm_Texture_InfoMeas2_Syto_10_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_ER_10_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_ER_3_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_ER_5_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Mito_10_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Mito_3_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Mito_5_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Syto_10_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Syto_3_00",
    "Median_Cytoplasm_Texture_InverseDifferenceMoment_Syto_5_00",
    "Median_Cytoplasm_Texture_SumAverage_ER_10_00",
    "Median_Cytoplasm_Texture_SumAverage_Mito_10_00",
    "Median_Cytoplasm_Texture_SumAverage_Mito_3_00",
    "Median_Cytoplasm_Texture_SumAverage_Mito_5_00",
    "Median_Cytoplasm_Texture_SumAverage_Syto_10_00",
    "Median_Cytoplasm_Texture_SumAverage_Syto_3_00",
    "Median_Cytoplasm_Texture_SumAverage_Syto_5_00",
    "Median_Cytoplasm_Texture_SumEntropy_Mito_10_00",
    "Median_Cytoplasm_Texture_SumEntropy_Mito_3_00",
    "Median_Cytoplasm_Texture_SumEntropy_Mito_5_00",
    "Median_Cytoplasm_Texture_SumEntropy_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_SumEntropy_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_SumEntropy_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_SumEntropy_Syto_10_00",
    "Median_Cytoplasm_Texture_SumEntropy_Syto_3_00",
    "Median_Cytoplasm_Texture_SumEntropy_Syto_5_00",
    "Median_Cytoplasm_Texture_SumVariance_Hoechst_10_00",
    "Median_Cytoplasm_Texture_SumVariance_Hoechst_3_00",
    "Median_Cytoplasm_Texture_SumVariance_Hoechst_5_00",
    "Median_Cytoplasm_Texture_SumVariance_Mito_10_00",
    "Median_Cytoplasm_Texture_SumVariance_Mito_3_00",
    "Median_Cytoplasm_Texture_SumVariance_Mito_5_00",
    "Median_Cytoplasm_Texture_SumVariance_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_SumVariance_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_SumVariance_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_SumVariance_Syto_10_00",
    "Median_Cytoplasm_Texture_SumVariance_Syto_3_00",
    "Median_Cytoplasm_Texture_SumVariance_Syto_5_00",
    "Median_Cytoplasm_Texture_Variance_Hoechst_10_00",
    "Median_Cytoplasm_Texture_Variance_Hoechst_3_00",
    "Median_Cytoplasm_Texture_Variance_Hoechst_5_00",
    "Median_Cytoplasm_Texture_Variance_Mito_10_00",
    "Median_Cytoplasm_Texture_Variance_Mito_3_00",
    "Median_Cytoplasm_Texture_Variance_Mito_5_00",
    "Median_Cytoplasm_Texture_Variance_Ph_golgi_10_00",
    "Median_Cytoplasm_Texture_Variance_Ph_golgi_3_00",
    "Median_Cytoplasm_Texture_Variance_Ph_golgi_5_00",
    "Median_Cytoplasm_Texture_Variance_Syto_10_00",
    "Median_Cytoplasm_Texture_Variance_Syto_3_00",
    "Median_Cytoplasm_Texture_Variance_Syto_5_00",
    "Median_Nuclei_AreaShape_Area",
    "Median_Nuclei_AreaShape_MajorAxisLength",
    "Median_Nuclei_AreaShape_MaxFeretDiameter",
    "Median_Nuclei_AreaShape_Perimeter",
    "Median_Nuclei_AreaShape_Solidity",
    "Median_Nuclei_Correlation_Correlation_Hoechst_Syto",
    "Median_Nuclei_Correlation_Correlation_Mito_Ph_golgi",
    "Median_Nuclei_Correlation_Correlation_Mito_Syto",
    "Median_Nuclei_Correlation_K_Mito_Ph_golgi",
    "Median_Nuclei_Correlation_K_Ph_golgi_Mito",
    "Median_Nuclei_Correlation_K_Ph_golgi_Syto",
    "Median_Nuclei_Correlation_K_Syto_Ph_golgi",
    "Median_Nuclei_Correlation_Overlap_Hoechst_Mito",
    "Median_Nuclei_Correlation_Overlap_Hoechst_Ph_golgi",
    "Median_Nuclei_Correlation_Overlap_Hoechst_Syto",
    "Median_Nuclei_Correlation_RWC_Mito_Syto",
    "Median_Nuclei_Correlation_RWC_Syto_Mito",
    "Median_Nuclei_Granularity_1_Mito",
    "Median_Nuclei_Granularity_2_Hoechst",
    "Median_Nuclei_Granularity_3_Mito",
    "Median_Nuclei_Granularity_3_Ph_golgi",
    "Median_Nuclei_Granularity_4_Mito",
    "Median_Nuclei_Intensity_LowerQuartileIntensity_Hoechst",
    "Median_Nuclei_Intensity_MADIntensity_Hoechst",
    "Median_Nuclei_Intensity_MADIntensity_Mito",
    "Median_Nuclei_Intensity_MaxIntensityEdge_Hoechst",
    "Median_Nuclei_Intensity_MaxIntensityEdge_Mito",
    "Median_Nuclei_Intensity_MaxIntensityEdge_Ph_golgi",
    "Median_Nuclei_Intensity_MaxIntensityEdge_Syto",
    "Median_Nuclei_Intensity_MaxIntensity_Hoechst",
    "Median_Nuclei_Intensity_MaxIntensity_Mito",
    "Median_Nuclei_Intensity_MaxIntensity_Ph_golgi",
    "Median_Nuclei_Intensity_MeanIntensityEdge_Hoechst",
    "Median_Nuclei_Intensity_MeanIntensity_Hoechst",
    "Median_Nuclei_Intensity_MedianIntensity_Hoechst",
    "Median_Nuclei_Intensity_MinIntensityEdge_Hoechst",
    "Median_Nuclei_Intensity_MinIntensity_Hoechst",
    "Median_Nuclei_Intensity_StdIntensityEdge_Mito",
    "Median_Nuclei_Intensity_StdIntensityEdge_Ph_golgi",
    "Median_Nuclei_Intensity_StdIntensityEdge_Syto",
    "Median_Nuclei_Intensity_StdIntensity_Mito",
    "Median_Nuclei_Intensity_StdIntensity_Ph_golgi",
    "Median_Nuclei_Intensity_UpperQuartileIntensity_Hoechst",
    "Median_Nuclei_RadialDistribution_FracAtD_Ph_golgi_3of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Mito_1of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Mito_2of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Mito_3of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Mito_4of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Ph_golgi_2of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Ph_golgi_3of4",
    "Median_Nuclei_RadialDistribution_MeanFrac_Ph_golgi_4of4",
    "Median_Nuclei_Texture_AngularSecondMoment_Hoechst_10_00",
    "Median_Nuclei_Texture_AngularSecondMoment_Hoechst_3_00",
    "Median_Nuclei_Texture_AngularSecondMoment_Hoechst_5_00",
    "Median_Nuclei_Texture_AngularSecondMoment_Mito_3_00",
    "Median_Nuclei_Texture_AngularSecondMoment_Mito_5_00",
    "Median_Nuclei_Texture_Contrast_Hoechst_3_00",
    "Median_Nuclei_Texture_Contrast_Hoechst_5_00",
    "Median_Nuclei_Texture_Contrast_Mito_10_00",
    "Median_Nuclei_Texture_Contrast_Mito_3_00",
    "Median_Nuclei_Texture_Contrast_Mito_5_00",
    "Median_Nuclei_Texture_Contrast_Ph_golgi_10_00",
    "Median_Nuclei_Texture_Contrast_Ph_golgi_3_00",
    "Median_Nuclei_Texture_Contrast_Ph_golgi_5_00",
    "Median_Nuclei_Texture_Correlation_Ph_golgi_3_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Hoechst_10_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Hoechst_3_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Hoechst_5_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Mito_10_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Mito_3_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Mito_5_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Ph_golgi_3_00",
    "Median_Nuclei_Texture_DifferenceEntropy_Ph_golgi_5_00",
    "Median_Nuclei_Texture_DifferenceVariance_Hoechst_10_00",
    "Median_Nuclei_Texture_DifferenceVariance_Hoechst_3_00",
    "Median_Nuclei_Texture_DifferenceVariance_Hoechst_5_00",
    "Median_Nuclei_Texture_DifferenceVariance_Mito_3_00",
    "Median_Nuclei_Texture_DifferenceVariance_Mito_5_00",
    "Median_Nuclei_Texture_DifferenceVariance_Ph_golgi_10_00",
    "Median_Nuclei_Texture_DifferenceVariance_Ph_golgi_3_00",
    "Median_Nuclei_Texture_DifferenceVariance_Ph_golgi_5_00",
    "Median_Nuclei_Texture_Entropy_Hoechst_3_00",
    "Median_Nuclei_Texture_Entropy_Hoechst_5_00",
    "Median_Nuclei_Texture_Entropy_Mito_10_00",
    "Median_Nuclei_Texture_Entropy_Mito_3_00",
    "Median_Nuclei_Texture_Entropy_Mito_5_00",
    "Median_Nuclei_Texture_InfoMeas2_Hoechst_10_00",
    "Median_Nuclei_Texture_InfoMeas2_Mito_10_00",
    "Median_Nuclei_Texture_InfoMeas2_Ph_golgi_10_00",
    "Median_Nuclei_Texture_InfoMeas2_Ph_golgi_3_00",
    "Median_Nuclei_Texture_InfoMeas2_Ph_golgi_5_00",
    "Median_Nuclei_Texture_InverseDifferenceMoment_Hoechst_10_00",
    "Median_Nuclei_Texture_InverseDifferenceMoment_Hoechst_3_00",
    "Median_Nuclei_Texture_InverseDifferenceMoment_Hoechst_5_00",
    "Median_Nuclei_Texture_InverseDifferenceMoment_Mito_10_00",
    "Median_Nuclei_Texture_InverseDifferenceMoment_Mito_3_00",
    "Median_Nuclei_Texture_InverseDifferenceMoment_Mito_5_00",
    "Median_Nuclei_Texture_SumAverage_Hoechst_10_00",
    "Median_Nuclei_Texture_SumAverage_Hoechst_3_00",
    "Median_Nuclei_Texture_SumAverage_Hoechst_5_00",
    "Median_Nuclei_Texture_SumEntropy_Mito_10_00",
    "Median_Nuclei_Texture_SumEntropy_Mito_3_00",
    "Median_Nuclei_Texture_SumEntropy_Mito_5_00",
    "Median_Nuclei_Texture_SumEntropy_Ph_golgi_10_00",
    "Median_Nuclei_Texture_SumEntropy_Ph_golgi_3_00",
    "Median_Nuclei_Texture_SumEntropy_Ph_golgi_5_00",
    "Median_Nuclei_Texture_SumVariance_Mito_10_00",
    "Median_Nuclei_Texture_SumVariance_Mito_3_00",
    "Median_Nuclei_Texture_SumVariance_Mito_5_00",
    "Median_Nuclei_Texture_SumVariance_Ph_golgi_10_00",
    "Median_Nuclei_Texture_SumVariance_Ph_golgi_3_00",
    "Median_Nuclei_Texture_SumVariance_Ph_golgi_5_00",
    "Median_Nuclei_Texture_Variance_Mito_10_00",
    "Median_Nuclei_Texture_Variance_Mito_3_00",
    "Median_Nuclei_Texture_Variance_Mito_5_00",
    "Median_Nuclei_Texture_Variance_Ph_golgi_10_00",
    "Median_Nuclei_Texture_Variance_Ph_golgi_3_00",
    "Median_Nuclei_Texture_Variance_Ph_golgi_5_00",
]

# Calculate XTICKS for the default feature set
x = 1
XTICKS = [x]
for comp in ["Median_Cytoplasm", "Median_Nuclei"]:
    for idx, p in enumerate(ACT_PROF_FEATURES[x:], 1):
        if p.startswith(comp):
            XTICKS.append(idx + x)
            x += idx
            break
XTICKS.append(len(ACT_PROF_FEATURES))

def is_close(a: float, b: float, abs_tol: float = 1e-6) -> bool:
    return abs(a - b) < abs_tol

def meta_data(df: pd.DataFrame) -> List[str]:
    """Returns the list of columns in the DataFrame that do *not* contain Cell Painting data."""
    return [x for x in df if not x.startswith("Median_")]

def feature_data(df: pd.DataFrame) -> List[str]:
    """Returns the list of columns in the DataFrame that *do* contain Cell Painting data."""
    return [x for x in df if x.startswith("Median_")]

def profile_sim(
    prof1: Iterable[float],
    prof2: Iterable[float],
) -> float:
    """Calculates the similarity of two activity_profiles of the same length.
    The profiles are compared by distance correlation
    ``scipy.spatial.distance.correlation()`` (same as Pearson correlation).
    The profile values are capped to +/-25.0 (np.clip() function).

    Parameters:
    ===========
        prof1: The first profile to compare.
        prof2: The second profile to compare.
            The two profiles have to be of equal length.

    Returns:
    ========
        Similarity value between 0.0 .. 1.0 (0.0 being very dissimilar and 1.0 identical)."""

    assert len(prof1) == len(
        prof2
    ), "Activity Profiles must have the same length to be compared."

    if not isinstance(prof1, np.ndarray):
        prof1 = np.array(prof1)
    prof1 = np.clip(prof1, -25.0, 25.0)
    if not isinstance(prof2, np.ndarray):
        prof2 = np.array(prof2)
    prof2 = np.clip(prof2, -25.0, 25.0)
    result = 1 - dist.correlation(prof1, prof2)
    if np.isnan(result) or result < 0.0:
        result = 0.0
    return result

def well_id_similarity(df: pd.DataFrame, well_id1: str, well_id2: str) -> float:
    """Calculate the similarity of the activity profiles from two compounds
    (identified by `Well_Id`). Returns value between 0 .. 1"""
    act1 = df[df["Well_Id"] == well_id1][ACT_PROF_FEATURES].values[0]
    act2 = df[df["Well_Id"] == well_id2][ACT_PROF_FEATURES].values[0]
    return round(profile_sim(act1, act2), 3)

def find_similar(
    df: pd.DataFrame,
    act_profile: Iterable[float],
    cutoff=75.0,
    max_num=5,
    features=ACT_PROF_FEATURES,
):
    """Filter the dataframe for activity profiles similar to the given one.
    `cutoff` gives the similarity threshold in percent, default is 75.
    The calculated similarity is added to the result DataFrame as a new column.
    The similarity value is given in percent (0.0 .. 100.0) in this case.

    Parameters:
    ===========
    df: pd.DataFrame
        The dataframe to filter.
    act_profile: Iterable[float]
        The activity profile to compare to.
    cutoff: float
        The similarity threshold.
    max_num: int
        The maximum number of results to return.
    features: List[str]
        The features to use for the similarity calculation.

    Returns a Pandas DF with the most similar entries (similarity in percent) or None when no similars are found."""

    act_features = features.copy()
    assert len(act_features) > 0
    decimals = {"Similarity": 1}
    if not isinstance(act_profile, np.ndarray):
        act_profile = np.array(act_profile)

    result = df.copy()
    # Pandas black belt!! :
    result["Similarity"] = result[act_features].apply(
        lambda x: profile_sim(
            x,
            act_profile,
        )
        * 100.0,
        axis=1,
    )
    result = result[result["Similarity"] >= cutoff]
    if len(result) == 0:
        return None
    result = result.sort_values("Similarity", ascending=False).head(max_num)
    result = result.round(decimals)
    return result

@functools.lru_cache
def get_func_cluster_names(prefix="") -> Optional[List[str]]:
    """Extract the cluster names from the median profile file names.
    If a `prefix` is given, it will be put in front of the names.
    Returns an alphabetically sorted list of cluster names."""
    # print(str(Path(__file__).absolute().))
    mask_files = glob(op.join(OUTPUT_DIR, "med_prof_*.tsv"))
    clusters = sorted([prefix + op.basename(x)[9:-4] for x in mask_files])
    if len(clusters) == 0:
        print(f"No clusters found in {OUTPUT_DIR}.")
        return None
    return clusters

@functools.lru_cache
def get_func_cluster_features(cluster: str, include_well_id=True) -> pd.DataFrame:
    """Extract the cluster features from the median profile files.

    Returns:
        a DataFrame WITH THE (artificial) Well_Id of the cluster
        AND the features (default).
        Set `include_well_id=False` to get the features without the Well_Id.

    Raises:
        FileNotFoundError: when the cluster feature file is not found."""
    parm_file = op.join(OUTPUT_DIR, f"med_prof_{cluster}.tsv")
    try:
        cl_parms = pd.read_csv(parm_file, sep="\t")
    except FileNotFoundError:
        raise FileNotFoundError(f"Cluster {cluster} not found in {OUTPUT_DIR}.")
    if not include_well_id:
        cl_parms = cl_parms.drop("Well_Id", axis=1)
    return cl_parms

def add_func_clusters(df: pd.DataFrame) -> pd.DataFrame:
    """Add the similarities to the functional clusters to the dataframe.

    Raises:
        ValueError: when no functional cluster definitions are found.
        FloatingPointError: when the similarity value could not be compared for equality.
            This is a safe guard, because comparing floats for equality is not reliable.

    Returns:
        The dataframe with the functional cluster similarities added."""

    def calc_sim(series, prof1, features):
        prof2 = series[features].values.astype("float64")
        result = round(100 * profile_sim(prof1, prof2), 1)
        return result

    func_clusters = get_func_cluster_names(prefix="")
    if func_clusters is None:
        raise ValueError("No functional clusters found.")

    result = df.copy()
    for cl in func_clusters:
        med_prof = pd.read_csv(op.join(OUTPUT_DIR, f"med_prof_{cl}.tsv"), sep="\t")
        cl_feat = sorted([x for x in med_prof.keys() if x.startswith("Median_")])
        prof1 = med_prof[cl_feat].values[0]
        assert len(cl_feat) == len(prof1)

        # [1] This line only works when the dataset is a Pandas DF.
        # If it is a Dask DF all Clusters get the same similarity value (of Uncoupler)
        result[f"Cluster_{cl}"] = result.apply(
            lambda s: calc_sim(s, prof1, cl_feat), axis=1
        )

    # Find the cluster with the highest Sim for each compound:
    clusters = [f"Cluster_{cl}" for cl in func_clusters]
    most_sim = {"Well_Id": [], "Cluster_High": [], "Cluster_Sim": []}
    for _, rec in result.iterrows():
        sim = rec[clusters].max()
        for cl in clusters:
            if is_close(rec[cl], sim):
                break
        else:
            # Fail-safe for comparing floats for equality.
            raise FloatingPointError(f"Could not find Sim {sim}.")
        most_sim["Well_Id"].append(rec["Well_Id"])
        most_sim["Cluster_High"].append(cl[8:])
        most_sim["Cluster_Sim"].append(sim)

    result = result.merge(pd.DataFrame(most_sim), on="Well_Id", how="left")
    return result

def cluster_features(df: pd.DataFrame, fraction: float):
    """The list of parameters that defines a cluster.

    Parameters:
    ===========
    df: pd.DataFrame
        The dataframe to select the features from.
    fraction: float
        The fraction of feature values that need to point in the same direction
        in order to be selected for the cluster.

    Returns: a list of selected feature names.
    """
    df_len = len(df)
    result = []
    for feat in ACT_PROF_FEATURES:
        count_plus = int((df[feat] >= 0.0).sum())
        count_minus = int((df[feat] < 0.0).sum())
        value = max(count_plus, count_minus) / df_len
        if value >= fraction:
            result.append(feat)
    return result

def remaining_features(cl_feat: Iterable[str]) -> List[str]:
    """Returns the list of features after removing the cluster-defining features from the full profile."""
    feat_set = set(cl_feat)
    result = [x for x in ACT_PROF_FEATURES if x not in feat_set]
    return result

def calc_median_profile(
    df: pd.DataFrame, cl_feat: List[str], cl_name: Union[str] = None
) -> pd.DataFrame:
    """Calculate the median profile of a cluster.

    Parameters:
    ===========
    df: pd.DataFrame
        The dataframe containing the cluster measurements.
    cl_feat: List[str]
        The list of features to use for the median profile.
    cl_name: Union[str]
        The optional name of the cluster.
        If given, the name will be used as the Well_Id.
        If not given, the Well_Id will be set to "Unknown".

    Returns: A DataFrame with the median profile and the cluster name as Well_Id.
    """
    cl_name = cl_name if cl_name is not None else "Unknown"
    med_prof = df[cl_feat].median().values
    df_mp = pd.DataFrame(data=(med_prof,), columns=cl_feat)
    df_mp["Well_Id"] = cl_name
    return df_mp

def heat_mpl(
    df,
    id_prop="Compound_Id",
    cmap="bwr",
    show=True,
    sort_parm=False,
    **kwargs,
):
    """Generates a heatmap of the Cell Painting profiles.

    Parameters:
    ===========
    df: pd.DataFrame
        The dataframe to plot.
    id_prop: str
        The column to use for labeling the rows in the plot.
    cmap: str
        The colormap to use for the heatmap (default: "bwr").
    show: bool
        Whether to show the plot or not (default: True).
        When False, the plot is saved to disk (see kwarg `save_to_file`)

    Keyword arguments:
    ==================
    colorbar: bool
        Whether to show the color bar or not (default: True)
    biosim: bool
        Whether to show the biological similarity (default: False)
    show_ind: bool
        Whether to show the Induction (Activity) or not (default: False)
    color_range: int
        The value used for the color range legend (default: 15)
    img_size: Optional[int]
        The size of the image (default: None)
    features: List[str]
        The features to use for the heatmap.
    save_to_file: Union[str, List[str]]
        Save the plot as file, requires `show`=False (default: "heatmap.png")
        A single file name or a list of file names can be given
        (e.g. when the plot should be saved in multiple formats).
    rcparams: dict
        Parameters mapped to matplotlib.rcParams
    """
    # not assigned to individual variables:
    #   colorbar
    biosim = kwargs.get("biosim", False)
    show_ind = kwargs.get("show_ind", False)
    color_range = kwargs.get("color_range", 15)
    img_size = kwargs.get("img_size", None)
    features = kwargs.get("features", None)
    save_to_file = kwargs.get("save_to_file", "heatmap.png")

    if features is None:
        features = ACT_PROF_FEATURES

    # Re-calculate XTICKS for non-default parameter sets
    if len(features) == len(ACT_PROF_FEATURES):
        xticks = XTICKS  # global var
    else:
        print("  - Re-calculating xticks...")
        # get positions of the compartments in the list of features
        x = 1
        xticks = [x]
        for comp in ["Median_Cytoplasm", "Median_Nuclei"]:
            for idx, p in enumerate(features[x:], 1):
                if p.startswith(comp):
                    xticks.append(idx + x)
                    x += idx
                    break
        xticks.append(len(features))

    df_len = len(df)
    if img_size is None:  # set defaults when no img_size is given
        if show:
            img_size = 15.0
            if biosim:
                img_size += 0.5
            if show_ind:
                img_size += 0.5
            if id_prop == "Well_Id":
                img_size += 1.0
        else:
            img_size = 19.0
    plt.style.use("seaborn-white")
    plt.style.use("seaborn-pastel")
    plt.style.use("seaborn-talk")
    plt.rcParams["axes.labelsize"] = 25
    if "rcparams" in kwargs:
        plt.rcParams.update(kwargs["rcparams"])

    if df_len == 1:
        height = 0.5
    elif df_len == 2:
        height = 2.0
    else:
        height = 1.1 + 0.35 * df_len
    plt.rcParams["figure.figsize"] = (img_size, height)
    plt.rcParams["axes.labelsize"] = 25
    plt.rcParams["ytick.labelsize"] = 20
    plt.rcParams["xtick.labelsize"] = 15
    fs_text = 18

    y_labels = []
    fp_list = []
    max_val = color_range  # using a fixed color range now
    min_val = -color_range
    ylabel_templ = "{}{}{}"
    ylabel_bs = ""
    ylabel_ind = ""
    id_prop_list = []
    fp = []
    for ctr, (_, rec) in enumerate(df.iterrows()):
        parm_list = features
        fp = [rec[x] for x in features]
        fp_view = [rec[x] for x in parm_list]
        fp_list.append(fp_view)
        id_prop_list.append(rec[id_prop])
        if biosim:
            if ctr == 0:
                prof_ref = fp
                ylabel_bs = "   --  |  "
            else:
                sim = profile_sim(prof_ref, fp) * 100
                ylabel_bs = "{:3.0f} |  ".format(sim)
        if show_ind:
            ylabel_ind = "{:3.0f} |  ".format(rec["Activity"])

        ylabel = ylabel_templ.format(ylabel_bs, ylabel_ind, rec[id_prop])
        y_labels.append(ylabel)

    # invert y axis:
    y_labels = y_labels[::-1]
    fp_list = fp_list[::-1]
    Z = np.asarray(fp_list)
    plt.xticks(xticks)
    plt.yticks(np.arange(df_len) + 0.5, y_labels)
    plt.pcolor(Z, vmin=min_val, vmax=max_val, cmap=cmap)
    plt.text(
        xticks[1] // 2, -1.1, "Cells", horizontalalignment="center", fontsize=fs_text
    )
    plt.text(
        xticks[1] + ((xticks[2] - xticks[1]) // 2),
        -1.1,
        "Cytoplasm",
        horizontalalignment="center",
        fontsize=fs_text,
    )
    plt.text(
        xticks[2] + ((xticks[3] - xticks[2]) // 2),
        -1.1,
        "Nuclei",
        horizontalalignment="center",
        fontsize=fs_text,
    )
    if kwargs.get("colorbar", True) and len(df) > 3:
        plt.colorbar()
    plt.tight_layout()
    if show:
        plt.show()
    else:
        if not isinstance(save_to_file, list):
            save_to_file = [save_to_file]
        for fn in save_to_file:
            plt.savefig(fn, bbox_inches="tight")

ext: py

reason: import numpy,import scipy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Created on Thu Feb 26 15:20:00 2022
CLI main file for Augmented Stacking:
The main file is used to run the augmented stacking algorithm to guide an operator in
correctly placing stones following a stacking algorithm and an AR system composed by a
projector and 3D camera (ZEDi2)
"""

import os
import sys
import subprocess
from threading import Thread

# # The package opencv is not found in virtual env, solve
# # the issue without importing the sys.path here. With this it works
sys.path.append('/usr/local/lib/python3.8/dist-packages')
import cv2

import dataset_IO
import stacking_algorithm
import distance_map
from util import terminal
import camera_capture
import matplotlib.pyplot as plt
import numpy as np
import open3d as o3d
import colorama as cr

# Path where the landscape is overwritten
_name_landscape = './landscape.ply'

# Set a target of mesh faces for the low-res mesh
_faces_target_stone_mesh = 500

# Set a target of max vertices for the captured scene mesh
_vertices_target_low_res_scene = 2000

# Set dir to store validated stone and landscape of each captured
DIR_AS_BUILT_ASSEMBLY = './as_built_status/'

# Stone poses recorder path
stone_pose_record_path = './temp/placedStones.txt'

def main():
    # -----------------------------------------------------------------------
    # [0] Decorator
    # -----------------------------------------------------------------------

    terminal.cat('./logo/logoascii')
    terminal.cat('./logo/credits.txt')

    print('Before to start make sure that:')
    print('-> You have done a calibration')
    print('-> You have a second screen with the same resolution as your main machine')

    # -----------------------------------------------------------------------
    # [1] Create as-built dir + start 3D visualizer
    # -----------------------------------------------------------------------

    # Create sub-dir for current session
    dir_as_built = dataset_IO.create_record_session_subdir(DIR_AS_BUILT_ASSEMBLY)

    # Set stone stacked counter for recording
    STONE_COUNTER = 0

    # Launch non-blocking visualizer
    subprocess.Popen(['python', './util/non_block_visualizer.py'])

    # ADD-ON: clean out preavious stone center record
    record_stone_file = './temp/placedStones.txt'
    dataset_IO.delete_file(record_stone_file)
    terminal.custom_print("[INFO]: stone record pose delted.")

    while(True):  # For each stone - loop

        while(True):  # Guide the pose - loop

            while(True):  # Find the pose - loop

                # -----------------------------------------------------------------------
                # [2] Download the stone mesh from dataset
                # -----------------------------------------------------------------------

                # Ask for stone label & download mesh and open it
                name_stone_mesh, stone_mesh = dataset_IO.download_github_raw_file()

                # Check the n faces for the download mesh if not downsample
                faces_stone_mesh = len(np.asarray(stone_mesh.triangles))
                if (faces_stone_mesh > _faces_target_stone_mesh):
                    terminal.custom_print(f"The mesh needs to be decimate for the stacking algorithms.\n"
                                        f" Number of vertices for downloaded mesh: {faces_stone_mesh}")
                    stone_mesh = stone_mesh.simplify_quadric_decimation(
                        target_number_of_triangles=_faces_target_stone_mesh)
                    new_faces_stone_mesh = len(np.asarray(stone_mesh.triangles))
                    terminal.custom_print(f" Number of vertices of the decimated mesh: {new_faces_stone_mesh}")

                # Check if the mesh is out of scale or not watertight, and confirm
                try:
                    if stone_mesh.get_volume() > 1.0:  # 1m3
                        stone_mesh = stone_mesh.scale(scale=1/1000,
                                                    center=stone_mesh.get_center())
                    break
                except:
                    terminal.error_print("ERROR: the imported mesh is corrupted or not water tight.\n"
                                        "Choose another stone from dataset ...")
                    continue

            # Write out the mesh (for algorithm to read)
            o3d.io.write_triangle_mesh(name_stone_mesh, stone_mesh)

            # -----------------------------------------------------------------------
            # [3] Capture the scene mesh + compute the mesh 6dof pose
            # -----------------------------------------------------------------------

            # Capture meshed scene
            landscape_mesh = camera_capture.get_mesh_scene(
                _vertices_target_low_res_scene)

            # # Save meshed scene
            terminal.custom_print("Writing out the captured mesh from 3d camera")
            o3d.io.write_triangle_mesh(
                _name_landscape, landscape_mesh, write_ascii=True)
            terminal.custom_print(f"Mesh out with name {_name_landscape}")

            # Compute stacking pose
            stacking_algorithm.compute(path_exec='./stacking_algorithm/build/main',
                                       path_mesh=name_stone_mesh,
                                       path_landscape=_name_landscape,  # TODO: param this
                                       config_file='./stacking_algorithm/data/input.txt',
                                       name_output='pose',
                                       dir_output='./temp')

            # Delete donwloaded mesh
            dataset_IO.delete_file(name_stone_mesh)

            # read the 6dof pose and store it as numpy array
            pose_matrix = dataset_IO.read_pose_6dof('./temp/pose.txt')
            print("Computation done: here's the computed 4x4 Pose Matrix:\n\n", pose_matrix)

            # Transform the low-res mesh for visualization
            stone_mesh.transform(pose_matrix)

            # Temporary store landscape and stone for visualization
            terminal.custom_print("Temporary storing the landscape and stone mesh before validation ...")
            STONE_COUNTER, path_stone_mesh, path_landscape_mesh = dataset_IO.save_current_built_layer(landscape_mesh=landscape_mesh,
                                                                                stone_mesh=stone_mesh,
                                                                                stone_label=name_stone_mesh,
                                                                                sub_dir=dir_as_built,
                                                                                stone_counter=STONE_COUNTER)

            # Merge transformed stone and landscape mesh
            merged_landscape = stone_mesh + landscape_mesh

            break

        # -----------------------------------------------------------------------
        # [4] Augmented feedback loop
        # -----------------------------------------------------------------------

        # First open the camera and close at the end
        zed, point_cloud = camera_capture.set_up_zed()

        # Create the 3D space model
        Live_3D_space = camera_capture.Live_3D_space(rock_mesh=stone_mesh,zed=zed,point_cloud=point_cloud)

        # Create image object
        image_drawer = camera_capture.Image_drawer(Live_3D_space=Live_3D_space)

        # Start live 2D projection to guide user to stone placement
        terminal.custom_print(f"When the stone is placed correnctly, Press <Escape> to close the tkinter window.")
        live_stream = camera_capture.Live_stream(Live_3D_space=Live_3D_space,image_drawer=image_drawer)
        live_stream.run()

        # Now that the loop is closed, close the camera
        zed.close()

        # Ask to validate the stone and store current layer results
        i_is_pose_stone_valid = None
        while not i_is_pose_stone_valid in ['Y','y','n','N']:
            i_is_pose_stone_valid = terminal.user_input('Do you validate the stone position? (y/n)\n>>> ')

        if i_is_pose_stone_valid not in ['Y', 'y']:
            # If the stones are not validated erase them from recording folder
            terminal.custom_print("Erasing the stone and scene from recording folder")
            os.remove(path_stone_mesh)
            os.remove(path_landscape_mesh)

            # Erase stone's pose record
            dataset_IO.erase_last_pose_stone(stone_pose_record_path)
            terminal.custom_print("[INFO]: The current stone pose is erased from the algorithm pose recorder")

        # Ask to place another stone or terminate CLI
        i_continue_stacking = None
        while not i_continue_stacking in ['Y','y','n','N']:
            i_continue_stacking = terminal.user_input('Do you want to stack another stone? (y/n)\n>>> ')
        if i_continue_stacking in ['N', 'n']:
            terminal.custom_print("The augmented stacking is shuting down ...")
            exit()
        terminal.custom_print("Validating the stone and scene in recording folder ...")

if __name__ == "__main__":
    main()

max_stars_repo_name: ibois-epfl/augmented-stacking

max_issues_repo_name: ibois-epfl/augmented-stacking

max_forks_repo_name: ibois-epfl/augmented-stacking

reason: import numpy


text:
import numpy as np

import paddle
import paddle.nn as nn

from paddle.nn.initializer import TruncatedNormal, KaimingNormal, Constant, Assign

# Common initializations
ones_ = Constant(value=1.)
zeros_ = Constant(value=0.)
kaiming_normal_ = KaimingNormal()
trunc_normal_ = TruncatedNormal(std=.02)

def orthogonal_(tensor, gain=1):
    r"""Fills the input `Tensor` with a (semi) orthogonal matrix, as
    described in `Exact solutions to the nonlinear dynamics of learning in deep
    linear neural networks` - Saxe, A. et al. (2013). The input tensor must have
    at least 2 dimensions, and for tensors with more than 2 dimensions the
    trailing dimensions are flattened.
    Args:
        tensor: an n-dimensional `torch.Tensor`, where :math:`n \geq 2`
        gain: optional scaling factor
    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.orthogonal_(w)
    """
    if tensor.ndimension() < 2:
        raise ValueError(
            "Only tensors with 2 or more dimensions are supported")

    if  paddle.fluid.data_feeder.convert_dtype(tensor.dtype) != 'float32':
        raise ValueError(
            "Only tensors in float32 dtype are supported")

    rows = tensor.shape[0]
    cols = tensor.numel() // rows
    flattened = np.random.randn(rows, cols).astype('float32')

    if rows < cols:
        flattened = flattened.T

    # Compute the qr factorization
    q, r = np.linalg.qr(flattened)

    # Make Q uniform according to https://arxiv.org/pdf/math-ph/0609050.pdf
    d = np.diag(r, 0)
    ph = paddle.sign(paddle.to_tensor(d))
    q = paddle.to_tensor(q) * ph

    if rows < cols:
        q.t()

    with paddle.no_grad():
        tensor.reshape(q.shape).set_value(q * gain)

    return tensor

# Common Functions
def load_model(model, url):
    path = paddle.utils.download.get_weights_path_from_url(url)
    model.set_state_dict(paddle.load(path))
    return model

def to_2tuple(x):
    return tuple([x] * 2)

def add_parameter(layer, datas, name=None):
    parameter = layer.create_parameter(
        shape=(datas.shape),
        default_initializer=Assign(datas)
    )
    if name:
        layer.add_parameter(name, parameter)
    return parameter

# Common Layers
def drop_path(x, drop_prob=0., training=False):
    """
        Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
        the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
        See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ...
    """
    if drop_prob == 0. or not training:
        return x
    keep_prob = paddle.to_tensor(1 - drop_prob)
    shape = (paddle.shape(x)[0], ) + (1, ) * (x.ndim - 1)
    random_tensor = keep_prob + paddle.rand(shape, dtype=x.dtype)
    random_tensor = paddle.floor(random_tensor)  # binarize
    output = x.divide(keep_prob) * random_tensor
    return output

class DropPath(nn.Layer):
    def __init__(self, drop_prob=None):
        super(DropPath, self).__init__()
        self.drop_prob = drop_prob

    def forward(self, x):
        return drop_path(x, self.drop_prob, self.training)

class Identity(nn.Layer):
    def __init__(self):
        super(Identity, self).__init__()

    def forward(self, input):
        return input

class Mlp(nn.Layer):
    def __init__(self,
                 in_features,
                 hidden_features=None,
                 out_features=None,
                 act_layer=nn.GELU,
                 drop=0.):
        super().__init__()
        out_features = out_features or in_features
        hidden_features = hidden_features or in_features
        self.fc1 = nn.Linear(in_features, hidden_features)
        self.act = act_layer()
        self.fc2 = nn.Linear(hidden_features, out_features)
        self.drop = nn.Dropout(drop)

    def forward(self, x):
        x = self.fc1(x)
        x = self.act(x)
        x = self.drop(x)
        x = self.fc2(x)
        x = self.drop(x)
        return x

class Attention(nn.Layer):
    def __init__(self,
                 dim,
                 num_heads=8,
                 qkv_bias=False,
                 qk_scale=None,
                 attn_drop=0.,
                 proj_drop=0.):
        super().__init__()
        self.num_heads = num_heads
        head_dim = dim // num_heads
        self.scale = qk_scale or head_dim**-0.5

        self.qkv = nn.Linear(dim, dim * 3, bias_attr=qkv_bias)
        self.attn_drop = nn.Dropout(attn_drop)
        self.proj = nn.Linear(dim, dim)
        self.proj_drop = nn.Dropout(proj_drop)

    def forward(self, x):
        # B= paddle.shape(x)[0]
        N, C = x.shape[1:]
        qkv = self.qkv(x).reshape((-1, N, 3, self.num_heads, C //
                                   self.num_heads)).transpose((2, 0, 3, 1, 4))
        q, k, v = qkv[0], qkv[1], qkv[2]

        attn = (q.matmul(k.transpose((0, 1, 3, 2)))) * self.scale
        attn = nn.functional.softmax(attn, axis=-1)
        attn = self.attn_drop(attn)

        x = (attn.matmul(v)).transpose((0, 2, 1, 3)).reshape((-1, N, C))
        x = self.proj(x)
        x = self.proj_drop(x)
        return x

class Unfold(nn.Layer):
    '''
    Fix the bug of nn.Unfold
    Will be updated sonn.
    '''
    def __init__(self,
                 kernel_size,
                 stride=1,
                 padding=0,
                 dilation=1,
                 name=None):
        super(Unfold, self).__init__()

        self.kernel_size = kernel_size
        self.dilation = dilation
        self.padding = padding
        self.stride = stride
        self.name = name

    def forward(self, input):
        return nn.functional.unfold(
            input, self.kernel_size, self.stride,
            self.padding, self.dilation, self.name
        )


text:
import numpy as np
from .sim import Game
from itertools import permutations

def get_scores(players):
    game = Game(players)
    game.run()

    return [player.score() for player in game.player_states]

def get_n_scores(players, n):
    return np.array([get_scores(players) for i in range(n)])

def get_num_wins(players, n):
    wins = np.zeros(len(players), dtype=np.int64)

    for i in range(n):
        game = Game(players)
        game.run()
        wins[game.get_winners()] += 1

    return wins

def get_num_wins_balanced(players, n):
    wins = np.zeros(len(players), dtype=np.int64)

    for perm in permutations(list(range(len(players)))):
        perm = np.array(perm)
        for i in range(n):
            game = Game([players[pi] for pi in perm])
            game.run()
            wins[perm[game.get_winners()]] += 1

    return wins

max_stars_repo_name: tomjnixon/dominionator

max_issues_repo_name: tomjnixon/dominionator

max_forks_repo_name: tomjnixon/dominionator


text:
import os
import json

import numpy as np
import matplotlib.pyplot as plt

from matplotlib import patches
from skimage.util import view_as_windows
from sklearn.metrics import precision_recall_curve
from keras.models import Model, Sequential
from keras.utils import np_utils

from helpers import get_model_from_json
from utils.img.collection import ImageCollection
from utils.dataset.helpers import get_image_collection
from utils.dataset.annotations import load_annotations, annotations_iter, get_rect_from_annotation
from utils.img.helpers import get_polygon_from_coord, get_polygon_from_rect_box

# Malisiewicz et al.
def non_max_suppression_fast(boxes, overlapThresh):
    # if there are no boxes, return an empty list
    if len(boxes) == 0:
        return []

    # if the bounding boxes integers, convert them to floats --
    # this is important since we'll be doing a bunch of divisions
    if boxes.dtype.kind == "i":
        boxes = boxes.astype("float")

    # initialize the list of picked indexes
    pick = []

    # grab the coordinates of the bounding boxes
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]

    # compute the area of the bounding boxes and sort the bounding
    # boxes by the bottom-right y-coordinate of the bounding box
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    idxs = np.argsort(y2)

    # keep looping while some indexes still remain in the indexes
    # list
    while len(idxs) > 0:
        # grab the last index in the indexes list and add the
        # index value to the list of picked indexes
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)

        # find the largest (x, y) coordinates for the start of
        # the bounding box and the smallest (x, y) coordinates
        # for the end of the bounding box
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])

        # compute the width and height of the bounding box
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)

        # compute the ratio of overlap
        overlap = (w * h) / area[idxs[:last]]

        # delete all indexes from the index list that have
        idxs = np.delete(idxs, np.concatenate(([last],
                                               np.where(overlap > overlapThresh)[0])))

    # return only the bounding boxes that were picked using the
    # integer data type
    return boxes[pick].astype("int")

def get_box(d):
    w = (140, 140)
    s = 35

class TowerDetector(object):
    def __init__(self, model, weights, window_size=(140, 140, 3), stride=35):
        if isinstance(model, Model) or isinstance(model, Sequential):
            self.model = model
        elif isinstance(model, str):
            self.model = get_model_from_json(model)

        self.model.load_weights(weights)

        # self.model.evaluate_generator()

        self.window_size = window_size
        self.stride = stride

    def detect_in_image(self, img):
        if len(img.shape) == 2:
            window_size = (self.window_size[0], self.window_size[1], 1)
            img = img.reshape((img.shape[0], img.shape[1], 1))
        else:
            window_size = self.window_size

        windows = view_as_windows(img, window_size, self.stride)
        windows_shape = windows.shape

        windows = windows.reshape((windows_shape[0] * windows_shape[1], windows_shape[3], windows_shape[4], 3))

        predictions = self.model.predict(windows, batch_size=32, verbose=1)

        y = np_utils.categorical_probas_to_classes(predictions)
        y = y.reshape(windows_shape[0], windows_shape[1])

        detections = zip(*np.where(y == 1))

        # print(detections)
        # print(predictions)

        bxs = []

        for c, r in detections:
            # print(p)
            bxs.append([
                r * self.stride,
                c * self.stride,
                r * self.stride + window_size[0],
                c * self.stride + window_size[1],
            ])

        bxs = non_max_suppression_fast(np.asarray(bxs), 0.1)
        # bxs = zip(*bxs)
        # print('Boxes :', bxs)

        return bxs

    def evaluate_network(self, annotations_file, n, threshold):
        nodes = load_annotations(annotations_file)
        y_truth = []
        y_pred = []

        TP = 0
        FP = 0
        FN = 0

        for i in range(n):
            tp = 0
            fp = 0
            fn = 0
            node = nodes[i]
            filename = node['filename']

            img = ImageCollection.load_image(filename)
            detections = self.detect_in_image(img)

            detection_polygons = []
            annotation_polygons = []

            # fig, ax = plt.subplots(1, 1, squeeze=True, figsize=(10, 10))
            # ax.axis('off')
            #
            # ax.imshow(img)
            # ax.set_adjustable('box-forced')

            for d in detections:
                detection_polygons.append(get_polygon_from_coord(*d))

                # patch = patches.Rectangle(
                #     (d[0], d[1]),
                #     d[2] - d[0],
                #     d[3] - d[1],
                #     fill=False,
                #     edgecolor="red"
                # )
                #
                # ax.add_patch(patch)

            for annotation in node['annotations']:
                rect = get_rect_from_annotation(annotation)
                annotation_polygons.append(get_polygon_from_rect_box(*rect))

                # patch = patches.Rectangle(
                #     (rect[0], rect[1]),
                #     rect[2],
                #     rect[3],
                #     fill=False,
                #     edgecolor="blue"
                # )
                #
                # ax.add_patch(patch)

            print('\nND :', len(detection_polygons))
            print('NA :', len(annotation_polygons))

            for annotation in annotation_polygons:
                if len(detection_polygons) == 0:
                    fn += 1

                    y_truth.append(1)
                    y_pred.append(0)
                else:
                    matches = 0
                    for detection in detection_polygons:
                        if annotation.intersects(detection):
                            intersection = annotation.intersection(detection)

                            intersection_area = intersection.area
                            union_area = annotation.area + detection.area - intersection.area

                            iou = intersection_area / union_area

                            print('IOU :', iou)

                            if iou > threshold:
                                tp += 1

                                y_pred.append(1)
                                y_truth.append(1)

                                matches += 1
                        # else:
                        #     fp += 1
                        #
                        #     y_pred.append(1)
                        #     y_truth.append(0)

                    if matches == 0:
                        fn += 1

                        y_truth.append(1)
                        y_pred.append(0)

            if tp < len(detection_polygons):
                fp += len(detection_polygons) - tp
                # fp += 1

                for _ in range(len(detection_polygons) - tp):
                    y_pred.append(1)
                    y_truth.append(0)

            # if len(annotation_polygons) == 0 and len(detection_polygons) > 0:
            #     for _ in range(len(detection_polygons)):
            #         fp += 1
            #
            #         y_truth.append(0)
            #         y_pred.append(1)

            print('TP :', tp)
            print('FP :', fp)
            print('FN :', fn)
            plt.show()

            TP += tp
            FP += fp
            FN += fn
        print('TP :', TP)
        print('FP :', FP)
        print('FN :', FN)
        print('Precision :', 1. * TP / (TP + FP))
        print('Recall :', 1. * TP / (TP + FN))

if __name__ == '__main__':
    from models.cnn3 import cnn_regularized
    import config

    ic = get_image_collection(config.affixed_tiles_dir)

    m = cnn_regularized(input_shape=(140, 140, 3))
    m.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy', 'precision', 'recall'])
    # Good one
    # w = '/home/tanuj/Workspace/power-grid-detection/training/cnn_140_rgb_corrected_full_lr_0.005000_sgd_he_normal__l1_0.000010_l2_0.000010_dropout_0.500000_r_training_weights_current.hdf5'

    # Ok
    # w = '/home/tanuj/Workspace/power-grid-detection/training/saved weights/cnn_140_rgb_corrected_full_lr_0.005000_sgd_he_normal__l1_0.000010_l2_0.000010_dropout_0.500000_r_training_weights_current.hdf5'

    # Best one
    w = '/home/tanuj/Workspace/power-grid-detection/training/cnn_140_rgb_corrected_full_finetune_decay_lr_0.050000_sgd_he_normal__l1_0.000010_l2_0.000010_dropout_0.500000_r_training_weights_best.hdf5'
    m = TowerDetector(m, weights=w)

    m.evaluate_network(config.corrected_annotations_file, 2000, 0.2)

    # ws = m.model.get_weights()
    # print(len(ws))
    # print(ws[0][:, :, :, 0].shape)

    # fig, ax = plt.subplots(8, 16, sharey='all', sharex='all', squeeze=True)

    # p = 0
    # for i in range(8):
    #     for j in range(16):
    #         ax[i, j].imshow(ws[0][:, :, :, p])
    #         p += 1

    # plt.show()

    # l1 = m.model.get_layer('activation_3')
    #
    # mo = Model(m.model.input, l1.output)
    #
    # im = ic[100][:140, :140, :]
    # res = mo.predict(im.reshape((1, 140, 140, 3)))
    #
    # print(res.shape)
    #
    # for i in range(res.shape[-1]):
    #     fig, (ax1, ax2) = plt.subplots(1, 2)
    #     ax1.imshow(im)
    #     ax2.imshow(res[0, :, :, i])
    #     plt.savefig('out_3/ex_%d.png' % i)
    #     plt.clf()
    #     print(i)

    # for i in ic:
    #     m.detect_in_image(i)

    # m.detect_in_image(ImageCollection.load_image(
    #     '/home/tanuj/Workspace/power-grid-detection/data/cache/google-maps/3x3_tiles/271124_175971_19.jpg'))

max_stars_repo_name: OpenGridMap/power-grid-detection

max_issues_repo_name: OpenGridMap/power-grid-detection

max_forks_repo_name: OpenGridMap/power-grid-detection


text:
from numpy import random

class Failure:
    # Defines if an activity has failed or not.

    # Initialization and instance variables
    def __init__(self, failure_rate: float) -> None:
        self.rate = float(failure_rate)

    # Public methods
    def check_failure(self) -> bool:
        # Returns True if failed, False if not.
        return random.choice([True, False], p=[self.rate, 1-self.rate])

    # Private methods
    def __repr__(self):
        return ', '.join("%s: %s" % item for item in vars(self).items())

max_stars_repo_name: RaghubirChimni/LogGenerator

max_issues_repo_name: GabrielSiq/LogGenerator

max_forks_repo_name: GabrielSiq/LogGenerator

reason: from numpy


text:
import sklearn.datasets

import numpy as np

def sigmoid(X):
    return 1 / (1 + np.exp(-X))

def deriv_sigmoid(X):
    return sigmoid(X) * (1 - sigmoid(X))

def mse(T, Y):
    return ((T - Y)**2).mean(axis=1)

def deriv_mse(T, Y):
    return 2 * (T - Y)

def run(network_input, network_output, batch_size, learning_rate, training_epochs):
    (quant_input, input_size) = network_input.shape
    (_, output_size) = network_output.shape

    total_batch = quant_input // batch_size

    layer_size_1 = 10
    layer_size_2 = 10

    w1 = 2 * np.random.rand(input_size, layer_size_1) - 1
    w2 = 2 * np.random.rand(layer_size_1, layer_size_2) - 1
    w3 = 2 * np.random.rand(layer_size_2, output_size) - 1

    for epoch in range(training_epochs):
        avg_cost = 0
        for i in range(total_batch):
            batch_input = network_input[i * batch_size:(i + 1) * batch_size]
            batch_output = network_output[i * batch_size:(i + 1) * batch_size]

            l0 = batch_input

            z1 = np.dot(l0, w1)
            l1 = sigmoid(z1)

            z2 = np.dot(l1, w2)
            l2 = sigmoid(z2)

            z3 = np.dot(l2, w3)
            output = sigmoid(z3)

            l3_delta = learning_rate * deriv_mse(batch_output, output) * deriv_sigmoid(z3)
            l2_delta = np.dot(l3_delta, w3.T) * deriv_sigmoid(z2)
            l1_delta = np.dot(l2_delta, w2.T) * deriv_sigmoid(z1)

            w3 += np.dot(l2.T, l3_delta)
            w2 += np.dot(l1.T, l2_delta)
            w1 += np.dot(l0.T, l1_delta)

            avg_cost += mse(batch_output, output).mean() / total_batch

        print("Epoch: {:04d} cost={:.9f}".format((epoch+1), avg_cost))

    return (w1, w2, w3)

# Código principal
if __name__ == "__main__":
    #QUANT_INPUT = 20
    #
    #INPUT_SIZE = 5
    #network_input = np.ones((QUANT_INPUT, INPUT_SIZE))
    #
    #OUTPUT_SIZE = 3
    #network_output = np.ones((QUANT_INPUT, OUTPUT_SIZE))

    # Carrega o dataset
    dataset = sklearn.datasets.load_iris()
    dataset_size = len(dataset.target)

    # Inicializa as variaveis de entrada e saída
    network_input = dataset.data
    network_output = np.zeros((dataset_size, 3))
    network_output[range(dataset_size), dataset.target] = 1    # Ajusta o formato da lista com os resultados

    # Embaralha a ordem dos dados
    shuffle = np.array(range(dataset_size))
    np.random.shuffle(shuffle)    # Determina uma ordem aleatória para selecionar as entradas
    network_input = network_input[shuffle]
    network_output = network_output[shuffle]

    # Divide os dados para treinamento e para teste
    training_size = int(dataset_size * 0.8)

    training_input = network_input[:training_size]
    training_output = network_output[:training_size]

    test_input = network_input[training_size:]
    test_output = network_output[training_size:]

    # Parâmetros da rede neural
    batch_size = 1
    learning_rate = 0.01
    training_epochs = 500

    # Realiza o treinamento e obtém os pesos
    (w1, w2, w3) = run(training_input, training_output, batch_size, learning_rate, training_epochs)

    # Realiza os teste
    z1 = np.dot(test_input, w1)
    l1 = sigmoid(z1)

    z2 = np.dot(l1, w2)
    l2 = sigmoid(z2)

    z3 = np.dot(l2, w3)
    output = sigmoid(z3)

    cost = mse(test_output, output).mean()
    print(cost)


text:
from sklearn.utils import shuffle
import copy
import parameters as p
import torch
import numpy as np

def train(X, y, model):
    losses, predictions = [], []
    error_count = 0
    error_list = []

    for i in range(len(X)):
        yhat = model.predict(X[i])
        loss = model.update(X[i], y[i])
        predictions.append(yhat)
        losses.append(loss)

        if torch.is_tensor(yhat):
            yhat = yhat.detach().numpy()

        if np.sign(yhat) != y[i]:
            error_count += 1

        error_list.append(error_count/(i+1.0))

    return losses, predictions, error_count/len(X), model.w, error_list

def cross_validation(X, y, model, num_folds, scenario_function):
    fold_errors = []
    fold_losses = []
    fold_weights = []
    fold_masks = []
    error_lists = []

    for i in range(num_folds):
        X, y = shuffle(X, y)
        X_copy = copy.deepcopy(X)
        fold_mask = scenario_function(X_copy, p.cov_strength)
        X_copy *= fold_mask
        fold_masks.append(fold_mask)
        model.reset()

        losses, predictions, error_rate, weights, error_list = train(X_copy, y, model)
        #print(error_rate)

        fold_errors.append(error_rate)
        fold_losses.append(losses)
        fold_weights.append(weights)
        error_lists.append(error_list)

    return fold_errors, fold_losses, fold_weights, fold_masks, error_lists

max_stars_repo_name: CopperWasp/PredictiveCoding

max_issues_repo_name: CopperWasp/PredictiveCoding

max_forks_repo_name: CopperWasp/PredictiveCoding


text:
import numpy as np
import math

# Contruye una matriz de probabilidades de transición
filename = './datos-examen.txt'
with open(filename, 'r') as f:
    q = [[float(num) for num in line.split(',')] for line in f]

q = np.array(q)
print(q)
print('')

num_rows, num_cols = q.shape

p = []
for x in range(num_rows):
    p.append(float(input('Introduzca la probabilidad %d: ' % x)))

print(p)
print('')

sumP = 0
for x in range(len(p)):
    sumP = sumP + float(p[x])

sum = 0
for i in range(0, num_rows):
    for j in range(0, num_cols):
        pi = p[i]
        qij = q[i][j]

        sumt = 0
        for t in range(0, num_rows):
            sumt += p[t] * q[t][j]

        sum += pi * qij * math.log(qij / sumt, 10)

print("Capacidad del canal (I(A, B)) =", sum)

max_stars_repo_name: JRolandoMH/tareas-info

max_issues_repo_name: JRolandoMH/tareas-info

max_forks_repo_name: JRolandoMH/tareas-info


text:
import tensorflow as tf
import numpy as np
from settings import float_type, jitter_level,std_qmu_init, np_float_type, np_int_type
from functions import eye, variational_expectations
from mean_functions import Zero
from kullback_leiblers import gauss_kl_white, gauss_kl_white_diag, gauss_kl, gauss_kl_diag
from conditionals import conditional
from quadrature import hermgauss

class ChainedGPs(object):
    """
    Chained Gaussian Processes

    The key reference for this algorithm is:
    ::
      @article{saul2016chained,
        title={Chained Gaussian Processes},
        author={Saul, Alan D and Hensman, James and Vehtari, Aki and Lawrence, Neil D},
        journal={arXiv preprint arXiv:1604.05263},
        year={2016}
      }

    """
    def __init__(self, X, Y, kerns,likelihood,Zs,mean_functions=None, whiten=True,q_diag=False, f_indices=None):
        '''
        - X is a data matrix, size N x D
        - Y is a data matrix, size N x R
        - kerns, likelihood, mean_functions are appropriate (single or list of) GPflow objects
        - Zs is a list of  matrices of pseudo inputs, size M[k] x C
        - num_latent is the number of latent process to use, default to
          Y.shape[1]
        - q_diag is a boolean. If True, the covariance is approximated by a
          diagonal matrix.
        - whiten is a boolean. If True, we use the whitened representation of
          the inducing points.
        '''
        self.likelihood = likelihood
        self.kerns = kerns
        self.C = len(kerns)
        self.mean_functions = [Zero() for _ in range(self.C)] if mean_functions is None else mean_functions
        self.f_indices = f_indices # function of one variable
        self.X = X
        self.Y = Y
        self.Zs = Zs
        self.num_inducing = [z.get_shape()[0] for z in Zs]
        self.num_latent = Y.get_shape()[-1]
        self.num_data = Y.get_shape()[0]
        self.whiten=whiten
        self.q_diag = q_diag
        self.initialize_inference()

    def initialize_inference(self):

        with tf.variable_scope("inference") as scope:
            self.q_mu,self.q_sqrt = [],[]
            for c in range(self.C):

                self.q_mu.append( tf.get_variable("q_mu%d"%c,[self.num_inducing[c], self.num_latent],\
                          initializer=tf.constant_initializer(np.random.randn(self.num_inducing[c], self.num_latent)*std_qmu_init,\
                                                              dtype=float_type)))

                if self.q_diag:
                    q_sqrt = np.ones((self.num_inducing[c], self.num_latent))
                    self.q_sqrt.append( tf.get_variable("q_sqrt%d"%c,[self.num_inducing[c],self.num_latent], \
                                          initializer=tf.constant_initializer(q_sqrt,dtype=float_type)) )

                else:
                    q_sqrt = np.array([np.eye(self.num_inducing[c]) for _ in range(self.num_latent)]).swapaxes(0, 2)
                    self.q_sqrt.append( tf.get_variable("q_sqrt%d"%c,[self.num_inducing[c],self.num_inducing[c],self.num_latent], \
                                          initializer=tf.constant_initializer(q_sqrt,dtype=float_type)) )

    def build_prior_KL(self):

        KL = tf.Variable(0,name='KL',trainable=False,dtype=float_type)
        for i in range(self.C):
            if self.whiten:
                if self.q_diag:
                    KL += gauss_kl_white_diag(self.q_mu[i], self.q_sqrt[i])
                else:
                    KL += gauss_kl_white(self.q_mu[i], self.q_sqrt[i])
            else:
                K = self.kerns[i].K(self.Zs[self.f_indices[i]]) + eye(self.num_inducing[i]) * jitter_level
                if self.q_diag:
                    KL += gauss_kl_diag(self.q_mu[i], self.q_sqrt[i], K)
                else:
                    KL += gauss_kl(self.q_mu[i], self.q_sqrt[i], K)
        return KL

    def get_covariate(self,Xnew,c):
        return tf.transpose(tf.gather(tf.transpose(Xnew),self.f_indices[c]))

    def build_predict_fs(self, Xnew):
        mus, vars = [],[]
        for c in range(self.C):
            x = self.get_covariate(Xnew,c)
            mu, var = conditional(x, self.Zs[c], self.kerns[c], self.q_mu[c],
                                     q_sqrt=self.q_sqrt[c], full_cov=False, whiten=self.whiten)
            mus.append(mu+self.mean_functions[c](x))
            vars.append(var)
        return tf.stack(mus),tf.stack(vars)

class SVAGP(ChainedGPs):
    """
    Sparse Variational Additive Gaussian Process

    Key reference is
    ::
      @inproceedings{adam2016scalable,
        title={Scalable transformed additive signal decomposition by non-conjugate Gaussian process inference},
        author={Adam, Vincent and Hensman, James and Sahani, Maneesh},
        booktitle={Machine Learning for Signal Processing (MLSP), 2016 IEEE 26th International Workshop on},
        pages={1--6},
        year={2016},
        organization={IEEE}
      }
    """

    def build_likelihood(self):
        """
        This gives a variational bound on the model likelihood.
        """
        cost = -self.build_prior_KL()
        fmean, fvar = self.build_predict_additive_predictor(self.X)
        var_exp = self.likelihood.variational_expectations(fmean, fvar, self.Y)
        cost += tf.reduce_sum(var_exp)
        return cost

    def build_predict_additive_predictor(self, Xnew):
        mus, vars = self.build_predict_fs(Xnew)
        return tf.reduce_sum(mus,0),tf.reduce_sum(vars,0)

max_stars_repo_name: UCL/SVAGP

max_issues_repo_name: UCL/SVAGP

max_forks_repo_name: UCL/SVAGP