Coverage for tests/images/test_bioio_server.py: 100%

189 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-31 11:24 +0000

1import numpy as np 

2from qubalab.images.bioio_server import BioIOServer 

3from qubalab.images.region_2d import Region2D 

4from qubalab.images.metadata.pixel_calibration import PixelCalibration, PixelLength 

5from ..res import multi_resolution_uint8_3channels, single_resolution_float_5d, single_resolution_float_5d_zarr, single_resolution_rgb_image 

6 

7 

8def test_uint8_3channels_image_name(): 

9 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

10 

11 name = bioio_server.metadata.name 

12 

13 assert name == multi_resolution_uint8_3channels.get_name() 

14 

15 bioio_server.close() 

16 

17 

18def test_uint8_3channels_image_shapes(): 

19 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

20 

21 shapes = bioio_server.metadata.shapes 

22 

23 assert shapes == (multi_resolution_uint8_3channels.get_shapes()[0], ) # The BioIO library does not properly support pyramids 

24 

25 bioio_server.close() 

26 

27 

28def test_uint8_3channels_image_pixel_calibration(): 

29 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

30 

31 pixel_calibration = bioio_server.metadata.pixel_calibration 

32 

33 assert pixel_calibration == PixelCalibration( 

34 PixelLength(multi_resolution_uint8_3channels.get_pixel_size_x_y_in_micrometers()), # The BioIO library does not currently handle unit attachment 

35 PixelLength(multi_resolution_uint8_3channels.get_pixel_size_x_y_in_micrometers()) 

36 ) 

37 

38 bioio_server.close() 

39 

40 

41def test_uint8_3channels_image_is_rgb(): 

42 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

43 

44 is_rgb = bioio_server.metadata.is_rgb 

45 

46 assert is_rgb 

47 

48 bioio_server.close() 

49 

50 

51def test_uint8_3channels_image_dtype(): 

52 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

53 

54 dtype = bioio_server.metadata.dtype 

55 

56 assert dtype == multi_resolution_uint8_3channels.get_dtype() 

57 

58 bioio_server.close() 

59 

60 

61def test_uint8_3channels_image_downsamples(): 

62 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

63 

64 downsamples = bioio_server.metadata.downsamples 

65 

66 assert downsamples == (multi_resolution_uint8_3channels.get_downsamples()[0], ) # The BioIO library does not properly support pyramids 

67 

68 bioio_server.close() 

69 

70 

71def test_read_uint8_3channels_image(): 

72 level = 0 

73 full_resolution = multi_resolution_uint8_3channels.get_shapes()[level] 

74 downsample = multi_resolution_uint8_3channels.get_downsamples()[level] 

75 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

76 expected_pixels = np.array( 

77 [[[multi_resolution_uint8_3channels.get_pixel_value(downsample, x, y, c) 

78 for x in range(full_resolution.x)] 

79 for y in range(full_resolution.y)] 

80 for c in range(full_resolution.c)], 

81 multi_resolution_uint8_3channels.get_dtype() 

82 ) 

83 

84 image = bioio_server.read_region( 

85 downsample, 

86 Region2D(width=bioio_server.metadata.width, height=bioio_server.metadata.height) 

87 ) 

88 

89 np.testing.assert_array_equal(image, expected_pixels) 

90 

91 bioio_server.close() 

92 

93 

94def test_read_uint8_3channels_image_with_dask(): 

95 level = 0 

96 full_resolution = multi_resolution_uint8_3channels.get_shapes()[level] 

97 downsample = multi_resolution_uint8_3channels.get_downsamples()[level] 

98 bioio_server = BioIOServer(multi_resolution_uint8_3channels.get_path()) 

99 expected_pixels = np.array( 

100 [[[multi_resolution_uint8_3channels.get_pixel_value(downsample, x, y, c) 

101 for x in range(full_resolution.x)] 

102 for y in range(full_resolution.y)] 

103 for c in range(full_resolution.c)], 

104 multi_resolution_uint8_3channels.get_dtype() 

105 ) 

106 

107 image = bioio_server.level_to_dask(level).compute() 

108 

109 np.testing.assert_array_equal(image, expected_pixels) 

110 

111 bioio_server.close() 

112 

113 

114def test_float_5d_image_name(): 

115 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

116 

117 name = bioio_server.metadata.name 

118 

119 assert name == single_resolution_float_5d.get_name() 

120 

121 bioio_server.close() 

122 

123 

124def test_float_5d_image_shapes(): 

125 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

126 

127 shapes = bioio_server.metadata.shapes 

128 

129 assert shapes == single_resolution_float_5d.get_shapes() 

130 

131 bioio_server.close() 

132 

133 

134def test_float_5d_image_pixel_calibration(): 

135 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

136 

137 pixel_calibration = bioio_server.metadata.pixel_calibration 

138 

139 assert pixel_calibration == PixelCalibration( 

140 PixelLength(single_resolution_float_5d.get_pixel_size_x_y_in_micrometers()), # The BioIO library does not currently handle unit attachment 

141 PixelLength(single_resolution_float_5d.get_pixel_size_x_y_in_micrometers()) 

142 ) 

143 

144 bioio_server.close() 

145 

146 

147def test_float_5d_image_is_not_rgb(): 

148 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

149 

150 is_rgb = bioio_server.metadata.is_rgb 

151 

152 assert not(is_rgb) 

153 

154 bioio_server.close() 

155 

156 

157def test_float_5d_image_dtype(): 

158 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

159 

160 dtype = bioio_server.metadata.dtype 

161 

162 assert dtype == single_resolution_float_5d.get_dtype() 

163 

164 bioio_server.close() 

165 

166 

167def test_float_5d_image_downsamples(): 

168 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

169 

170 downsamples = bioio_server.metadata.downsamples 

171 

172 assert downsamples == single_resolution_float_5d.get_downsamples() 

173 

174 bioio_server.close() 

175 

176 

177def test_read_float_5d_image(): 

178 full_resolution = single_resolution_float_5d.get_shapes()[0] 

179 z = int(full_resolution.z / 2) 

180 t = int(full_resolution.t / 2) 

181 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

182 expected_pixels = np.array( 

183 [[[single_resolution_float_5d.get_pixel_value(x, y, c, z, t) 

184 for x in range(full_resolution.x)] 

185 for y in range(full_resolution.y)] 

186 for c in range(full_resolution.c)], 

187 single_resolution_float_5d.get_dtype() 

188 ) 

189 

190 image = bioio_server.read_region( 

191 1, 

192 Region2D(width=bioio_server.metadata.width, height=bioio_server.metadata.height, z=z, t=t) 

193 ) 

194 

195 np.testing.assert_array_equal(image, expected_pixels) 

196 

197 bioio_server.close() 

198 

199 

200def test_read_float_5d_image_with_dask(): 

201 full_resolution = single_resolution_float_5d.get_shapes()[0] 

202 bioio_server = BioIOServer(single_resolution_float_5d.get_path()) 

203 expected_pixels = np.array( 

204 [[[[[single_resolution_float_5d.get_pixel_value(x, y, c, z, t) 

205 for x in range(full_resolution.x)] 

206 for y in range(full_resolution.y)] 

207 for z in range(full_resolution.z)] 

208 for c in range(full_resolution.c)] 

209 for t in range(full_resolution.t)], 

210 single_resolution_float_5d.get_dtype() 

211 ) 

212 

213 image = bioio_server.level_to_dask(0).compute() 

214 

215 np.testing.assert_array_equal(image, expected_pixels) 

216 

217 bioio_server.close() 

218 

219 

220 

221def test_float_5d_zarr_image_name(): 

222 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

223 

224 name = bioio_server.metadata.name 

225 

226 assert name == single_resolution_float_5d_zarr.get_name() 

227 

228 bioio_server.close() 

229 

230 

231def test_float_5d_zarr_image_shapes(): 

232 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

233 

234 shapes = bioio_server.metadata.shapes 

235 

236 assert shapes == single_resolution_float_5d_zarr.get_shapes() 

237 

238 bioio_server.close() 

239 

240 

241def test_float_5d_zarr_image_pixel_calibration(): 

242 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

243 

244 pixel_calibration = bioio_server.metadata.pixel_calibration 

245 

246 assert pixel_calibration == PixelCalibration( 

247 PixelLength(single_resolution_float_5d_zarr.get_pixel_size_x_y_in_micrometers()), # The BioIO library does not currently handle unit attachment 

248 PixelLength(single_resolution_float_5d_zarr.get_pixel_size_x_y_in_micrometers()), 

249 PixelLength(single_resolution_float_5d_zarr.get_pixel_size_x_y_in_micrometers()) 

250 ) 

251 

252 bioio_server.close() 

253 

254 

255def test_float_5d_zarr_image_is_not_rgb(): 

256 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

257 

258 is_rgb = bioio_server.metadata.is_rgb 

259 

260 assert not(is_rgb) 

261 

262 bioio_server.close() 

263 

264 

265def test_float_5d_zarr_image_dtype(): 

266 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

267 

268 dtype = bioio_server.metadata.dtype 

269 

270 assert dtype == single_resolution_float_5d_zarr.get_dtype() 

271 

272 bioio_server.close() 

273 

274 

275def test_float_5d_zarr_image_downsamples(): 

276 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

277 

278 downsamples = bioio_server.metadata.downsamples 

279 

280 assert downsamples == single_resolution_float_5d_zarr.get_downsamples() 

281 

282 bioio_server.close() 

283 

284 

285def test_read_float_5d_zarr_image(): 

286 full_resolution = single_resolution_float_5d_zarr.get_shapes()[0] 

287 z = int(full_resolution.z / 2) 

288 t = int(full_resolution.t / 2) 

289 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

290 expected_pixels = np.array( 

291 [[[single_resolution_float_5d_zarr.get_pixel_value(x, y, c, z, t) 

292 for x in range(full_resolution.x)] 

293 for y in range(full_resolution.y)] 

294 for c in range(full_resolution.c)], 

295 single_resolution_float_5d_zarr.get_dtype() 

296 ) 

297 

298 image = bioio_server.read_region( 

299 1, 

300 Region2D(width=bioio_server.metadata.width, height=bioio_server.metadata.height, z=z, t=t) 

301 ) 

302 

303 np.testing.assert_array_equal(image, expected_pixels) 

304 

305 bioio_server.close() 

306 

307 

308def test_read_float_5d_zarr_image_with_dask(): 

309 full_resolution = single_resolution_float_5d_zarr.get_shapes()[0] 

310 bioio_server = BioIOServer(single_resolution_float_5d_zarr.get_path()) 

311 expected_pixels = np.array( 

312 [[[[[single_resolution_float_5d_zarr.get_pixel_value(x, y, c, z, t) 

313 for x in range(full_resolution.x)] 

314 for y in range(full_resolution.y)] 

315 for z in range(full_resolution.z)] 

316 for c in range(full_resolution.c)] 

317 for t in range(full_resolution.t)], 

318 single_resolution_float_5d_zarr.get_dtype() 

319 ) 

320 

321 image = bioio_server.level_to_dask(0).compute() 

322 

323 np.testing.assert_array_equal(image, expected_pixels) 

324 

325 bioio_server.close() 

326 

327 

328def test_rgb_image_name(): 

329 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

330 

331 name = bioio_server.metadata.name 

332 

333 assert name == single_resolution_rgb_image.get_name() 

334 

335 bioio_server.close() 

336 

337 

338def test_rgb_image_shapes(): 

339 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

340 

341 shapes = bioio_server.metadata.shapes 

342 

343 assert shapes == single_resolution_rgb_image.get_shapes() 

344 

345 bioio_server.close() 

346 

347 

348def test_rgb_image_pixel_calibration(): 

349 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

350 

351 pixel_calibration = bioio_server.metadata.pixel_calibration 

352 

353 assert pixel_calibration == PixelCalibration( 

354 PixelLength(single_resolution_rgb_image.get_pixel_size_x_y_in_micrometers()), # The BioIO library does not currently handle unit attachment 

355 PixelLength(single_resolution_rgb_image.get_pixel_size_x_y_in_micrometers()) 

356 ) 

357 

358 bioio_server.close() 

359 

360 

361def test_rgb_image_is_rgb(): 

362 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

363 

364 is_rgb = bioio_server.metadata.is_rgb 

365 

366 assert is_rgb 

367 

368 bioio_server.close() 

369 

370 

371def test_rgb_image_dtype(): 

372 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

373 

374 dtype = bioio_server.metadata.dtype 

375 

376 assert dtype == single_resolution_rgb_image.get_dtype() 

377 

378 bioio_server.close() 

379 

380 

381def test_rgb_image_downsamples(): 

382 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

383 

384 downsamples = bioio_server.metadata.downsamples 

385 

386 assert downsamples == single_resolution_rgb_image.get_downsamples() 

387 

388 bioio_server.close() 

389 

390 

391def test_read_rgb_image(): 

392 full_resolution = single_resolution_rgb_image.get_shapes()[0] 

393 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

394 expected_pixels = np.array( 

395 [[[single_resolution_rgb_image.get_pixel_value(x, y, c) 

396 for x in range(full_resolution.x)] 

397 for y in range(full_resolution.y)] 

398 for c in range(full_resolution.c)], 

399 single_resolution_rgb_image.get_dtype() 

400 ) 

401 

402 image = bioio_server.read_region( 

403 1, 

404 Region2D(width=bioio_server.metadata.width, height=bioio_server.metadata.height) 

405 ) 

406 

407 np.testing.assert_array_equal(image, expected_pixels) 

408 

409 bioio_server.close() 

410 

411 

412def test_read_rgb_image_with_dask(): 

413 full_resolution = single_resolution_rgb_image.get_shapes()[0] 

414 bioio_server = BioIOServer(single_resolution_rgb_image.get_path()) 

415 expected_pixels = np.array( 

416 [[[single_resolution_rgb_image.get_pixel_value(x, y, c) 

417 for x in range(full_resolution.x)] 

418 for y in range(full_resolution.y)] 

419 for c in range(full_resolution.c)], 

420 single_resolution_rgb_image.get_dtype() 

421 ) 

422 

423 image = bioio_server.level_to_dask(0).compute() 

424 

425 np.testing.assert_array_equal(image, expected_pixels) 

426 

427 bioio_server.close()