Certainly! Below is a Python program that takes a list of building history events as input (like the one you've provided) and creates a diagram representing the history of the building. This diagram will visualize the sequence of events, showing how each event is connected through predecessors and successors.
We'll use the networkx library to build the graph and matplotlib along with pygraphviz for plotting. The pygraphviz library is used to leverage Graphviz layouts, which are excellent for hierarchical and directed graphs.
Here's the code:
# First, make sure you have the necessary libraries installed:
# You can install them via pip if you haven't already:
# pip install networkx matplotlib pygraphviz
import networkx as nx
import matplotlib.pyplot as plt
def create_building_history_diagram(event_list):
"""
Creates a diagram of the building's history from a list of events.
Args:
event_list (list): List of event dictionaries.
Returns:
None. Displays the diagram using matplotlib.
"""
# Create a directed graph
G = nx.DiGraph()
# Add nodes with labels
for event in event_list:
event_id = event['id']
type_event = event.get('type_event', '')
date_event = event.get('date_event', '')
# Combine relevant information for the label
label_parts = [f"ID: {event_id}"]
if type_event:
label_parts.append(f"Type: {type_event}")
if date_event:
label_parts.append(f"Date: {date_event}")
label = '\n'.join(label_parts)
G.add_node(event_id, label=label)
# Add edges based on predecessor relationships
for event in event_list:
event_id = event['id']
predecessors = event.get('predecessor_ids', [])
for pred_id in predecessors:
G.add_edge(pred_id, event_id)
# Generate positions for all nodes using graphviz layout
try:
pos = nx.nx_agraph.graphviz_layout(G, prog='dot') # Requires PyGraphviz
except:
pos = nx.spring_layout(G) # Fallback to spring layout if graphviz is not available
# Get node labels
labels = nx.get_node_attributes(G, 'label')
# Draw the graph
plt.figure(figsize=(12, 8))
nx.draw(G, pos, with_labels=True, labels=labels, node_size=3000, node_color='lightblue', font_size=8, arrows=True)
# Show the plot
plt.title("Building History Diagram")
plt.show()
# Example usage with your provided data
if __name__ == "__main__":
event_list = [
{
"id": -14,
"change": [],
"sujet": [],
"type_event": [],
"commune": "LYON 7EME",
"initial_cadastre": "CD243,CD244",
"initial_volume": "",
"initial_lot": "0",
"successors_id": [],
"predecessor_ids": [
1
],
"date_event": "-"
},
{
"id": 1,
"change": [
"LOT_0"
],
"sujet": [
"LOT_1",
"LOT_2"
],
"type_event": "lot",
"commune": "LYON 7EME",
"initial_cadastre": "CD 243 à 244",
"initial_volume": "",
"initial_lot": "1 à 2",
"successors_id": [
-14
],
"predecessor_ids": [
2
],
"date_event": "24/07/2009"
},
{
"id": 2,
"change": [
"LOT_1",
"LOT_2"
],
"sujet": [
"LOT_10"
],
"type_event": "lot",
"commune": [
"LYON 7EME"
],
"initial_cadastre": [
"CD 243 à 244"
],
"initial_volume": [
""
],
"initial_lot": [
"10"
],
"successors_id": [
1
],
"predecessor_ids": [
3
],
"date_event": "15/05/2005"
},
{
"id": 3,
"change": [
"CAD_CD243",
"CAD_CD244"
],
"sujet": [
"CAD_CD33"
],
"type_event": "cadastre",
"commune": [
"LYON 7EME"
],
"initial_cadastre": [
"CD 33"
],
"initial_volume": [
""
],
"initial_lot": [
""
],
"successors_id": [
2
],
"predecessor_ids": [
4
],
"date_event": "10/02/2000"
},
{
"id": 4,
"change": [
"CAD_CD33"
],
"sujet": [],
"type_event": "cadastre",
"commune": [
"LYON 7EME"
],
"initial_cadastre": [
""
],
"initial_volume": [
""
],
"initial_lot": [
""
],
"successors_id": [
3
],
"predecessor_ids": [],
"date_event": "01/01/1995"
}
]
create_building_history_diagram(event_list)
How the Code Works:
-
Import Necessary Libraries: We import networkx for graph operations, matplotlib.pyplot for plotting, and attempt to use nx_agraph for Graphviz layouts.
-
Define the create_building_history_diagram Function: This function processes the list of events and builds a directed graph.
-
Add Nodes: For each event, we add a node to the graph with a label containing the event's ID, type, and date.
-
Add Edges: We add edges from each event's predecessors to the event itself, reflecting the historical flow.
-
Generate Positions: If pygraphviz is available, we use graphviz_layout with the 'dot' program to lay out the graph hierarchically. If not, we fall back to the spring layout.
-
Draw the Graph: We use networkx.draw to render the graph with the calculated positions and labels.
-
Example Usage: We include an if __name__ == "__main__": block to demonstrate how to use the function with your provided data.
Instructions to Run the Program:
-
Install Required Libraries:
Make sure you have the necessary libraries installed. You can install them using pip:
pip install networkx matplotlib pygraphviz
Note: To use pygraphviz, you need to have Graphviz installed on your system. You can download it from Graphviz's official website, or install it via your package manager.
-
For Windows: Download the installer from the website and add the Graphviz bin directory to your system PATH.
-
For macOS: Install via Homebrew:
brew install graphviz
-
For Linux: Install via your distribution's package manager. For example, on Debian/Ubuntu:
sudo apt-get install graphviz
-
Save the Script:
Save the Python script to a file, e.g., building_history_diagram.py.
-
Run the Script:
Run the script using Python:
python building_history_diagram.py
This will display the building history diagram in a window.
Understanding the Diagram:
-
Nodes: Each node represents an event in the building's history. The label shows the event's ID, type, and date.
-
Edges: An edge from node A to node B indicates that event B follows event A (i.e., A is a predecessor of B).
-
Hierarchy: The diagram is laid out such that predecessors are above their successors, providing a visual timeline from the earliest events at the top to the most recent events at the bottom (since negative IDs are the most recent).
Customizing the Diagram:
If you want to include more information in the node labels or customize the appearance further, you can modify the label_parts and adjust the nx.draw parameters accordingly.
Handling Missing Dates:
In cases where the date_event might be missing or set to "-", you may want to handle that in the label:
if date_event and date_event != "-":
label_parts.append(f"Date: {date_event}")
This ensures that only valid dates are displayed.
Possible Extensions:
-
Interactive Diagrams: For more interactive diagrams, consider using libraries like pyvis or plotly.
-
Exporting Diagrams: You can save the diagram to a file instead of displaying it by using plt.savefig('diagram.png') before plt.show().
-
Error Handling: Add more robust error handling for missing keys or invalid data in the event list.
I hope this program helps you generate the building history diagrams needed for the notary. Let me know if you have any questions or need further assistance!