OnurKerimoglu commited on
Commit
d730ea7
·
1 Parent(s): 0975e8a

error handling for plotting when fetching data goes wrong

Browse files
Files changed (3) hide show
  1. app.py +3 -3
  2. images/plot_error.png +0 -0
  3. src/technical_analysis.py +36 -4
app.py CHANGED
@@ -52,14 +52,14 @@ with gr.Blocks() as demo:
52
  analyze_button.click(
53
  fn=plot_stock,
54
  inputs=[ticker],
55
- outputs=plot_output,
56
- api_name="analyze_stock")
57
  md_output = gr.Markdown(label="Output Box")
58
  analyze_button.click(
59
  fn=ask_stock_agent,
60
  inputs=[agent_instance, ticker],
61
  outputs=md_output,
62
- api_name="analyze_stock")
63
 
64
 
65
  if __name__ == "__main__":
 
52
  analyze_button.click(
53
  fn=plot_stock,
54
  inputs=[ticker],
55
+ outputs=[plot_output],
56
+ api_name="plot_stock")
57
  md_output = gr.Markdown(label="Output Box")
58
  analyze_button.click(
59
  fn=ask_stock_agent,
60
  inputs=[agent_instance, ticker],
61
  outputs=md_output,
62
+ api_name="ask_stock_agent")
63
 
64
 
65
  if __name__ == "__main__":
images/plot_error.png ADDED
src/technical_analysis.py CHANGED
@@ -52,7 +52,6 @@ class TechnicalAnalysis():
52
  def run(
53
  self
54
  ) -> None:
55
- # fetch data from yf
56
  """
57
  Main entry point for the TechnicalAnalysis object.
58
  This method:
@@ -60,6 +59,7 @@ class TechnicalAnalysis():
60
  - computes the technical analysis metrics
61
  - plots the price and TA metrics.
62
  """
 
63
  self.df = self.fetch_data()
64
  # add the features based on technical analysis
65
  if self.df.shape[0] > 0:
@@ -79,10 +79,13 @@ class TechnicalAnalysis():
79
  else:
80
  fig = None
81
  else:
82
- fig = None
 
 
 
83
 
84
  return self.df, fig
85
-
86
  def fetch_data(
87
  self
88
  ) -> pd.DataFrame:
@@ -286,10 +289,39 @@ class TechnicalAnalysis():
286
  ax.fill_between(df.index, df['Low'], df['High'], color='gray', alpha=0.3)
287
  ax.grid(True, linestyle='--', alpha=0.7)
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
 
290
  if __name__ == '__main__':
291
  ticker = 'GOOG'
292
- df = TechnicalAnalysis(ticker, debug=False).run()
293
  print(f'columns: {df.columns}')
294
 
295
 
 
52
  def run(
53
  self
54
  ) -> None:
 
55
  """
56
  Main entry point for the TechnicalAnalysis object.
57
  This method:
 
59
  - computes the technical analysis metrics
60
  - plots the price and TA metrics.
61
  """
62
+ # fetch data from yf
63
  self.df = self.fetch_data()
64
  # add the features based on technical analysis
65
  if self.df.shape[0] > 0:
 
79
  else:
80
  fig = None
81
  else:
82
+ if self.plot_ta:
83
+ fig = self.get_fetcherror_fig(message='failed fetching data')
84
+ else:
85
+ fig = None
86
 
87
  return self.df, fig
88
+
89
  def fetch_data(
90
  self
91
  ) -> pd.DataFrame:
 
289
  ax.fill_between(df.index, df['Low'], df['High'], color='gray', alpha=0.3)
290
  ax.grid(True, linestyle='--', alpha=0.7)
291
 
292
+ def get_fetcherror_fig(
293
+ self,
294
+ message
295
+ ) -> plt.Figure:
296
+ """
297
+ Fetches images/plot_error.png, annotates it and returns it as a matplotlib.pyplot.Figure object
298
+ Args:
299
+ message (str): message to be annotated on the displayed image
300
+ Returns:
301
+ plt.Figure: figure object containing the annotated image
302
+ """
303
+
304
+ fig, ax = plt.subplots(
305
+ figsize=(5, 5)
306
+ )
307
+
308
+ # Load and display the image
309
+ parentdir = os.path.dirname(os.path.dirname(__file__))
310
+ fname = os.path.join(parentdir, 'images', 'plot_error.png')
311
+ img = plt.imread(fname)
312
+ ax.imshow(img)
313
+
314
+ # Remove axes ticks and labels
315
+ # ax.set_xticks([])
316
+ # ax.set_yticks([])
317
+ ax.axis('off') # removes both ticks and axes lines completely
318
+
319
+ ax.text(0.5, 0.05, message, fontsize=20, ha='center', va='center', transform=ax.transAxes)
320
+ return fig
321
 
322
  if __name__ == '__main__':
323
  ticker = 'GOOG'
324
+ df, fig = TechnicalAnalysis(ticker, plot_ta=True, savefig=False, debug=False).run()
325
  print(f'columns: {df.columns}')
326
 
327