text:
import os
import pandas as pd
import numpy as np
from unittest import TestCase

from ..base.constants import (
    SOURCE, TARGET, LABEL, NULL_VALUES, PUBMED, EXPERIMENT_TYPE
)
from ..data_mining.tools import (
    _format_label, _format_labels, _split_label,
    _null_to_none, _make_ppi_tuples,

    xy_from_interaction_frame,
    labels_from_interaction_frame,
    ppis_from_interaction_frame,

    make_interaction_frame,
    map_network_accessions,

    remove_nan,
    remove_labels,
    remove_min_counts,
    remove_self_edges,
    merge_labels,
    remove_duplicates,

    process_interactions
)

def dataframes_are_equal(df1: pd.DataFrame, df2: pd.DataFrame):
    # df1 = df1.replace(to_replace=NULL_VALUES, value=str(None), inplace=False)
    # df2 = df2.replace(to_replace=NULL_VALUES, value=str(None), inplace=False)
    return df1.equals(df2)

class TestNullToNone(TestCase):

    def test_can_convert_null_to_none(self):
        for value in NULL_VALUES:
            self.assertIs(None, _null_to_none(value))

class TestMakePPITuples(TestCase):

    def test_sorts_by_alpha(self):
        self.assertEqual(
            [('A', 'B')],
            _make_ppi_tuples(['B'], ['A'])
        )

    def test_converts_null_to_none(self):
        for value in NULL_VALUES:
            self.assertEqual(
                [tuple(sorted(['None', 'A']))],
                _make_ppi_tuples([value], ['A'])
            )

class TestSplitLabel(TestCase):

    def test_strip_whitespace(self):
        label = ' hello , world '
        expected = ['hello', 'world']
        self.assertEqual(expected, _split_label(label))

    def test_splits_on_comma_by_default(self):
        label = 'hello,world'
        expected = ['hello', 'world']
        self.assertEqual(expected, _split_label(label))

    def test_splits_on_sep(self):
        label = 'hello|,world'
        expected = ['hello', ',world']
        self.assertEqual(expected, _split_label(label, sep='|'))

    def test_strips_white_space(self):
        label = ' hello world '
        expected = ["hello world"]
        self.assertEqual(expected, _split_label(label))

    def test_can_handle_none(self):
        for value in NULL_VALUES:
            self.assertEqual([None], _split_label(value))

    def test_filters_nones(self):
        for value in NULL_VALUES:
            self.assertEqual([None], _split_label(
                '{},{}'.format(value, value)))

class TestFormatLabel(TestCase):

    def test_strip_whitespace(self):
        label = ' hello '
        expected = "Hello"
        self.assertEqual(expected, _format_label(label))

    def test_caps(self):
        label = 'Hello'
        expected = "Hello"
        self.assertEqual(expected, _format_label(label))

    def test_replace_whitespace_with_dash(self):
        label = 'Hello world'
        expected = "Hello-world"
        self.assertEqual(expected, _format_label(label))

    def test_can_handle_none(self):
        for value in NULL_VALUES:
            self.assertEqual(None, _format_label(value))

class TestFormatLabels(TestCase):

    def test_splits_on_comma(self):
        labels = ['hello,world']
        expected = [["Hello", "World"]]
        self.assertEqual(expected, _format_labels(labels))

    def test_splits_get_own_list(self):
        labels = ['hello,world', 'hello,world']
        expected = [["Hello", "World"], ["Hello", "World"]]
        self.assertEqual(expected, _format_labels(labels))

    def test_removes_duplicates_from_each_label_if_true(self):
        labels = ['hello,hello']
        expected = [["Hello"]]
        self.assertEqual(expected, _format_labels(labels))

    def test_removes_null_duplicates(self):
        for value in NULL_VALUES:
            labels = ['{},none'.format(value)]
            expected = [[None]]
            result = _format_labels(labels)
            self.assertEqual(expected, result)

    def test_sorts_each_label_after_split(self):
        labels = ['world,hello']
        expected = [["Hello", "World"]]
        result = _format_labels(labels)
        self.assertEqual(expected, result)

    def test_joins_if_true(self):
        labels = ['world,hello']
        expected = ["Hello,World"]
        result = _format_labels(labels, join=True)
        self.assertEqual(expected, result)

    def test_can_handle_null_values(self):
        for value in NULL_VALUES:
            self.assertEqual([[None]], _format_labels([value]))

class TestLabelsFromInteractionFrame(TestCase):

    def test_converts_null_to_None(self):
        for value in NULL_VALUES:
            iframe = pd.DataFrame(
                {SOURCE: ['A', 'A'], TARGET: ['A', 'A'], LABEL: ['cat', value]}
            )
            labels = labels_from_interaction_frame(iframe)
            self.assertEqual(labels, [['Cat'], [None]])

    def test_removes_multilabel_null(self):
        for value in NULL_VALUES:
            iframe = pd.DataFrame(
                {
                    SOURCE: ['A', 'A'], TARGET: ['A', 'A'],
                    LABEL: ['{},cat'.format(value), '{},whale'.format(value)]
                }
            )
            labels = labels_from_interaction_frame(iframe)
            self.assertEqual(labels, [['Cat'], ['Whale']])

    def test_returns_label_row_in_same_order_as_input(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B', 'A'], TARGET: ['B', 'A'], LABEL: ['dog', 'cat']}
        )
        labels = labels_from_interaction_frame(iframe)
        expected = [['Dog'], ['Cat']]
        self.assertEqual(labels, expected)

    def test_returns_sorted_labels(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B'], TARGET: ['A'], LABEL: ['dog,cat']}
        )
        labels = labels_from_interaction_frame(iframe)
        expected = [['Cat', 'Dog']]
        self.assertEqual(labels, expected)

    def test_splits_labels_on_comma(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B'], TARGET: ['B'], LABEL: ['cat,dog']}
        )
        labels = labels_from_interaction_frame(iframe)
        expected = [['Cat', 'Dog']]
        self.assertEqual(labels, expected)

    def test_replaces_whitespace_for_dash_in_labels(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B'], TARGET: ['B'], LABEL: ['dog cat']}
        )
        labels = labels_from_interaction_frame(iframe)
        expected = [['Dog-cat']]
        self.assertEqual(labels, expected)

    def test_removes_duplicate_labels(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B'], TARGET: ['B'], LABEL: ['activation,activation']}
        )
        labels = labels_from_interaction_frame(iframe)
        expected = [['Activation']]
        self.assertEqual(labels, expected)

    def test_strips_trailing_whitespace_from_labels(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B'], TARGET: ['B'], LABEL: [' activation ']}
        )
        labels = labels_from_interaction_frame(iframe)
        expected = [['Activation']]
        self.assertEqual(labels, expected)

class TestPPIsFromInteractionFrame(TestCase):

    def test_returns_ppis_in_same_order(self):
        iframe = pd.DataFrame(
            {SOURCE: ['A', 'B'], TARGET: ['B', 'A'], LABEL: [None, None]}
        )
        ppis = ppis_from_interaction_frame(iframe)
        expected = [("A", "B"), ("B", "A")]
        self.assertEqual(ppis, expected)

    def test_does_not_alter_duplicate_rows(self):
        iframe = pd.DataFrame(
            {SOURCE: ['A', 'A'], TARGET: ['A', 'A'], LABEL: [None, None]}
        )
        ppis = ppis_from_interaction_frame(iframe)
        self.assertEqual(ppis, [("A", "A"), ("A", "A")])

    def test_returns_ppis_in_same_order_as_input(self):
        iframe = pd.DataFrame(
            {SOURCE: ['B', 'A'], TARGET: ['B', 'A'], LABEL: [None, None]}
        )
        ppis = ppis_from_interaction_frame(iframe)
        expected = [("B", "B"), ("A", "A")]
        self.assertEqual(ppis, expected)

    def test_returns_null_values_as_None(self):
        for value in NULL_VALUES:
            iframe = pd.DataFrame(
                {SOURCE: [value, 'A'], TARGET: ['B', 'A'], LABEL: ['1', '2']}
            )
            ppis = ppis_from_interaction_frame(iframe)
            expected = [(None, "B"), ("A", "A")]
            self.assertEqual(ppis, expected)

class TestXYFromInteractionFrame(TestCase):

    def test_integration(self):
        iframe = pd.DataFrame(
            {
                SOURCE: ['B', None], TARGET: ['B', 'A'],
                LABEL: ['None,,cat', 'dog,cat,Dog,blue whale,None,none']
            }
        )
        ppis, labels = xy_from_interaction_frame(iframe)
        self.assertEqual(ppis, [("B", "B"), (None, "A")])
        self.assertEqual(
            labels, [['Cat'], ['Blue-whale', 'Cat', 'Dog']]
        )

class TestMakeInteractionFrame(TestCase):

    def test_sorts_source_and_target_alphabetically(self):
        sources = ['B']
        targets = ['A']
        labels = ['1']

        result = make_interaction_frame(sources, targets, labels)
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['B'], LABEL: ['1'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_converts_null_value_to_NaN(self):
        sources = ['B'] * len(NULL_VALUES)
        targets = ['A'] * len(NULL_VALUES)
        labels = list(NULL_VALUES)

        result = make_interaction_frame(sources, targets, labels)
        expected = pd.DataFrame(
            {
                SOURCE: ['A'] * len(NULL_VALUES),
                TARGET: ['B'] * len(NULL_VALUES),
                LABEL: [None] * len(NULL_VALUES),
                PUBMED: [None] * len(NULL_VALUES),
                EXPERIMENT_TYPE: [None] * len(NULL_VALUES)
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_formats_labels(self):
        sources = ['B']
        targets = ['A']
        labels = ['World hello']

        result = make_interaction_frame(sources, targets, labels)
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['B'], LABEL: ['World-hello'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_pmids_and_psimis_are_not_sorted(self):
        sources = ['B'] * 2
        targets = ['A'] * 2
        labels = ['activation'] * 2
        extra_1 = ["1001", "1000"]
        extra_2 = ["MI:2", "MI:1"]
        result = make_interaction_frame(
            sources, targets, labels, pmids=extra_1, psimis=extra_2)
        expected = pd.DataFrame(
            {
                SOURCE: ['A']*2, TARGET: ['B']*2, LABEL: ['Activation']*2,
                EXPERIMENT_TYPE: ["MI:2", "MI:1"], PUBMED: ["1001", "1000"]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_source_target_are_not_sorted_if_sort_is_false(self):
        sources = ['B'] * 2
        targets = ['A'] * 2
        labels = ['activation'] * 2
        extra_1 = ["1001", "1000"]
        extra_2 = ["MI:2", "MI:1"]
        result = make_interaction_frame(
            sources, targets, labels, pmids=extra_1, psimis=extra_2, sort=False)
        expected = pd.DataFrame(
            {
                SOURCE: ['B']*2, TARGET: ['A']*2, LABEL: ['Activation']*2,
                EXPERIMENT_TYPE: ["MI:2", "MI:1"], PUBMED: ["1001", "1000"]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

class TestRemoveNaN(TestCase):

    def test_removes_row_if_source_is_None(self):
        sources = [None, 'A']
        targets = ['A', 'B']
        labels = ['1', '2']
        result = remove_nan(make_interaction_frame(sources, targets, labels))
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['B'], LABEL: ['2'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_removes_row_if_target_is_None(self):
        sources = ['A', 'A']
        targets = [None, 'B']
        labels = ['1', '2']
        result = remove_nan(make_interaction_frame(sources, targets, labels))
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['B'], LABEL: ['2'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_removes_row_if_label_is_None(self):
        sources = ['A', 'A']
        targets = ['B', 'B']
        labels = [None, '2']
        result = remove_nan(make_interaction_frame(sources, targets, labels))
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['B'], LABEL: ['2'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_removed_row_if_contains_anything_from_NULL_VALUES(self):
        for value in NULL_VALUES:
            sources = ['A', 'A']
            targets = ['B', 'B']
            labels = [value, '2']
            result = remove_nan(
                make_interaction_frame(sources, targets, labels)
            )
            expected = pd.DataFrame(
                {
                    SOURCE: ['A'], TARGET: ['B'], LABEL: ['2'],
                    PUBMED: [None], EXPERIMENT_TYPE: [None]
                },
                columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
            )
            self.assertTrue(dataframes_are_equal(expected, result))

    def test_resets_index(self):
        sources = ['A', 'A']
        targets = ['B', 'B']
        labels = [None, '2']
        result = remove_nan(make_interaction_frame(
            sources, targets, labels)
        ).index
        expected = pd.Index([0])
        self.assertTrue(expected.equals(result))

    def test_ignores_null_values_in_pmid_and_psimi(self):
        sources = ['B']
        targets = ['A']
        labels = ['activation']
        extra_1 = [None]
        extra_2 = [None]

        result = remove_nan(
            make_interaction_frame(sources, targets, labels, extra_1, extra_2)
        )
        expected = make_interaction_frame(
            ['A'], ['B'], ['Activation'], extra_1, extra_2
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_removes_row_if_column_included_in_subset(self):
        for value in NULL_VALUES:
            sources = ['B']
            targets = ['A']
            labels = ['activation']
            extra_1 = [value]
            extra_2 = [value]

            result = remove_nan(
                make_interaction_frame(
                    sources, targets, labels, extra_1, extra_2),
                subset=[PUBMED, EXPERIMENT_TYPE]
            )
            self.assertTrue(result.empty)

class TestRemoveLabels(TestCase):

    def test_removes_rows_with_label_in_exclusion_list(self):
        iframe = make_interaction_frame(['B', 'A'], ['B', 'A'], ['1', '2'])
        result = remove_labels(iframe, ['1'])
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['A'], LABEL: ['2'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_case_sensitive(self):
        iframe = pd.DataFrame(
            {
                SOURCE: ['B', 'A'], TARGET: ['B', 'A'], LABEL: ['cat', 'Cat'],
                PUBMED: [None]*2, EXPERIMENT_TYPE: [None]*2
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        result = remove_labels(iframe, ['cat'])
        expected = pd.DataFrame(
            {
                SOURCE: ['A'], TARGET: ['A'], LABEL: ['Cat'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_exclude_labels_doesnt_exclude_multilabel_samples(self):
        iframe = make_interaction_frame(['B'], ['B'], ['Cat,Dog'])
        result = remove_labels(iframe, ['Cat'])
        expected = pd.DataFrame(
            {
                SOURCE: ['B'], TARGET: ['B'], LABEL: ['Cat,Dog'],
                PUBMED: [None], EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_exclude_labels_excludes_nan_labels(self):
        for value in NULL_VALUES:
            iframe = make_interaction_frame(['B'], ['B'], [value])
            result = remove_labels(iframe, NULL_VALUES)
            self.assertTrue(result.empty)

    def test_resets_index(self):
        iframe = make_interaction_frame(['B', 'A'], ['B', 'A'], ['1', '2'])
        result = remove_labels(iframe, ['1']).index
        self.assertTrue(result.equals(pd.Index([0])))

class TestRemoveLabelWithMinCount(TestCase):

    def test_remove_labels_with_count_less_than_min_count(self):
        iframe = make_interaction_frame(
            ['B', 'A', 'C'], ['B', 'A', 'C'], ['1', '2', '1']
        )
        result = remove_min_counts(iframe, min_count=2)
        expected = pd.DataFrame(
            {
                SOURCE: ['B', 'C'], TARGET: ['B', 'C'], LABEL: ['1', '1'],
                PUBMED: [None] * 2, EXPERIMENT_TYPE: [None] * 2
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(dataframes_are_equal(expected, result))

    def test_doesnt_error_if_NULL_VALUES_present(self):
        iframe = make_interaction_frame(
            ['B', 'A', 'C'], ['B', 'A', 'C'], [None, None, None]
        )
        result = remove_min_counts(iframe, min_count=2)
        self.assertTrue(dataframes_are_equal(iframe, result))

    def test_null_also_treated_as_a_label(self):
        iframe = make_interaction_frame(
            ['B', 'A', 'C'], ['B', 'A', 'C'], [None, None, None]
        )
        result = remove_min_counts(iframe, min_count=4)
        self.assertTrue(result.empty)

    def test_resets_index(self):
        iframe = make_interaction_frame(
            ['B', 'A', 'C'], ['B', 'A', 'C'], ['1', '2', '1']
        )
        result = remove_min_counts(iframe, min_count=2).index
        self.assertTrue(pd.Index([0, 1]).equals(result))

class TestRemoveSelfEdges(TestCase):

    def test_removes_self_edges(self):
        iframe = make_interaction_frame(
            ['B'], ['B'], ['1']
        )
        result = remove_self_edges(iframe)
        self.assertTrue(result.empty)

    def test_removes_self_edges_that_are_nan(self):
        iframe = make_interaction_frame(
            [None], [None], ['1']
        )
        result = remove_self_edges(iframe)
        self.assertTrue(result.empty)

    def test_doesnt_remove_regular_edges(self):
        iframe = make_interaction_frame(
            ['A'], ['B'], ['1']
        )
        result = remove_self_edges(iframe)
        self.assertTrue(result.equals(iframe))

    def test_resets_index(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'A'], ['1', '2']
        )
        result = remove_self_edges(iframe).index
        self.assertTrue(pd.Index([0]).equals(result))

class TestMergeLabels(TestCase):

    def test_joins_labels_by_comma(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Cat', 'Dog']
        )
        result = merge_labels(iframe)
        self.assertEqual(list(result[LABEL]), ['Cat,Dog'])

    def test_sorts_labels(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['dog', 'cat']
        )
        result = merge_labels(iframe)
        self.assertEqual(list(result[LABEL]), ['Cat,Dog'])

    def test_doesnt_merge_non_null_with_null(self):
        for value in NULL_VALUES:
            iframe = make_interaction_frame(
                ['B', 'B'], ['B', 'B'], [value, 'Cat']
            )
            result = merge_labels(iframe)
            self.assertEqual(list(result[LABEL]), ['Cat'])

    def test_can_merge_already_merged_labels(self):
        iframe = make_interaction_frame(
            ['B', 'A'], ['A', 'B'], ['dog,cat', 'eel,dog']
        )
        merged = merge_labels(iframe)
        expected = ['Cat,Dog,Eel']
        result = list(merged[LABEL])
        self.assertEqual(result, expected)

    def test_can_merge_null_labels(self):
        for value1 in NULL_VALUES:
            for value2 in NULL_VALUES:
                iframe = make_interaction_frame(
                    ['B', 'A'], ['A', 'B'], [value1, value2]
                )
                merged = merge_labels(iframe)
                expected = [None]
                result = list(merged[LABEL])
                self.assertEqual(result, expected)

    def test_removes_duplicate_labels(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog']
        )
        merged = merge_labels(iframe)
        expected = ['Dog']
        result = list(merged[LABEL])
        self.assertEqual(result, expected)

    def test_removes_whitespace(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], [' Dog ', ' Cat ']
        )
        merged = merge_labels(iframe)
        expected = ['Cat,Dog']
        result = list(merged[LABEL])
        self.assertEqual(result, expected)

    def test_ignores_ppi_direction(self):
        iframe = make_interaction_frame(
            ['A', 'B'], ['B', 'A'], ['Dog', 'Cat']
        )
        merged = merge_labels(iframe)
        expected = ['Cat,Dog']
        result = list(merged[LABEL])
        self.assertEqual(result, expected)
        self.assertEqual(list(merged[SOURCE]), ['A'])
        self.assertEqual(list(merged[TARGET]), ['B'])

    def test_capitalises_labels(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['DOG', 'DOG']
        )
        merged = merge_labels(iframe)
        expected = ['Dog']
        result = list(merged[LABEL])
        self.assertEqual(result, expected)

    def test_resets_index(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog']
        )
        result = merge_labels(iframe).index
        self.assertTrue(result.equals(pd.Index([0])))

    def test_pmids_are_comma_joined(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1001'], None
        )
        merged = merge_labels(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000,1001'])
        self.assertEqual(psimis, ['None,None'])

    def test_psimi_order_is_not_mixed(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1001'], [None, 'MI:1|MI:2']
        )
        merged = merge_labels(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000,1001'])
        self.assertEqual(psimis, ['None,MI:1|MI:2'])

    def test_premerged_psimi_pmids_can_be_merged_again(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000,1001', '1001'], ['None,None', 'MI:1|MI:2']
        )
        merged = merge_labels(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000,1001'])
        self.assertEqual(psimis, ['None,MI:1|MI:2'])

        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000,1001', '1001'], ['MI:1,None', 'MI:1|MI:2']
        )
        merged = merge_labels(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000,1001'])
        self.assertEqual(psimis, ['MI:1,MI:1|MI:2'])

    def test_psimi_pmids_filter_out_nan(self):
        for value in NULL_VALUES:
            iframe = make_interaction_frame(
                ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
                ['1000', '1001'], [value, value]
            )
            merged = merge_labels(iframe)
            pmids = list(merged[PUBMED])
            psimis = list(merged[EXPERIMENT_TYPE])
            self.assertEqual(pmids, ['1000,1001'])
            self.assertEqual(psimis, ['None,None'])

    def test_nan_pmids_filtered_out(self):
        for value in NULL_VALUES:
            iframe = make_interaction_frame(
                ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
                [value, '1001'], ['mi:1', 'mi:2']
            )
            merged = merge_labels(iframe)
            pmids = list(merged[PUBMED])
            psimis = list(merged[EXPERIMENT_TYPE])
            self.assertEqual(pmids, ['1001'])
            self.assertEqual(psimis, ['MI:2'])

    def test_psimi_group_has_no_duplicates_and_is_sorted(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1000'], [None, 'MI:3|MI:2|mi:2']
        )
        merged = merge_labels(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, ['MI:2|MI:3'])

    def test_psimi_group_is_uppercase(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1000'], [None, '|mi:2']
        )
        merged = merge_labels(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, ['MI:2'])

class TestRemoveDuplicates(TestCase):

    def test_removes_identical_rows(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog']
        )
        result = remove_duplicates(iframe)
        self.assertEqual(list(result[LABEL]), ['Dog'])

    def test_doesnt_remove_if_label_is_different(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Cat']
        )
        result = remove_duplicates(iframe)
        self.assertEqual(list(result[LABEL]), ['Dog', 'Cat'])

    def test_can_handle_null_labels(self):
        for value1 in NULL_VALUES:
            for value2 in NULL_VALUES:
                iframe = make_interaction_frame(
                    ['B', 'B'], ['B', 'B'], [value1, value2]
                )
                result = remove_duplicates(iframe)
                self.assertEqual(list(result[LABEL]), [None])

    def test_can_detect_merged_labels(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog,Cat', 'Dog,Cat']
        )
        result = remove_duplicates(iframe)
        self.assertEqual(list(result[LABEL]), ['Cat,Dog'])

    def test_can_detect_merged_labels_out_of_order(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['dog,cat', 'cat,dog']
        )
        result = remove_duplicates(iframe)
        self.assertEqual(list(result[LABEL]), ['Cat,Dog'])

    def test_removes_reverse_duplicates(self):
        iframe = make_interaction_frame(
            ['B', 'A'], ['A', 'B'], ['dog', 'dog']
        )
        result = remove_duplicates(iframe)
        self.assertEqual(list(result[LABEL]), ['Dog'])

    def test_resets_index(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['dog', 'dog']
        )
        result = remove_duplicates(iframe).index
        self.assertTrue(pd.Index([0]).equals(result))

    def test_pmids_merged_when_removing_duplicate(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1001'], None
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000,1001'])
        self.assertEqual(psimis, ['None,None'])

    def test_pmid_duplicates_removed(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1000'], None
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, [None])

    def test_psimis_are_group_sorted(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1000'], ['mi:2', 'mi:1']
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, ['MI:1|MI:2'])

    def test_additional_columns_removes_trailing_whitespace(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            [' 1000', '1000 '], [' mi:2 ', ' mi:1 ']
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, ['MI:1|MI:2'])

    def test_psimi_order_is_not_mixed(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1001', '1000'], ['MI:0', 'MI:1|MI:2']
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1001,1000'])
        self.assertEqual(psimis, ['MI:0,MI:1|MI:2'])

    def test_premerged_psimi_pmids_can_be_merged_again(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000,1001', '1001'], ['MI:1,None', 'MI:1|MI:2']
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000,1001'])
        self.assertEqual(psimis, ['MI:1,MI:1|MI:2'])

    def test_psimi_pmids_filter_out_nan(self):
        for value in NULL_VALUES:
            iframe = make_interaction_frame(
                ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
                ['1000', '1001'], [value, value]
            )
            merged = remove_duplicates(iframe)
            pmids = list(merged[PUBMED])
            psimis = list(merged[EXPERIMENT_TYPE])
            self.assertEqual(pmids, ['1000,1001'])
            self.assertEqual(psimis, ['None,None'])

    def test_nan_pmids_filtered_out(self):
        for value in NULL_VALUES:
            iframe = make_interaction_frame(
                ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
                [value, '1001'], ['mi:1', 'mi:2']
            )
            merged = remove_duplicates(iframe)
            pmids = list(merged[PUBMED])
            psimis = list(merged[EXPERIMENT_TYPE])
            self.assertEqual(pmids, ['1001'])
            self.assertEqual(psimis, ['MI:2'])

    def test_psimi_group_has_no_duplicates_and_is_sorted(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1000'], [None, 'MI:3|MI:2|mi:2']
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, ['MI:2|MI:3'])

    def test_psimi_group_is_uppercase(self):
        iframe = make_interaction_frame(
            ['B', 'B'], ['B', 'B'], ['Dog', 'Dog'],
            ['1000', '1000'], [None, '|mi:2']
        )
        merged = remove_duplicates(iframe)
        pmids = list(merged[PUBMED])
        psimis = list(merged[EXPERIMENT_TYPE])
        self.assertEqual(pmids, ['1000'])
        self.assertEqual(psimis, ['MI:2'])

class TestProcessInteractions(TestCase):

    # def test_default_drop_nan_uses_source_target_and_label_as_column(self):
    #     iframe = make_interaction_frame(
    #         [None, 'B', 'C'],
    #         ['A', 'C', 'C'],
    #         ['1', '2', '3'],
    #         ['1', '2', None],
    #         ['1', None, '3']
    #     )
    #     expected = make_interaction_frame(
    #         ['B', 'C'],
    #         ['C', 'C'],
    #         ['2', '3'],
    #         ['2', None],
    #         [None, '3']
    #     )
    #     result = process_interactions(
    #         iframe, drop_nan='default', allow_self_edges=True,
    #         allow_duplicates=True, exclude_labels=None,
    #         min_counts=None, merge=False
    #     )
    #     self.assertTrue(dataframes_are_equal(result, expected))

    # def test_normalises_nan_values(self):
    #     for value in NULL_VALUES:
    #         iframe = make_interaction_frame(
    #             [value, 'B', 'C'],
    #             ['A', value, 'C'],
    #             ['1', '2', value],
    #             [value] * 3,
    #             [value] * 3
    #         )
    #         expected = make_interaction_frame(
    #             [None, 'B', 'C'],
    #             ['A', None, 'C'],
    #             ['1', '2', None],
    #             [None] * 3,
    #             [None] * 3
    #         )
    #         result = process_interactions(
    #             iframe, drop_nan=None, allow_self_edges=True,
    #             allow_duplicates=True, exclude_labels=None,
    #             min_counts=None, merge=False
    #         )
    #         self.assertTrue(dataframes_are_equal(expected, result))

    # def test_drop_nan_bypasses_when_none_is_suppled(self):
    #     iframe = make_interaction_frame(
    #         [None, 'B', 'C'],
    #         ['A', None, 'B'],
    #         ['1', '2', None],
    #         [None] * 3,
    #         [None] * 3
    #     )
    #     result = process_interactions(
    #         iframe, drop_nan=None, allow_self_edges=True,
    #         allow_duplicates=True, exclude_labels=None,
    #         min_counts=None, merge=False
    #     )
    #     self.assertTrue(dataframes_are_equal(iframe, result))

    # def test_drop_nan_uses_columns_when_supplied(self):
    #     iframe = make_interaction_frame(
    #         [None, 'B', 'C'],
    #         ['A', 'C', 'C'],
    #         ['1', '2', '3'],
    #         ['1', '2', '3'],
    #         ['1', None, '3']
    #     )
    #     result = process_interactions(
    #         iframe, drop_nan=[SOURCE, PUBMED, EXPERIMENT_TYPE],
    #         allow_self_edges=True,
    #         allow_duplicates=True, exclude_labels=None,
    #         min_counts=None, merge=False
    #     )
    #     expected = make_interaction_frame(
    #         ['C'], ['C'], ['3'], ['3'], ['3']
    #     )
    #     self.assertTrue(dataframes_are_equal(expected, result))

    def test_process_interactions_integration_test(self):
        interactions = []
        from io import StringIO
        file_ = StringIO(
            "source\ttarget\tlabel\tpmid\tpsimi\n"
            "O95398\tP51828\tactivation\t1\tMI:1\n"
            "O95398\tP51828\tbinding/association\t2,1\tNone,MI:2\n"
            "O43749\tO60262\tactivation\t3\tMI:3\n"
            "Q8NHA4\tQ13131\tactivation\t-\t-\n"
            "Q8NHA4\tQ13131\tbinding/association\tNaN\tMI:3\n"
            "P35626\tQ8NHA4\tphosphorylation\t\t\n"
            "Q8NHA4\tP35626\tphosphorylation\t\t\n"
            "P54840\tQ13131\tinhibition\t4\tMI:4\n"
            "P54840\tP54840\tinhibition\t4\tMI:4\n"
            "P35626\tO95398\tNone\tNone\tNone\n"
        )
        file_.readline()

        for line in file_:
            s, t, l, x, y = line.split('\t')
            interactions.append((s, t, l, x, y))

        iframe = make_interaction_frame(
            [s.strip() for s, *_ in interactions],
            [t.strip() for _, t, *_ in interactions],
            [l.strip() for _, _, l, *_ in interactions],
            [x.strip() for _, _, _, x, _ in interactions],
            [y.strip() for _, _, _, _, y in interactions]
        )
        result = process_interactions(
            iframe, drop_nan='default', allow_self_edges=False,
            allow_duplicates=False, exclude_labels=['inhibition'],
            min_counts=2, merge=True
        )
        expected = pd.DataFrame(
            {
                SOURCE: ['O95398', 'O43749', 'Q13131'],
                TARGET: ['P51828', 'O60262', 'Q8NHA4'],
                LABEL: [
                    'Activation,Binding/association',
                    'Activation',
                    'Activation,Binding/association'
                ],
                PUBMED: ["1,2", "3", None],
                EXPERIMENT_TYPE: ["MI:1|MI:2,None", "MI:3", None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        self.assertTrue(result.equals(expected))

class TestMapNetwork(TestCase):

    def test_can_map_one_to_one(self):
        network_map = {
            'A': ['C'],
            'B': ['D'],
            'C': ['C']
        }
        iframe = pd.DataFrame(
            {
                SOURCE: ['A', 'B', 'C'],
                TARGET: ['C', 'B', 'A'],
                LABEL: [None, None, None],
                PUBMED: [None, None, None],
                EXPERIMENT_TYPE: [None, None, None]

            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        expected = pd.DataFrame(
            {
                SOURCE: ['C', 'D', 'C'],
                TARGET: ['C', 'D', 'C'],
                LABEL: [None, None, None],
                PUBMED: [None, None, None],
                EXPERIMENT_TYPE: [None, None, None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        result = map_network_accessions(
            iframe, network_map, drop_nan=None, allow_self_edges=True,
            allow_duplicates=True, min_counts=None, merge=False
        )
        self.assertTrue(dataframes_are_equal(result, expected))

    def test_can_map_one_to_many(self):
        network_map = {
            'A': ['C', 'D'],
        }
        iframe = pd.DataFrame(
            {
                SOURCE: ['A'],
                TARGET: ['A'],
                LABEL: [None],
                PUBMED: [None],
                EXPERIMENT_TYPE: [None]
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        expected = pd.DataFrame(
            {
                SOURCE: ['C', 'C', 'C', 'D'],
                TARGET: ['C', 'D', 'D', 'D'],
                LABEL: [None] * 4,
                PUBMED: [None] * 4,
                EXPERIMENT_TYPE: [None] * 4
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        result = map_network_accessions(
            iframe, network_map, drop_nan=None, allow_self_edges=True,
            allow_duplicates=True, min_counts=None, merge=False
        )
        self.assertTrue(dataframes_are_equal(result, expected))

    def test_will_correctly_map_meta_columns(self):
        network_map = {
            'A': ['C', 'D'],
        }
        iframe = pd.DataFrame(
            {
                SOURCE: ['A'],
                TARGET: ['A'],
                LABEL: [None],
                PUBMED: ['1,2'],
                EXPERIMENT_TYPE: ['MI:1|MI:2,None']
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        expected = pd.DataFrame(
            {
                SOURCE: ['C', 'C', 'C', 'D'],
                TARGET: ['C', 'D', 'D', 'D'],
                LABEL: [None] * 4,
                PUBMED: ['1,2'] * 4,
                EXPERIMENT_TYPE: ['MI:1|MI:2,None'] * 4
            },
            columns=[SOURCE, TARGET, LABEL, PUBMED, EXPERIMENT_TYPE]
        )
        result = map_network_accessions(
            iframe, network_map, drop_nan=None, allow_self_edges=True,
            allow_duplicates=True, min_counts=None, merge=False
        )
        self.assertTrue(dataframes_are_equal(result, expected))

max_stars_repo_name: Aniket-Pradhan/pyPPI

max_issues_repo_name: Aniket-Pradhan/pyPPI

max_forks_repo_name: Aniket-Pradhan/pyPPI


text:
import tensorflow as tf
import networkx as nx
from utils import graph_generator as gg
import numpy as np

# gg.watts_strogats(32, 4, 0.75)

def conv_block(input, kernels, filters, strides, dropout_rate, training, scope):
    with tf.variable_scope(scope):
        input = tf.nn.relu(input)
        input = tf.layers.separable_conv2d(input, filters=filters, kernel_size=[kernels, kernels], strides=[strides, strides], padding='SAME')
        input = tf.layers.batch_normalization(input, training=training)
        input = tf.layers.dropout(input, rate=dropout_rate, training=training)
    return input

def build_stage(input, filters, dropout_rate, training, graph_data, scope):
    graph, graph_order, start_node, end_node = graph_data

    interms = {}
    with tf.variable_scope(scope):
        for node in graph_order:
            if node in start_node:
                interm = conv_block(input, 3, filters, 2, dropout_rate, training, scope='node' + str(node))
                interms[node] = interm
            else:
                in_node = list(nx.ancestors(graph, node))
                if len(in_node) > 1:
                    with tf.variable_scope('node' + str(node)):
                        weight = tf.get_variable('sum_weight', shape=len(in_node), dtype=tf.float32, constraint=lambda x: tf.clip_by_value(x, 0, np.infty))
                        weight = tf.nn.sigmoid(weight)
                        interm = weight[0] * interms[in_node[0]]
                        for idx in range(1, len(in_node)):
                            interm += weight[idx] * interms[in_node[idx]]
                        interm = conv_block(interm, 3, filters, 1, dropout_rate, training, scope='conv_block' + str(node))
                        interms[node] = interm
                elif len(in_node) == 1:
                    interm = conv_block(interms[in_node[0]], 3, filters, 1, dropout_rate, training, scope='node' + str(node))
                    interms[node] = interm

        output = interms[end_node[0]]
        for idx in range(1, len(end_node)):
            output += interms[end_node[idx]]

        return output

def small_regime(input, stages, filters, classes, dropout_rate, graph_model, graph_param, graph_file_path, init_subsample, training):
    with tf.variable_scope('conv1'):
        if init_subsample is True:
            input = tf.layers.separable_conv2d(input, filters=int(filters/2), kernel_size=[3, 3], strides=[2, 2], padding='SAME')
            input = tf.layers.batch_normalization(input, training=training)
        else:
            input = tf.layers.separable_conv2d(input, filters=int(filters / 2), kernel_size=[3, 3], strides=[1, 1],
                                               padding='SAME')

    input = conv_block(input, 3, filters, 2, dropout_rate, training, 'conv2')

    for stage in range(3, stages+1):
        graph_data = gg.graph_generator(graph_model, graph_param, graph_file_path, 'conv' + str(stage) + '_' + graph_model)
        input = build_stage(input, filters, dropout_rate, training, graph_data, 'conv' + str(stage))
        filters *= 2

    with tf.variable_scope('classifier'):
        input = conv_block(input, 1, 1280, 1, dropout_rate, training, 'conv_block_classifier')
        input = tf.layers.average_pooling2d(input, pool_size=input.shape[1:3], strides=[1, 1])
        input = tf.layers.flatten(input)
        input = tf.layers.dense(input, units=classes)

    return input

def regular_regime(input, stages, filters, classes, dropout_rate, graph_model, graph_param, graph_file_path, training):
    with tf.variable_scope('conv1'):
        input = tf.layers.separable_conv2d(input, filters=int(filters/2), kernel_size=[3, 3], strides=[2, 2], padding='SAME')
        input = tf.layers.batch_normalization(input, training=training)

    for stage in range(2, stages+1):
        graph_data = gg.graph_generator(graph_model, graph_param, graph_file_path, 'conv' + str(stage) + '_' + graph_model)
        input = build_stage(input, filters, dropout_rate, training, graph_data, 'conv' + str(stage))
        filters *= 2

    with tf.variable_scope('classifier'):
        input = conv_block(input, 1, 1280, 1, dropout_rate, training, 'conv_block_classifier')
        input = tf.layers.average_pooling2d(input, pool_size=input.shape[1:3], strides=[1, 1])
        input = tf.layers.flatten(input)
        input = tf.layers.dense(input, units=classes)
        input = tf.layers.dropout(input, rate=dropout_rate)

    return input

def my_regime(input, stages, filters, classes, dropout_rate, graph_model, graph_param, graph_file_path, init_subsample, training): #regular regime 기반
    with tf.variable_scope('conv1'):
        if init_subsample is True:
            input = tf.layers.separable_conv2d(input, filters=int(filters/2), kernel_size=[3, 3], strides=[2, 2], padding='SAME')
            input = tf.layers.batch_normalization(input, training=training)
        else:
            input = tf.layers.separable_conv2d(input, filters=int(filters / 2), kernel_size=[3, 3], strides=[1, 1],
                                               padding='SAME')
            input = tf.layers.batch_normalization(input, training=training)

    for stage in range(2, stages+1):
        graph_data = gg.graph_generator(graph_model, graph_param, graph_file_path, 'conv' + str(stage) + '_' + graph_model)
        input = build_stage(input, filters, dropout_rate, training, graph_data, 'conv' + str(stage))
        filters *= 2

    with tf.variable_scope('classifier'):
        input = conv_block(input, 1, 1280, 1, dropout_rate, training, 'conv_block_classifier')
        input = tf.layers.average_pooling2d(input, pool_size=input.shape[1:3], strides=[1, 1])
        input = tf.layers.flatten(input)
        input = tf.layers.dropout(input, rate=0.3, training=training)
        input = tf.layers.dense(input, units=classes)

    return input

def my_small_regime(input, stages, filters, classes, dropout_rate, graph_model, graph_param, graph_file_path, init_subsample, training): #regular regime 기반
    with tf.variable_scope('conv1'):
        if init_subsample is True:
            input = tf.layers.separable_conv2d(input, filters=int(filters/2), kernel_size=[3, 3], strides=[2, 2], padding='SAME')
            input = tf.layers.batch_normalization(input, training=training)
        else:
            input = tf.layers.separable_conv2d(input, filters=int(filters / 2), kernel_size=[3, 3], strides=[1, 1],
                                               padding='SAME')
            input = tf.layers.batch_normalization(input, training=training)

    input = conv_block(input, 3, filters, 1, dropout_rate, training, 'conv2')

    for stage in range(3, stages+1):
        graph_data = gg.graph_generator(graph_model, graph_param, graph_file_path, 'conv' + str(stage) + '_' + graph_model)
        input = build_stage(input, filters, dropout_rate, training, graph_data, 'conv' + str(stage))
        filters *= 2

    with tf.variable_scope('classifier'):
        input = conv_block(input, 1, 1280, 1, dropout_rate, training, 'conv_block_classifier')
        input = tf.layers.average_pooling2d(input, pool_size=input.shape[1:3], strides=[1, 1])
        input = tf.layers.flatten(input)
        input = tf.layers.dropout(input, rate=0.3, training=training)
        input = tf.layers.dense(input, units=classes)

    return input

ext: py

reason: import numpy,import networkx


text:
import numpy as np
import os
import math

def dot_product(a,b):
    return(a[0]*b[0]+a[1]*b[1]+a[2]*b[2])

def angle(a,b):
    dot=dot_product(a,b)
    mag_a=(a[0]**2+a[1]**2+a[2]**2)**0.5
    mag_b=(b[0]**2+b[1]**2+b[2]**2)**0.5
    return math.acos(dot/(mag_a*mag_b))
a=np.zeros(3)
b=np.zeros(3)
print("Use Rectangular Coordinats x, y and z")
for i in range(3):
    a[i]=int(input("write the"+str(i)+"element of a"))
    b[i]=int(input("write the"+str(i)+"element of b"))
print("The vectors are\n",a,"\n",b)
print("Dot Product is", dot_product(a,b))
print("Angle between two vectors is (in radians)", angle(a,b), "and in degrees",(180/math.pi)*angle(a,b))

max_stars_repo_path: Dot Product.py

max_stars_repo_name: kennethsequeira/Hello-world

max_issues_repo_path: Dot Product.py

max_issues_repo_name: kennethsequeira/Hello-world

max_forks_repo_path: Dot Product.py

max_forks_repo_name: kennethsequeira/Hello-world

reason: import numpy


text:
"""
Tests for pylearn2.expr.basic
"""

import numpy as np

from pylearn2.expr.basic import log_sum_exp
from pylearn2.utils import sharedX

def test_log_sum_exp_1():
    """
    Tests that the stable log sum exp matches the naive one for
    values near 1.
    """

    rng = np.random.RandomState([2015, 2, 9])
    x = 1. + rng.randn(5) / 10.
    naive = np.log(np.exp(x).sum())
    x = sharedX(x)
    stable = log_sum_exp(x).eval()
    assert np.allclose(naive, stable)

def test_log_sum_exp_2():
    """
    Tests that the stable log sum exp succeeds for extreme values."
    """

    x = np.array([-100., 100.])
    x = sharedX(x)
    stable = log_sum_exp(x).eval()
    assert np.allclose(stable, 100.)


text:
'''
This example script computes the finite difference density
derivative using both numerical integration and complex pole
summation. It serves as a demonstration and a validation of
the pole summation method. For the given parameters below,
the numerical energy grid contains 708 energy points (it would
be larger if the lower bound wasn't truncated by Emin) and the
complex pole summation contains only 26. Not only do these
results agree within 1 part in 10,000 at 1/16th the compu-
tational cost, due to its construction, the complex pole sum-
mation is more accurate. Where possible, the complex pole
summation method should be used over numerical integration.
'''

import matplotlib.pyplot as plt
import numpy as np
import nanonet.tb as tb
from nanonet.negf.greens_functions import simple_iterative_greens_function, surface_greens_function
from nanonet.negf import pole_summation_method
from nanonet.negf.pole_summation_method import fermi_fun

# First we design a tight-binding model. We choose a 15 site model
# so that it is symmetric and that features may be clearly ob-
# served, one or two site models would look to similar to clearly
# differentiate whether something erroneously similar was happening

a = tb.Orbitals('A')
a.add_orbital('s', 0)

tb.Orbitals.orbital_sets = {'A': a}
tb.set_tb_params(PARAMS_A_A={'ss_sigma': -1})

xyz_file = """15
A cell
A1       0.0000000000    0.0000000000    0.0000000000
A2       1.0000000000    0.0000000000    0.0000000000
A3       2.0000000000    0.0000000000    0.0000000000
A4       3.0000000000    0.0000000000    0.0000000000
A5       4.0000000000    0.0000000000    0.0000000000
A6       5.0000000000    0.0000000000    0.0000000000
A7       6.0000000000    0.0000000000    0.0000000000
A8       7.0000000000    0.0000000000    0.0000000000
A9       8.0000000000    0.0000000000    0.0000000000
A10      9.0000000000    0.0000000000    0.0000000000
A11     10.0000000000    0.0000000000    0.0000000000
A12     11.0000000000    0.0000000000    0.0000000000
A13     12.0000000000    0.0000000000    0.0000000000
A14     13.0000000000    0.0000000000    0.0000000000
A15     14.0000000000    0.0000000000    0.0000000000
"""

h = tb.Hamiltonian(xyz=xyz_file, nn_distance=1.1)
h.initialize()
h.set_periodic_bc([[0, 0, 1.0]])
h_l, h_0, h_r = h.get_hamiltonians()

# Now that the Hamiltonian is constructed within the TB
# framework, we set our numerical parameters to be
# examined. We choose two endpoints mu +/- dmu,
# the temperature being evaluated, the relative tolerance
# of the pole summation and the numerical integration

# Emin = -3.98, is the point where there are
# no more states to the left, we can use this
# to reduce number of points in the evaluation

Emin = -3.98
muL = -3.9175
muR = -3.9025
muC = 0.5*(muL + muR)  # This is the energy the derivative is being evaluated at
kT = 0.001
reltol = 10**-8
p = np.ceil(-np.log(reltol))  # Integer number of kT away to get the desired relative tolerance.

lowbnd = max(Emin, muL - p*kT)
uppbnd = muR + p*kT
# We chose to have our energy spacing be at most 3*kT/40
numE = round((uppbnd-lowbnd)/(0.075*kT)) + 1

# numE = round((muR - muL + 2*p*kT)/(0.075*kT)) + 1

# We generate our grid for numerical integration, paying mind about the FD tails at muL-p*kT and muR + p*kT.
energy = np.linspace(lowbnd, uppbnd, numE)
# energy = np.linspace(muL - p*kT, muR + p*kT, numE)

# Initialize the storage of the surface Green's funs.
sgf_l = []
sgf_r = []

# We compute the numerical evaluation of the Green's
# function on the real energy grid. This is by far
# the most expensive part of this example.

for E in energy:
    # Note that though the surface Green's function technique is very fast, it can
    # have slight errors due to choice of numerical cutoffs, if the solution is
    # accurate the simple iterative will return the same answer immediately
    # the user can comment it out if they wish to trust those evaluations.
    sf = surface_greens_function(E, h_l, h_0, h_r, damp=0.00001j)
    L = sf[0]
    R = sf[1]

    L = simple_iterative_greens_function(E, h_l, h_0, h_r, damp=0.00001j, initialguess=L)
    R = simple_iterative_greens_function(E, h_r, h_0, h_l, damp=0.00001j, initialguess=R)

    sgf_l.append(L)
    sgf_r.append(R)

# Convert the lists into arrays
sgf_l = np.array(sgf_l)
sgf_r = np.array(sgf_r)

# Compute the energy-wise Green's function
num_sites = h_0.shape[0]
gf = np.linalg.pinv(np.multiply.outer(energy, np.identity(num_sites)) - h_0 - sgf_l - sgf_r)

# We preallocate the arrays to store our integrated solutions
backwardsint = np.zeros(num_sites)
forwardsint = np.zeros(num_sites)
centralint = np.zeros(num_sites)
LDOS = []

# The integral is just a sum of the weighted diagonals for whichever integral we are computing
for j, E in enumerate(energy):
    gf0 = gf[j, :, :]
    gdiag = np.diag(gf0)
    backwardsint = backwardsint + gdiag*(fermi_fun(E, muC, kT) - fermi_fun(E, muL, kT))
    forwardsint = forwardsint + gdiag*(fermi_fun(E, muR, kT) - fermi_fun(E, muC, kT))
    centralint = centralint + gdiag*(fermi_fun(E, muR, kT) - fermi_fun(E, muL, kT))
    LDOS.append(np.sum(gdiag))

# Now we have completed our numerical integration,
# we move onto pole summation, simply choose the
# chemical potentials the temperature and the rel-
# ative error as before and the method spits out
# one set of poles and two sets of residues for
# the backwards and forwards differences with the
# central being their sum.
poles, residuesL, residuesR = pole_summation_method.pole_finite_difference(muL, muR, kT, reltol)

# Allocate the surface green's functions for poles.
sgf2_l = []
sgf2_r = []

# Do each of the poles, note that the imaginary
# part of the energy is the damping parameter
# and the real part works as normal.
for E in poles:
    # Note that though the surface Green's function technique is very fast, it can
    # have slight errors due to choice of numerical cutoffs, if the solution is
    # accurate the simple iterative will return the same answer immediately
    sf = surface_greens_function(np.real(E), h_l, h_0, h_r, damp=1j*np.imag(E))
    L2 = sf[0]
    R2 = sf[1]

    L2 = simple_iterative_greens_function(np.real(E), h_l, h_0, h_r, damp=1j*np.imag(E), initialguess=L2)
    R2 = simple_iterative_greens_function(np.real(E), h_r, h_0, h_l, damp=1j*np.imag(E), initialguess=R2)

    sgf2_l.append(L2)
    sgf2_r.append(R2)

sgf2_l = np.array(sgf2_l)
sgf2_r = np.array(sgf2_r)

gf2 = np.linalg.pinv(np.multiply.outer(poles, np.identity(num_sites)) - h_0 - sgf2_l - sgf2_r)

backwardspole = np.zeros(num_sites)
forwardspole = np.zeros(num_sites)
centralpole = np.zeros(num_sites)

# Add up the weighted contributions
for j, E in enumerate(poles):
    gf00 = gf2[j, :, :]
    gdiag2 = np.diag(gf00)
    backwardspole = backwardspole + residuesL[j]*gdiag2
    forwardspole = forwardspole + residuesR[j]*gdiag2
    centralpole = centralpole + (residuesR[j] + residuesL[j])*gdiag2

# dE to correct numerical integration
dE = (energy[1]-energy[0])
dmu = (muR-muL)/2

# Backwards Finite Diff First Derivative
backwardsint = -2*np.imag(backwardsint)*dE/(1*dmu)
backwardspole = -2*np.imag(backwardspole)/(1*dmu)

# Forwards Finite Diff First Derivative
forwardsint = -2*np.imag(forwardsint)*dE/(1*dmu)
forwardspole = -2*np.imag(forwardspole)/(1*dmu)

# Centred Finite Diff First Derivative
centralint = -2*np.imag(centralint)*dE/(2*dmu)
centralpole = -2*np.imag(centralpole)/(2*dmu)

# LDOS
LDOS = -2*np.imag(LDOS)
#plt.plot(energy,LDOS, color='#464646', linestyle='solid')
#plt.show()

# Works
plt.plot(backwardsint, color='#951158', linestyle='dashed')
plt.plot(backwardspole, color='#D40F7D')
plt.plot(forwardsint, color='#00808B', linestyle='dashed')
plt.plot(forwardspole, color='#00AEC7')
plt.plot(centralint, color='#899600', linestyle='dashed')
plt.plot(centralpole, color='#C4D600')

plt.show()

# Now we compute the relative difference between the two answers, note this isn't really an error
# as we're computing the same approximate thing two different ways, it's to check if they agree.
errbackward = np.log10(0.5*np.linalg.norm(backwardsint-backwardspole)/np.linalg.norm(backwardsint+backwardspole))
errforward = np.log10(0.5*np.linalg.norm(forwardsint-forwardspole)/np.linalg.norm(forwardsint+forwardspole))
errcentred = np.log10(0.5*np.linalg.norm(centralint-centralpole)/np.linalg.norm(centralint+centralpole))

print("Relative backwards differences discrepancy = 10^"+f'{errbackward:.3f}')
print("Relative forward differences discrepancy = 10^"+f'{errforward:.3f}')
print("Relative central differences discrepancy = 10^"+f'{errcentred:.3f}')

max_stars_repo_name: freude/NanoNet

max_issues_repo_name: floatingCatty/NanoNet

max_forks_repo_name: floatingCatty/NanoNet


text:
# -*- coding: UTF-8 -*
import numpy as np
import tensorflow as tf
from Dataset import save_images
from ops import conv2d, lrelu, instance_norm, de_conv, fully_connect
import scipy
import tarfile, sys
from six.moves import urllib
import os, math

class Gaze_GAN(object):

    # build model
    def __init__(self, dataset, config):

        self.dataset = dataset

        # input hyper
        self.output_size = config.image_size
        self.channel = dataset.channel
        self.batch_size = config.batch_size
        self.pos_number = config.pos_number
        self.pretrain_model_index = config.pretrain_model_index
        self.pretrain_model_dir = config.pretrain_model_dir
        self.evaluation_path = config.evaluation_path
        self.MODEL_DIR = config.MODEL_DIR

        # output hyper
        self.sample_dir = config.sample_dir
        self.model_dir = config.model_dir
        self.log_dir = config.log_dir
        self.is_supervised = config.is_supervised

        self.result_dir = config.result_dir
        self.testresult_dir = config.testresult_dir
        self.batch_num = dataset.test_num / self.batch_size

        # model hyper
        self.lam_percep = config.lam_percep
        self.lam_recon = config.lam_recon
        self.lam_ss = config.lam_ss
        self.loss_type = config.loss_type
        self.use_sp = config.use_sp
        self.log_vars = []

        # trainning hyper
        self.g_learning_rate = config.g_learning_rate
        self.d_learning_rate = config.d_learning_rate

        self.height = 30
        self.width = 50

        self.beta1 = config.beta1
        self.beta2 = config.beta2

        self.lr_init = config.learning_rate_init
        self.max_iters = config.max_iters

        # placeholder
        self.input_left_labels = tf.placeholder(tf.float32, [self.batch_size, self.pos_number])
        self.input_right_labels = tf.placeholder(tf.float32, [self.batch_size, self.pos_number])
        self.input = tf.placeholder(tf.float32, [self.batch_size, self.output_size, self.output_size, self.channel])
        self.mask = tf.placeholder(tf.float32, [self.batch_size, self.output_size, self.output_size, self.channel])
        self.domain_label = tf.placeholder(tf.int32, [self.batch_size])

        self.list_all_members()

    def list_all_members(self):
        for name, value in vars(self).items():
            print('%s=%s' % (name, value))

    def build_model(self):

        self.lr_decay = tf.placeholder(tf.float32, None, name='lr_decay')
        self.incomplete_img = self.input * (1 - self.mask)
        self.local_input_left = self.crop_and_resize(self.input, self.input_left_labels)
        self.local_input_right = self.crop_and_resize(self.input, self.input_right_labels)

        self.angle_invar_left_real = self.encode(self.local_input_left, reuse=False)
        self.angle_invar_right_real = self.encode(self.local_input_right, reuse=True)

        self.recon_img = self.generator(self.incomplete_img, self.mask, self.angle_invar_left_real,
                                        self.angle_invar_right_real, reuse=False)

        self.local_recon_img_left = self.crop_and_resize(self.recon_img, self.input_left_labels)
        self.local_recon_img_right = self.crop_and_resize(self.recon_img, self.input_right_labels)

        self.angle_invar_left_recon = self.encode(self.local_recon_img_left, reuse=True)
        self.angle_invar_right_recon = self.encode(self.local_recon_img_right, reuse=True)

        self.new_recon_img = self.incomplete_img + self.recon_img * self.mask

        if self.is_supervised:

            self.D_real_gan_logits, self.real_logits_0, self.real_logits_1 \
                = self.discriminator(self.input, self.local_input_left, self.local_input_right,
                                                        self.angle_invar_left_real, self.angle_invar_right_real, reuse=False)
            self.D_fake_gan_logits, self.fake_logits_0, self.fake_logits_1 \
                = self.discriminator(self.new_recon_img, self.local_recon_img_left, self.local_recon_img_right,
                                                        self.angle_invar_left_real, self.angle_invar_right_real, reuse=True)
            self.real_class_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                                labels=tf.one_hot(tf.zeros(shape=[self.batch_size], dtype=tf.int32), 2), logits=self.real_logits_0)) + \
                                    tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                                labels=tf.one_hot(tf.ones(shape=[self.batch_size], dtype=tf.int32), 2), logits=self.real_logits_1))

            self.fake_class_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                                labels=tf.one_hot(tf.zeros(shape=[self.batch_size], dtype=tf.int32), 2), logits=self.fake_logits_0)) + \
                                    tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
                                labels=tf.one_hot(tf.ones(shape=[self.batch_size], dtype=tf.int32), 2), logits=self.fake_logits_1))

        else:

            self.D_real_gan_logits = self.discriminator(self.input, self.local_input_left, self.local_input_right,
                                                        self.angle_invar_left_real, self.angle_invar_right_real, reuse=False)
            self.D_fake_gan_logits = self.discriminator(self.new_recon_img, self.local_recon_img_left, self.local_recon_img_right,
                                                        self.angle_invar_left_real, self.angle_invar_right_real, reuse=True)

        if self.loss_type == 0:
            self.d_gan_loss = self.loss_hinge_dis(self.D_real_gan_logits, self.D_fake_gan_logits)
            self.g_gan_loss = self.loss_hinge_gen(self.D_fake_gan_logits)
        elif self.loss_type == 1:
            self.d_gan_loss = self.loss_dis(self.D_real_gan_logits, self.D_fake_gan_logits)
            self.g_gan_loss = self.loss_gen(self.D_fake_gan_logits)
        elif self.loss_type == 2:
            self.d_gan_loss = self.d_lsgan_loss(self.D_real_gan_logits, self.D_fake_gan_logits)
            self.g_gan_loss = self.g_lsgan_loss(self.D_fake_gan_logits)

        self.percep_loss = tf.reduce_mean(tf.abs(self.angle_invar_left_real - self.angle_invar_left_recon)) + \
                           tf.reduce_mean(tf.abs(self.angle_invar_right_real - self.angle_invar_right_recon))

        self.recon_loss = tf.reduce_mean(tf.reduce_sum(tf.abs(self.new_recon_img - self.input),
                                                              axis=[1, 2, 3])/ (self.width * self.height * self.channel))

        if self.is_supervised:
            self.D_loss = self.d_gan_loss + self.lam_ss * self.real_class_loss
            self.G_loss = self.g_gan_loss + self.lam_recon * self.recon_loss \
                          + self.lam_percep * self.percep_loss + self.lam_ss * self.fake_class_loss
        else:
            self.D_loss = self.d_gan_loss
            self.G_loss = self.g_gan_loss + self.lam_recon * self.recon_loss + self.lam_percep * self.percep_loss

        self.log_vars.append(("D_loss", self.D_loss))
        self.log_vars.append(("G_loss", self.G_loss))

        self.t_vars = tf.trainable_variables()
        self.d_vars = [var for var in self.t_vars if 'discriminator' in var.name]
        self.ed_vars = [var for var in self.t_vars if 'generator' in var.name]
        self.e_vars = [var for var in self.t_vars if 'encode' in var.name]

        self.saver = tf.train.Saver()
        self.pretrain_saver = tf.train.Saver(self.e_vars)

        for k, v in self.log_vars:
            tf.summary.scalar(k, v)

    def crop_and_resize(self,input, boxes):
       return tf.image.crop_and_resize(input, boxes=boxes,box_ind=range(0, self.batch_size),
                                       crop_size=[self.output_size / 2, self.output_size / 2])

    def loss_gen(self, d_fake_logits):
        return tf.reduce_mean(tf.nn.softplus(-d_fake_logits))

    def loss_dis(self, d_real_logits, d_fake_logits):
        l1 = tf.reduce_mean(tf.nn.softplus(-d_real_logits))
        l2 = tf.reduce_mean(tf.nn.softplus(d_fake_logits))
        return l1 + l2

    def loss_hinge_dis(self, d_real_logits, d_fake_logits):
        loss = tf.reduce_mean(tf.nn.relu(1.0 - d_real_logits))
        loss += tf.reduce_mean(tf.nn.relu(1.0 + d_fake_logits))
        return loss

    def loss_hinge_gen(self, d_fake_logits):
        loss = - tf.reduce_mean(d_fake_logits)
        return loss

    def d_lsgan_loss(self, d_real_logits, d_fake_logits):
        return tf.reduce_mean((d_real_logits - 0.9)*2) + tf.reduce_mean((d_fake_logits)*2)

    def g_lsgan_loss(self, d_fake_logits):
        return tf.reduce_mean((d_fake_logits - 0.9)*2)

    def test(self):

        init = tf.global_variables_initializer()
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        with tf.Session(config=config) as sess:
            sess.run(init)
            ckpt = tf.train.get_checkpoint_state(self.model_dir)
            print('Load checkpoint')
            if ckpt and ckpt.model_checkpoint_path:
                print ckpt.model_checkpoint_path
                self.saver.restore(sess, ckpt.model_checkpoint_path)
                print('Load Succeed!')
            else:
                print 'Do not exists any checkpoint,Load Failed!'
                exit()

            _,_,_, testbatch, testmask = self.dataset.input()
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            batch_num = 1000 / self.batch_size
            for j in range(batch_num):
                real_test_batch, real_eye_pos = sess.run([testbatch, testmask])
                batch_masks, batch_left_eye_pos, batch_right_eye_pos = self.get_Mask_and_pos(real_eye_pos)
                f_d = {self.input: real_test_batch,
                       self.mask: batch_masks,
                       self.input_left_labels: batch_left_eye_pos,
                       self.input_right_labels: batch_right_eye_pos}

                for i in range(self.batch_size):

                    output = sess.run([self.input, self.new_recon_img], feed_dict=f_d)

                    save_images(np.reshape(output[0][i], newshape=(1, self.output_size, self.output_size, self.channel)),
                                [1, 1], '{}/{:02d}_{:2d}_input.jpg'.format(self.testresult_dir + "4", j, i))
                    save_images(np.reshape(output[1][i], newshape=(1, self.output_size, self.output_size, self.channel)),
                                [1, 1], '{}/{:02d}_{:2d}_recon.jpg'.format(self.testresult_dir + "3", j, i))
                    save_images(np.reshape(output[3][i], newshape=(1, self.output_size/2, self.output_size/2, self.channel)),
                                [1, 1], '{}/{:02d}_{:2d}_recon_local_right.jpg'.format(self.testresult_dir, j, i))

            coord.request_stop()
            coord.join(threads)

    def train(self):

        opti_D = tf.train.AdamOptimizer(self.d_learning_rate * self.lr_decay,beta1=self.beta1, beta2=self.beta2).\
                                        minimize(loss=self.D_loss, var_list=self.d_vars)
        opti_G = tf.train.AdamOptimizer(self.g_learning_rate * self.lr_decay,beta1=self.beta1, beta2=self.beta2).\
                                        minimize(loss=self.G_loss, var_list=self.ed_vars)

        init = tf.global_variables_initializer()
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True

        with tf.Session(config=config) as sess:

            sess.run(init)

            start_step = 0
            ckpt = tf.train.get_checkpoint_state(self.model_dir)
            if ckpt and ckpt.model_checkpoint_path:
                start_step = int(ckpt.model_checkpoint_path.split('model_', 2)[1].split('.', 2)[0])
                self.saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                try:
                    self.pretrain_saver.restore(sess, os.path.join(self.pretrain_model_dir,
                                                               'model_{:06d}.ckpt'.format(self.pretrain_model_index)))
                except:
                    print(" Self-Guided Model path may not be correct")

            summary_op = tf.summary.merge_all()
            summary_writer = tf.summary.FileWriter(self.log_dir, sess.graph)
            step = start_step
            lr_decay = self.lr_init

            print("Start read dataset")

            image_path, train_images, train_eye_pos, test_images, test_eye_pos = self.dataset.input()
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            print("Start entering the looping")
            real_test_batch, real_test_pos = sess.run([test_images, test_eye_pos])

            while step <= self.max_iters:

                if step > 20000 and step % 2000 == 0:
                    lr_decay = (self.max_iters - step) / float(self.max_iters - 20000)

                real_batch_image_path, real_batch_image, real_eye_pos = sess.run([image_path, train_images, train_eye_pos])
                batch_masks, batch_left_eye_pos, batch_right_eye_pos = self.get_Mask_and_pos(real_eye_pos)

                f_d = {self.input: real_batch_image,
                       self.mask: batch_masks,
                       self.input_left_labels: batch_left_eye_pos,
                       self.input_right_labels: batch_right_eye_pos,
                       self.lr_decay: lr_decay}

                # optimize D
                sess.run(opti_D, feed_dict=f_d)
                # optimize G
                sess.run(opti_G, feed_dict=f_d)

                summary_str = sess.run(summary_op, feed_dict=f_d)
                summary_writer.add_summary(summary_str, step)

                if step % 500 == 0:

                    if self.is_supervised:

                        output_loss = sess.run([self.D_loss, self.G_loss, self.lam_recon * self.recon_loss,
                                                self.lam_percep * self.percep_loss, self.real_class_loss, self.fake_class_loss], feed_dict=f_d)
                        print("step %d D_loss=%.8f, G_loss=%.4f, Recon_loss=%.4f, Percep_loss=%.4f, "
                              "Real_class_loss=%.4f, Fake_class_loss=%.4f, lr_decay=%.4f" %
                              (step, output_loss[0], output_loss[1], output_loss[2], output_loss[3], output_loss[4], output_loss[5], lr_decay))

                    else:

                        output_loss = sess.run([self.D_loss, self.G_loss, self.lam_recon * self.recon_loss,
                                                self.lam_percep * self.percep_loss], feed_dict=f_d)

                        print("step %d D_loss=%.8f, G_loss=%.4f, Recon_loss=%.4f, Percep_loss=%.4f, lr_decay=%.4f" %
                                        (step, output_loss[0], output_loss[1], output_loss[2], output_loss[3], lr_decay))

                if np.mod(step, 2000) == 0:

                    train_output_img = sess.run([self.local_input_left, self.local_input_right, self.incomplete_img,
                                                 self.recon_img,self.new_recon_img, self.local_recon_img_left,
                                                 self.local_recon_img_right], feed_dict=f_d)

                    batch_masks, batch_left_eye_pos, batch_right_eye_pos = self.get_Mask_and_pos(real_test_pos)

                    #for test
                    f_d = {self.input: real_test_batch, self.mask: batch_masks,
                           self.input_left_labels: batch_left_eye_pos, self.input_right_labels: batch_right_eye_pos,
                           self.lr_decay: lr_decay}

                    test_output_img = sess.run([self.incomplete_img, self.recon_img, self.new_recon_img], feed_dict=f_d)
                    output_concat = np.concatenate([real_batch_image, train_output_img[2], train_output_img[3],
                                                    train_output_img[4]], axis=0)
                    local_output_concat = np.concatenate([train_output_img[0], train_output_img[1], train_output_img[5],
                                                         train_output_img[6]], axis=0)
                    test_output_concat = np.concatenate([real_test_batch, test_output_img[0], test_output_img[2],
                                                         test_output_img[1]], axis=0)

                    save_images(local_output_concat, [local_output_concat.shape[0] / self.batch_size, self.batch_size],
                                            '{}/{:02d}_local_output.jpg'.format(self.sample_dir, step))
                    save_images(output_concat, [output_concat.shape[0]/self.batch_size, self.batch_size],
                                            '{}/{:02d}_output.jpg'.format(self.sample_dir, step))
                    save_images(test_output_concat, [test_output_concat.shape[0]/self.batch_size, self.batch_size],
                                            '{}/{:02d}_test_output.jpg'.format(self.sample_dir, step))

                if np.mod(step, 20000) == 0:
                    self.Inception_score(sess, test_images, test_eye_pos, step)
                    self.FID_score(sess, test_images, test_eye_pos, step)
                    self.saver.save(sess, os.path.join(self.model_dir, 'model_{:06d}.ckpt'.format(step)))

                step += 1

            save_path = self.saver.save(sess, os.path.join(self.model_dir, 'model_{:06d}.ckpt'.format(step)))
            summary_writer.close()

            coord.request_stop()
            coord.join(threads)

            print "Model saved in file: %s" % save_path

    def discriminator(self, incom_x, local_x_left, local_x_right, guided_fp_left, guided_fp_right, reuse=False):

        with tf.variable_scope("discriminator") as scope:

            if reuse == True:
                scope.reuse_variables()
            # Global Discriminator Dg
            x = incom_x
            for i in range(6):
                output_dim = np.minimum(16 * np.power(2, i+1), 256)
                x = lrelu(conv2d(x, output_dim=output_dim, use_sp=self.use_sp, name='dis_conv_global_{}'.format(i)))

            x = tf.reshape(x, shape=[self.batch_size, -1])
            ful_global = fully_connect(x, output_size=output_dim, use_sp=self.use_sp, scope='dis_FC1')

            if self.is_supervised:

                with tf.variable_scope("local_d"):
                    # Local Discriminator Dl
                    x = local_x_left
                    for i in range(5):
                        output_dim = np.minimum(16 * np.power(2, i+1), 256)
                        x = lrelu(conv2d(x, output_dim=output_dim, use_sp=self.use_sp, name='dis_conv_local_{}'.format(i)))
                    x = tf.reshape(x, shape=[self.batch_size, -1])
                    logits0 = fully_connect(x, output_size=2, use_sp=self.use_sp, scope='dis_class')
                    ful_local_left = fully_connect(x, output_size=output_dim, use_sp=self.use_sp, scope='dis_FC2')

                with tf.variable_scope("local_d") as scope:
                    scope.reuse_variables()

                    x = local_x_right
                    for i in range(5):
                        output_dim = np.minimum(16 * np.power(2, i + 1), 256)
                        x = lrelu(conv2d(x, output_dim=output_dim, use_sp=self.use_sp, name='dis_conv_local_{}'.format(i)))

                    x = tf.reshape(x, shape=[self.batch_size, -1])
                    logits1 = fully_connect(x, output_size=2, use_sp=self.use_sp, scope='dis_class')
                    ful_local_right = fully_connect(x, output_size=output_dim, use_sp=self.use_sp, scope='dis_FC2')

                ful_local = tf.concat([ful_local_left, ful_local_right], axis=1)

            else:

                x = tf.concat([local_x_left, local_x_right], axis=3)
                for i in range(5):
                    output_dim = np.minimum(16 * np.power(2, i + 1), 256)
                    x = lrelu(conv2d(x, output_dim=output_dim, use_sp=self.use_sp, name='dis_conv_local_{}'.format(i)))

                x = tf.reshape(x, shape=[self.batch_size, -1])
                ful_local = fully_connect(x, output_size=output_dim * 2, use_sp=self.use_sp, scope='dis_FC2')

            # Concatenation
            ful = tf.concat([ful_global, ful_local, guided_fp_left, guided_fp_right], axis=1)
            ful = tf.nn.relu(fully_connect(ful, output_size=512, use_sp=self.use_sp, scope='dis_FC3'))
            gan_logits = fully_connect(ful, output_size=1, use_sp=self.use_sp, scope='dis_FC4')

            if self.is_supervised:
                return gan_logits, logits0, logits1

            else:
                return gan_logits

    def generator(self, input_x, img_mask, guided_fp_left, guided_fp_right, use_sp=False, reuse=False):

        with tf.variable_scope("generator") as scope:

            if reuse == True:
                scope.reuse_variables()

            x = tf.concat([input_x, img_mask], axis=3)
            u_fp_list = []
            for i in range(6):
                c_dim = np.minimum(16 * np.power(2, i), 256)
                if i == 0:
                    x = tf.nn.relu(
                        instance_norm(conv2d(x, output_dim=c_dim, k_w=7, k_h=7, d_w=1, d_h=1, use_sp=use_sp,
                                             name='conv_{}'.format(i)),scope='conv_IN_{}'.format(i)))
                else:
                    x = tf.nn.relu(
                        instance_norm(conv2d(x, output_dim=c_dim, k_w=4, k_h=4, d_w=2, d_h=2, use_sp=use_sp,
                                             name='conv_{}'.format(i)),scope='conv_IN_{}'.format(i)))
                    if i < 5:
                        u_fp_list.append(x)

            bottleneck = tf.reshape(x, shape=[self.batch_size, -1])
            bottleneck = fully_connect(bottleneck, output_size=256, use_sp=use_sp, scope='FC1')
            bottleneck = tf.concat([bottleneck, guided_fp_left, guided_fp_right], axis=1)

            de_x = tf.nn.relu(fully_connect(bottleneck, output_size=256*8*8, use_sp=use_sp, scope='FC2'))
            de_x = tf.reshape(de_x, shape=[self.batch_size, 8, 8, 256])

            for i in range(5):
                c_dim = np.maximum(256 / np.power(2, i), 16)
                output_dim = 16 * np.power(2, i)
                de_x = tf.nn.relu(instance_norm(de_conv(de_x, output_shape=[self.batch_size, output_dim, output_dim, c_dim], use_sp=use_sp,
                                                            name='deconv_{}'.format(i)), scope='deconv_IN_{}'.format(i)))
                if i < 4:
                    de_x = tf.concat([de_x, u_fp_list[len(u_fp_list) - (i+1)]], axis=3)

            recon_img1 = conv2d(de_x, output_dim=3, k_w=7, k_h=7, d_h=1, d_w=1, use_sp=use_sp, name='output_conv')
            return tf.nn.tanh(recon_img1)

    def encode(self, x, reuse=False):

        with tf.variable_scope("encode") as scope:

            if reuse == True:
                scope.reuse_variables()

            conv1 = tf.nn.relu(
                instance_norm(conv2d(x, output_dim=32, k_w=7, k_h=7, d_w=1, d_h=1, name='e_c1'), scope='e_in1'))
            conv2 = tf.nn.relu(
                instance_norm(conv2d(conv1, output_dim=64, k_w=4, k_h=4, d_w=2, d_h=2, name='e_c2'), scope='e_in2'))
            conv3 = tf.nn.relu(
                instance_norm(conv2d(conv2, output_dim=128, k_w=4, k_h=4, d_w=2, d_h=2, name='e_c3'), scope='e_in3'))
            conv4 = tf.nn.relu(
                instance_norm(conv2d(conv3, output_dim=128, k_w=4, k_h=4, d_w=2, d_h=2, name='e_c4'), scope='e_in4'))

            bottleneck = tf.reshape(conv4, [self.batch_size, -1])
            content = fully_connect(bottleneck, output_size=128, scope='e_ful1')

            return content

    def get_Mask_and_pos(self, eye_pos, flag=0):

        eye_pos = eye_pos
        batch_mask = []
        batch_left_eye_pos = []
        batch_right_eye_pos = []
        for i in range(self.batch_size):

            current_eye_pos = eye_pos[i]
            left_eye_pos = []
            right_eye_pos = []

            if flag == 0:

                mask = np.zeros(shape=[self.output_size, self.output_size, self.channel])
                scale = current_eye_pos[1] - 15
                down_scale = current_eye_pos[1] + 15
                l1_1 =int(scale)
                u1_1 =int(down_scale)
                #x
                scale = current_eye_pos[0] - 25
                down_scale = current_eye_pos[0] + 25
                l1_2 = int(scale)
                u1_2 = int(down_scale)

                mask[l1_1:u1_1, l1_2:u1_2, :] = 1.0
                left_eye_pos.append(float(l1_1)/self.output_size)
                left_eye_pos.append(float(l1_2)/self.output_size)
                left_eye_pos.append(float(u1_1)/self.output_size)
                left_eye_pos.append(float(u1_2)/self.output_size)

                scale = current_eye_pos[3] - 15
                down_scale = current_eye_pos[3] + 15
                l2_1 = int(scale)
                u2_1 = int(down_scale)

                scale = current_eye_pos[2] - 25
                down_scale = current_eye_pos[2] + 25
                l2_2 = int(scale)
                u2_2 = int(down_scale)

                mask[l2_1:u2_1, l2_2:u2_2, :] = 1.0

                right_eye_pos.append(float(l2_1) / self.output_size)
                right_eye_pos.append(float(l2_2) / self.output_size)
                right_eye_pos.append(float(u2_1) / self.output_size)
                right_eye_pos.append(float(u2_2) / self.output_size)

            batch_mask.append(mask)
            batch_left_eye_pos.append(left_eye_pos)
            batch_right_eye_pos.append(right_eye_pos)

        return np.array(batch_mask), np.array(batch_left_eye_pos), np.array(batch_right_eye_pos)

    #for columbia
    def get_eye_region(self, eye_pos, flag=0):

        batch_mask = []
        eye_pos = eye_pos
        # print eye_pos
        batch_left_eye_pos = []
        batch_right_eye_pos = []
        for i in range(self.batch_size):

            current_eye_pos = eye_pos[i]
            left_eye_pos = []
            right_eye_pos = []
            # eye
            if flag == 0:

                # left eye, y
                mask = np.zeros(shape=[self.output_size, self.output_size, self.channel])

                scale = current_eye_pos[0] - self.height / 2  # current_eye_pos[3] / 2
                down_scale = current_eye_pos[0] + self.height / 2  # current_eye_pos[3] / 2
                l1_1 = int(scale)
                u1_1 = int(down_scale)

                # x
                scale = current_eye_pos[1] - self.width / 2  # current_eye_pos[2] / 2
                down_scale = current_eye_pos[1] + self.width / 2  # current_eye_pos[2] / 2
                l1_2 = int(scale)
                u1_2 = int(down_scale)

                mask[l1_1:u1_1, l1_2:u1_2, :] = 1.0

                left_eye_pos.append(float(l1_1) / self.output_size)
                left_eye_pos.append(float(l1_2) / self.output_size)
                left_eye_pos.append(float(u1_1) / self.output_size)
                left_eye_pos.append(float(u1_2) / self.output_size)

                # right eye, y
                scale = current_eye_pos[2] - self.height / 2  # current_eye_pos[7] / 2
                down_scale = current_eye_pos[2] + self.height / 2  # current_eye_pos[7] / 2

                l2_1 = int(scale)
                u2_1 = int(down_scale)

                # x
                scale = current_eye_pos[3] - self.width / 2  # current_eye_pos[6] / 2
                down_scale = current_eye_pos[3] + self.width / 2  # current_eye_pos[6] / 2

                l2_2 = int(scale)
                u2_2 = int(down_scale)

                mask[l2_1:u2_1, l2_2:u2_2, :] = 1.0

                right_eye_pos.append(float(l2_1) / self.output_size)
                right_eye_pos.append(float(l2_2) / self.output_size)
                right_eye_pos.append(float(u2_1) / self.output_size)
                right_eye_pos.append(float(u2_2) / self.output_size)

            batch_mask.append(mask)
            batch_left_eye_pos.append(left_eye_pos)
            batch_right_eye_pos.append(right_eye_pos)

        return np.array(batch_mask), np.array(batch_left_eye_pos), np.array(batch_right_eye_pos)

    def _init_inception(self):
        DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'

        if not os.path.exists(self.MODEL_DIR):
            os.makedirs(self.MODEL_DIR)
        filename = DATA_URL.split('/')[-1]
        filepath = os.path.join(self.MODEL_DIR, filename)
        if not os.path.exists(filepath):
            def _progress(count, block_size, total_size):
                sys.stdout.write('\r>> Downloading %s %.1f%%' % (
                    filename, float(count * block_size) / float(total_size) * 100.0))
                sys.stdout.flush()

            filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
            print()
            statinfo = os.stat(filepath)
            print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
        tarfile.open(filepath, 'r:gz').extractall(self.MODEL_DIR)
        with tf.gfile.FastGFile(os.path.join(
                self.MODEL_DIR, 'classify_image_graph_def.pb'), 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            _ = tf.import_graph_def(graph_def, name='')
        # Works with an arbitrary minibatch size.

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        config.allow_soft_placement = True

        with tf.Session(config=config, graph=tf.get_default_graph()) as sess:
            pool3 = sess.graph.get_tensor_by_name('pool_3:0')
            ops = pool3.graph.get_operations()
            for op_idx, op in enumerate(ops):
                for o in op.outputs:
                    #print(o)
                    shape = o.get_shape()
                    shape = [s.value for s in shape]
                    new_shape = []
                    for j, s in enumerate(shape):
                        if s == 1 and j == 0:
                            new_shape.append(None)
                        else:
                            new_shape.append(s)
                    o._shape = tf.TensorShape(new_shape)

            w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1]
            logits = tf.matmul(tf.squeeze(pool3), w)

            self.softmax = tf.nn.softmax(logits)
            self.pool3 = pool3

    def get_data_feeded(self, sess, train_images, train_eye_pos):

        real_batch_image, real_eye_pos = sess.run([train_images, train_eye_pos])
        batch_masks, batch_left_eye_pos, batch_right_eye_pos = self.get_Mask_and_pos(real_eye_pos)

        f_d = {self.input: real_batch_image,
               self.mask: batch_masks,
               self.input_left_labels: batch_left_eye_pos,
               self.input_right_labels: batch_right_eye_pos}

        return f_d

    def transform_image(self, image):
        return (image + 1) * 127.5

    def Inception_score(self, sess, train_images, train_eye_pos, step):

        print("Computing Inception scores")
        bs = self.batch_size
        validate_numbers = 10000
        preds1 = []
        preds2 = []
        n_batches = int(math.ceil(float(validate_numbers) / float(bs)))
        for i in range(n_batches):

            if i % 1000 == 0:
                sys.stdout.write(".")
                sys.stdout.flush()

            f_d = self.get_data_feeded(sess, train_images=train_images, train_eye_pos=train_eye_pos)
            validate_output_img = sess.run([self.local_input_left,
                                            self.local_input_right,
                                            self.local_recon_img_left,
                                            self.local_recon_img_right
                                            ], feed_dict=f_d)

            pred1 = sess.run(self.softmax, {'ExpandDims:0': self.transform_image(validate_output_img[2])})
            preds1.append(pred1)
            pred2 = sess.run(self.softmax, {'ExpandDims:0': self.transform_image(validate_output_img[3])})
            preds2.append(pred2)

        preds1 = np.concatenate(preds1, 0)
        scores1 = []
        for i in range(10):
            part = preds1[(i * preds1.shape[0] // 10):((i + 1) * preds1.shape[0] // 10), :]
            kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
            kl = np.mean(np.sum(kl, 1))
            scores1.append(np.exp(kl))

        print("\n%d", step, "Inception Scores for Left Eyes", str(np.mean(scores1)) + "+-" + str(np.std(scores1)))

        preds2 = np.concatenate(preds2, 0)
        scores2 = []
        for i in range(10):
            part = preds2[(i * preds2.shape[0] // 10):((i + 1) * preds2.shape[0] // 10), :]
            kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
            kl = np.mean(np.sum(kl, 1))
            scores2.append(np.exp(kl))
        print("\n%d", step, "Inception Scores for Right Eyes", str(np.mean(scores2)) + "+-" + str(np.std(scores2)))

        f = open('{}/incpetion_socre_log2.txt'.format(self.evaluation_path), 'a')  # append write
        f.writelines('{:04}:{}: {}\n'.format(step, str(np.mean(scores1)) + "+-" + str(np.std(scores1)),
                                             str(np.mean(scores2)) + "+-" + str(np.std(scores2))))
        f.close()

    def FID_score(self,sess, train_images, train_eye_pos, step):

        bs = self.batch_size
        validate_numbers = 10000
        nbatches = validate_numbers // bs
        n_used_imgs = nbatches * bs
        pred_arr1 = np.empty((n_used_imgs, 2048))
        pred_arr2 = np.empty((n_used_imgs, 2048))
        pred_arr_r = np.empty((n_used_imgs, 2048))
        pred_arr_r2 = np.empty((n_used_imgs, 2048))

        for i in range(nbatches):

            if i % 1000 == 0:
                sys.stdout.write(".")
                sys.stdout.flush()

            start = i * bs
            end = start + bs

            f_d = self.get_data_feeded(sess, train_images=train_images, train_eye_pos=train_eye_pos)

            validate_output_img = sess.run([self.local_input_left,
                                            self.local_input_right,
                                            self.local_recon_img_left,
                                            self.local_recon_img_right
                                            ], feed_dict=f_d)

            pred = sess.run(self.pool3, {'ExpandDims:0': self.transform_image(validate_output_img[2])})
            pred_arr1[start:end] = pred.reshape(bs, -1)
            pred = sess.run(self.pool3, {'ExpandDims:0': self.transform_image(validate_output_img[3])})
            pred_arr2[start:end] = pred.reshape(bs, -1)
            # Get the activations for real sampels
            pred2 = sess.run(self.pool3, {'ExpandDims:0': self.transform_image(validate_output_img[0])})
            pred_arr_r[start:end] = pred2.reshape(bs, -1)

            pred2 = sess.run(self.pool3, {'ExpandDims:0': self.transform_image(validate_output_img[1])})
            pred_arr_r2[start:end] = pred2.reshape(bs, -1)

        g_mu1 = np.mean(pred_arr1, axis=0)
        g_sigma1 = np.cov(pred_arr1, rowvar=False)

        g_mu2 = np.mean(pred_arr2, axis=0)
        g_sigma2 = np.cov(pred_arr2, rowvar=False)

        g_mu_r = np.mean(pred_arr_r, axis=0)
        g_sigma_r = np.cov(pred_arr_r, rowvar=False)

        g_mu_r2 = np.mean(pred_arr_r2, axis=0)
        g_sigma_r2 = np.cov(pred_arr_r2, rowvar=False)

        m = np.square(g_mu1 - g_mu_r).sum()
        s, _ = scipy.linalg.sqrtm(np.dot(g_sigma1, g_sigma_r), disp=False)  # EDIT: added
        dist1 = m + np.trace(g_sigma1 + g_sigma_r - 2 * s)

        print("FID1 for left: ", np.real(dist1))

        m = np.square(g_mu2 - g_mu_r2).sum()
        s, _ = scipy.linalg.sqrtm(np.dot(g_sigma2, g_sigma_r2), disp=False)  # EDIT: added
        dist2 = m + np.trace(g_sigma2 + g_sigma_r2 - 2 * s)

        print("FID2 for right: ", np.real(dist2))

        f = open('{}/fid_socre_log2.txt'.format(self.evaluation_path), 'a')  # append write
        f.writelines('{:04}:{}: {}\n'.format(step, str(np.real(dist1)), str(np.real(dist2))))
        f.close()

reason: import numpy,import scipy