Skip to content

Commit

Permalink
Remove separate input/output axes (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan authored Feb 4, 2025
1 parent 144be14 commit 25c4fd3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 93 deletions.
53 changes: 23 additions & 30 deletions src/main/java/qupath/bioimageio/spec/tensor/axes/Axes.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Axis[] deserialize(JsonElement jsonElement, Type type, JsonDeserializatio
}
return axes;
}
// todo: input or output???
// todo: handle separate input or output axis types if needed
if (jsonElement.isJsonArray()) {
var arr = jsonElement.getAsJsonArray();
Axis[] axes = new Axis[arr.size()];
Expand All @@ -70,44 +70,37 @@ public Axis[] deserialize(JsonElement jsonElement, Type type, JsonDeserializatio
var id = deserializeField(context, oj, "id", String.class, "");
var desc = deserializeField(context, oj, "description", String.class, "");
Size size = deserializeSize(context, oj.get("size"), oj.get("scale"));
switch (oj.get("type").getAsString()) {
case "time":
axes[i] = new TimeAxes.TimeInputAxis(
id, desc,
TimeAxes.TimeUnit.valueOf(oj.get("unit").getAsString().toUpperCase()),
oj.get("scale").getAsDouble(),
size
);
break;
case "channel":
axes[i] = switch (oj.get("type").getAsString()) {
case "time" -> new TimeAxes.TimeAxis(
id, desc,
TimeAxes.TimeUnit.valueOf(oj.get("unit").getAsString().toUpperCase()),
oj.get("scale").getAsDouble(),
size
);
case "channel" -> {
var namesJSON = oj.get("channel_names").getAsJsonArray();
List<String> names = new LinkedList<>();
for (JsonElement n : namesJSON) {
names.add(n.getAsString());
}
axes[i] = new ChannelAxis(
yield new ChannelAxis(
id, desc,
names
);
break;
case "index":
axes[i] = new IndexAxes.IndexInputAxis(id, desc, size);
break;
case "space":
axes[i] = new SpaceAxes.SpaceInputAxis(
id, desc,
deserializeField(context, oj, "unit", String.class, ""),
deserializeField(context, oj, "scale", Double.class, 1.0),
size
);
break;
case "batch":
axes[i] = new BatchAxis(id, desc, deserializeField(context, oj, "size", Integer.class, 1));
break;
default:
}
case "index" -> new IndexAxes.IndexAxis(id, desc, size);
case "space" -> new SpaceAxes.SpaceAxis(
id, desc,
deserializeField(context, oj, "unit", String.class, ""),
deserializeField(context, oj, "scale", Double.class, 1.0),
size
);
case "batch" -> new BatchAxis(id, desc, deserializeField(context, oj, "size", Integer.class, 1));
default -> {
logger.error("Unknown object {}", oj);
axes[i] = null;
}
yield null;
}
};
}
return axes;
}
Expand Down
21 changes: 2 additions & 19 deletions src/main/java/qupath/bioimageio/spec/tensor/axes/IndexAxes.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public double getScale() {
}
}

static class IndexInputAxis extends IndexAxisBase {
static class IndexAxis extends IndexAxisBase {
private final Size size;
private final boolean concatenable = false;

IndexInputAxis(String id, String description, Size size) {
IndexAxis(String id, String description, Size size) {
super(id, description);
this.size = size;
}
Expand All @@ -63,21 +63,4 @@ public void validate(List<? extends BaseTensor> tensors) {
}
}

static class IndexOutputAxis extends IndexAxisBase {
private Size size;

IndexOutputAxis(String id, String description) {
super(id, description);
}

@Override
public Size getSize() {
return size;
}

@Override
public void validate(List<? extends BaseTensor> tensors) {
size.validate(tensors);
}
}
}
25 changes: 4 additions & 21 deletions src/main/java/qupath/bioimageio/spec/tensor/axes/SpaceAxes.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ public SpaceUnit getUnit() {
}
}

static class SpaceInputAxis extends SpaceAxisBase {
static class SpaceAxis extends SpaceAxisBase {
private final Size size;
private final boolean concatenable = false;

SpaceInputAxis(String id, String description, String unit, double scale, Size size) {
SpaceAxis(String id, String description, String unit, double scale, Size size) {
super(id, description, unit.isEmpty() ? "NO_UNIT" : unit, scale);
this.size = size;
}
Expand All @@ -129,30 +129,13 @@ public void validate(List<? extends BaseTensor> tensors) {
}
}

static class SpaceOutputAxis extends SpaceAxisBase {
private final Size size;

SpaceOutputAxis(String id, String description, String unit, double scale, Size size) {
super(id, description, unit, scale);
this.size = size;
}

@Override
public Size getSize() {
return this.size;
}

@Override
public void validate(List<? extends BaseTensor> tensors) {
getSize().validate(tensors);
}
}

static class SpaceOutputAxisWithHalo extends SpaceOutputAxis implements WithHalo {
static class SpaceAxisWithHalo extends SpaceAxis implements WithHalo {
private ReferencedSize size;
private final int halo;

SpaceOutputAxisWithHalo(String id, String description, String unit, double scale, Size size, int halo) {
SpaceAxisWithHalo(String id, String description, String unit, double scale, Size size, int halo) {
super(id, description, unit, scale, size);
this.halo = halo;
}
Expand Down
27 changes: 4 additions & 23 deletions src/main/java/qupath/bioimageio/spec/tensor/axes/TimeAxes.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public TimeUnit getUnit() {
}
}

static class TimeInputAxis extends TimeAxisBase {
static class TimeAxis extends TimeAxisBase {
private final Size size;

TimeInputAxis(String id, String description, TimeUnit unit, double scale, Size size) {
TimeAxis(String id, String description, TimeUnit unit, double scale, Size size) {
super(id, description, unit, scale);
this.size = size;
}
Expand All @@ -70,29 +70,10 @@ public void validate(List<? extends BaseTensor> tensors) {
}
}

static class TimeOutputAxis extends TimeAxisBase {
private final Size size;

TimeOutputAxis(String id, String description, TimeUnit unit, double scale, Size size) {
super(id, description, unit, scale);
this.size = size;
}

@Override
public Size getSize() {
return size;
}

@Override
public void validate(List<? extends BaseTensor> tensors) {
getSize().validate(tensors);
}
}

static class TimeOutputAxisWithHalo extends TimeOutputAxis implements WithHalo {
static class TimeAxisWithHalo extends TimeAxis implements WithHalo {
private final int halo;

TimeOutputAxisWithHalo(String id, String description, TimeUnit unit, double scale, Size size, int halo) {
TimeAxisWithHalo(String id, String description, TimeUnit unit, double scale, Size size, int halo) {
super(id, description, unit, scale, size);
this.halo = halo;
}
Expand Down

0 comments on commit 25c4fd3

Please sign in to comment.