<commit_before>from scikits.audiolab import wavread
import sys, math
import svgwrite
import xml.etree.ElementTree as etree

samples = int(sys.argv[2])
data, fs, enc = wavread(sys.argv[1])
if data.ndim == 1: # mono
	maxValues = data
else: # stereo
	maxValues = data.max(axis = 1)
perRing = len(data)/samples

gap = 5.0
inner = 2.5
width = 181

minD = 5
step = 1.5
maxD = 21.5
steps = int((maxD - minD)/step) + 1
print steps

highest = [maxValues[(perRing*i):(perRing*(i+1))].max() for i in range(samples)]
print highest
biggest = max(highest)
perStep = biggest/steps
print perStep
rings = [int(math.ceil(x/perStep)) for x in highest]
print rings

rings = rings *2

counts = [sum([1 for r in rings if r == x]) for x in range(1, steps+1)]
print counts
for i in range(steps):
	toprint = "".join(["*" if x > i else " " for x in rings])
	print toprint

fname = sys.argv[1].replace("wav", "svg")
dwg = svgwrite.Drawing(size = ("181mm", "181mm"))

location = [5,5]
blue = "rgb(0,0,255)"
format = "%.2fmm"

for ring in rings:
	diameter = (ring-1)*step + minD

	if location[0] + diameter > width:
		location[0] = 5
		location[1] += maxD + gap

	radius = diameter / 2.0
	dwg.add(dwg.circle(center = (format%(location[0] + radius), format%(location[1] + radius)),
                                   r = format %radius ,
                                   stroke_width = "0.01mm",
                                   stroke = blue,
                                   fill = "none"))
	dwg.add(dwg.circle(center = (format%(location[0] + radius), format%(location[1] + radius)),
                                   r = format % (inner/2.0),
                                   stroke_width = "0.01mm",
                                   stroke = blue,
                                   fill = "none"))

	location[0] += diameter + gap

def indent(elem, level=0):
    i = "\n" + level*"\t"
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "\t"
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

root = dwg.get_xml()
indent(root)
open(fname, "w").write(etree.tostring(root))<commit_msg>Pull Necklace code into a separate function<commit_after>from scikits.audiolab import wavread
import math
import svgwrite
import xml.etree.ElementTree as etree

def createNecklace(filename, samples):
	data, fs, enc = wavread(filename)
	if data.ndim == 1: # mono
		maxValues = data
	else: # stereo
		maxValues = data.max(axis = 1)
	perRing = len(data)/samples

	gap = 5.0
	inner = 2.5
	width = 181

	minD = 5
	step = 1.5
	maxD = 21.5
	steps = int((maxD - minD)/step) + 1
	print steps

	highest = [maxValues[(perRing*i):(perRing*(i+1))].max() for i in range(samples)]
	print highest
	biggest = max(highest)
	perStep = biggest/steps
	print perStep
	rings = [int(math.ceil(x/perStep)) for x in highest]
	print rings

	rings = rings *2

	counts = [sum([1 for r in rings if r == x]) for x in range(1, steps+1)]
	print counts
	for i in range(steps):
		toprint = "".join(["*" if x > i else " " for x in rings])
		print toprint

	fname = filename.replace("wav", "svg")
	dwg = svgwrite.Drawing(size = ("181mm", "181mm"))

	location = [5,5]
	blue = "rgb(0,0,255)"
	format = "%.2fmm"

	for ring in rings:
		diameter = (ring-1)*step + minD

		if location[0] + diameter > width:
			location[0] = 5
			location[1] += maxD + gap

		radius = diameter / 2.0
		dwg.add(dwg.circle(center = (format%(location[0] + radius), format%(location[1] + radius)),
	                                   r = format %radius ,
	                                   stroke_width = "0.01mm",
	                                   stroke = blue,
	                                   fill = "none"))
		dwg.add(dwg.circle(center = (format%(location[0] + radius), format%(location[1] + radius)),
	                                   r = format % (inner/2.0),
	                                   stroke_width = "0.01mm",
	                                   stroke = blue,
	                                   fill = "none"))

		location[0] += diameter + gap

	# http://stackoverflow.com/a/4590052/320546
	def indent(elem, level=0):
	    i = "\n" + level*"\t"
	    if len(elem):
	        if not elem.text or not elem.text.strip():
	            elem.text = i + "\t"
	        if not elem.tail or not elem.tail.strip():
	            elem.tail = i
	        for elem in elem:
	            indent(elem, level+1)
	        if not elem.tail or not elem.tail.strip():
	            elem.tail = i
	    else:
	        if level and (not elem.tail or not elem.tail.strip()):
	            elem.tail = i

	root = dwg.get_xml()
	indent(root)
	return etree.tostring(root)

if __name__ == "__main__":
	import sys
	samples = int(sys.argv[2])
	filename = sys.argv[1]
	xml = createNecklace(filename, samples)
	fname = filename.replace("wav", "svg")
	open(fname, "w").write(xml)
{'fraction_non_alphanumeric': 0.14077574325654657, 'fraction_numerical': 0.02598936798582398, 'mean_word_length': 2.0787878787878786, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2164292', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1691, 'n_words': 473}