Reading a Pajek Dataset into Networkx - networkx

I am looking to convert a Pajek dataset into a networkx Graph(). The dataset comes from Costa Rican Family Ties. I am using the very handy networkx.read_pajek(pathname) function, but am running into some trouble. I do the following commands on my terminal window (iPython) after changing to the right directory, importing networkx as nx and matplotlib.pyplot as plt:
>> G = nx.read_pajek('SanJuanSur.paj')
>> nx.draw(G)
>> fig = plt.figure(figsize = (15, 10))
>> nx.draw(G)
>> plt.show()
Something unusual is happening--obviously, and was hoping a seasoned person might be able to help me. It looks like the .paj file has many different parts jammed into the one file I have downloaded. Not really sure how to break it up since there is no comments and seems like Pajek in general is meant to operate with a GUI.
>> G.node['f49']
{'id': '49', 'shape': '0.5000', 'x': 0.5533, 'y': 0.3766}
Was hoping to use the status of the nodes (attributes that range from 0-14). Thank you!

The networkx Pajek file reader can't handle the .paj format files with extra "partition" data in it.
Unfortunately it seems like it works. But obviously you are getting extra nodes and edges.
If you remove everything in the file SanJuanSur2.net starting at *Edges to the end you can read it with networkx.read_pajek().

Related

Create Matlab figures in LaTeX using matlabfrag or alternatives

I want to include Matlab figures into latex preferably with vectorised formats but with LaTeX MATLAB fonts for all text. I have been using the MATLAB function matlab2tikz which worked perfect for simple figures, but now my figures have too many data points which cause an error. So matlab2tikz is not suitable.
I've been looking at matlabfrag which I think will accomplish what I want, but when I run the script in LaTeX as detailed by the user guide it has an error File not found.
This is my code:
\documentclass{article}
\usepackage{pstool}
\begin{document}
\psfragfig{FileName}
\end{document}
Where FileName is the name of the .eps and .tex that matlabfrag creates. Has anyone come across this problem? Or recommend any other functions/methods to use?
I'm using Texmaker on Windows 7.
My advice would be to rethink your workflow.
Instead of reusing your Matlab code to plot figures and be dissappointed by ever changing outputs with matlab2tikz, start reusing your latex code to plot figures and don't bother about plotting in Matlab anymore (at least not for beautiful plots).
matlab2tikz is just generating latex code based on the latex-package pgfplots. To understand the working of this package is pretty easy, as it is intended to be similar to Matlab.
So why bother and always let matlab2tikz do the work for you? Because again and again you won't be entirely happy with the results. Just try to write the pgfplots-code from scratch and just load the data from Matlab.
Here is a convenient function I wrote to create latex-ready text files:
function output = saveData( filename, header, varargin )
in = varargin;
numCols = numel(in);
if all(cellfun(#isvector, in))
maxLength = max(cellfun(#numel, in));
output = cell2mat(cellfun(#(x) [x(:); NaN(maxLength - numel(x) + 1,1)],in,'uni',0));
fid = fopen(filename, 'w');
fprintf(fid, [repmat('%s\t',1,numCols),'\r\n'], header{:});
fclose(fid);
dlmwrite(filename,output,'-append','delimiter','\t','precision','%.6f','newline', 'pc');
else
disp('saveData: only vector inputs allowed')
end
end
Which could for example look like the following, in case of a bode diagram:
w G0_mag G0_phase GF_mag GF_phase
10.000000 40.865743 -169.818991 0.077716 -0.092491
10.309866 40.345290 -169.511901 0.082456 -0.101188
10.629333 39.825421 -169.196073 0.087474 -0.110690
10.958700 39.306171 -168.871307 0.092787 -0.121071
11.298273 38.787575 -168.537404 0.098411 -0.132411
In your tikzpicture you can then just load that file by
\pgfplotstableread[skip first n=1]{mydata.txt}\mydata
and store the table into the variable \mydata.
Now check pfgplots how to plot your data. You will find the basic plot command \addplot
\addplot table [x expr= \thisrowno{0}, y expr= \thisrowno{3} ] from \mydata;
where you directly access the columns of your text file by \thisrowno{0} (confusing, I know).
Regarding your problem with to many data points: pgfplots offers the key each nth point={ ... } to speed things up. But I'd rather filter/decimate the data already in Matlab.
The other way around is also possible, if you have to few data points the key smooth smoothes things up.

Getting values in Seaborn boxplot

I would like to get the specific values by a boxplot generated in Seaborn
(i.e., media, quartile). For example, in the boxplot below (source: link)
Is there a any way to get the media and quartiles instead of manually estimation?
import numpy as np
import seaborn as sns
sns.set(style="ticks", palette="muted", color_codes=True)
# Load the example planets dataset
planets = sns.load_dataset("planets")
# Plot the orbital period with horizontal boxes
ax = sns.boxplot(x="distance", y="method", data=planets,
whis=np.inf, color="c")
I would encourage you to become familiar with using pandas to extract quantitative information from a dataframe. For instance, a simple thing you could to do to get the values you are looking for (and other useful ones) would be:
planets.groupby("method").distance.describe().unstack()
which prints a table of useful values for each method.
Or if you just want the median:
planets.groupby("method").distance.median()
Sometimes I use my data as a list of arrays instead of pandas. So for that, you might need:
min(d), np.quantile(d, 0.25), np.median(d), np.quantile(d, 0.75), max(d)

read tow Column txt file in specific directory and import to matlab

I have two Column txt file every Column contain the speed on dc motor. I want to plot every Column with the time and compaire the two curves.
I tried this code, but not working:
fid = fopen('C:\Users\Hussam Yonis\Desktop\recive.txt','r');
KK = fscanf(fid,'%f %f',[2,50]);
t=0:0.05:0.05*length(a(:,1))-0.05;
plot(t,fid(:,1),'b',t,fid(:,2),'r')
fid is just a pointer corresponding to the opened file and does not have several dimensions, so fid(:,2) will give a matrix dimensions exceeded error. You want to plot the data that came out of the file, KK in your case. Try this:
plot(t,KK(:,1),'b',t,KK(:,2),'r')
I also suspect you might have your indexing the wrong way round, although as your code is not minimal, complete and verifiable, it is difficult to say. You might find you need the following command:
plot(t,KK(1,:),'b',t,KK(2,:),'r')

Any good visualization program for my situation?

Suppose you have a hash table where keys are strings and values are integers. Do you have any idea to visualize the hash table as a bar graph or histogram such that x-axis represents the key strings and y-axis represents the range of values?
Thanks in advance!
I don't know exactly what you are looking for but this is something you could do. You can use python with matplotlib. The following piece of code might help you to achieve what you want.
import matplotlib.pyplot as plt
hashtable = {'key1':10 , 'key2':20 , 'key3':14}
plt.bar(range(len(hashtable)), hashtable.values(), align='center')
plt.xticks(range(len(hashtable)), hashtable.keys())
plt.show()
For that you need to install python and matplotlib.

cannot import tif file into matlab

i am trying to import a .tif image into matlab with the following code
>> aa = imread('house.tif');
i get the error
Error using rtifc
TIFF library error: '_TIFFVSetField: C:\Users\user\Documents\MATLAB\house.tif: Null count
for "Tag 34022" (type 1, writecount -3, passcount 1).'.
Error in readtif (line 49)
[X, map, details] = rtifc(args);
Error in imread (line 434)
[X, map] = feval(fmt_s.read, filename, extraArgs{:});
as i am using matlab for the first time in my life i really have no idea what this error means. Please help is required in this matter.
MATLAB R2012b has a bug and it cannot read TIFF files properly. More information can be found here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/326232
Probably Matlab does not support the specific type of tif. In Matlab's defence, tif is not an easy file-format to read. It supports plenty of compression schemes, multiple pages and who knows what. I'd convert the tif to png and go with that.
Update: A quick Google search revealed that "rtifc" is a Matlab mex-wrapper around libtiff. Your error appears to come from libtiff. If the latter can't read it, your tif will probably be problematic for a lot of other applications too.
Another thing you could try is use the implementation tiffread from François Nedelec's group at EMBL. http://www.embl.de/ExternalInfo/nedelec/misc/matlab/tiffread29.m. It's heavily used by biology folks all over the world. I've been using it for many years.