Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle infinities #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DIR ?= /tmp/
DIR ?= /tmp/omero-build

all: blitz/Units.ice blitz2 blitz3 blitz4 blitz5 formats/UnitsFactory.java model model2 sql xsd

Expand Down Expand Up @@ -68,14 +68,15 @@ xsd/units-conversion.xsl: $(units)
./gen.py --markup --combine templates/xsd $(units) > $@

move:
/bin/mv $(model_enums) $(DIR)/components/model/src/ome/model/enums
/bin/mv $(model_objs) $(DIR)/components/model/src/ome/model/units
/bin/mv $(blitz_ice) $(DIR)/components/blitz/resources/omero/model/
/bin/mv $(blitz_java) $(DIR)/components/blitz/src/omero/model/
/bin/mv blitz/Units.ice $(DIR)/components/blitz/resources/omero/model
/bin/mv $(blitz_py) $(DIR)/components/tools/OmeroPy/src
/bin/mv $(blitz_cpp) $(DIR)/components/tools/OmeroCpp/src/omero/model
/bin/mv $(blitz_h) $(DIR)/components/tools/OmeroCpp/src/omero/model
/bin/mv $(model_enums) $(DIR)/omero-model/src/main/java/ome/model/enums
/bin/mv $(model_objs) $(DIR)/omero-model/src/main/java/ome/model/units
/bin/mv $(blitz_ice) $(DIR)/omero-blitz/src/main/slice/omero/model/
/bin/mv $(blitz_java) $(DIR)/omero-blitz/src/main/java/omero/model/
/bin/mv blitz/Units.ice $(DIR)/omero-blitz/src/main/slice/omero/model
echo Following disabled:
echo /bin/mv $(blitz_py) $(DIR)/components/tools/OmeroPy/src
echo /bin/mv $(blitz_cpp) $(DIR)/components/tools/OmeroCpp/src/omero/model
echo /bin/mv $(blitz_h) $(DIR)/components/tools/OmeroCpp/src/omero/model

bfmove:
/bin/mv xsd/units-conversion.xsl $(BFDIR)/components/specification/transforms
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Small Genshi set up for the generation of
various products which require unit information
from the 5.1 OME schema.
from the 5.1 OME schema.

Steps
-----
- `conda env create -f environment.yml`
- If necessary, `python omesym/run.py --python > equations.py`
- `make DIR=/opt/omero-build all move`
9 changes: 9 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: ome-units
channels:
- defaults
dependencies:
- python=2.7
- pip
- pip:
- genshi
- sympy<1.1
1 change: 1 addition & 0 deletions equations.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion omesym/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def add_si(key, value, units, equations):
for viz, sym, factor in for_each():
x = "%s%s" % (sym, key)
s = Symbol(x)
units[s] = sym.upper() + value
units[s] = str(sym).upper() + value
eq = Eq(key, factor * s)
equations.append(eq)

Expand Down
13 changes: 7 additions & 6 deletions templates/blitz.ice
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module omero {

/**
* Unit of ${name} which is used through the model. This is not
* an [omero::model::IObject] implementation and as such does
* an {@link omero.model.IObject} implementation and as such does
* not have an ID value. Instead, the entire object is embedded
* into the containing class, so that the value and unit rows
* can be found on the table itself (e.g. ${dotexample(name)}
Expand All @@ -61,30 +61,31 @@ module omero {

/**
* PositiveFloat value
*/
**/
double value;

omero::model::enums::Units${name} unit;

/**
* Actual value for this unit-based field. The interpretation of
* the value is only possible along with the [omero::model::enums::Units${name}]
* enum.
* the value is only possible along with the
* {@link omero.model.enums.Units${name}} enum.
**/
double getValue();

void setValue(double value);

/**
* [omero::model::enums::Units${name}] instance which is an [omero::model::IObject]
* {@link omero.model.enums.Units${name}} instance which is an
* {@link omero.model.IObject}
* meaning that its ID is sufficient for identifying equality.
**/
omero::model::enums::Units${name} getUnit();

void setUnit(omero::model::enums::Units${name} unit);

/**
* Returns the possibly unicode representation of the "unit"
* Returns the possibly unicode representation of the ""unit""
* value for display.
**/
string getSymbol();
Expand Down
45 changes: 34 additions & 11 deletions templates/blitz.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,29 +199,52 @@ public void destroy() {
* Copy constructor that converts the given {@link omero.model.${name}}
* based on the given enum string.
*
* If either the source or the target unit is null or if no conversion
* is possible between the two types (e.g. PIXELS), an
* {@link IllegalArgumentException} will be thrown. If the conversion
* results in an overflow, a {@link BigResult} will be thrown, unless
* the input value was already Infinite or NaN, in which case that will
* be the return value.
*
* @param target String representation of the CODE enum
* @throws IllegalArgumentException if the source or target unit
* is null or an unconvertible type (e.g. PIXELS)
* @throws BigResult if the conversion leads to an infinite or NaN result
*/
public ${name}I(${name} value, String target) throws BigResult {
String source = value.getUnit().toString();

final Units${name} sourceUnit = value.getUnit();
final Units${name} targetUnit = Units${name}.valueOf(target);
if (sourceUnit == null || targetUnit == null) {
throw new IllegalArgumentException(String.format(
"conversion impossible from %s to %s",
sourceUnit, targetUnit));
}

final String source = value.getUnit().toString();
if (target.equals(source)) {
setValue(value.getValue());
setUnit(value.getUnit());
} else {
Units${name} targetUnit = Units${name}.valueOf(target);
Conversion conversion = conversions.get(value.getUnit()).get(target);
} else {
final Conversion conversion = conversions.get(sourceUnit).get(targetUnit);
if (conversion == null) {
throw new RuntimeException(String.format(
throw new IllegalArgumentException(String.format(
"%f %s cannot be converted to %s",
value.getValue(), value.getUnit(), target));
}
double orig = value.getValue();
BigDecimal big = conversion.convert(orig);
double converted = big.doubleValue();
if (Double.isInfinite(converted)) {
throw new BigResult(big,
"Failed to convert " + source + ":" + target);
double converted = orig;
if (!Double.isFinite(orig)) {
// Infinite or NaN
// Do nothing. Use orig
} else {
BigDecimal big = conversion.convert(orig);
converted = big.doubleValue();
if (!Double.isFinite(converted)) {
throw new BigResult(big,
"Failed to convert " + source + ":" + target);
}
}

setValue(converted);
setUnit(targetUnit);
}
Expand Down