Find communities in a graph with Louvain - python

I read T0.pkl that contains a directed graph created with networkx, thus I've turned that one in a igraph graph.
Next, I applied Louvain to the graph and now I've a louvain.VertexPartition.ModularityVertexPartition and I don't know how to use it
G0_nx = nx.read_gpickle("../snapshots_original/T0.pkl")
G0_nx_edges = list(G0_nx.edges)
G0_nx_nodes = list(G0_nx.nodes)
G0_ig = igraph.Graph(directed=True)
G0_ig.add_vertices(G0_nx_nodes)
G0_ig.add_edges(G0_nx_edges)
partition = louvain.find_partition(G0_ig, lv.ModularityVertexPartition)

First you can plot it to visualize the clustering:
igraph.plot(partition)
If you try to print it with a simple
print(partition)
you will obtain something similar to:
Clustering with V elements and n clusters
[cluster_id]: node1, node2, node3... (the nodes in the n-th cluster)
For example:
Clustering with 33 elements and 3 clusters
[0] 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
[1] 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21
[2] 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
Afterwards you can access the nodes within a cluster just like using a list with the desired cluster id as index:
print(partition[1])
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
You should also check the Louvain library docs, where other features such as temporal community detection are outlined.

Related

Nested dict loop

I just wrote some code the should simulate parcel orders for a series of 90 day,
This part makes csv files for all order days:
Period = list(range(1,90))
for item in Preiod:
"some code to simulate the days, which is irrelevant here"
.to_csv(rf'file_location_Day{item}.csv)
I then use a solver to make routes for the vehicles that deliver the parcels. this solver opens the csv file, and gives an output like this(nested list):
[[36, 27, 11, 15, 41, 35, 20, 42, 24, 21, 37, 5, 25, 39, 12, 31], [3, 34, 32, 33, 47, 9, 16, 13, 28, 22, 17, 43, 7, 45, 38], [6, 30, 1, 40, 4, 8, 46, 23, 14, 18, 29, 44, 10, 26, 2, 19]]
In this case 3 lists: vehicle 1 delivers parcel 36 then 27 then 11, vehicle 2 delivers parcel 3 then 34, then 32 etc. This nested list is stored as results.routs
I would like to store this data in a nested dictionary, i think this is allready close:
Routes = {}
Routes[Day1] = result.routes
But i'd like an extra level:
Routes[Day1],[vehicle1] =
Routes[Day1],[vehicle1], [ParcelNumber, x-coordinate, y-coordinate] =
This is because i will later need to search the coordinates for all corresponding parcel locations. (bit like vlookup in excel)
e.g. in the csv file search the 36th parcel location, pick the x and y coordinate in the two columns next to it, and then paste it in the corresponding lists in the dict.
My biggest challange: How do i make a loop for this in a neat way

Find indices of line intersection in shapely

I would like to find a better solution for what I am proposing below. I am trying to find the indices associated with a line intersection when using the shapely library. Solutions from other libraries are welcome.
Right now I am iterating through the location coordinates and storing the index where an intersection is observed. I would like to do away with the loop and create a more streamlined function.
The code below results in a single intersection/crossing.
line_crossings = []
latitude = [10, 11, 12, 13, 14, 15, 16, 17 ,18]
longitude = [7, 9, 11, 13, 17, 19, 23, 25 ,29]
location = np.column_stack((latitude, longitude))
C = (14.5, 14.5)
D = (12.3, 12.5)
line2 = LineString([C, D])
for idx in range(0, len(location)-1):
A = (latitude[idx], longitude[idx])
B = (latitude[idx+1], longitude[idx+1])
line1 = LineString([A, B])
int_pt = line2.intersection(line1)
if int_pt.type == 'Point':
print(int_pt)
line_crossings.append(idx)
Update
It would seem the quickest way to get the coordinates of the crossings is as follows:
latitude = [10, 11, 12, 13, 14, 15, 16, 17 ,16, 15, 14, 13, 12, 11, 10]
longitude = [7, 9, 11, 13, 17, 19, 23, 25 ,29, 25, 23, 13, 13, 13, 11]
location = LineString([i for i in zip(latitude,longitude)])
C = (14.5, 14.5)
D = (12.3, 12.5)
gate = LineString([C, D])
[[i.x, i.y] for i in location.intersection(gate)]
But I need to be able to get the index in the location variable where the intersection occurs. Is it possible to get this using the list comprehension?

How to create an astrology chart in Python from planetary data (derived from a custom algorithm) that dynamically updates with user input?

Using my algorithm, I can take space-time data as an input list & generate accurate planetary & astrological data as a 2D lists as follows:
Input_list: [Year, Month, Day, Hour, Minute, Second, Latitude, Longitude, Altitude]
Input_sample: [1990, 4, 9, 21, 12, 30, 22.51361111111111, 88.3411111111111, 9.14]
Output:2 2D lists "GRAHA POSITIONS" & "HOUSE CUSPS"
--------------------GRAHA POSITIONS-----------------------------
[Planet, Z.sign, degree, minute, second, nakshatra, pada, angle_in_float]
['Sun', 'Aries', 19, 30, 30, 'Revati', 4, 19.508579713622044]
['Moon', 'Libra', 14, 36, 20, 'Chitra', 1, 14.60574883925213]
['Mercury', 'Taurus', 8, 14, 2, 'Bharani', 2, 8.23414995759422]
['Venus', 'Pisces', 3, 21, 55, 'Shatavishak', 2, 3.3653055864194243]
['Mars', 'Aquarius', 21, 33, 43, 'Dhanistha', 3, 21.562001016537806]
['Jupiter', 'Cancer', 3, 45, 55, 'Ardra', 3, 3.765369522224674]
['Saturn', 'Capricorn', 24, 49, 1, 'U.Ashadha', 3, 24.81699905482151]
['Rahu', 'Aquarius', 14, 21, 35, 'Dhanistha', 1, 14.359770567077646]
['Ketu', 'Leo', 14, 21, 35, 'Ashlesha', 3, 14.359770567077646]
----------------------HOUSE CUSPS-------------------------------
[Cusp, Z.sign, degree, minute, second, nakshatra, pada, angle_in_float]
[1, 'Scorpio', 29, 16, 11, 'Anuradha', 2, 29.269861999821757]
[2, 'Sagittarius', 28, 55, 44, 'Moola', 3, 28.929165427870714]
[3, 'Aquarius', 0, 37, 54, 'Sravana', 1, 0.6317628121900043]
[4, 'Pisces', 3, 49, 57, 'Shatavishak', 3, 3.8325043513459605]
[5, 'Aries', 5, 53, 38, 'U.Bhadrapada', 4, 5.893938324894634]
[6, 'Taurus', 4, 20, 36, 'Bharani', 1, 4.343341833871818]
[7, 'Taurus', 29, 16, 11, 'Krittika', 4, 29.269861999821728]
[8, 'Gemini', 28, 55, 44, 'Ardra', 1, 28.929165427870714]
[9, 'Leo', 0, 37, 54, 'Pushya', 3, 0.6317628121900043]
[10, 'Virgo', 3, 49, 57, 'P.Phalguni', 1, 3.832504351345932]
[11, 'Libra', 5, 53, 38, 'Hasta', 2, 5.893938324894634]
[12, 'Scorpio', 4, 20, 36, 'Svati', 3, 4.34334183387179]
Now I want to use this data to create visualizations as as South Indian horoscope chart. I've added an example chart to show what I want to achieve.Sample chart
In the image, the chart background with zodiac symbols in the bottom right is static, I can simply use an image for that. The planets & asc symbol i.e. HOUSE CUSPS [0][0] (red slanted line in Scorpio) are vectors, which I'd like to import into my program & assign to planet names i.e. GRAHA POSITIONS[i][0] from the above 2D list.
The number above a planet is its angle in the rashi i.e. GRAHA POSITIONS[i][2,3,4] & the text below is its Nakshatra & pada i.e. GRAHA POSITIONS[i][5,6].
The little black numbers in the bottom left of a rashi are the cusps in that rashi i.e. HOUSE CUSPS[i][0].
Is it reasonable to try to build this idea in Python? If yes, then how? If no, then please guide me.
I'm an amateur programmer by need, with only few months of experience in Python. I'm very eager to build this software for research in statistical astrology aimed to predict natural calamities decades ahead & at a fraction of the current cost.
Thank you for any help in advance.
Yes you can most certainly do this in Python and I would suggest looking into Pillow/PIL. Basically you need to look at this as more of image manipulation rather than a chart/graph. You start with a basic background image and add all details on top of it.
Just out of curiosity, how are you generating the planetary & astrological data?

Find communities in a large graph with million vertices in Python using igraph and leidenalg

I am trying to get some experience with using igraph to find communities. Following the documentation and using the zachary karate club dataset, I was able to find the optimal number of communities with regards to modularity.
import leidenalg as la
import igraph as ig
file = 'data/com-dblp.ungraph.txt'
#G = ig.Graph.Read_Edgelist(file)
G = ig.Graph.Famous('Zachary')
partition = la.find_partition(G, la.ModularityVertexPartition)
print(partition)
print("Modularity of this partition: ", ig.Graph.modularity(G, partition))
ig.plot(partition)
Following is the output:
Clustering with 34 elements and 4 clusters
[0] 8, 9, 14, 15, 18, 20, 22, 26, 29, 30, 32, 33
[1] 0, 1, 2, 3, 7, 11, 12, 13, 17, 19, 21
[2] 23, 24, 25, 27, 28, 31
[3] 4, 5, 6, 10, 16
Modularity of this partition: 0.4197896120973044
However, how can I move this approach to work with larger datasets like the DBLP dataset provided by Snap which has 317080 nodes? When I try the same approach it outputs 109092 clusters with modularity of 0.8288599603794473.

How would I complete this using a loop?

Let's say I have a list in Python that contains 24 elements. I'm using each element in the list to use in a sentence, for example: (for this example the list contains numbers, but in the real case it contains names, places, etc
s = [Steve, 25, Bill, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24]
If I needed to write 3 sentences each sentence using elements 1-8 from the list like this:
Sentence 1:
It is s[1] degrees outside, my name is s[2]. My friend is s[0].
The sentence would go on using elements 1-8, I only used 3 for an example. I need to write three of these sentences and need to use a loop because there could be any amount of sentences needed to write.
The elements go in the same place for each sentence, so in the next sentence it would be:
It is s[8] degrees outside, my name is s[9]. My friend is [7].
Is there anyway to complete this with a loop? I'm hoping you guys understand my question. The loop gets an index out of range error because I'm running it N times (which in this case is 3)
sentence = "It is {s[1]} degrees outside, my name is {s[2]}. My friend is {s[0]}."
s = ['Steve', 25, 'Bill', 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24]
for g in zip(*[iter(s)]*3):
print sentence.format(s=g)
maybe ... at least

Categories

Resources