-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate.py
64 lines (54 loc) · 2.04 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Translator:
def __init__(self, detector, map={}):
# No f, so as not to confuse with frequency
self.alphabet = 'abcdeghijklmnopqrstuvwxyz'
self.detector = detector
self.mapping = map
if len(self.mapping) == 0:
print('\nMap:\n')
for i, key in enumerate(self.detector.parameters):
self.mapping[key] = ' ' + self.alphabet[i] + ' '
print(self.mapping[key] + '\t\t:\t\t' + self.detector.names[key])
print('''
\nPlease ensure thet each symbol is preceded and followed by a space.
\nSet up a dictionary where the key is the name of your noise class,
all one word, and the value is the function describing its noise curve,
using the key printed above.
''')
def translate(self, string):
# Invert the mapping
self.inversemapping = {v: k for k, v in self.mapping.items()}
for value in self.inversemapping:
string = string.replace(value, \
'detector.parameters[\'' + \
self.inversemapping[value] + \
'\']')
return string
def generateNoiseScript(self, noiseDictionary):
# Takes a dictionary that defines a new noise function like
# {NoiseKey : 'FunctionString'}
# Create the new noise file
newNoiseFile = open('newnoises.py', 'w')
newNoiseFile.write('# User-defined noises\n\n')
self.classnames = []
for className in noiseDictionary:
self.classnames.append(className)
function = self.translate(noiseDictionary[className])
newNoiseFile.write("""
class {clsnm}:
@staticmethod
def Get{clsnm}(f, detector):
return {fnct}
@classmethod
def ComputePoint(cls, f, detector):
return cls.Get{clsnm}(f, detector)
""".format(clsnm=className, fnct=function))
newNoiseFile.close()
def getNewNoiseClasses(self):
import newnoises
newClasses = {}
for key in newnoises.__dict__:
for clsnm in self.classnames:
if clsnm in key:
newClasses[clsnm] = newnoises.__dict__[key]
return newClasses