Skip to content

Commit

Permalink
cleaned
Browse files Browse the repository at this point in the history
  • Loading branch information
vturrisi committed Mar 13, 2019
1 parent acc80d5 commit 7d2be7c
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 51 deletions.
136 changes: 136 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Created by https://www.gitignore.io/api/python,visualstudiocode
# Edit at https://www.gitignore.io/?templates=python,visualstudiocode

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

### Python Patch ###
.venv/

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

# End of https://www.gitignore.io/api/python,visualstudiocode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The input file must be a .csv file, and the output is a .tex file. The [ASC|DEC]
if you want to skip the first column of the dataset, sometimes used for identification of the sample.

```bash
python nemenyi/nemenyi.py examples/input.csv examples/output.tex --ignore_first --descending
python nemenyi/nemenyi.py examples/input.csv examples/output.tex --descending
```

## Requirements
Expand Down
43 changes: 24 additions & 19 deletions examples/output.tex
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
\begin{figure} \centering \begin{tikzpicture}[xscale=2]
\node (Label) at (1.362067349834422,0.7){\tiny{CD=0.27011224972403675}}; % the label
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm}, very thick, color = black] (1.2,0.5) -- (1.524134699668844,0.5);
\foreach \x in {1.2,1.524134699668844} \draw[thick,color = black] (\x, 0.4) -- (\x, 0.6);
\node (Label) at (1.1669470680467668, 0.7){\tiny{CD = 0.3338941360935335}; % the label
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm},very thick, color = black] (1.0,0.5) -- (1.3338941360935335,0.5);
\foreach \x in {1.0, 1.3338941360935335} \draw[thick,color = black] (\x, 0.4) -- (\x, 0.6);

\draw[gray, thick](1.2,0) -- (6.0,0);
\foreach \x in {1.2,2.4,3.6,4.8,6.0} \draw (\x cm,1.5pt) -- (\x cm, -1.5pt);
\node (Label) at (1.2,0.2){\tiny{0}};
\node (Label) at (2.4,0.2){\tiny{1}};
\node (Label) at (3.6,0.2){\tiny{2}};
\node (Label) at (4.8,0.2){\tiny{3}};
\node (Label) at (6.0,0.2){\tiny{4}};
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm}, very thick, color = black](2.9441176470588237,-0.25) -- (3.2535294117647053,-0.25);
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm}, very thick, color = black](3.1535294117647057,-0.4) -- (3.538235294117647,-0.4);
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm}, very thick, color = black](4.005294117647059,-0.25) -- (4.3088235294117645,-0.25);
\node (Point) at (2.9941176470588236, 0){}; \node (Label) at (0.5,0.8500000000000001){\scriptsize{rf}}; \draw (Point) |- (Label);
\node (Point) at (3.2035294117647055, 0){}; \node (Label) at (0.5,1.1500000000000001){\scriptsize{xgb}}; \draw (Point) |- (Label);
\node (Point) at (4.258823529411765, 0){}; \node (Label) at (6.5,0.8500000000000001){\scriptsize{majority}}; \draw (Point) |- (Label);
\node (Point) at (4.055294117647059, 0){}; \node (Label) at (6.5,1.1500000000000001){\scriptsize{random}}; \draw (Point) |- (Label);
\node (Point) at (3.488235294117647, 0){}; \node (Label) at (6.5,1.4500000000000002){\scriptsize{svm}}; \draw (Point) |- (Label);
\draw[gray, thick](1.0,0) -- (6.0,0);
\foreach \x in {1.0,2.0,3.0,4.0,5.0,6.0} \draw (\x cm,1.5pt) -- (\x cm, -1.5pt);
\node (Label) at (1.0,0.2){\tiny{0}};
\node (Label) at (2.0,0.2){\tiny{1}};
\node (Label) at (3.0,0.2){\tiny{2}};
\node (Label) at (4.0,0.2){\tiny{3}};
\node (Label) at (5.0,0.2){\tiny{4}};
\node (Label) at (6.0,0.2){\tiny{5}};
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm},very thick, color = black](3.0990196078431373,-0.25) -- (3.4098039215686273,-0.25);
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm},very thick, color = black](3.3098039215686277,-0.4) -- (3.6892156862745096,-0.4);
\draw[decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm},very thick, color = black](4.147058823529412,-0.25) -- (4.432352941176471,-0.25);
\node (Point) at (2.272549019607843, 0){};\node (Label) at (0.5,0.8500000000000001){\scriptsize{truth}}; \draw (Point) |- (Label);
\node (Point) at (3.149019607843137, 0){};\node (Label) at (0.5,1.1500000000000001){\scriptsize{rf}}; \draw (Point) |- (Label);
\node (Point) at (3.3598039215686275, 0){};\node (Label) at (0.5,1.4500000000000002){\scriptsize{xgb}}; \draw (Point) |- (Label);
\node (Point) at (4.382352941176471, 0){};\node (Label) at (6.5,0.8500000000000001){\scriptsize{majority}}; \draw (Point) |- (Label);
\node (Point) at (4.382352941176471, 0){}; \node (Label) at (6.5,0.8500000000000001){\scriptsize{majority}}; \draw (Point) |- (Label);
\node (Point) at (4.197058823529412, 0){};\node (Label) at (6.5,1.1500000000000001){\scriptsize{random}}; \draw (Point) |- (Label);
\node (Point) at (4.197058823529412, 0){}; \node (Label) at (6.5,1.1500000000000001){\scriptsize{random}}; \draw (Point) |- (Label);
\node (Point) at (3.6392156862745098, 0){};\node (Label) at (6.5,1.4500000000000002){\scriptsize{svm}}; \draw (Point) |- (Label);
\node (Point) at (3.6392156862745098, 0){}; \node (Label) at (6.5,1.4500000000000002){\scriptsize{svm}}; \draw (Point) |- (Label);
\end{tikzpicture}
\caption{Nemenyi post hoc test}
\label{fig:nemeny}
\label{fig:nemenyi}
\end{figure}
Binary file modified nemenyi/__pycache__/to_latex.cpython-37.pyc
Binary file not shown.
24 changes: 12 additions & 12 deletions nemenyi/nemenyi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import math
import os
import sys
Expand All @@ -7,8 +8,6 @@
from scipy.special import gammaln
from scipy.stats import friedmanchisquare, norm, rankdata

import argparse

from to_latex import writeTex


Expand All @@ -29,16 +28,17 @@
desc = args.descending
ignore_first_column = args.ignore_first_column

print("Input: {}".format(input_file))
print("Output: {}".format(output_file))
print("DEC? {}".format(desc))
print("Skip? {}".format(ignore_first_column))
print("input file: {}".format(input_file))
print("output file: {}".format(output_file))
print("descending? {}".format(desc))
print("ignore first column? {}".format(ignore_first_column))

data = pd.read_csv(input_file)
if ignore_first_column:
data = data.drop(data.columns[0], axis=1)
data = data.iloc[:, 1:]

nrow, ncol = data.shape
columns = data.columns
data = data.values

qAlpha5pct = [1.960, 2.344, 2.569, 2.728, 2.850, 2.948, 3.031, 3.102, 3.164, 3.219, 3.268, 3.313, 3.354, 3.391,
3.426, 3.458, 3.489, 3.517, 3.544, 3.569, 3.593, 3.616, 3.637, 3.658, 3.678, 3.696, 3.714, 3.732]
Expand All @@ -48,16 +48,16 @@


dataAsRanks = np.full(data.shape, np.nan)
for i in range(nrow):
dataAsRanks[i, :] = rankdata(data.iloc[i, :])
for i, row in enumerate(data):
dataAsRanks[i, :] = rankdata(row)
if desc:
dataAsRanks[i, :] = len(dataAsRanks[i, :]) - dataAsRanks[i, :] + 1

nrow, ncol = data.shape
critDiff = math.sqrt((ncol * (ncol + 1.0)) / (6.0 * nrow))
critDiff_5 = qAlpha5pct[ncol - 2] * critDiff
critDiff_10 = qAlpha10pct[ncol - 2] * critDiff

ranks = np.mean(dataAsRanks, 0)

writeTex(data.columns, ranks, cd=critDiff_5, file_tex=output_file)

writeTex(columns, ranks, critDiff_5, output_file)
58 changes: 39 additions & 19 deletions nemenyi/to_latex.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import numpy as np
import math

import numpy as np


def normalize_by_width(ranks, width):
return ([x * width / len(ranks) for x in ranks])


def get_segments(ranks, cd):
conected_points = []
for i in range(len(ranks)):
Expand Down Expand Up @@ -37,7 +40,7 @@ def findInner(segments, ranks):
return -1


def writeTex(names, ranks, cd, width=7, file_tex="output.tex"):
def writeTex(names, ranks, cd, output_file, width=7):

names_sorted = [x for _, x in sorted(zip(ranks, names))]
ranks_sorted = sorted(ranks)
Expand All @@ -46,7 +49,9 @@ def writeTex(names, ranks, cd, width=7, file_tex="output.tex"):

ranks_normalized = normalize_by_width(ranks_sorted, width - 1)

lineFormat = "decorate,decoration={snake,amplitude=.4mm,segment length=1.5mm,post length=0mm}, very thick, color = black";
lineFormat = ('decorate,decoration={snake,amplitude=.4mm,'
'segment length=1.5mm,post length=0mm},'
'very thick, color = black')

points = range(1, len(ranks) + 1)
points = normalize_by_width(points, width - 1)
Expand All @@ -58,15 +63,24 @@ def writeTex(names, ranks, cd, width=7, file_tex="output.tex"):
rightLabelPosition = margin + len(ranks) * (width - 1.) / len(ranks)

text_script = "\\begin{figure} \\centering \\begin{tikzpicture}[xscale=2]\n"
text_script = text_script + "\\node (Label) at ({},0.7)".format(points[0] + normCd / 2.) + "{\\tiny{CD=" + "{}".format(cd) + "}}; % the label\n"
text_script = text_script + "\\draw[" + lineFormat + "] ({},0.5) -- ({},0.5);\n".format(points[0], points[0] + normCd)
text_script = text_script + "\\foreach \\x in {" + "{},{}".format(points[0], points[0] + normCd) + "} \\draw[thick,color = black] (\\x, 0.4) -- (\\x, 0.6);\n \n"
text_script = text_script + "\\draw[gray, thick]({},0) -- ({},0); \n".format(points[0], points[lastRank])
text_script = text_script + "\\foreach \\x in {"
text_script = text_script + ",".join(map(str, points)) + "} \\draw (\\x cm,1.5pt) -- (\\x cm, -1.5pt);\n"

text_script = text_script + ('\\node (Label) at ({}, 0.7)'
'{{\\tiny{{CD = {}}}; % the label\n'.format(points[0] + normCd / 2, cd))

text_script = text_script + ('\\draw[{}] ({},0.5) -- ({},0.5);\n'.format(lineFormat, points[0], points[0] + normCd))

text_script = text_script + ('\\foreach \\x in {{{}, {}}} \\'
'draw[thick,color = black] (\\x, 0.4)'
' -- (\\x, 0.6);\n \n'.format(points[0], points[0] + normCd))

text_script = text_script + ('\\draw[gray, thick]({},0) -- ({},0); \n'.format(points[0], points[lastRank]))

point_names = ','.join(map(str, points))
text_script = text_script + ('\\foreach \\x in {{{}}} '
'\\draw (\\x cm,1.5pt) -- (\\x cm, -1.5pt);\n'.format(point_names))

for idx, p in enumerate(points):
text_script = text_script + "\\node (Label) at ({},0.2)".format(p)+ "{\\tiny{" + "{}".format(idx) + "}};\n"
text_script = text_script + "\\node (Label) at ({},0.2){{\\tiny{{{}}}}};\n".format(p, idx)

startY = -0.25
deltaY = -0.15
Expand All @@ -87,23 +101,29 @@ def writeTex(names, ranks, cd, width=7, file_tex="output.tex"):
if x1 - previousX2 <= 0.1 and yaxe[prev] == y:
y = y + deltaY
yaxe.append(y)
text_script = text_script + "\\draw[" + lineFormat + "]({},{}) -- ({},{});\n".format(x1, yaxe[idx], x2, yaxe[idx])
text_script = text_script + "\\draw[{}]({},{}) -- ({},{});\n".format(lineFormat, x1,
yaxe[idx], x2, yaxe[idx])

base = 0.25 + 0.2 * len(yaxe)
x1 = 0.3

for idx in range(int(len(names) / 2)):
text_script = text_script + "\\node (Point) at ({}, 0)".format(ranks_normalized[idx]) + "{};" + " \\node (Label) at ({},{})".format(leftLabelPosition, idx * x1 + base)
text_script = text_script + "{\\scriptsize{" + "{}".format(names_sorted[idx]) + "}}; \\draw (Point) |- (Label);\n"
text_script = text_script + ('\\node (Point) at ({}, 0){{}};'
'\\node (Label) at ({},{})'.format(ranks_normalized[idx],
leftLabelPosition, idx * x1 + base))
text_script = text_script + "{{\\scriptsize{{{}}}}}; \\draw (Point) |- (Label);\n".format(names_sorted[idx])

for idx in range(len(names) - 1, int((len(names) / 2)) - 1, -1):
text_script = text_script + "\\node (Point) at ({}, 0)".format(ranks_normalized[idx]) + "{};" + " \\node (Label) at ({},{})".format(rightLabelPosition, (len(names) - (idx + 1)) * x1 + base)
text_script = text_script + "{\\scriptsize{" + "{}".format(names_sorted[idx]) + "}}; \\draw (Point) |- (Label);\n"
text_script = text_script + ('\\node (Point) at ({}, 0){{}};'
'\\node (Label) at ({},{})'.format(ranks_normalized[idx],
rightLabelPosition,
(len(names) - (idx + 1)) * x1 + base))
text_script = text_script + "{{\\scriptsize{{{}}}}}; \\draw (Point) |- (Label);\n".format(names_sorted[idx])

text_script = text_script + "\\end{tikzpicture}\n"
text_script = text_script + "\\caption{Nemenyi post hoc test}\n"
text_script = text_script + "\\label{fig:nemeny}\n"
text_script = text_script + "\\label{fig:nemenyi}\n"
text_script = text_script + "\\end{figure}\n"

text_file = open(file_tex, "w")
text_file.write(text_script)
text_file.close()
with open(output_file, 'w') as f:
f.write(text_script)

0 comments on commit 7d2be7c

Please sign in to comment.