Does anybody now if there is any module that could let me import a graph (network) in Julia?
Working with Python, I used the graph-tool package, which served me very well! I have my graphs in .gt file format. Can I use any module in Julia, so that I can import them there?
I have searched for LightGraphs and Junet, which is fairly new but cannot seem to see any "import" section in the documentation.
The most straightforward solution is to convert your gt files to graphml format, which is compatible with LightGraphs, and is the recommended alternative format by graph-tool.
Suppose you have a ".gt" file that was generated in the past by the following python code:
from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1,v2)
g.save('g.gt')
Start a new python session, and convert from "gt" to "graphml" format:
import graph_tool as gt
g = gt.Graph()
g.load('g.gt')
g.save('g.xml.gz')
Then, in julia, use LightGraphs with the GraphIO package to load from the GraphML file:
using LightGraphs, GraphIO
D = loadgraphs("g.xml.gz", GraphMLFormat())
#> Dict{String,LightGraphs.AbstractGraph} with 1 entry:
# "G" => {2, 1} directed simple Int64 graph
If you'd like to use PyCall to perform the conversion directly from within julia (i.e. in a script), here's how:
using PyCall
#pyimport graph_tool as gt
G = gt.Graph()
G[:load]("g.gt")
G[:save]("g.xml.gz")
(Note that this implies python and the graph-tool library are already installed on your machine and accessible from julia).
In theory, if you prefer graph-tools and you're used to its syntax and wish to keep working directly with the .gt file format, you can use it via PyCall from within julia throughout as above. Whether this is preferable to migrating over to LightGraphs which is designed for julia though is another thing. It's your call :)
PS. Greetings from Leamington, fellow Leamingtonian!
Graph importing for LightGraphs is now in GraphIO.jl. Supported import formats currently include
GML
Graph6
GEXF
GraphML
Pajek NET
DOT
with more formats coming soon.
Related
I used to retrieve pandas dataframe from ROOT file using tree.pandas.df() function in Uproot4(2 years ago). However, I got the below errors when I ran my code recently. Could anyone tell me what the problem is?
f = uproot.open(inputFile)
treeName = "myTreeName"
tree = f[treeName]
myDf = tree.pandas.df('branchName',entrystop=nEvent, flatten = False)
AttributeError: 'Model_TTree_v19' object has no attribute 'pandas'
In Uproot version 3, a special function named TTree.pandas.df created Pandas DataFrames.
In Uproot version 4 (and above), all of the functions that produce arrays have a library argument that specifies which library to use to represent the arrays. library="pd" makes Pandas DataFrames.
This change is described in the Uproot 3 → 4 cheat-sheet, the new argument is described in several places in the Getting Started Guide, as well as in all the reference documentation for array-fetching functions, such as TTree.arrays.
Im looking for an R Package that can read and extract the data of a barcode of an scanned image.
The barcode is in "Interleaved 2 of 5" format.
Is there any solution in R for this task or do I have to move to Python for this?
I would rather stick to R.
I solved this problem by running a python scrypt from the R scrypt.
I used the reticulate R package which provides an R interface to Python modules, classes, and functions.
The python scrypt example (decode_photo.py) is given below - pyzbar and PIL packages are required:
from pyzbar.pyzbar import decode
from PIL import Image
def decode_my_photo(file_name):
result = decode(Image.open(file_name))
decoded_items =[]
for x in result:
decoded_items.append(str(x.data).replace("'","").replace("b",""))
return(decoded_items)
The R scrypt is the following:
library(reticulate)
con <- "path_to_barcode_image.png"
use_python("C:\\Users\\MyName\\Anaconda3\\") #can be different
source_python("decode_photo.py")
decoded_value <- decode_my_photo(con)
I do not have any code to post, but just a question.
There are several tools I am aware of to read SDMX files in R (an SDMX is an XML file for exchanging statistical data) like for instance
https://github.com/opensdmx/rsdmx
https://github.com/amattioc/SDMX
but does anyone know a way to export some data to an SDMX format for dissemination?
Any suggestion is welcome!
This is not a ‘pure’ R solution, but the Python sdmx1 package is fully usable through reticulate, and allows to programmatically generate SDMX objects and then serialize them as SDMX-ML (XML). For example:
# Use reticulate to import the Python package
> library(reticulate)
> sdmx <- import("sdmx")
# Create an (empty) DataMessage object
> msg <- sdmx$message$DataMessage()
# Convert to XML
> xml <- sdmx$to_xml(msg, pretty_print = TRUE)
# Write to file using the built-in R method
# The Python 'bytes' object must be decoded to a string
> write(xml$decode(), file = "message.xml")
This gives output like:
<mes:GenericData xmlns:com="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/common" xmlns:data="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/structurespecific" xmlns:str="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/structure" xmlns:mes="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message" xmlns:gen="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic" xmlns:footer="http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message/footer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<mes:Header>
<mes:Test>false</mes:Test>
</mes:Header>
</mes:GenericData>
For more information on authoring more complex messages using sdmx1, there is a page “HOWTO Generate SDMX-ML from Python objects” in the documentation.
Question: What is the difference between using and import in Julia when I'm building my own module?
My guess based on reading the docs: using is used to bring another module into the name-space of the current module. import is used to bring specific types/functions/variables from other modules into the name-space of the current module.
So, how wrong am I?
The Julia Modules documentation states:
The import keyword [...] only
operates on a single name at a time. It does not add modules to be
searched the way using does. import also differs from using in that
functions must be imported using import to be extended with new
methods. [...] Functions whose names are only visible via using cannot be extended.
(Emphasis mine.)
For example, you can use import to add methods to Base.show to pretty-print your own types, but not with using.
There is also importall that imports all exported names of a module.
(This answer refers to Julia 0.6; the documentation was reworded for 1.0.)
The documentation (updated link for Julia 1.4) about this is excellent. Here's the excerpt which I find to be the most succinct summary:
(a demo module to make the examples below specific)
module MyModule
export x, y
x() = "x"
y() = "y"
p() = "p"
end
(this is a table in the documentation, but StackOverflow still won't add support for tables so... reformatted)
Command
using MyModule
in-scope: All exported names (x and y), MyModule.x, MyModule.y, and MyModule.p
extensible: MyModule.x, MyModule.y, and MyModule.p
using MyModule: x, p
in-scope: x and p
extensible: (nothing)
import MyModule
in-scope: MyModule.x, MyModule.y, and MyModule.p
extensible: MyModule.x, MyModule.y, and MyModule.p
import MyModule.x, MyModule.p
in-scope: x and p
extensible: x and p
import MyModule: x, p
in-scope: x and p
extensible: x and p
A summary of the main difference, in a way that I find easy to remember:
using NiceStuff allows usage access to exported names without the module qualifier, which import NiceStuff doesn't; and
import NiceStuff: nice allows extension access (adding methods) to the specified function without the module qualifier, which using NiceStuff: nice doesn't.
And a minor difference:
X as Y syntax is allowed for individual identifiers with both using and import (using Random: randstring as rstr, import Random: randstring as rstr) but for the module name itself, import Random as Rnd is allowed while using Random as Rnd is an error.
Some other points I found useful from the Modules docs page
using ModuleName is the only form for which export lists matter at all.
import NiceStuff is equivalent to using NiceStuff: NiceStuff.
I am trying to import the R package 'forecast; in netbeans to use its functions. I have managed to make the JRI connection and also to import the javaGD library and experimented with it with a certain success. The problem about the forecasting package is that I cannot find the corresponding JAR files so to include them as a library in my project. I am loading it normally : re.eval(library(forecast)), but when I implement one of the library's function, a null value is returned. Although I am quite sure that the code is correct I am posting it just in case.
tnx in advance
Rengine re = new Rengine(Rargs, false, null);
System.out.println("rengine created, waiting for R!");
if(!re.waitForR())
{
System.out.println("cannot load R");
return;
}
re.eval("library(forecast)");
re.eval("library(tseries)");
re.eval("myData <- read.csv('C:/.../I-35E-NB_1.csv', header=F, dec='.', sep=',')");
System.out.println(re.eval("myData"));
re.eval("timeSeries <- ts(myData,start=1,frequency=24)");
System.out.println("this is time series object : " + re.eval("timeSeries"));
re.eval("fitModel <- auto.arima(timeSeries)");
REXP fc = re.eval("forecast(fitModel, n=20)");
System.out.println("this is the forecast output values: " + fc);
You did not convert values from R into java, you should first create a numerical vector of auto.arima output in R, and then use the method .asDoubleArray() to read it into java.
I gave a complete example in [here] How I can load add-on R libraries into JRI and execute from Java? , that shows exactly How to use the auto.arima function in Java using JRI.