convert hdf5 file to grayscale - julia

How do I convert a .h5 file to grayscale in Julia?
I tried this
img = stack[:,:,100] #just some hdf5 file
img = convert(Image{Images.Gray}, img)
I get this error:
LoadError: PyError (:PyObject_Call) <type 'exceptions.TypeError'>
TypeError(u'Image data can not convert to float',)

My guess is you're also using some other package that defines Image, and this conflicts with the definition in Images.jl. Let's imagine that other package is called PythonImage. Try it like this:
using Colors # that way you don't have to say `Images.Gray`
import PythonImage, Images # `import` rather than `using` prevents conflicts
imgg = convert(Images.Image{Gray}, img)
# ...if you need the other one, use `PythonImage.Image`
Or you can using Images and import PythonImage (or vice versa). The only thing you can't do is using for both of them and expect everything to work.

Related

Read in coordinates of SVG path / polygon

I'm trying to import the coordinates of a path in an SVG file created with illustrator into R.
I thought I might read the SVG into R with grImport2, which should in theory be importing SVG files, but I think they might only handle SVG files generated by the Cairo device.
Let's say I want to import the following .svg file:
Here is my attempt at loading an SVG file. If I read the content correctly, it should just contain 1 (complicated) path. The warning is the same one I get when loading my SVG file created by Adobe Illustrator. Be warned that the code below will get stuck for some time!
file <- "https://upload.wikimedia.org/wikipedia/commons/d/db/Brain_Drawing.svg"
download.file(file, tmp <- tempfile(fileext = ".svg"))
# Don't run the following line, it will get your R session stuck!
x <- grImport2::readPicture(tmp)
#> Warning message:
#> In checkValidSVG(doc, warn = warn) :
#> This picture was not generated by Cairo graphics; errors may result
unlink(tmp)
My ideal output would be a data.frame with at least x and y coordinates of (anchor)points and perhaps some metadata that can tell different paths apart. I don't need curves and arcs interpolated or anything like that.
Are there any other packages I'm not aware of that might import this? Is there a way to convert the SVG to one that I can read into R?
Nice question Teunbrand - thanks for posting. The issue does seem to be getting grImport2 to read non-Cairo svg. So actually, we just need another translation step: use package rsvg to read in a non-Cairo svg and and write the same thing with Cairo. Oddly enough, it has a function to do this, called rsvg_svg. So we can read the remote file as a Picture object in a single line without even creating a local tmp file:
file <- "https://upload.wikimedia.org/wikipedia/commons/d/db/Brain_Drawing.svg"
svg <- grImport2::readPicture(rawToChar(rsvg::rsvg_svg(file)))
Unfortunately, a Picture is a deeply listed S4 object which is harder to navigate than a grob tree. I'm sure a person of your caliber could coerce it into a data frame, but it's not trivial, and I won't attempt it here. At least the components themselves look easy enough to harvest:
svg#content[[1]]#content[[1]]#d#segments[[3]]
#> An object of class "PathCurveTo"
#> Slot "x":
#> [1] 791.8359 789.5286 787.1382 784.6293 782.0041 779.2962 776.5600 773.8555 771.2318
#> [10] 768.712 766.2875 764.8359
#>
#> Slot "y":
#> [1] 8.191406 8.209760 8.239691 8.282891 8.340521 8.412835 8.499026 8.597442 8.706126
#> [10] 8.82357 8.949580 9.031250
Anyway, there are a few nice utility functions that allow you to do cool stuff like this:
brainGrob <- grImport2::pictureGrob(svg)
ggplot2::ggplot() + ggplot2::geom_point(ggplot2::aes(1, 1))
grid::grid.draw(brainGrob)
I haven't seen an arbitrary SVG drawn in grid before, so I was pleased to find this, prompted by your question. Thanks again.

Can I know what is in my .Rdata file without opening it [duplicate]

I sometimes use print( load( "myDataFile.RData" ) ) to list the contents of a data file when I load it. Is there a way to list the contents without loading the objects contained in the data file?
I do not think you could do that without loading the object.
A solution could be to save the R objects with a wrapper to save, which function would save the object AND the structure of the object to a special Rdata file. Later you could load the special binary file with a wrapper to load, where you could specify to only list the structure of the data.
I have done something like this in a very basic package, named saves, can be found on CRAN.
Update: I made up a very simple metadata solution
save.ls <- function(x, file) {
save(list=x, file=file)
l <- ls()
save(l, file=paste(file, 'ls', sep=''))
}
load.ls <- function(file) {
attach(paste(file, 'ls', sep=''));
return(l)
detach(pos=2)
}
Save with save.ls instead of save and load with load.ls to test. Meta information is saved in separate file (ending in "ls"), but the mechanism could be improved easily e.g. making a tar archive (like I do in the package linked above) of the Rdata object and the file containing the metadata.
attach(file);ls(pos=2);detach(pos=2)
That'll do it. Probably. #untested
In R v3.0.1 the load() function got a new argument. Loading an RData file with
load("mydata.RData", verbose=TRUE)
will show you the objects that are loaded. Of course, it still means you have to load the object.
Maybe,
load( "myDataFile.RData",ex<-new.env() )
content=ls.str(ex)

How to import multiple matlab files into R (Using package R.Matlab)

Thank you in advance for your're help. I am using R to analyse some data that is initially created in Matlab. I am using the package "R.Matlab" and it is fantastic for 1 file, but I am struggling to import multiple files.
The working script for a single file is as follows...
install.packages("R.matlab")
library(R.matlab)
x<-("folder_of_files")
path <- system.file("/home/ashley/Desktop/Save/2D Stream", package="R.matlab")
pathname <- file.path(x, "Test0000.mat")
data1 <- readMat(pathname)
And this works fantastic. The format of my files is 'Name_0000.mat' where between files the name is a constant and the 4 digits increase, but not necesserally by 1.
My attempt to load multiple files at once was along these lines...
for (i in 1:length(temp))
data1<-list()
{data1[[i]] <- readMat((get(paste(temp[i]))))}
And also in multiple other ways that included and excluded path and pathname from the loop, all of which give me the same error:
Error in get(paste(temp[i])) :
object 'Test0825.mat' not found
Where 0825 is my final file name. If you change the length of the loop it is always just the name of the final one.
I think the issue is that when it pastes the name it looks for that object, which as of yet does not exist so I need to have the pasted text in speach marks, yet I dont know how to do that.
Sorry this was such a long post....Many thanks

Convert BED file to WIG file

Do you know any way to convert easily (either by R, or with other program) a BED file to WIG?
Can you give me some guidelines?
Take a look at the rtracklayer package and specifically at the following man pages:
?import
?export
Here is a specific example of how I would convert .bed to .wig. As #Paolo implied, it's a straight-forward procedure:
library(rtracklayer) #bioconductor
bed_loaded <- import(con="~/Downloads/my_bed.bed.gz", format="bed") #no need to unzip .gz
# bed_loaded <- import.bed(con="~/Downloads/my_bed.bed") #if you unzip
export.wig(object=bed_loaded, con="~/Downloads/bed2wig.wig")
Note that you import and export both have methods (wig, bed, bigwig or bw, etc.). You may directly use them without specifying the method but specifying format argument.
This GitHub tutorial will be helpful.

save file in XYZ format as vector (GML or shp)

I am using QGIS software. I would like to show value of each raster cell as label.
My idea (I don't know any plugin or any functionality from QGIS which allow to it easier) is to export raster using gdal2xyz.py into coordinates-value format and then save it as vector (GML or shapefile). For this second task, I try to use
*gdal_polygonize.py:*
gdal_polygonize.py rainfXYZ.txt rainf.shp Creating output rainf.shp of
format GML.
0...10...20...30...40...50...60...70...80...90...100 - done.
unfortunately I am unable to load created file (even if I change the extension to .gml)
ogr2ogr tool don't even recognize this format.
yes - sorry I forgot to add such information.
In general after preparing CSV file (using gdal2xyz.py with -csv option),
I need to add one line at begining of it:
"Longitude,Latitude,Value" (without the quotes)
Then I need to create a VRT file which contain
*> <OGRVRTDataSource>
> <OGRVRTLayer name="Shapefile_name">
> <SrcDataSource>Shapefile_name.csv</SrcDataSource>
> <GeometryType>wkbPoint</GeometryType>
>
> <GeometryField encoding="PointFromColumns" x="Longitude"
> y="Latitude"/>
> </OGRVRTLayer> </OGRVRTDataSource>*
Run the command "ogr2ogr -select Value Shapefile_name.shp Shapefile_name.vrt". I got the file evap_OBC.shp and two other associated files.
For the sake of archive completeness, this question has also been asked on GDAL mailing list as thread save raster as point-vector file. It seems Chaitanya provided solution for it.

Resources