forked from ome/omero-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.py
159 lines (123 loc) · 3.84 KB
/
conversions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 University of Dundee & Open Microscopy Environment.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Conversion utilities for changing between units.
"""
from __future__ import division
from builtins import str
from past.utils import old_div
from builtins import object
class Conversion(object):
"""
Base-functor like object which can be used for preparing complex
equations for converting from one unit to another. Primarily these
classes and static methods are used via code-generation. Sympy-generated
strings are placed directly into code. If the proper imports are in place,
then a top-level Conversion (usually of type Add or
Mul is returned from the evaluation).
"""
def __init__(self, *conversions):
self.conversions = conversions
def __call__(self, original):
raise NotImplemented()
def join(self, sym):
sb = sym.join([str(x) for x in self.conversions])
return "(%s)" % sb
class Add(Conversion):
"""
Sums all the Conversion instances which
are passed in to the constructor.
"""
def __call__(self, original):
rv = 0.0
for c in self.conversions:
rv += c(original)
return rv
def __str__(self):
return self.join(" + ")
class Int(Conversion):
"""
Simple representation of a possibly
very large integer.
"""
def __init__(self, i):
if isinstance(i, int):
self.i = i
else:
self.i = float(i) # Handles big strings
def __call__(self, original):
return self.i
def __str__(self):
return str(self.i)
class Mul(Conversion):
"""
Multiplies all the Conversion instances which
are passed in to the constructor.
"""
def __call__(self, original):
rv = 1.0
for c in self.conversions:
rv *= c(original)
return rv
def __str__(self):
return self.join(" * ")
class Pow(Conversion):
"""
Raises the first argument (base) to
the power of the second (exponent).
"""
def __init__(self, base, exp):
self.base = base
self.exp = exp
def __call__(self, original):
return self.base ** self.exp
def __str__(self):
return "(%s ** %s)" % (self.base, self.exp)
class Rat(Conversion):
"""
Divides the first argument (numerator)
by the second (denominator).
"""
def __init__(self, n, d):
self.n = n
self.d = d
def unwrap(self, x, original):
if isinstance(x, (int, float, str)):
return float(x)
else:
return x(original)
def __call__(self, original):
n = self.unwrap(self.n, original)
d = self.unwrap(self.d, original)
return old_div(float(n), d)
def __str__(self):
return "(%s / %s)" % (self.n, self.d)
class Sym(Conversion):
"""
Represents the variable of the source
unit and simply returns the original
value passed to it.
"""
def __init__(self, s):
self.s = s
def __call__(self, original):
return float(original)
def __str__(self):
return "x"