update_predictions.rkt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #lang racket/base
  2. ; Add new predictions, update predictions, add outcomes
  3. (require racket/cmdline)
  4. (require racket/date)
  5. (require db)
  6. (define progname "update_predictions.rkt")
  7. ; load configuration file
  8. (require (file "../config.rkt"))
  9. ; give us the date in YYYY-MM-DD format
  10. (date-display-format 'iso-8601)
  11. (define (pending)
  12. (write-string "Functionality not yet implemented.\n"))
  13. ; set up command line arguments
  14. (define mode (command-line
  15. #:program "update_prediction"
  16. #:args ([updatetype "help"]) ; (add, update, outcome help)
  17. updatetype))
  18. ; print some help
  19. (define (printhelp)
  20. (write-string (string-append "Usage: "
  21. progname " MODE\n\n"))
  22. (write-string "Where MODE is one of:\n")
  23. (write-string " add\t\t - add new prediction to database.\n")
  24. (write-string " update\t\t - update a prediction with results.\n")
  25. (write-string " list-open\t - Show all predictions that do not yet have outcomes.\n")
  26. (write-string " score\t\t - Calculate and display Brier scores for predictions with logged outcomes.\n")
  27. (write-string " help\t\t - Show this help message.\n")
  28. (write-string "\nCopyright 2019 George C. Privon\n"))
  29. ; set up a condensed prompt for getting information
  30. (define (getinput prompt)
  31. (write-string prompt)
  32. (write-string ": ")
  33. (read-line))
  34. ; add a new prediction
  35. (define (addpred)
  36. ; manually get incremented ID
  37. (define nID (+ 1
  38. (query-value conn "SELECT ID FROM predictions order by ID desc limit 1")))
  39. (define prediction (getinput "Enter the prediction"))
  40. (define fprob (getinput "What is your forecast probability? "))
  41. (define comments (getinput "Comments on the forecast"))
  42. (define categories (getinput "Enter any categories (comma-separated)"))
  43. (define date (getinput "Enter the date of the forecast (YYYY-MM-DD)"))
  44. (query-exec conn "INSERT INTO predictions (ID, date, prediction, forecast, comments, categories) values (?,?, ?, ?, ?, ?)"
  45. nID date prediction fprob comments categories))
  46. ; is the outcome of a specified prediction known?
  47. (define (knownoutcome? ID)
  48. (number? (query-value conn
  49. "SELECT outcome FROM predictions WHERE ID=? ORDER BY DATE DESC LIMIT 1"
  50. ID)))
  51. ; print a prediction without a known outcome
  52. ; TODO: beautify output, add latest prediction to the output
  53. (define (printpred ID)
  54. (cond
  55. [(not (knownoutcome? ID)) (print (query-row conn "SELECT ID, prediction FROM predictions where ID=? ORDER BY date DESC LIMIT 1"
  56. ID))])
  57. (display "\n"))
  58. ; update a prediction
  59. (define (updatepred ID)
  60. (define option (getinput "Enter \"1\" to add an updated prediction or \"2\" to enter an outcome"))
  61. (pending)
  62. ;TODO: get new prediction or outcome and enter into db
  63. )
  64. ; enter an outcome for a prediction
  65. (define (showoutcome)
  66. (pending))
  67. ; print open predictions
  68. (define (printopen)
  69. (define uIDs (query-list conn
  70. "SELECT DISTINCT ID FROM predictions"))
  71. (map printpred uIDs))
  72. ; find unresolved predictions
  73. (define (findpending)
  74. (printopen)
  75. (define upID (getinput "Please enter a prediction number to edit (enter 0 or nothing to exit)"))
  76. (cond
  77. [(eq? (string->number upID) 0) (exit)]
  78. [(string->number upID) (updatepred (string->number upID))]
  79. [else (exit)]))
  80. ; compute Brier score for all predictions with outcomes
  81. (define (score)
  82. (pending)
  83. )
  84. ; make sure we can use the sqlite3 connection
  85. (cond (not (sqlite3-available?))
  86. (error "Sqlite3 library not available."))
  87. ; open the database file
  88. (define conn (sqlite3-connect #:database dbloc))
  89. ; determine which mode we're in
  90. (cond
  91. [(regexp-match "help" mode) (printhelp)]
  92. [(regexp-match "add" mode) (addpred)]
  93. [(regexp-match "update" mode) (findpending)]
  94. [(regexp-match "list-open" mode) (printopen)]
  95. [(regexp-match "score" mode) (score)]
  96. [else (error(string-append "Unknown mode. Try " progname " help\n\n"))])
  97. ; close the databse
  98. (disconnect conn)