Skip to content

Commit

Permalink
Make fetching space/time units more robust (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan authored Feb 6, 2025
1 parent d2e0175 commit fb0e0a3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/main/java/qupath/bioimageio/spec/tensor/axes/Axes.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Axis[] deserialize(JsonElement jsonElement, Type type, JsonDeserializatio
axes[i] = switch (oj.get("type").getAsString()) {
case "time" -> new TimeAxes.TimeAxis(
id, desc,
TimeAxes.TimeUnit.valueOf(oj.get("unit").getAsString().toUpperCase()),
TimeAxes.TimeUnit.getUnit(oj.get("unit").getAsString()),
oj.get("scale").getAsDouble(),
size
);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/qupath/bioimageio/spec/tensor/axes/SpaceAxes.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class SpaceAxes {

/**
* Possible SI and imperial units for distance in physical space (some more likely to be used than others).
* Possible SI and imperial units for distance in physical space.
*/
public enum SpaceUnit {
ATTOMETER("attometer"),
Expand Down Expand Up @@ -61,22 +61,22 @@ public enum SpaceUnit {

private final String unit;

SpaceUnit(String s) {
this.unit = s;
SpaceUnit(String unit) {
this.unit = unit;
}

/**
* Get the SpaceUnit corresponding to a String value.
* @param unit The unit as a string
* @return The corresponding unit, or null if not found.
* @param unit the unit as a string
* @return the corresponding unit, or NO_UNIT if not found.
*/
public static SpaceUnit getUnit(String unit) {
for (var su: SpaceUnit.values()) {
for (var su: values()) {
if (su.unit.equalsIgnoreCase(unit)) {
return su;
}
}
return null;
return NO_UNIT;
}
}

Expand All @@ -85,7 +85,7 @@ abstract static class SpaceAxisBase extends AxisBase implements ScaledAxis {
private final double scale;

SpaceAxisBase(String id, String description, String unit, double scale) {
this(id, description, SpaceUnit.valueOf(unit.toUpperCase()), scale);
this(id, description, SpaceUnit.getUnit(unit), scale);
}

SpaceAxisBase(String id, String description, SpaceUnit unit, double scale) {
Expand Down
71 changes: 47 additions & 24 deletions src/main/java/qupath/bioimageio/spec/tensor/axes/TimeAxes.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,55 @@ public int getHalo() {
}

/**
* Possible SI units for time.
* Possible units for time.
*/
public enum TimeUnit {
ATTOSECOND,
CENTISECOND,
DAY,
DECISECOND,
EXASECOND,
FEMTOSECOND,
GIGASECOND,
HECTOSECOND,
HOUR,
KILOSECOND,
MEGASECOND,
MICROSECOND,
MILLISECOND,
MINUTE,
NANOSECOND,
PETASECOND,
PICOSECOND,
SECOND,
TERASECOND,
YOCTOSECOND,
YOTTASECOND,
ZEPTOSECOND,
ZETTASECOND
ATTOSECOND("attosecond"),
CENTISECOND("centisecond"),
DAY("day"),
DECISECOND("decisecond"),
EXASECOND("exasecond"),
FEMTOSECOND("femtosecond"),
GIGASECOND("gigasecond"),
HECTOSECOND("hectosecond"),
HOUR("hour"),
KILOSECOND("kilosecond"),
MEGASECOND("megasecond"),
MICROSECOND("microsecond"),
MILLISECOND("millisecond"),
MINUTE("minute"),
NANOSECOND("nanosecond"),
PETASECOND("petasecond"),
PICOSECOND("picosecond"),
SECOND("second"),
TERASECOND("terasecond"),
YOCTOSECOND("yoctosecond"),
YOTTASECOND("yottasecond"),
ZEPTOSECOND("zeptosecond"),
ZETTASECOND("zettasecond"),
NO_UNIT("");

private final String unit;

TimeUnit(String unit) {
this.unit = unit;
}

/**
* Get the TimeUnit corresponding to a String value.
* @param unit the unit as a string
* @return the corresponding unit, or NO_UNIT if not found.
*/
public static TimeUnit getUnit(String unit) {
for (var tu: values()) {
if (tu.unit.equalsIgnoreCase(unit)) {
return tu;
}
}
return NO_UNIT;
}
}



}

0 comments on commit fb0e0a3

Please sign in to comment.