Note
Click here to download the full example code
Example showing OTR anomalie¶
Here we investigate the routing specification for a train with interesting OTRs.
from tom.plot import plot_train, plot_graph
from tom.tom import make_train_from_yml, TrainRun, RouteSection, Route
from tom.util import example, dump_routing_info_as_xml
Load example from yaml specification
_, t_spec_file = example('../tests/data', 'otr-test')
print(t_spec_file.read_text())
Out:
---
coreID: 30AB
version: 1
sections:
- departure_station: A
arrival_station: B
travel_time: '01:00:00'
calendar:
begin: '2020-12-01'
end: '2020-12-02'
id: 10
color: salmon
succ:
- 20
- 40
- departure_station: B
arrival_station: C
departure_time: '00:30:00'
travel_time: '12:00:00'
calendar:
begin: '2020-12-02'
end: '2020-12-02'
id: 20
color: red
succ:
- 30
- departure_station: B
arrival_station: C
departure_time: '00:30:00'
travel_time: '12:00:00'
stop_time: '24:00:00'
calendar:
begin: '2020-12-04'
end: '2020-12-04'
id: 40
color: green
succ:
- 30
- departure_station: C
arrival_station: D
travel_time: '12:00:00'
calendar:
begin: '2020-12-02'
end: '2020-12-04'
# Exclude Thu 3.12. No section run on this day!
mask: 'Wed Fri'
id: 30
color: blue
Create train object and show its train id.
t = make_train_from_yml(t_spec_file)
t.train_id()
Out:
'TR/8350/30AB/00/2020'
Route Sections¶
From which sections the train is composed?
section: RouteSection
for section in t.sections:
print(section.description(), "\n")
Out:
ID : 10.v1
Calender : 01/12 to 02/12 11
Start at: 23:30 in A
Arrival at: 00:30 in B
Successors: [20, 40]
ID : 20.v1
Calender : 02/12 to 02/12 1
Start at: 00:30 in B
Arrival at: 12:30 in C
Successors: [30]
ID : 40.v1
Calender : 04/12 to 04/12 1
Start at: 00:30 in B
Arrival at: 12:30 in C
Successors: [30]
ID : 30.v1
Calender : 02/12 to 04/12 101
Start at: 12:30 in C
Arrival at: 00:30 in D
Successors: []
Section graph¶
The section graph is computed using the successor relation:
sg = t.section_graph()
plot_graph(sg)
Routes¶
Print all possible routes. Routes are calculated from all possible paths in the section graph.
route: Route
for route in t.routes():
print(route.description(), "\n")
Out:
Route : A-B-C-D
Key : 10-20-30
Calendar : 01/12 to 01/12 1
Start at: 23:30 in A
Arrival at: 00:30 in B
Arrival at: 12:30 in C
Arrival at: 00:30 in D
Route : A-B-C-D
Key : 10-40-30
Calendar : 02/12 to 02/12 1
Start at: 23:30 in A
Arrival at: 00:30 in B
Arrival at: 12:30 in C
Arrival at: 00:30 in D
Section runs¶
For each day of the calendar of a section a SectionRun is created. The section runs are the rows of RouteSection.to_dataframe:
for section in t.sections:
print(f"{section.section_id}: {section}")
print(section.to_dataframe(), "\n")
Out:
10: A-B
ID A B
2020-12-01 10 2020-12-01 23:30:00 2020-12-02 00:30:00
2020-12-02 10 2020-12-02 23:30:00 2020-12-03 00:30:00
20: B-C
ID B C
2020-12-02 20 2020-12-02 00:30:00 2020-12-02 12:30:00
40: B-C
ID B C
2020-12-04 40 2020-12-04 00:30:00 2020-12-04 12:30:00
30: C-D
ID C D
2020-12-02 30 2020-12-02 12:30:00 2020-12-03 00:30:00
2020-12-04 30 2020-12-04 12:30:00 2020-12-05 00:30:00
TrainRuns¶
Each TrainRun defines a row in the timetable of the train above
tr: TrainRun
for tr in t.train_run_iterator():
print(tr)
for sr in tr.sections_runs:
print(sr)
print("\n")
Out:
TR/8350/30AB/10/2020/2020-12-01
10.v1:2020-12-01 23:30 OTR=-1 A-B 2020-12-02 00:30 OTR=0
20.v1:2020-12-02 00:30 OTR=0 B-C 2020-12-02 12:30 OTR=0
30.v1:2020-12-02 12:30 OTR=0 C-D 2020-12-03 00:30 OTR=1
TR/8350/30AB/10/2020/2020-12-02
10.v1:2020-12-02 23:30 OTR=-2 A-B 2020-12-03 00:30 OTR=-1
40.v1:2020-12-04 00:30 OTR=0 B-C 2020-12-04 12:30 OTR=0
30.v1:2020-12-04 12:30 OTR=0 C-D 2020-12-05 00:30 OTR=1
RoutingInformation as TrainInformation¶
An XML Dump of the routing information of version 1
print(dump_routing_info_as_xml(t))
Out:
<TrainInformation xmlns="http://taf-jsg.info/schemes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://taf-jsg.info/schemes file:///../tests/data/xml/taf_cat_complete_sector.xsd" RouteInfoVersion="1">
<RouteSection SectionVersion="1">
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>10</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10000</LocationPrimaryCode>
<PrimaryLocationName>A</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>23:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10001</LocationPrimaryCode>
<PrimaryLocationName>B</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>1</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedCalendar>
<BitmapDays>11</BitmapDays>
<ValidityPeriod>
<StartDateTime>2020-12-01T00:00:00</StartDateTime>
<EndDateTime>2020-12-02T00:00:00</EndDateTime>
</ValidityPeriod>
</PlannedCalendar>
<Successors>
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>20</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>40</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
</Successors>
</RouteSection>
<RouteSection SectionVersion="1" HasReferenceCalender="true">
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>20</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10001</LocationPrimaryCode>
<PrimaryLocationName>B</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10002</LocationPrimaryCode>
<PrimaryLocationName>C</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>12:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedCalendar>
<BitmapDays>1</BitmapDays>
<ValidityPeriod>
<StartDateTime>2020-12-02T00:00:00</StartDateTime>
<EndDateTime>2020-12-02T00:00:00</EndDateTime>
</ValidityPeriod>
</PlannedCalendar>
<Successors>
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>30</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
</Successors>
</RouteSection>
<RouteSection SectionVersion="1" HasReferenceCalender="true">
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>40</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10001</LocationPrimaryCode>
<PrimaryLocationName>B</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10002</LocationPrimaryCode>
<PrimaryLocationName>C</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>12:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedCalendar>
<BitmapDays>1</BitmapDays>
<ValidityPeriod>
<StartDateTime>2020-12-04T00:00:00</StartDateTime>
<EndDateTime>2020-12-04T00:00:00</EndDateTime>
</ValidityPeriod>
</PlannedCalendar>
<Successors>
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>30</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
</Successors>
</RouteSection>
<RouteSection SectionVersion="1">
<SectionID>
<ObjectType>RS</ObjectType>
<Company>8350</Company>
<Core>--------30AB</Core>
<Variant>30</Variant>
<TimetableYear>2020</TimetableYear>
</SectionID>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10002</LocationPrimaryCode>
<PrimaryLocationName>C</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>12:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation>
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10003</LocationPrimaryCode>
<PrimaryLocationName>D</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>1</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedCalendar>
<BitmapDays>101</BitmapDays>
<ValidityPeriod>
<StartDateTime>2020-12-02T00:00:00</StartDateTime>
<EndDateTime>2020-12-04T00:00:00</EndDateTime>
</ValidityPeriod>
</PlannedCalendar>
</RouteSection>
<Route key="10-20-30">
<PlannedCalendar>
<BitmapDays>1</BitmapDays>
<ValidityPeriod>
<StartDateTime>2020-12-01T00:00:00</StartDateTime>
<EndDateTime>2020-12-01T00:00:00</EndDateTime>
</ValidityPeriod>
</PlannedCalendar>
<PlannedJourneyLocation JourneyLocationTypeCode="01">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10000</LocationPrimaryCode>
<PrimaryLocationName>A</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>23:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation JourneyLocationTypeCode="04">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10001</LocationPrimaryCode>
<PrimaryLocationName>B</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>1</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation JourneyLocationTypeCode="04">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10002</LocationPrimaryCode>
<PrimaryLocationName>C</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>12:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation JourneyLocationTypeCode="03">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10003</LocationPrimaryCode>
<PrimaryLocationName>D</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>1</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
</Route>
<Route key="10-40-30">
<PlannedCalendar>
<BitmapDays>1</BitmapDays>
<ValidityPeriod>
<StartDateTime>2020-12-02T00:00:00</StartDateTime>
<EndDateTime>2020-12-02T00:00:00</EndDateTime>
</ValidityPeriod>
</PlannedCalendar>
<PlannedJourneyLocation JourneyLocationTypeCode="01">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10000</LocationPrimaryCode>
<PrimaryLocationName>A</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>23:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation JourneyLocationTypeCode="04">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10001</LocationPrimaryCode>
<PrimaryLocationName>B</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>1</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation JourneyLocationTypeCode="04">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10002</LocationPrimaryCode>
<PrimaryLocationName>C</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>12:30:00</Time>
<Offset>0</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
<PlannedJourneyLocation JourneyLocationTypeCode="03">
<CountryCodeISO>DE</CountryCodeISO>
<LocationPrimaryCode>10003</LocationPrimaryCode>
<PrimaryLocationName>D</PrimaryLocationName>
<TimingAtLocation>
<Timing>
<Time>00:30:00</Time>
<Offset>1</Offset>
</Timing>
</TimingAtLocation>
</PlannedJourneyLocation>
</Route>
</TrainInformation>
Total running time of the script: ( 0 minutes 2.684 seconds)