My Interface to Indian Railways(part 2)

Note: You can find the code backing this demo here and the part 1 here.

In part 1 we painted a rosy picture of a world without corrupted data. Now, things are about to get real:

Graceful Degradation

Let's see the route of train number 14006:

In [1]:
import train_of_thought as tot

tot.see_train_route(14006)
Out[1]:

We can see that the coordinates we got for Nayagaon and Bhagwanpur are wrong but the distance from source is correct. This is often the case and thus we can use the distance from source to find whether the coordinates for a particular station are wrong. The way we do it is by calculating the haversine distance between coordinates of consecutive stations and comparing that with the distance between them calculated using distance from source:

In [2]:
tot.see_corrected_train_route(14006)
The coordinates for the following station(s) were invalid and hence they were not plotted:
--- NAYAGAON ( NYO ) , Arrival: 15:02 , Departure: 15:03 , Halt: 1 min , Day: 2 , Distance: 1065 km
--- BHAGWANPUR ( BNR ) , Arrival: 16:04 , Departure: 16:05 , Halt: 1 min , Day: 2 , Distance: 1102 km
Out[2]:

The same fault tolerance is also available for live running status:

In [3]:
tot.see_corrected_live_train_status(14006, "14-08-2017")
----- Live running status of: 14006 ( LICHCHIVI EXPRESS ) -----
Train departed from KANPUR GOODS MARSHAL(GMC) and late by 20 minutes.
The coordinates for the following station(s) were invalid and hence they were not plotted:
          name code       status actarr  actarr_date actdep  day has_arrived has_departed scharr  scharr_date schdep
32    NAYAGAON  NYO  0 mins late  15:02  15 Aug 2017  15:03    1       False        False  15:02  15 Aug 2017  15:03
36  BHAGWANPUR  BNR  0 mins late  16:04  15 Aug 2017  16:05    1       False        False  16:04  15 Aug 2017  16:05
Out[3]:

Handling False Positives

Sometimes while removing locations with wrong coordinates our algorithm might end up removing a few locations with correct coordinates too. In that case we can jump in and override its decisions:

In [4]:
tot.see_train_route(19019)
Out[4]:

Well, the above route is a mess, let's try and clean it up:

In [5]:
tot.see_corrected_train_route(19019)
The coordinates for the following station(s) were invalid and hence they were not plotted:
--- OKHLA ( OKA ) , Arrival: 05:03 , Departure: 05:04 , Halt: 1 min , Day: 2 , Distance: 1365 km
--- DEROL ( DRL ) , Arrival: 07:43 , Departure: 07:44 , Halt: 1 min , Day: 1 , Distance: 430 km
--- SANT ROAD ( SAT ) , Arrival: 08:44 , Departure: 08:46 , Halt: 2 min , Day: 1 , Distance: 473 km
--- BORDI ( BIO ) , Arrival: 09:50 , Departure: 09:52 , Halt: 2 min , Day: 1 , Distance: 538 km
--- ANAS ( ANAS ) , Arrival: 09:58 , Departure: 10:00 , Halt: 2 min , Day: 1 , Distance: 546 km
--- AMARGARH ( AGR ) , Arrival: 10:47 , Departure: 10:49 , Halt: 2 min , Day: 1 , Distance: 591 km
--- BILDI ( BILD ) , Arrival: 11:24 , Departure: 11:26 , Halt: 2 min , Day: 1 , Distance: 620 km
--- THURIA ( THUR ) , Arrival: 15:19 , Departure: 15:21 , Halt: 2 min , Day: 1 , Distance: 735 km
--- GAROT ( GOH ) , Arrival: 16:19 , Departure: 16:20 , Halt: 1 min , Day: 1 , Distance: 789 km
--- MORAK ( MKX ) , Arrival: 17:28 , Departure: 17:30 , Halt: 2 min , Day: 1 , Distance: 849 km
--- DARA ( DARA ) , Arrival: 17:43 , Departure: 17:45 , Halt: 2 min , Day: 1 , Distance: 864 km
--- GURLA ( GQL ) , Arrival: 19:44 , Departure: 19:46 , Halt: 2 min , Day: 1 , Distance: 917 km
--- KESHORAI PATAN ( KPTN ) , Arrival: 19:55 , Departure: 19:57 , Halt: 2 min , Day: 1 , Distance: 925 km
--- KAPREN ( KPZ ) , Arrival: 20:13 , Departure: 20:15 , Halt: 2 min , Day: 1 , Distance: 945 km
--- LAKHERI ( LKE ) , Arrival: 20:30 , Departure: 20:32 , Halt: 2 min , Day: 1 , Distance: 972 km
--- INDARGARH ( IDG ) , Arrival: 20:45 , Departure: 20:47 , Halt: 2 min , Day: 1 , Distance: 983 km
--- SAWAI MADHOPUR ( SWM ) , Arrival: 21:28 , Departure: 21:30 , Halt: 2 min , Day: 1 , Distance: 1019 km
--- MALARNA ( MLZ ) , Arrival: 21:53 , Departure: 21:55 , Halt: 2 min , Day: 1 , Distance: 1049 km
Out[5]:

The only two false positives in this case are Keshorai Patan(KPTN) and Lakheri(LKE) and we can override the algorithm's decision to not include them in the map:

In [6]:
tot.see_corrected_train_route(19019, override = ("LKE", "KPTN"))
The coordinates for the following station(s) were invalid and hence they were not plotted:
--- OKHLA ( OKA ) , Arrival: 05:03 , Departure: 05:04 , Halt: 1 min , Day: 2 , Distance: 1365 km
--- DEROL ( DRL ) , Arrival: 07:43 , Departure: 07:44 , Halt: 1 min , Day: 1 , Distance: 430 km
--- SANT ROAD ( SAT ) , Arrival: 08:44 , Departure: 08:46 , Halt: 2 min , Day: 1 , Distance: 473 km
--- BORDI ( BIO ) , Arrival: 09:50 , Departure: 09:52 , Halt: 2 min , Day: 1 , Distance: 538 km
--- ANAS ( ANAS ) , Arrival: 09:58 , Departure: 10:00 , Halt: 2 min , Day: 1 , Distance: 546 km
--- AMARGARH ( AGR ) , Arrival: 10:47 , Departure: 10:49 , Halt: 2 min , Day: 1 , Distance: 591 km
--- BILDI ( BILD ) , Arrival: 11:24 , Departure: 11:26 , Halt: 2 min , Day: 1 , Distance: 620 km
--- THURIA ( THUR ) , Arrival: 15:19 , Departure: 15:21 , Halt: 2 min , Day: 1 , Distance: 735 km
--- GAROT ( GOH ) , Arrival: 16:19 , Departure: 16:20 , Halt: 1 min , Day: 1 , Distance: 789 km
--- MORAK ( MKX ) , Arrival: 17:28 , Departure: 17:30 , Halt: 2 min , Day: 1 , Distance: 849 km
--- DARA ( DARA ) , Arrival: 17:43 , Departure: 17:45 , Halt: 2 min , Day: 1 , Distance: 864 km
--- GURLA ( GQL ) , Arrival: 19:44 , Departure: 19:46 , Halt: 2 min , Day: 1 , Distance: 917 km
--- KAPREN ( KPZ ) , Arrival: 20:13 , Departure: 20:15 , Halt: 2 min , Day: 1 , Distance: 945 km
--- INDARGARH ( IDG ) , Arrival: 20:45 , Departure: 20:47 , Halt: 2 min , Day: 1 , Distance: 983 km
--- SAWAI MADHOPUR ( SWM ) , Arrival: 21:28 , Departure: 21:30 , Halt: 2 min , Day: 1 , Distance: 1019 km
--- MALARNA ( MLZ ) , Arrival: 21:53 , Departure: 21:55 , Halt: 2 min , Day: 1 , Distance: 1049 km
Out[6]:

The same works for live running status too:

In [7]:
tot.see_corrected_live_train_status(19019, "14-08-2017", override = ("LKE", "KPTN"))
----- Live running status of: 19019 ( DEHRADUN EXP ) -----
Train departed from FATEH SINGHPURA(FSP) and late by 32 minutes.
The coordinates for the following station(s) were invalid and hence they were not plotted:
              name  code        status actarr  actarr_date actdep  day has_arrived has_departed scharr  scharr_date schdep
77           OKHLA   OKA   0 mins late  05:03  15 Aug 2017  05:04    1       False        False  05:03  15 Aug 2017  05:04
17           DEROL   DRL   6 mins late  07:49  14 Aug 2017  07:51    0        True         True  07:43  14 Aug 2017  07:44
20       SANT ROAD   SAT   6 mins late  08:50  14 Aug 2017  08:51    0        True         True  08:44  14 Aug 2017  08:46
25           BORDI   BIO  11 mins late  10:01  14 Aug 2017  10:02    0        True         True  09:50  14 Aug 2017  09:52
26            ANAS  ANAS  12 mins late  10:10  14 Aug 2017  10:11    0        True         True  09:58  14 Aug 2017  10:00
30        AMARGARH   AGR  16 mins late  11:03  14 Aug 2017  11:04    0        True         True  10:47  14 Aug 2017  10:49
34           BILDI  BILD  23 mins late  11:47  14 Aug 2017  11:48    0        True         True  11:24  14 Aug 2017  11:26
44          THURIA  THUR   8 mins late  15:27  14 Aug 2017  15:28    0        True         True  15:19  14 Aug 2017  15:21
48           GAROT   GOH   2 mins late  16:21  14 Aug 2017  16:23    0        True         True  16:19  14 Aug 2017  16:20
52           MORAK   MKX   2 mins late  17:30  14 Aug 2017  17:32    0        True         True  17:28  14 Aug 2017  17:30
53            DARA  DARA   8 mins late  17:51  14 Aug 2017  17:53    0        True         True  17:43  14 Aug 2017  17:45
56           GURLA   GQL   5 mins late  19:49  14 Aug 2017  19:50    0        True         True  19:44  14 Aug 2017  19:46
58          KAPREN   KPZ   7 mins late  20:20  14 Aug 2017  20:22    0        True         True  20:13  14 Aug 2017  20:15
60       INDARGARH   IDG  15 mins late  21:00  14 Aug 2017  21:03    0        True         True  20:45  14 Aug 2017  20:47
61  SAWAI MADHOPUR   SWM  16 mins late  21:44  14 Aug 2017  21:48    0        True         True  21:28  14 Aug 2017  21:30
62         MALARNA   MLZ  17 mins late  22:10  14 Aug 2017  22:12    0        True         True  21:53  14 Aug 2017  21:55
Out[7]: