{"issue":{"created_at":"2011-12-06T13:17:46Z","state":"open","updated_at":"2011-12-06T21:15:07Z","comments":1,"user":{"gravatar_id":"6c2496a73573bf0c9ac85f71db561475","id":483428,"url":"https://api.github.com/users/cgohlke","login":"cgohlke","avatar_url":"https://secure.gravatar.com/avatar/6c2496a73573bf0c9ac85f71db561475?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"Consider lines 78 to 82 in the util.dtype._convert function:\r\n\r\n    image = image.astype(np.float64)\r\n    out = image - imin\r\n    out *= scale\r\n    out += shift\r\n    out = round_fn(out).astype(dtype)\r\n\r\nThis is potentially very memory and computationally intensive. For example, in the case of uint8 to uint16 conversion, two intermediate arrays of 16 times the size of the input array will be created and the conversion could be done by simply shifting bits instead of using double precision floating point operations.\r\n\r\nSecond, the scale and round functions are not always correct. For example, double to uint8 should be converted as\r\n\r\n    np.uint8(image * np.nextafter(image.dtype.type(256), image.dtype.type(0)))\r\n\r\ninstead of \r\n\r\n   np.round(image * 255).astype(np.uint8)\r\n\r\nTry with image = np.array([1.0/257.0])","assignee":null,"id":2461937,"labels":[],"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":92,"title":"util.dtype._convert is inefficient and not always correct","html_url":"https://github.com/scikits-image/scikits-image/issues/92","closed_at":null,"url":"https://api.github.com/repos/scikits-image/scikits-image/issues/92","milestone":null},"action":"created","comment":{"created_at":"2011-12-06T21:15:07Z","updated_at":"2011-12-06T21:15:07Z","user":{"gravatar_id":"b30b909765488cf4cf9de4a496f9a408","id":45071,"url":"https://api.github.com/users/stefanv","login":"stefanv","avatar_url":"https://secure.gravatar.com/avatar/b30b909765488cf4cf9de4a496f9a408?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"I welcome suggestions of how to improve these conversions so that they work for all types.  The challenge is to keep the code simple and fairly generic, but to still avoid copying where possible.  I'm very glad that we decided to factor out these utility functions; they are now cropping up all over the place, and we can fix performance, accuracy etc. in this one spot.\r\n\r\nMaybe you can explain your last example to me in more detail, because I'm not sure why 1/257 should map to 0 rather than 1.  Our range is from 0 to 255, so currently the idea is to do interpolation: t * 255 where t varies between 0 and 1.  In your case, t=1/257=0.0039, and t * 255 = 0.9922.\r\n","id":3038865,"url":"https://api.github.com/repos/scikits-image/scikits-image/issues/comments/3038865"}}