Exemplo n.º 1
0
 def test_no_fail_readme(self):
     pd.options.display.max_rows = 8
     sankey(self.data['true'],
            self.data['predicted'],
            aspect=20,
            colorDict=self.colorDict,
            fontsize=12,
            figureName=self.figure_name)
Exemplo n.º 2
0
 def test_no_fail_readme(self):
     # This is not working yet...
     sankey(left=self.data['customer'],
            right=self.data['good'],
            rightWeight=self.data['revenue'],
            aspect=20,
            fontsize=20,
            figureName=self.figure_name)
Exemplo n.º 3
0
 def test_label_mismatch(self):
     """ sankey raises a LabelMismatch when data doesn't match the labels"""
     with self.assertRaises(LabelMismatch):
         sankey(
             self.data['true'],
             self.data['predicted'],
             leftLabels=['banana', 'orange', 'blueberry', 'apple', 'lime'],
             rightLabels=['orange', 'banana', 'blueberry', 'apple'])
Exemplo n.º 4
0
 def test_no_fail_readme(self):
     pd.options.display.max_rows = 8
     sankey(
         [self.data["true"], self.data["predicted"]],
         aspect=20,
         color_dict=self.color_dict,
         fontsize=12,
         figure_name=self.figure_name,
     )
Exemplo n.º 5
0
 def test_label_mismatch(self):
     """ sankey raises a LabelMismatch when data doesn't match the labels"""
     with self.assertRaises(LabelMismatch):
         sankey(
             self.data["true"],
             self.data["predicted"],
             leftLabels=["banana", "orange", "blueberry", "apple", "lime"],
             rightLabels=["orange", "banana", "blueberry", "apple"],
         )
Exemplo n.º 6
0
 def test_bad_color_labels(self):
     """ sankey raise a value error when there is not enough color info"""
     bad_color_dict = {"apple": "#f71b1b", "orange": "#f78c1b"}
     with self.assertRaises(ValueError) as value_error:
         sankey(self.data["true"],
                self.data["predicted"],
                colorDict=bad_color_dict)
     self.assertIn(": lime, blueberry, banana, kiwi",
                   str(value_error.exception))
Exemplo n.º 7
0
 def test_bad_color_labels(self):
     """ sankey raise a value error when there is not enough color info"""
     bad_color_dict = {'apple': '#f71b1b', 'orange': '#f78c1b'}
     with self.assertRaises(ValueError) as value_error:
         sankey(self.data['true'],
                self.data['predicted'],
                aspect=20,
                colorDict=bad_color_dict,
                fontsize=12,
                figureName=self.figure_name)
     self.assertIn(': blueberry, lime, banana', str(value_error.exception))
 def test_no_fail_readme(self):
     # This is not working yet...
     weight = self.data["revenue"].values[1:].astype(float)
     sankey(
         left=self.data["customer"].values[1:],
         right=self.data["good"].values[1:],
         rightWeight=weight,
         leftWeight=weight,
         aspect=20,
         fontsize=10,
         figureName=self.figure_name,
     )
Exemplo n.º 9
0
    def test_bad_color_labels(self):
        """ sankey raise a value error when there is not enough color info"""
        bad_color_dict = {"apple": "#f71b1b", "orange": "#f78c1b"}

        with self.assertRaises(ValueError) as value_error:
            sankey(
                [self.data["true"], self.data["predicted"]],
                aspect=20,
                color_dict=bad_color_dict,
                fontsize=12,
                figure_name=self.figure_name,
            )
        self.assertIn(": blueberry, lime, banana", str(value_error.exception))
Exemplo n.º 10
0
    def tex(self, other_question):
        """ Return a tikz Sankey Diagram of two questions.

        The question used during initialization will be left and down the other
        question will be right and up. Cardinality constraint used for the
        other question are the same for both question.

        See this question https://tex.stackexchange.com/questions/40159/
        in order for it to work with your latex file.

        :param Question other_question: the question we compare to. """
        if not isinstance(other_question, Question):
            msg = "Expected a 'Question' and got '{}'".format(other_question)
            msg += " (a '{}').".format(other_question.__class__.__name__)
            raise TypeError(msg)
        self.other_question = other_question
        self.cardinality = self.question.sorted_answers_cardinality(
            self.min_cardinality,
            self.group_together,
            self.group_by_letter_case,
            self.group_by_slugify,
            self.filter,
            self.sort_answer,
            other_question=other_question,
        )
        q1 = []
        q2 = []
        for answer_to_q1, cardinality_to_q2 in list(self.cardinality.items()):
            for answer_to_q2, number_of_time in list(
                    cardinality_to_q2.items()):
                for _ in range(number_of_time):
                    q1.append(answer_to_q1)
                    q2.append(answer_to_q2)
        df = DataFrame(data={self.question.text: q1, other_question.text: q2})
        name = "tex/q{}_vs_q{}".format(self.question.pk, other_question.pk)
        sankey(
            df[self.question.text],
            df[other_question.text],
            aspect=20,
            fontsize=10,
            figureName=name,
        )
        return Question2TexSankey.TEX_SKELETON % (
            name[4:],
            self.question.pk,
            other_question.pk,
            self.get_caption(),
        )
Exemplo n.º 11
0
    def tex(self):
        """Return a tikz Sankey Diagram of two questions.

        The question used during initialization will be left and down the other
        question will be right and up. Cardinality constraint used for the
        other question are the same for both question.

        See this question https://tex.stackexchange.com/questions/40159/
        in order for it to work with your latex file.

        :param Question other_question: the question we compare to."""
        if not SANKEY:
            raise SankeyNotInstalled()
        self.cardinality = self.question.sorted_answers_cardinality(
            self.min_cardinality,
            self.group_together,
            self.group_by_letter_case,
            self.group_by_slugify,
            self.filter,
            self.sort_answer,
            other_question=self.other_question,
        )
        q1 = []
        q2 = []
        for answer_to_q1, cardinality_to_q2 in list(self.cardinality.items()):
            for answer_to_q2, number_of_time in list(
                    cardinality_to_q2.items()):
                q1 += [answer_to_q1] * number_of_time
                q2 += [answer_to_q2] * number_of_time
        df = DataFrame(data={
            self.question.text: q1,
            self.other_question.text: q2
        })
        name = "tex/q{}_vs_q{}".format(self.question.pk,
                                       self.other_question.pk)
        sankey(df[self.question.text],
               df[self.other_question.text],
               aspect=20,
               fontsize=10,
               figureName=name)
        return Question2TexSankey.TEX_SKELETON % (
            name[4:],
            self.question.pk,
            self.other_question.pk,
            self.get_caption(),
        )
Exemplo n.º 12
0
 def test_no_fail_readme(self):
     ax = sankey(
         self.data["true"],
         self.data["predicted"],
         aspect=20,
         colorDict=self.colorDict,
         fontsize=12,
     )
     self.assertIsInstance(ax, plt.Axes)
Exemplo n.º 13
0
def main(args):
    gold_labels, pred_labels = [], []
    with open(args.prediction) as f:
        for line in f:
            sample = json.loads(line)
            gold_labels.append(sample['gold_label'])
            pred_labels.append(sample['pred_label'])

    colorDict = {
        "neutral": '#FEC925',
        "contradiction": '#FA1E44',
        "entailment": '#5AB190'
    }
    sankey(gold_labels,
           pred_labels,
           aspect=20,
           figureName="gold_pred",
           leftLabels=["neutral", "contradiction", "entailment"],
           rightLabels=["neutral", "contradiction", "entailment"],
           colorDict=colorDict)
Exemplo n.º 14
0
 def test_no_fail_readme(self):
     weight = self.data["revenue"].values[1:].astype(float)
     ax = sankey(
         left=self.data["customer"].values[1:],
         right=self.data["good"].values[1:],
         rightWeight=weight,
         leftWeight=weight,
         aspect=20,
         fontsize=10,
     )
     self.assertIsInstance(ax, plt.Axes)
Exemplo n.º 15
0
def ScanpySankey(adata,var1,var2,aspect=20,
                fontsize=12, figureName="cell type", leftLabels=['Default'],
    rightLabels=['Default']):
    from pysankey import sankey
    colordict={**ExtractColor(adata,var1,str),
               **ExtractColor(adata,var2,str)}
    if 'Default' in leftLabels:
        leftLabels=sorted(adata.obs[var1].unique().tolist())
    if 'Default' in rightLabels:
        rightLabels=sorted(adata.obs[var2].unique().tolist())
    return sankey(adata.obs[var1],adata.obs[var2],aspect=aspect,colorDict=colordict,
fontsize=fontsize,figureName=figureName,leftLabels=leftLabels,rightLabels=rightLabels)
Exemplo n.º 16
0
    def test_deprecated_parameters(self):
        """ Test if deprecation warnings are correctly triggered """
        with self.assertWarns(DeprecationWarning):
            sankey(
                self.data["true"],
                self.data["predicted"],
                colorDict=self.colorDict,
                figureName=self.figure_name,
            )

        with self.assertWarns(DeprecationWarning):
            sankey(
                self.data["true"],
                self.data["predicted"],
                colorDict=self.colorDict,
                closePlot=True,
            )

        with self.assertWarns(DeprecationWarning):
            sankey(
                self.data["true"],
                self.data["predicted"],
                colorDict=self.colorDict,
                figSize=(8, 8),
            )
Exemplo n.º 17
0
def main(args):
    original_labels = []
    with open(args.original) as f:
        for line in f:
            original_labels.append(json.loads(line)['label'])

    mirror_labels = []
    with open(args.mirror) as f:
        for line in f:
            mirror_labels.append(json.loads(line)['label'])

    colorDict = {
        "neutral": '#FEC925',
        "contradiction": '#FA1E44',
        "entailment": '#5AB190'
    }
    sankey(
        original_labels, mirror_labels,
        aspect=20, figureName="original_mirror",
        leftLabels=["neutral", "contradiction", "entailment"],
        rightLabels=["neutral", "contradiction", "entailment"],
        colorDict=colorDict
    )
Exemplo n.º 18
0
 def test_wrong_aspect_value(self):
     """ sankey raises a ValueError when aspect is set to a value <= 0"""
     with self.assertRaises(ValueError):
         sankey([], [], aspect=0)
     with self.assertRaises(ValueError):
         sankey([], [], aspect=-15)
Exemplo n.º 19
0
 def test_nulls_in_frame(self):
     """ sankey raises a NullsInFrame when left or right data is null"""
     with self.assertRaises(NullsInFrame):
         sankey([None], self.data["predicted"])
Exemplo n.º 20
0
df1 = pd.read_csv('pysankey/tests/fruits.txt',
                  sep=' ',
                  names=['true', 'predicted'])
colorDict = {
    'apple': '#f71b1b',
    'blueberry': '#1b7ef7',
    'banana': '#f3f71b',
    'lime': '#12e23f',
    'orange': '#f78c1b',
    'kiwi': '#9BD937'
}

ax1 = sankey(
    df1['true'],
    df1['predicted'],
    aspect=20,
    colorDict=colorDict,
    leftLabels=['banana', 'orange', 'blueberry', 'apple', 'lime'],
    rightLabels=['orange', 'banana', 'blueberry', 'apple', 'lime', 'kiwi'],
    fontsize=12)

plt.show()  # to display
# plt.savefig('fruit.png', bbox_inches='tight') # uncomment to save

# --- Second example with weight in customers-goods.csv ---

df2 = pd.read_csv('pysankey/tests/customers-goods.csv',
                  sep=',',
                  names=['id', 'customer', 'good', 'revenue'])
weight = df2['revenue'].values[1:].astype(float)

ax2 = sankey(left=df2['customer'].values[1:],
Exemplo n.º 21
0
 def test_right_color(self):
     ax = sankey(self.data["true"], self.data["predicted"], rightColor=True)
     self.assertIsInstance(ax, plt.Axes)
Exemplo n.º 22
0
import pandas as pd
from collections import Counter
from pysankey import sankey

df = pd.read_csv('biprozorro_data_chernivtsi_17_19.csv')
df['Очікувана вартість'] = (df['Очікувана вартість'].str.split()).apply(
    lambda x: float(x[0].replace(',', '')) / 100)

sankey(left=df['Організатор'],
       right=df['Постачальники'],
       rightWeight=df['Очікувана вартість'].astype(float),
       aspect=10,
       fontsize=3,
       figureName="GeneralSankey")

# Organizers analysis
organizers_data = []
organizers = df['Організатор'].unique()
for organizer in organizers:
    organizer_data = {}
    organizer_df = df[(df['Організатор'] == organizer)]
    organizer_data['organizer'] = organizer
    organizer_data['count'] = organizer_df['Очікувана вартість'].count()
    organizer_data['sum'] = organizer_df['Очікувана вартість'].sum()
    organizer_data['max'] = organizer_df['Очікувана вартість'].max()
    organizer_data['min'] = organizer_df['Очікувана вартість'].min()
    organizer_data['mean'] = organizer_df['Очікувана вартість'].mean()
    organizers_data.append(organizer_data)

    sankey(left=organizer_df['Організатор'],
           right=organizer_df['Постачальники'],