Browse code

Fixed save and display single

nilseling authored on 20/03/2020 13:33:43
Showing 6 changed files

... ...
@@ -69,6 +69,7 @@ importFrom(methods,validObject)
69 69
 importFrom(raster,as.raster)
70 70
 importFrom(stats,quantile)
71 71
 importFrom(tools,file_ext)
72
+importFrom(tools,file_path_sans_ext)
72 73
 importFrom(utils,packageVersion)
73 74
 importFrom(viridis,inferno)
74 75
 importFrom(viridis,viridis)
... ...
@@ -73,11 +73,11 @@ NULL
73 73
   # legend
74 74
   if(!("legend" %in% names(dotArgs))){
75 75
     legendparam <- list(colour_by.title.font = 1,
76
-                        colour_by.title.cex = 0.7,
76
+                        colour_by.title.cex = NULL,
77 77
                         colour_by.labels.cex = NULL,
78 78
                         colour_by.legend.cex = NULL,
79 79
                         outline_by.title.font = 1,
80
-                        outline_by.title.cex = 0.7,
80
+                        outline_by.title.cex = NULL,
81 81
                         outline_by.labels.cex = NULL,
82 82
                         outline_by.legend.cex = NULL,
83 83
                         margin = 2)
... ...
@@ -129,7 +129,7 @@ NULL
129 129
   if(!("display" %in% names(dotArgs))){
130 130
     dotArgs$display <- "all"
131 131
   } else {
132
-    if(is.character(dotArgs$display) ||
132
+    if(!is.character(dotArgs$display) ||
133 133
        !(dotArgs$display %in% c("all", "single"))){
134 134
       stop("Invalid 'display' entry.")
135 135
     }
... ...
@@ -357,7 +357,7 @@ NULL
357 357
                     "'colour_by.title.cex' should be a single number"))
358 358
       }
359 359
     } else {
360
-      legendparam$colour_by.title.cex <- 0.7
360
+      legendparam$colour_by.title.cex <- NULL
361 361
     }
362 362
 
363 363
     if("colour_by.labels.cex" %in% names(legendparam)){
... ...
@@ -401,7 +401,7 @@ NULL
401 401
                     "'outline_by.title.cex' should be a single number"))
402 402
       }
403 403
     } else {
404
-      legendparam$outline_by.title.cex <- 0.7
404
+      legendparam$outline_by.title.cex <- NULL
405 405
     }
406 406
 
407 407
     if("outline_by.labels.cex" %in% names(legendparam)){
... ...
@@ -298,6 +298,7 @@
298 298
 # Custom function to display images
299 299
 #' @importFrom S4Vectors SimpleList
300 300
 #' @importFrom EBImage Image
301
+#' @importFrom tools file_ext file_path_sans_ext
301 302
 #' @importFrom graphics par plot rasterImage strheight text
302 303
 #' @importFrom grDevices png jpeg tiff dev.off
303 304
 .displayImages <- function(object, image, exprs_values, outline_by,
... ...
@@ -346,7 +347,7 @@
346 347
     cur_out <- list()
347 348
   }
348 349
 
349
-  if(!is.null(plottingParam$save_image)){
350
+  if(!is.null(plottingParam$save_image) && plottingParam$display == "all"){
350 351
     image_location <- plottingParam$save_image$filename
351 352
     image_scale <- plottingParam$save_image$scale
352 353
     cur_ext <- file_ext(image_location)
... ...
@@ -371,7 +372,7 @@
371 372
       yaxs="i", xaxt="n", yaxt="n", col = "white")
372 373
   on.exit(par(cur_par))
373 374
 
374
-  if(plottingParam$display = "all"){
375
+  if(plottingParam$display == "all"){
375 376
     plot(x_len, y_len, type="n", xlab="", ylab="",
376 377
          asp = 1, ylim = rev(y_len))
377 378
   }
... ...
@@ -389,13 +390,44 @@
389 390
       ybottom <- i*m_height - (m_height - dim_y)/2 + (i-1) * margin
390 391
       xright <- j*m_width - (m_width - dim_x)/2 + (j-1) * margin
391 392
       ytop <- (i-1)*m_height + (m_height - dim_y)/2 + (i-1) * margin
392
-      if(plottingParam$display = "all"){
393
+
394
+      # If Images should be saved
395
+      if(!is.null(plottingParam$save_image) && plottingParam$display == "single"){
396
+        image_location <- plottingParam$save_image$filename
397
+        image_scale <- plottingParam$save_image$scale
398
+        cur_ext <- file_ext(image_location)
399
+
400
+        # File name
401
+        cur_name <- paste0(file_path_sans_ext(image_location),
402
+                           "_", ind, ".", cur_ext)
403
+        if(cur_ext == "png"){
404
+          png(cur_name, width = image_scale * dim_x,
405
+              height = image_scale * dim_y, units = "px",
406
+              pointsize = 12 * image_scale)
407
+        } else if(cur_ext == "jpeg"){
408
+          jpeg(image_location, width = image_scale * dim_x,
409
+               height = image_scale * dim_y, units = "px",
410
+               pointsize = 12 * image_scale)
411
+        } else if(cur_ext == "tiff"){
412
+          tiff(image_location, width = image_scale * dim_x,
413
+               height = image_scale * dim_y, units = "px",
414
+               pointsize = 12 * image_scale)
415
+        }
416
+
417
+        cur_par <- par(bty="n", mai=c(0,0,0,0), xaxs="i",
418
+                       yaxs="i", xaxt="n", yaxt="n", col = "white")
419
+        on.exit(par(cur_par))
420
+      }
421
+
422
+      if(plottingParam$display == "all"){
393 423
         rasterImage(Image(out_img[[ind]]),
394 424
                     xleft,
395 425
                     ybottom,
396 426
                     xright,
397 427
                     ytop)
398 428
       } else {
429
+        plot(c(0, dim_x), c(0, dim_y), type="n", xlab="", ylab="",
430
+             asp = 1, ylim = rev(c(0, dim_y)))
399 431
         rasterImage(Image(out_img[[ind]]),
400 432
                     xleft = 0,
401 433
                     ybottom = dim_y,
... ...
@@ -411,29 +443,53 @@
411 443
 
412 444
       if(ind != legend_ind && !is.null(plottingParam$scale_bar)){
413 445
         if(plottingParam$scale_bar$frame == "all"){
414
-          # Plot scale bar
415
-          .plotScaleBar(plottingParam$scale_bar,
416
-                        xleft = 0, xright = dim_x,
417
-                        ytop = 0, ybottom = dim_y,
418
-                        image_scale)
446
+          if(plottingParam$display == "all"){
447
+            .plotScaleBar(plottingParam$scale_bar,
448
+                          xl = xleft, xr = xright,
449
+                          yt = ytop, yb = ybottom,
450
+                          image_scale)
451
+          } else {
452
+            .plotScaleBar(plottingParam$scale_bar,
453
+                          xl = 0, xr = dim_x,
454
+                          yt = 0, yb = dim_y,
455
+                          image_scale)
456
+          }
419 457
         } else {
420 458
           cur_ind <- legend_ind + as.integer(plottingParam$scale_bar$frame)
421 459
           if(ind == cur_ind && !is.null(plottingParam$scale_bar)){
422
-            # Plot scale bar
423
-            .plotScaleBar(plottingParam$scale_bar,
424
-                          xleft = 0, xright = dim_x,
425
-                          ytop = 0, ybottom = dim_y,
426
-                          image_scale)
460
+            if(plottingParam$display == "all"){
461
+              .plotScaleBar(plottingParam$scale_bar,
462
+                            xl = xleft, xr = xright,
463
+                            yt = ytop, yb = ybottom,
464
+                            image_scale)
465
+              } else {
466
+                .plotScaleBar(plottingParam$scale_bar,
467
+                            xl = 0, xr = dim_x,
468
+                            yt = 0, yb = dim_y,
469
+                            image_scale)
470
+              }
427 471
             }
428 472
           }
429 473
         }
430 474
 
431 475
       # Plot title on images
432 476
       if(ind != legend_ind && !is.null(plottingParam$image_title)){
433
-        .plotImageTitle(out_img, mask, image, img_id,
434
-                        ind, legend_ind, plottingParam$image_title, dim_x,
435
-                        xleft = 0, xright = dim_x,
436
-                        ytop = 0, ybottom = dim_y)
477
+        if(plottingParam$display == "all"){
478
+          .plotImageTitle(out_img, mask, image, img_id,
479
+                          ind, legend_ind, plottingParam$image_title, dim_x,
480
+                          xl = xleft, xr = xright,
481
+                          yt = ytop, yb = ybottom)
482
+        } else {
483
+          .plotImageTitle(out_img, mask, image, img_id,
484
+                          ind, legend_ind, plottingParam$image_title, dim_x,
485
+                          xl = 0, xr = dim_x,
486
+                          yt = 0, yb = dim_y)
487
+        }
488
+      }
489
+
490
+      # Close device
491
+      if(!is.null(plottingParam$save_image) && plottingParam$display == "single"){
492
+        dev.off()
437 493
       }
438 494
 
439 495
       if(plottingParam$return_plot && plottingParam$display == "single"){
... ...
@@ -493,11 +549,14 @@
493 549
   if(!is.null(colour_by) &&
494 550
      (all(colour_by %in% rownames(object)) || !is.null(image))){
495 551
 
552
+    # Maximum title width
553
+    title_width <- max(strwidth(colour_by, font = colour_by.title.font))
554
+
496 555
     # Maximum label width
497 556
     if(is.null(image)){
498 557
       all_max <- max(assay(object, exprs_values)[colour_by,])
499 558
     } else {
500
-      all_max <- max(getChannels(image, colour_by)[[1]])
559
+      all_max <- unlist(lapply(getChannels(image, colour_by), max))
501 560
     }
502 561
     label_width <- max(strwidth(format(round(all_max, 1), nsmall = 1)))
503 562
 
... ...
@@ -524,6 +583,13 @@
524 583
                         format(round(cur_max/2, 1), nsmall = 1),
525 584
                         format(round(cur_max, 1), nsmall = 1))
526 585
 
586
+        # Define title cex
587
+        if(is.null(colour_by.title.cex)){
588
+          title_cex <- (cur_space_x/1.5)/title_width
589
+        } else {
590
+          title_cex <- colour_by.title.cex
591
+        }
592
+
527 593
         # Define label cex
528 594
         if(is.null(colour_by.labels.cex)){
529 595
           label_cex <- (cur_space_x/2)/label_width
... ...
@@ -536,7 +602,7 @@
536 602
         text(x = cur_x, y = cur_y - cur_space_y/2,
537 603
              label = colour_by[i], col = "black",
538 604
              font = colour_by.title.font,
539
-             cex = colour_by.title.cex, adj = c(0.5, 1))
605
+             cex = title_cex, adj = c(0.5, 1))
540 606
         text(x=cur_x- cur_space_x/4 + 2,
541 607
              y = seq(cur_y - cur_space_y/2 + cur_space_y/4,
542 608
                      cur_y + cur_space_y/2 - cur_space_y/8, length.out = 3),
... ...
@@ -566,13 +632,22 @@
566 632
                       format(round(cur_max/2, 1), nsmall = 1),
567 633
                       format(round(cur_max, 1), nsmall = 1))
568 634
       label_width <- max(strwidth(rev(cur_labels)))
635
+      title_width <- strwidth(colour_by, font = colour_by.title.font)
569 636
 
570 637
       cur_legend <- as.raster(matrix(rev(colorRampPalette(cur_col$colour_by[[1]])(101)),
571 638
                                      ncol=1))
639
+
640
+      # Define title cex
641
+      if(is.null(colour_by.title.cex)){
642
+        title_cex <- (cur_space_x/1.5)/title_width
643
+      } else {
644
+        title_cex <- colour_by.title.cex
645
+      }
646
+
572 647
       text(x = cur_x + cur_space_x/2, y = cur_y,
573 648
            label = colour_by, col = "black",
574 649
            font = colour_by.title.font,
575
-           cex = colour_by.title.cex, adj = c(0.5, 1))
650
+           cex = title_cex, adj = c(0.5, 1))
576 651
 
577 652
       # Define label cex
578 653
       if(is.null(colour_by.labels.cex)){
... ...
@@ -634,13 +709,22 @@
634 709
                       format(round(cur_max/2, 1), nsmall = 1),
635 710
                       format(round(cur_max, 1), nsmall = 1))
636 711
       label_width <- max(strwidth(rev(cur_labels)))
712
+      title_width <- strwidth(outline_by, font = colour_by.title.font)
637 713
 
638 714
       cur_legend <- as.raster(matrix(rev(colorRampPalette(cur_col$outline_by[[1]])(101)),
639 715
                                      ncol=1))
716
+
717
+      # Define title cex
718
+      if(is.null(colour_by.title.cex)){
719
+        title_cex <- (cur_space_x/1.5)/title_width
720
+      } else {
721
+        title_cex <- outline_by.title.cex
722
+      }
723
+
640 724
       text(x = cur_x + cur_space_x/2, y = cur_y,
641 725
            label = outline_by, col = "black",
642 726
            font = outline_by.title.font,
643
-           cex = outline_by.title.cex, adj = c(0.5, 1))
727
+           cex = title_cex, adj = c(0.5, 1))
644 728
 
645 729
       # Define label cex
646 730
       if(is.null(outline_by.labels.cex)){
... ...
@@ -732,7 +816,7 @@
732 816
 #' @importFrom graphics strwidth strheight text rasterImage legend
733 817
 #' @importFrom raster as.raster
734 818
 .plotImageTitle <- function(out_img, mask, image, img_id, ind, legend_ind, image_title, dim_x,
735
-                            xleft, xright, ytop, ybottom){
819
+                            xl, xr, yt, yb){
736 820
 
737 821
   if(!is.null(image_title$text)){
738 822
     cur_title <- image_title$text[ind - legend_ind]
... ...
@@ -753,31 +837,32 @@
753 837
   cur_margin.y <- image_title$margin[2]
754 838
   cur_font <- image_title$font
755 839
 
756
-  text_params <- list(labels = cur_title, col = cur_col, cex = cur_cex, font = cur_font)
840
+  text_params <- list(labels = cur_title, col = cur_col,
841
+                      cex = cur_cex, font = cur_font)
757 842
 
758 843
   if(cur_position == "top"){
759
-    do.call(text, append(list(x = xleft + dim_x/2,
760
-                              y = ytop + cur_margin.y,
844
+    do.call(text, append(list(x = xl + dim_x/2,
845
+                              y = yt + cur_margin.y,
761 846
                               adj = 0.5), text_params))
762 847
   } else if(cur_position == "bottom"){
763
-    do.call(text, append(list(x = xleft + dim_x/2,
764
-                              y = ybottom - cur_margin.y,
848
+    do.call(text, append(list(x = xl + dim_x/2,
849
+                              y = yb - cur_margin.y,
765 850
                               adj = 0.5), text_params))
766 851
   } else if(cur_position == "topleft"){
767
-    do.call(text, append(list(x = xleft + cur_margin.x,
768
-                              y = ytop + cur_margin.y,
852
+    do.call(text, append(list(x = xl + cur_margin.x,
853
+                              y = yt + cur_margin.y,
769 854
                               adj = 0), text_params))
770 855
   } else if(cur_position == "topright"){
771
-    do.call(text, append(list(x = xright - cur_margin.x,
772
-                              y = ytop + cur_margin.y,
856
+    do.call(text, append(list(x = xr - cur_margin.x,
857
+                              y = yt + cur_margin.y,
773 858
                               adj = 1), text_params))
774 859
   } else if(cur_position == "bottomleft"){
775
-    do.call(text, append(list(x = xleft + cur_margin.x,
776
-                              y = ybottom - cur_margin.y,
860
+    do.call(text, append(list(x = xl + cur_margin.x,
861
+                              y = yb - cur_margin.y,
777 862
                               adj = 0), text_params))
778 863
   } else if(cur_position == "bottomright"){
779
-    do.call(text, append(list(x = xright - cur_margin.x,
780
-                              y = ybottom - cur_margin.y,
864
+    do.call(text, append(list(x = xr - cur_margin.x,
865
+                              y = yb - cur_margin.y,
781 866
                               adj = 1), text_params))
782 867
   }
783 868
 }
... ...
@@ -8,6 +8,9 @@
8 8
 \alias{normalize}
9 9
 \alias{normalize,ImageList-method}
10 10
 \title{Manipulating ImageList objects}
11
+\value{
12
+An ImageList object containing the manipulated Images
13
+}
11 14
 \description{
12 15
 Methods to change pixel values in ImageList objects. In the
13 16
   following sections, \code{object} is an \linkS4class{ImageList} object
... ...
@@ -63,7 +66,7 @@ clipping range of the input intensity values (see
63 66
 \examples{
64 67
 data(pancreasImages)
65 68
 
66
-# Scale images to create segmentaion masks
69
+# Scale images to create segmentation masks
67 70
 cur_files <- list.files(system.file("extdata", package = "SingleCellMapper"),
68 71
                         pattern = "mask.tiff", full.names = TRUE)
69 72
 x <- loadImages(cur_files)
... ...
@@ -13,6 +13,16 @@
13 13
 \item{image_title}{TODO}
14 14
 
15 15
 \item{save_image}{TODO}
16
+
17
+\item{return_plot}{TODO}
18
+
19
+\item{return_images}{TODO}
20
+
21
+\item{legend}{TODO}
22
+
23
+\item{margin}{TODO}
24
+
25
+\item{display}{TODO}
16 26
 }
17 27
 \value{
18 28
 TODO
... ...
@@ -23,10 +33,7 @@ TODO
23 33
 \section{Setting further parameters}{
24 34
 
25 35
 TODO
26
-# legend list(title, size) also allow NULL
27
-# return_plot TODO
28
-# return_images TODO
29
-# margin between images
36
+# scale TRUE FALSE
30 37
 }
31 38
 
32 39
 \examples{
... ...
@@ -527,11 +527,56 @@ test_that("plotting-param: images can be plotted individually.", {
527 527
   data("pancreasMasks")
528 528
   data("pancreasSCE")
529 529
 
530
+  #' @param missing_colour TODO
531
+  #' @param background_colour TODO
532
+  #' @param scale_bar TODO
533
+  #' @param image_title TODO
534
+  #' @param save_image TODO
535
+  #' @param return_plot TODO
536
+  #' @param return_images TODO
537
+  #' @param legend TODO
538
+  #' @param margin TODO
539
+  #' @param display TODO
540
+
530 541
   # Works
542
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA")))
543
+  expect_silent(plotPixels(pancreasImages, display = "all"))
544
+  expect_silent(plotPixels(pancreasImages, display = "single"))
545
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA", "CD44"),
546
+                           display = "single"))
547
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA", "CD44"),
548
+                           scale_bar = list(frame = 3),
549
+                           display = "single"))
550
+  # scale_bar
551
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA", "CD44"),
552
+                           scale_bar = list(position = "topright",
553
+                                            frame = 3,
554
+                                            margin = c(20,20)),
555
+                           display = "single"))
556
+
557
+  # image_title
558
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA", "CD44"),
559
+                           image_title = list(text = c(1,2,3),
560
+                                              cex = 3),
561
+                           display = "single"))
562
+
563
+  # image_title
564
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA", "CD44"),
565
+                           image_title = list(text = c(1,2,3),
566
+                                              cex = 3),
567
+                           display = "single"))
568
+
569
+  # save_image
570
+  expect_silent(plotPixels(pancreasImages, colour_by = c("H3", "SMA", "CD44"),
571
+                           save_image = list(filename = "~/Desktop/test.png",
572
+                                             scale = 10),
573
+                           display = "single"))
574
+
575
+  # Fix height of image title and legend
576
+
577
+
578
+  # Check with all other entries
531 579
 
532
-  # Check return plot
533
-  # check scale bar and names
534
-  # check save_image
535 580
 
536 581
   expect_silent(plotPixels(pancreasImages))
537 582
   expect_silent(plotPixels(pancreasImages, margin = 2))