Skip to content

Commit

Permalink
added sphinx documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Neywiny committed Aug 16, 2019
1 parent 10352dc commit be4f083
Show file tree
Hide file tree
Showing 60 changed files with 14,289 additions and 171 deletions.
25 changes: 13 additions & 12 deletions DylAFC.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/python3.6
import os
import socket
import time
import numpy as np
from sys import argv
from tkinter import *
from PIL import ImageTk, Image
import os
import socket
from numpy.random import random, randint
from time import sleep, time
class AFC:
"""A Class for doing AFC studies. This one only does 2AFC. It is used for integrating with a tkinter interface as seen in DylAFC."""
def __init__(self, posDir: str, negDir: str, ansDir: str, ip: str, port: str, n0: int, n1: int, logFile: str):
Expand Down Expand Up @@ -37,7 +38,7 @@ def connect(self):
s.connect((self.ip, int(self.port)))
break
except ConnectionRefusedError:
sleep(0.1)
time.sleep(0.1)
data: bytes = s.recv(10)
if data == b"I'm ready!":
s.send(b'\x02' + self.n0.to_bytes(4, 'little') + self.n1.to_bytes(4, 'little') + b'\x03')
Expand Down Expand Up @@ -103,14 +104,14 @@ def run(self):
if self.mode.get() == 'training':
if self.counter <= 0 and self.decision == -1: #put up new pictures
if self.ready:
if random() > 0.5:
self.imgID1: int = randint(0, self.n0 - 1)
self.imgID2: int = randint(self.n0, self.n1 + self.n0 - 1)
if np.random.random() > 0.5:
self.imgID1: int = np.random.randint(0, self.n0 - 1)
self.imgID2: int = np.random.randint(self.n0, self.n1 + self.n0 - 1)
self.showPics(self.imgID1, self.imgID2)
self.answer: str = 'right'
else:
self.imgID1: int = randint(self.n0, self.n1 + self.n0 - 1)
self.imgID2: int = randint(0, self.n0 - 1)
self.imgID1: int = np.random.randint(self.n0, self.n1 + self.n0 - 1)
self.imgID2: int = np.random.randint(0, self.n0 - 1)
self.showPics(self.imgID1, self.imgID2)
self.answer: str = 'left'
self.ready: bool = False
Expand Down Expand Up @@ -144,7 +145,7 @@ def run(self):
self.data: bytes = data
#print(img1, img2)
self.showPics(self.imgID1, self.imgID2)
self.t1: float = time()
self.t1: float = time.time()
root.update()
self.ready: bool = False
except ConnectionResetError:
Expand All @@ -161,7 +162,7 @@ def run(self):
payload: bytes = (0).to_bytes(4, 'little') + self.data[1:5]
des: int = self.imgID2
try:
self.f.write(f"{self.images[self.imgID1].filename} {self.images[self.imgID2].filename} {self.images[des].filename} {time() - self.t1}\n")
self.f.write(f"{self.images[self.imgID1].filename} {self.images[self.imgID2].filename} {self.images[des].filename} {time.time() - self.t1}\n")
self.s.send(b'\x02' + payload + b'\x03')
except ConnectionResetError:
print("Client Disconnected")
Expand Down
33 changes: 18 additions & 15 deletions DylAnalyzer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/python3.6
import pickle
import json
import ROC1
import tqdm
import sys
import os
import numpy as np
np.seterr(divide='ignore', invalid='ignore')
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from os import stat
from sys import getsizeof, argv
from scipy import stats
from multiprocessing import Pool
from DylComp import Comparator
Expand Down Expand Up @@ -35,7 +36,7 @@ def analyzeMergeSims(fileName: str, length: int, layers: int, justOne: bool=Fals
varEstimates: np.ndarray = np.zeros((layers, 0))
aucs: np.ndarray = np.zeros((layers, 0))
iters: int = 0
fileLength: int = stat(fileName).st_size
fileLength: int = os.stat(fileName).st_size
old: int = 0
with open(fileName, "rb") as f, tqdm.tqdm(total=fileLength, unit="B", unit_scale=True, disable=not bar) as pBar:
# each simulation is one pickle, so need to depickle one at a time
Expand Down Expand Up @@ -78,7 +79,7 @@ def analyzeMergeSims(fileName: str, length: int, layers: int, justOne: bool=Fals

# update how many bytes were read
pBar.update(f.tell() - old)
pBar.desc: str = f"{iters}/{iterEstimate}, {reshapeCount}, {getsizeof(unpickler)}"
pBar.desc: str = f"{iters}/{iterEstimate}, {reshapeCount}, {sys.getsizeof(unpickler)}"
old: int = f.tell()

if justOne:
Expand Down Expand Up @@ -244,18 +245,20 @@ def analyzeReaderStudies(resultsFile, directory, n0):


if __name__ == "__main__":
if len(argv) > 1 and (argv[1] == '2' or argv[1] == '1'):
if argv[1] == '2':
if len(sys.argv) > 1:
if sys.argv[1] == '2' and len(sys.argv) >= 4:
test: int = 2
elif argv[1] == '1':
elif sys.argv[1] == '1':
test: int = 1
else:
test: int = -1
else:
test = -1
test: int = -1
if test == 1:
# Shows the 5 plot dashboard for studies
length: int = 256
layers: int = 8
varEstimate, avgAUC, avgMSETrues, avgMSEEmpiric, avgComps, avgHanleyMNeil, avgEstimates, avgMinSeps, varAUCnp, stdVarEstimate, avgPC, iters = analyzeMergeSims(argv[2], length, layers, bar=True)
varEstimate, avgAUC, avgMSETrues, avgMSEEmpiric, avgComps, avgHanleyMNeil, avgEstimates, avgMinSeps, varAUCnp, stdVarEstimate, avgPC, iters = analyzeMergeSims(sys.argv[2], length, layers, bar=True)
labels: list = [f'{np.median(list(filter(lambda x: x != 0, avgMinSeps[0]))):3.02f}']
for val in np.median(avgMinSeps, axis=0)[1:]:
labels.append(f'{val:3.02f}')
Expand Down Expand Up @@ -336,9 +339,9 @@ def permutation(arr: list, D0: list, D1: list):
n0: int = 128
n1: int = 128

with open(argv[2]) as f:
with open(sys.argv[2]) as f:
results: dict = json.load(f)
with open(argv[3]) as f:
with open(sys.argv[3]) as f:
names: list = f.read().split()
if max((len(files) for files in results.values())) == 4:
fig, (scatterAxes, timeAxes, tauAxes) = plt.subplots(ncols=3, nrows=3)
Expand Down Expand Up @@ -439,11 +442,11 @@ def permutation(arr: list, D0: list, D1: list):

print(f"{reader} {np.mean(scaleTimes):0.3f}\t\t{np.std(scaleTimes):0.3f}\t\t{np.mean(mergeTimes):0.3f}\t\t\t{np.std(mergeTimes):0.3f}\t\t{tau:0.3f}\t{np.std(taus):0.3f}")
fig.set_size_inches(12, 8)
if len(argv) == 5:
plt.savefig(argv[4], bbox_inches = 'tight', pad_inches = 0)
if len(sys.argv) == 5:
plt.savefig(sys.argv[4], bbox_inches = 'tight', pad_inches = 0)
else:
plt.show()
else:
print("Usage:")
print(f"{__file__} [json results file] [optional output directory]")
print(f"{__file__} [simulation results file]")
print(f"{__file__} 1 [simulation results file]")
print(f"{__file__} 2 [json results file for reader studies] [names.txt filename] [optional output directory]")
7 changes: 3 additions & 4 deletions DylComp.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#! /usr/bin/python3
import pickle
import numpy as np
from scipy.stats import kendalltau
from ROC1 import rocxy
np.seterr(all="ignore")
from warnings import filterwarnings
filterwarnings("ignore")
import socket
from numpy.random import random
from pickle import dump
class Comparator:
"""A class for comparing 2 values.
Controlled with the optimizaiton level and if you want random decisions or not
Expand Down Expand Up @@ -310,7 +309,7 @@ def max(self, arr: list, tryingAgain=False) -> (int, int):
print(data, self.desHist)
raise ConnectionError("shoulda gotten that")
self.record(arr)
flipped: bool = random() > 0.5
flipped: bool = np.random.random() > 0.5
if flipped:
payload: bytes = b'\x02' + arr[1].to_bytes(4, 'little') + arr[0].to_bytes(4, 'little') + b'\x03'
else:
Expand Down Expand Up @@ -431,4 +430,4 @@ def max(self, arr: list, tryingAgain=False) -> (int, int):
plt.savefig("temp.svg")
replace("temp.svg", "figure.svg")
with open(argv[3], "wb") as f:
dump((avgROC, roc4), f)
pickle.dump((avgROC, roc4), f)
1 change: 1 addition & 0 deletions DylData.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python3.6
import numpy as np
np.seterr(all="ignore")
def continuousScale(*args):
Expand Down
1 change: 1 addition & 0 deletions DylMath.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python3.6
import ROC1
import numpy as np
np.set_printoptions(threshold=np.inf)
Expand Down
1 change: 1 addition & 0 deletions DylMerger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python3.6
class MultiMerger:
def __init__(self, groups: list, comp, start=0, stop=0, toggle:bool=True):
groups: list = list(filter(lambda x: len(x) > 0, groups))
Expand Down
47 changes: 26 additions & 21 deletions DylScale.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#! /usr/bin/python3
import os
from numpy import random
from sys import argv
from tkinter.constants import *
from tkinter import *
from PIL import ImageTk, Image, ImageDraw
from time import time
from numpy.random import shuffle
class Rating:
"""A class for displaying an image and recording the ratins and timings of the reader.
Label parameter is the label where the image is going to be drawn.
Expand Down Expand Up @@ -33,7 +33,7 @@ def __init__(self, posDir: str, negDir: str, n: int, outputFile: str, offset: st
self.images: list = [ImageTk.PhotoImage(img) for img in self.images]
for i, image in enumerate(self.images):
setattr(image, "filename", names[i])
shuffle(self.images)
random.shuffle(self.images)
self.label = label
print()

Expand Down Expand Up @@ -159,7 +159,7 @@ def __init__(self, master, from_color, to_color, width=None, height=None, orient
ax2.plot(times)
ax2.set_title(f"Avg time {np.mean(times):0.2f}s")
plt.show()
elif len(argv) < 5:
elif len(argv) != 5 and len(argv) != 6:
print(f"Usage: \n\tpython3 {__file__} [signal present directory] [signal absent directory] [n] [output file] [offset (optional)]\n\tpython3 {__file__} [results file]")
else:
IMGWIDTH: int = 600
Expand All @@ -171,21 +171,26 @@ def __init__(self, master, from_color, to_color, width=None, height=None, orient
label = Label(root)
if len(argv) < 6:
argv.append("0")
rating = Rating(*argv[1:], label)
label.configure(image=rating.images[0])
label.grid(row=1, column=0)
ticks = Frame(root)
ticks.grid(row=1, column=1)
for tickmark in range(0, 110, 10):
text = Label(ticks, text=str(tickmark))
row: int = 10-(tickmark//10)
text.grid(row=row, column=0, sticky=E, pady=18)
gradient = GradientFrame(root, from_color="#000000", to_color="#FFFFFF", height=IMGHEIGHT, width=100, orient=HORIZONTAL)
gradient.grid(row=1, column=2)
rating.canvas = gradient
gradient.bind("<Button-1>", rating.drawBar)
button = Button(text="next", command=rating.next)
button.grid(row=2, column=2)
root.bind("<Return>", rating.next)
rating.t1 = time()
root.mainloop()
try:
rating = Rating(*argv[1:], label)
label.configure(image=rating.images[0])
label.grid(row=1, column=0)
ticks = Frame(root)
ticks.grid(row=1, column=1)
for tickmark in range(0, 110, 10):
text = Label(ticks, text=str(tickmark))
row: int = 10-(tickmark//10)
text.grid(row=row, column=0, sticky=E, pady=18)
gradient = GradientFrame(root, from_color="#000000", to_color="#FFFFFF", height=IMGHEIGHT, width=100, orient=HORIZONTAL)
gradient.grid(row=1, column=2)
rating.canvas = gradient
gradient.bind("<Button-1>", rating.drawBar)
button = Button(text="next", command=rating.next)
button.grid(row=2, column=2)
root.bind("<Return>", rating.next)
rating.t1 = time()
root.mainloop()

except ValueError:
print("Invalid Arguments")
root.destroy()
2 changes: 1 addition & 1 deletion DylSort.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/python3.6
import numpy as np
from sys import argv
from DylMath import runStats, graphROCs
Expand Down Expand Up @@ -266,7 +267,6 @@ def treeMergeSort(arr: list, comp, statParams=None, n: int=2, combGroups: bool=T
length: int = int(2**power*(2/3))
power += 1
yStep: float = length / power
print(yStep)
fig, axes = plt.subplots(ncols=2, nrows=1)
for ax in axes:
if ax == axes[0]:
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Graphs and Where to Find Them
``python3 DylSort.py 5``
- Average ROC for each layer as a merge simulation progresses ->
``python3 DylSort.py 3 <overlapping (defualt True)>``
- ROC curves for merge sort vs elo -> ``python3 Elo.py``
- ROC curves for merge sort vs elo -> ``python3 elo.py``

.. |forthebadge made-with-python| image:: http://ForTheBadge.com/images/badges/made-with-python.svg
:target: https://www.python.org/
Expand Down
38 changes: 8 additions & 30 deletions docs/DylAnalyzer.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="ROC1.html">ROC1</a><br>
<a href="json.html">json</a><br>
</td><td width="25%" valign=top><a href="numpy.html">numpy</a><br>
<a href="numpy.html">numpy</a><br>
</td><td width="25%" valign=top><a href="os.html">os</a><br>
<a href="pickle.html">pickle</a><br>
</td><td width="25%" valign=top><a href="matplotlib.pyplot.html">matplotlib.pyplot</a><br>
<a href="scipy.stats.html">scipy.stats</a><br>
</td><td width="25%" valign=top><a href="tqdm.html">tqdm</a><br>
</td></tr></table></td></tr></table><p>
<a href="matplotlib.pyplot.html">matplotlib.pyplot</a><br>
</td><td width="25%" valign=top><a href="scipy.stats.html">scipy.stats</a><br>
<a href="sys.html">sys</a><br>
<a href="tqdm.html">tqdm</a><br>
</td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#eeaa77">
<td colspan=3 valign=bottom>&nbsp;<br>
Expand All @@ -42,36 +44,12 @@
<dl><dt><a name="-analyzeScaleStudy"><strong>analyzeScaleStudy</strong></a>(fileName:str, names:list=None) -&gt; tuple</dt><dd><tt>Analyzes&nbsp;a&nbsp;scale&nbsp;study.<br>
If&nbsp;names&nbsp;parameter&nbsp;given,&nbsp;filters&nbsp;for&nbsp;only&nbsp;those&nbsp;names.<br>
Names&nbsp;can&nbsp;be&nbsp;any&nbsp;type&nbsp;of&nbsp;iterable.</tt></dd></dl>
<dl><dt><a name="-getsizeof"><strong>getsizeof</strong></a>(...)</dt><dd><tt><a href="#-getsizeof">getsizeof</a>(object,&nbsp;default)&nbsp;-&gt;&nbsp;int<br>
&nbsp;<br>
Return&nbsp;the&nbsp;size&nbsp;of&nbsp;object&nbsp;in&nbsp;bytes.</tt></dd></dl>
<dl><dt><a name="-stat"><strong>stat</strong></a>(path, *, dir_fd=None, follow_symlinks=True)</dt><dd><tt>Perform&nbsp;a&nbsp;stat&nbsp;system&nbsp;call&nbsp;on&nbsp;the&nbsp;given&nbsp;path.<br>
&nbsp;<br>
&nbsp;&nbsp;path<br>
&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;to&nbsp;be&nbsp;examined;&nbsp;can&nbsp;be&nbsp;string,&nbsp;bytes,&nbsp;a&nbsp;path-like&nbsp;object&nbsp;or<br>
&nbsp;&nbsp;&nbsp;&nbsp;open-file-descriptor&nbsp;int.<br>
&nbsp;&nbsp;dir_fd<br>
&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;not&nbsp;None,&nbsp;it&nbsp;should&nbsp;be&nbsp;a&nbsp;file&nbsp;descriptor&nbsp;open&nbsp;to&nbsp;a&nbsp;directory,<br>
&nbsp;&nbsp;&nbsp;&nbsp;and&nbsp;path&nbsp;should&nbsp;be&nbsp;a&nbsp;relative&nbsp;string;&nbsp;path&nbsp;will&nbsp;then&nbsp;be&nbsp;relative&nbsp;to<br>
&nbsp;&nbsp;&nbsp;&nbsp;that&nbsp;directory.<br>
&nbsp;&nbsp;follow_symlinks<br>
&nbsp;&nbsp;&nbsp;&nbsp;If&nbsp;False,&nbsp;and&nbsp;the&nbsp;last&nbsp;element&nbsp;of&nbsp;the&nbsp;path&nbsp;is&nbsp;a&nbsp;symbolic&nbsp;link,<br>
&nbsp;&nbsp;&nbsp;&nbsp;stat&nbsp;will&nbsp;examine&nbsp;the&nbsp;symbolic&nbsp;link&nbsp;itself&nbsp;instead&nbsp;of&nbsp;the&nbsp;file<br>
&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;link&nbsp;points&nbsp;to.<br>
&nbsp;<br>
dir_fd&nbsp;and&nbsp;follow_symlinks&nbsp;may&nbsp;not&nbsp;be&nbsp;implemented<br>
&nbsp;&nbsp;on&nbsp;your&nbsp;platform.&nbsp;&nbsp;If&nbsp;they&nbsp;are&nbsp;unavailable,&nbsp;using&nbsp;them&nbsp;will&nbsp;raise&nbsp;a<br>
&nbsp;&nbsp;NotImplementedError.<br>
&nbsp;<br>
It's&nbsp;an&nbsp;error&nbsp;to&nbsp;use&nbsp;dir_fd&nbsp;or&nbsp;follow_symlinks&nbsp;when&nbsp;specifying&nbsp;path&nbsp;as<br>
&nbsp;&nbsp;an&nbsp;open&nbsp;file&nbsp;descriptor.</tt></dd></dl>
</td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#55aa55">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>

<tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><strong>__annotations__</strong> = {}<br>
<strong>argv</strong> = ['/usr/lib/python3.6/pydoc.py', '-w', './']</td></tr></table>
<td width="100%"><strong>__annotations__</strong> = {}</td></tr></table>
</body></html>
Loading

0 comments on commit be4f083

Please sign in to comment.