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

Time unit fixes #386

Merged
merged 4 commits into from
Jul 22, 2021
Merged
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
37 changes: 21 additions & 16 deletions plugin/omero_iviewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,19 @@ def image_data(request, image_id, conn=None, **kwargs):
px = image.getPrimaryPixels().getPhysicalSizeX()
if (px is not None):
size = image.getPixelSizeX(True)
value = format_value_with_units(size)
value = format_pixel_size_with_units(size)
rv['pixel_size']['unit_x'] = value[0]
rv['pixel_size']['symbol_x'] = value[1]
py = image.getPrimaryPixels().getPhysicalSizeY()
if (py is not None):
size = image.getPixelSizeY(True)
value = format_value_with_units(size)
value = format_pixel_size_with_units(size)
rv['pixel_size']['unit_y'] = value[0]
rv['pixel_size']['symbol_y'] = value[1]
pz = image.getPrimaryPixels().getPhysicalSizeZ()
if (pz is not None):
size = image.getPixelSizeZ(True)
value = format_value_with_units(size)
value = format_pixel_size_with_units(size)
rv['pixel_size']['unit_z'] = value[0]
rv['pixel_size']['symbol_z'] = value[1]

Expand Down Expand Up @@ -551,10 +551,11 @@ def delta_t_data(request, image_id, conn=None, **kwargs):
for info in info_list:
t_index = info.theT.getValue()
if info.deltaT is not None:
value = format_value_with_units(info.deltaT)
timemap[t_index] = value[0]
secs = get_converted_value(info.deltaT, "SECOND")
timemap[t_index] = secs
if delta_t_unit_symbol is None:
delta_t_unit_symbol = value[1]
# Get unit symbol for first info only
delta_t_unit_symbol = info.deltaT.getSymbol()
for t in range(image.getSizeT()):
if t in timemap:
time_list.append(timemap[t])
Expand All @@ -565,7 +566,20 @@ def delta_t_data(request, image_id, conn=None, **kwargs):
return JsonResponse(rv)


def format_value_with_units(value):
def get_converted_value(obj, units):
"""
Convert the length or time object to units and return value

@param obj value object
@param units string, e.g. "SECOND"
"""
unitClass = obj.getUnit().__class__
unitEnum = getattr(unitClass, str(units))
obj = obj.__class__(obj, unitEnum)
return obj.getValue()


def format_pixel_size_with_units(value):
"""
Formats the response for methods above.
Returns [value, unitSymbol]
Expand All @@ -579,15 +593,6 @@ def format_value_with_units(value):
if unit == "MICROMETER":
unit = lengthunit(length)
length = lengthformat(length)
elif unit == "MILLISECOND":
length = length/1000.0
unit = value.getSymbol()
elif unit == "MINUTE":
length = 60*length
unit = value.getSymbol()
elif unit == "HOUR":
length = 3600*length
unit = value.getSymbol()
else:
unit = value.getSymbol()
return (length, unit)
Expand Down
8 changes: 3 additions & 5 deletions src/controls/dimension-slider.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@
${image_config.image_info.dimensions['max_' + dim]}
</span>
<span show.bind="image_config.image_info.image_delta_t.length > 0"
class="timestamp">
${image_config.image_info.dimensions[dim] &lt;
image_config.image_info.image_delta_t.length ?
image_config.image_info.image_delta_t[
image_config.image_info.dimensions[dim]] : '&nbsp;'}
title="${image_config.image_info.formatDeltaT(image_config.image_info.image_delta_t[image_config.image_info.dimensions[dim]], true)}"
class="timestamp">
${image_config.image_info.formatDeltaT(image_config.image_info.image_delta_t[image_config.image_info.dimensions[dim]])}
</span>
</template>
</template>
2 changes: 1 addition & 1 deletion src/controls/dimension-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export default class DimensionSlider {
this.dim.toUpperCase() + ":" + (newDimVal+1);
if (this.dim === 't' && imgInf.image_delta_t.length > 0 &&
newDimVal < imgInf.image_delta_t.length)
sliderTip += " [" + imgInf.image_delta_t[newDimVal] + "]";
sliderTip += " [" + imgInf.formatDeltaT(imgInf.image_delta_t[newDimVal]) + "]";
sliderValueSpan.text(sliderTip);
let percent = (ui.value / (imgInf.dimensions['max_' + this.dim] - 1)) * 100;
if (this.dim === 'z') {
Expand Down
86 changes: 53 additions & 33 deletions src/model/image_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,51 +642,71 @@ export default class ImageInfo {

/**
* Sets image_delta_t member from response
* after formatting it to hours:minutes:seconds:milliseconds
*
* @private
* @param {Object} response the response object
* @memberof ImageInfo
*/
setFormattedDeltaT(response) {

this.image_delta_t = response.delta_t

// original units
this.image_delta_t_unit = response.delta_t_unit_symbol;
}

/**
* Formats delta-T to 'dd hh:mm:ss:milliseconds'
*
* @param {Number} t time in seconds
* @param {Boolean} verbose if true, use e.g. '10 days 01 hours minutes seconds'
* @memberof ImageInfo
*/
formatDeltaT(t, verbose=false) {
// avoid further IEEE inaccuracies for remainders
// by using multiplier and rounding to integer (at ms effectively)
const precision = 1000;

let deltaTisAllZeros = true;
response.delta_t.map((t) => {
t = Math.round(t * precision);
let deltaTformatted = "";
t = Math.round(t * precision);
let deltaTformatted = "";

// put minus in front
let isNegative = t < 0;
if (isNegative) {
deltaTformatted += "-";
t = -t;
}
if (t !== 0) deltaTisAllZeros = false;
// hrs
let hours = parseInt(t / (3600 * precision));
deltaTformatted += ("00" + hours).slice(-2) + ":";
t -= (hours * 3600 * precision);
// minutes
let mins = parseInt(t / (60 * precision));
deltaTformatted += ("00" + mins).slice(-2) + ":";
t -= (mins * (60 * precision));
// seconds
let secs = parseInt(t / precision);
deltaTformatted += ("00" + secs).slice(-2) + ".";
// milliseconds
let millis = t - (secs * precision);
deltaTformatted += ("000" + millis).slice(-3);
this.image_delta_t.push(deltaTformatted);
});
// put minus in front
let isNegative = t < 0;
if (isNegative) {
deltaTformatted += "-";
t = -t;
}
// days
let days = parseInt(t / (24 * 3600 * precision));
t -= (days * 24 * 3600 * precision);
// hrs
let hours = parseInt(t / (3600 * precision));
t -= (hours * 3600 * precision);
// minutes
let mins = parseInt(t / (60 * precision));
t -= (mins * (60 * precision));
// seconds
let secs = parseInt(t / precision);
// milliseconds
let millis = t - (secs * precision);

function pad(value, length=2) {
return ("000" + value).slice(-length);
}

// we reset to deltaT to [] if all zeros
if (deltaTisAllZeros) this.image_delta_t = [];
// original units
this.image_delta_t_unit = response.delta_t_unit_symbol;
}
function s(value) {
return value == 1 ? "" : "s";
}
if (days != 0) {
deltaTformatted += (verbose ? (days + ` day${s(days)} `) : (pad(days) + " "));
}
if (verbose) {
deltaTformatted += `${hours} hour${s(hours)} ${mins} minute${s(mins)} ${secs}.${pad(millis, 3)} seconds`
} else {
deltaTformatted += `${pad(hours)}:${pad(mins)}:${pad(secs)}.${pad(millis, 3)}`
}
return deltaTformatted;
};

/**
* Retrieves the copied rendering settings
Expand Down