You are an expert python developper. For a french notary company, write a python fonction that create the cascading pedigree chart of french buildings.
The function takes a list of events of this kind [
{
"id": -14,
"change": [],
"sujet": [],
"type_event": [],
"commune": "LYON 7ème",
"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 7ème",
"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 7ème",
"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 7ème",
"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 7ème",
"initial_cadastre": "",
"initial_volume": "",
"initial_lot": "",
"successors_id": [3],
"predecessor_ids": [],
"date_event": "01/01/1995"
},
{
"id": 5,
"change": ["CAD_CD35"],
"sujet": [],
"type_event": "cadastre",
"commune": "Paris 7ème",
"initial_cadastre": "",
"initial_volume": "",
"initial_lot": "",
"successors_id": [],
"predecessor_ids": [],
"date_event": "01/01/1995"
},
{
"id": 6,
"change": ["VOL_1"],
"sujet": [],
"type_event": "volume",
"commune": "Paris 7ème",
"initial_cadastre": "CD45",
"initial_volume": "",
"initial_lot": "",
"successors_id": [],
"predecessor_ids": [],
"date_event": "01/01/1990"
}
]
and create a cascading pedigree chart diagramme of it. sujets rectangles are parents and changement rectangles are children. an evenement rectagle are between parents and childreen and is liked to both of them. The event rectangles contains, event type, date and commune, initial_cadastre,initial_volume_initial_lot. The link are strlines from top to botom. Older event are up, and youger are down exactly as in a family tree. lot volume and cadastre should be of different color.
The cascading pedigree chart diagrame should be profetionnal. Remove CAD_, LOT_, VOL_ when using the labels.
The style should be perfect, like it was made by a mastermind of marketing.
The function should be capable of handling any length of list and any kind of event in the list. Take your time and craft the perfect function.
Assistant
python
def create_pedigree_chart(events):
import graphviz
from collections import defaultdict, deque
# Function to extract entity label and color
def get_entity_label_color(entity):
for prefix in ['LOT_', 'VOL_', 'CAD_']:
if entity.startswith(prefix):
label = entity[len(prefix):]
color = entity_colors.get(prefix[:-1], 'white')
return label, color
return entity, 'white'
# Build mapping from event ids to events
events_by_id = {event['id']: event for event in events}
# Build the event graph
# Build adjacency lists for topological sort
in_degree = defaultdict(int)
graph = defaultdict(list)
for event_id, event in events_by_id.items():
in_degree[event_id] = 0 # Initialize in-degree
for event_id, event in events_by_id.items():
for pred_id in event['predecessor_ids']:
if pred_id in events_by_id:
graph[pred_id].append(event_id)
in_degree[event_id] += 1
# Topological sort to assign levels
queue = deque([event_id for event_id in in_degree if in_degree[event_id] == 0])
levels = {}
visited = set()
while queue:
event_id = queue.popleft()
if event_id in visited:
continue
visited.add(event_id)
pred_levels = [levels[pred_id] for pred_id in events_by_id[event_id]['predecessor_ids'] if pred_id in levels]
level = max(pred_levels) + 1 if pred_levels else 0
levels[event_id] = level
for succ_id in graph[event_id]:
in_degree[succ_id] -= 1
if in_degree[succ_id] == 0:
queue.append(succ_id)
# For unconnected events
for event_id in in_degree:
if event_id not in levels:
levels[event_id] = 0
# Create Graphviz Digraph
dot = graphviz.Digraph(comment='Pedigree Chart', strict=True)
dot.graph_attr.update(rankdir='TB', splines='ortho', nodesep='0.5', ranksep='0.5')
dot.node_attr.update(shape='rectangle', style='filled,rounded', fontname='Helvetica', fontsize='10')
# Define colors for entity types
entity_colors = {
'LOT': '#add8e6', # Light Blue
'CAD': '#90ee90', # Light Green
'VOL': '#ffb6c1' # Light Pink
}
# Create nodes for entities
entity_nodes = {} # Mapping from entity identifier to node id
# Create nodes for events
for event_id, event in events_by_id.items():
# Prepare event node label
label_parts = [
f"Type: {event['type_event']}",
f"Date: {event['date_event']}",
f"Commune: {event['commune']}",
f"Cadastre: {event['initial_cadastre']}",
f"Volume: {event['initial_volume']}",
f"Lot: {event['initial_lot']}"
]
label = '\n'.join([part for part in label_parts if part.split(': ')[1]])
node_id = f"event_{event_id}"
dot.node(node_id, label=label, fillcolor='white')
# Create edges and entity nodes
for event_id, event in events_by_id.items():
node_id = f"event_{event_id}"
# Sujet nodes and edges (parents)
for sujet in event['sujet']:
label, color = get_entity_label_color(sujet)
# Create entity node if it doesn't exist
if sujet not in entity_nodes:
entity_node_id = f"entity_{sujet}"
entity_nodes[sujet] = entity_node_id
dot.node(entity_node_id, label=label, fillcolor=color)
else:
entity_node_id = entity_nodes[sujet]
# Edge from entity to event (parent to event)
dot.edge(entity_node_id, node_id)
# Change nodes and edges (children)
for change in event['change']:
label, color = get_entity_label_color(change)
# Create entity node if it doesn't exist
if change not in entity_nodes:
entity_node_id = f"entity_{change}"
entity_nodes[change] = entity_node_id
dot.node(entity_node_id, label=label, fillcolor=color)
else:
entity_node_id = entity_nodes[change]
# Edge from event to entity (event to child)
dot.edge(node_id, entity_node_id)
# Adjust event nodes' ranks according to levels
levels_events = defaultdict(list)
for event_id, level in levels.items():
node_id = f"event_{event_id}"
levels_events[level].append(node_id)
# Create subgraphs to control ranks
for level, node_ids in levels_events.items():
with dot.subgraph() as s:
s.attr(rank='same')
for node_id in node_ids:
s.node(node_id)
# Save and render the diagram
dot.format = 'png'
dot.render('pedigree_chart', view=True)
# Sample usage with the provided events list
events = [
{
"id": -14,
"change": [],
"sujet": [],
"type_event": [],
"commune": "LYON 7ème",
"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 7ème",
"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 7ème",
"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 7ème",
"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 7ème",
"initial_cadastre": "",
"initial_volume": "",
"initial_lot": "",
"successors_id": [3],
"predecessor_ids": [],
"date_event": "01/01/1995"
},
{
"id": 5,
"change": ["CAD_CD35"],
"sujet": [],
"type_event": "cadastre",
"commune": "Paris 7ème",
"initial_cadastre": "",
"initial_volume": "",
"initial_lot": "",
"successors_id": [],
"predecessor_ids": [],
"date_event": "01/01/1995"
},
{
"id": 6,
"change": ["VOL_1"],
"sujet": [],
"type_event": "volume",
"commune": "Paris 7ème",
"initial_cadastre": "CD45",
"initial_volume": "",
"initial_lot": "",
"successors_id": [],
"predecessor_ids": [],
"date_event": "01/01/1990"
}
]
create_pedigree_chart(events)
This function processes the provided list of events and generates a cascading pedigree chart diagram using Graphviz. The events are sorted based on their predecessor relationships to ensure that older events are placed at the top and newer events at the bottom, just like in a family tree.
In the diagram:
Event Rectangles: Contain details such as event type, date, commune, initial cadastre, initial volume, and initial lot.
Entity Rectangles: Represent the "sujet" (parents) and "change" (children). They are colored differently based on their type:
Light Blue: Lots
Light Green: Cadastres
Light Pink: Volumes
Edges: Connect entities to events and events to entities, showing the relationships.
Layout: The diagram is arranged from top to bottom, with older events at the top. The ranks are controlled to align events horizontally when they are at the same level.
The prefixes CAD_, LOT_, and VOL_ are removed from the labels to make them cleaner.
This function is designed to handle any length of the event list and any type of events. The use of Graphviz allows for professional styling, and the function ensures that the diagram is clean and market-ready.
Note: Ensure that the Graphviz software is installed on your system for this code to run successfully. You can install the graphviz Python package using pip install graphviz, but you also need the Graphviz binaries, which can be downloaded from the official Graphviz website.