Exemple #1
0
def current_sqm(request, size=800, interval=24, field='brightness'):
    dpi = 72
    figsize = (size / dpi, 400 / dpi)
    title = {
        'brightness': 'Sky Brightness, mag/sq.arcsec',
        'temp': 'Sensor temperature, C'
    }.get(field, '')

    #sqm = MeteoSqm.objects.filter(time__range=[datetime.datetime.utcnow()-datetime.timedelta(hours=interval), datetime.datetime.utcnow()]).order_by('-time')
    sqm = MeteoSqm.objects.filter(time__range=[
        timezone.now() - datetime.timedelta(hours=interval),
        timezone.now()
    ]).order_by('-time')
    x = []
    y = []

    tz = pytz.timezone('UTC')

    for s in sqm:
        x.append(s.time)
        y.append(getattr(s, field))

    fig = Figure(facecolor='white', figsize=figsize)
    ax = fig.add_subplot(111)
    ax.plot_date(x, y, '-', drawstyle='steps')
    ax.xaxis.set_major_formatter(
        DateFormatter('%H:%M:%S', tz=pytz.timezone(settings.TIME_ZONE)))

    ax.set_xlabel("Time")

    if title:
        ax.set_ylabel(title)
    else:
        ax.set_ylabel(field)

    ax.set_title("Sky Quality Meter, %s" %
                 (datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S MSK')))

    fig.autofmt_xdate()

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/jpeg')
    canvas.print_jpeg(response, dpi=120)

    return response
class Plot:
    """
    wrapper and interface for matplotlib canvas to
    include in reports
    """
    FIG_SIZE = (FIG_WIDTH, FIG_HEIGHT) = 6, 3  # in inches

    PLOT_COLS = PLOT_ROWS = PLOT_POS = 1
    _SUBPLOT_GRID = int(str(PLOT_ROWS) + str(PLOT_COLS) + str(PLOT_POS))

    def __init__(self, x_labels=None):
        self.fig = Figure(Plot.FIG_SIZE)
        self.canvas = FigureCanvas(self.fig)
        self.axes: Axes = self.fig.add_subplot(Plot._SUBPLOT_GRID)
        self.x_labels = x_labels

    def add_line(self, y_data: List[Number], x_data: List[Number], **kwargs
                 ) -> None:
        """
        add a line to the plot
        """
        new_line = Line2D(
                xdata=x_data,
                ydata=y_data,
                **kwargs
        )
        self.axes.add_line(new_line)

    @property
    def bytes(self) -> io.BytesIO:
        image_data = io.BytesIO()
        self.fig.savefig(image_data, format='png')
        image_data.seek(0)
        return image_data

    def resize_plot_axes(self, x_min, x_max, y_min, y_max):
        self.axes.set_xlim(x_min, x_max)
        self.axes.set_ylim(y_min, y_max)

    def save_as_jpeg(self, file_path: str):
        if file_path.endswith('jpeg'):
            self.canvas.print_jpeg(file_path)
        else:
            self.canvas.print_jpeg(f'{file_path}.jpeg')

    def show_signals(self, signals: List[int], x_data: List,
                     y_data: List[float]):
        if not signals:
            return

        x_data = [
            data_index if signal > 0 else 0
            for data_index, signal in zip(x_data, signals)
        ]
        y_data = [
            val if signal > 0 else 0
            for val, signal in zip(y_data, signals)
        ]
        while True:
            try:
                x_data.remove(0)
                y_data.remove(0)
            except ValueError:
                break  # remove until none left

        self.add_line(
                x_data=x_data,
                y_data=y_data,
                color='r',
                marker='o',
                markersize=2.2,
                linestyle='None'
        )
Exemple #3
0
def adbl(request):
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    from sklearn.preprocessing import MinMaxScaler
    from sklearn.model_selection import train_test_split
    from math import sqrt
    from datetime import timedelta
    import matplotlib.dates as mdates
    from sklearn.metrics import mean_squared_error
    import tensorflow as tf
    import django

    import warnings
    warnings.filterwarnings('ignore')

    def getfromdatabase():
        conn = psycopg2.connect(host="localhost",
                                dbname="postgres",
                                user="******",
                                password="******")
        cur = conn.cursor()
        cur.execute(
            """select openprice,maxprice,minprice,closingprice,date from stockdatacopy where  symbol = 'PLIC' order by date;"""
        )
        row = cur.fetchall()
        conn.commit()
        cur.close()
        #print(row)
        return row

# Reading the historical data of stocks from the web

    data = getfromdatabase()
    alphabet = "open high low close Date "
    columns = alphabet.split()  #split string into a list
    html_data = pd.DataFrame(
        data, columns=columns)  # load the dataset as a pandas data frame
    df = html_data.copy()

    # df.head()

    df.drop(['Date'], 1, inplace=True)  # Dropping unnecessary columns
    #print(df)
    plt.figure(figsize=(15, 5))
    plt.plot(df.open.values, color='red', label='open')
    plt.plot(df.close.values, color='green', label='close')
    plt.plot(df.low.values, color='blue', label='low')
    plt.plot(df.high.values, color='black', label='high')
    plt.title('Stock price')
    plt.xlabel('time [days]')
    plt.ylabel('Price in rs')
    plt.legend(loc='best')
    plt.show()

    plt.savefig('trends.jpeg', format='jpeg')

    sc = MinMaxScaler()
    scaled_data = sc.fit_transform(df)

    tstep = 30
    # since we are looking 60 timesteps back, we can start start looping over only after 60th record in our training set
    data = []

    # create all possible sequences of length seq_len
    for i in range(len(scaled_data) - tstep):
        data.append(scaled_data[i:i + tstep])

    data = np.array(data)

    # Using 10% of data each for validation and test purpose
    valid_set_size = int(np.round(0.1 * data.shape[0]))
    test_set_size = valid_set_size
    train_set_size = data.shape[0] - 2 * valid_set_size
    # Creating Train data
    x_train = data[:train_set_size, :-1, :]
    y_train = data[:train_set_size, -1, :]
    # Creating Validation data
    x_valid = data[train_set_size:train_set_size + valid_set_size, :-1, :]
    y_valid = data[train_set_size:train_set_size + valid_set_size, -1, :]
    # Creating Test data
    x_test = data[train_set_size + valid_set_size:, :-1, :]
    y_test = data[train_set_size + valid_set_size:, -1, :]

    index_in_epoch = 0
    perm_array = np.arange(x_train.shape[0])
    np.random.shuffle(perm_array)

    # function to get the next batch
    def next_batch(batch_size):
        global index_in_epoch, x_train, perm_array
        start = index_in_epoch
        index_in_epoch += batch_size
        #print(index_in_epoch)
        if index_in_epoch > x_train.shape[0]:
            #print( x_train.shape[0])
            np.random.shuffle(perm_array)  # shuffle permutation array
            start = 0  # start next epoch
            index_in_epoch = batch_size

        end = index_in_epoch
        return x_train[perm_array[start:end]], y_train[perm_array[start:end]]

    # 4 features
    num_inputs = 4
    # Num of steps in each batch
    num_time_steps = tstep - 1
    # 100 neuron layer
    num_neurons = 200
    num_outputs = 4
    learning_rate = 0.001
    # how many iterations to go through (training steps)
    num_train_iterations = 100
    # Size of the batch of data
    batch_size = 50
    # number of LSTM layers
    n_layers = 2

    # Creating Placeholders for X and y.
    # The shape for these placeholders should be [None,num_time_steps-1,num_inputs] and [None, num_time_steps-1, num_outputs]
    # The reason we use num_time_steps-1 is because each of these will be one step shorter than the original time steps size,
    # because we are training the RNN network to predict one point into the future based on the input sequence.
    X = tf.placeholder(tf.float32, [None, num_time_steps, num_inputs])
    y = tf.placeholder(tf.float32, [None, num_outputs])

    # use Basic RNN Cell
    cell = [
        tf.contrib.rnn.BasicRNNCell(num_units=num_neurons,
                                    activation=tf.nn.elu)
        for layer in range(n_layers)
    ]

    # Creatinmg stacked LSTM
    multi_layer_cell = tf.contrib.rnn.MultiRNNCell(cell)

    # Now pass in the cells variable into tf.nn.dynamic_rnn, along with your first placeholder (X)
    outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)

    stacked_rnn_outputs = tf.reshape(outputs, [-1, num_neurons])
    stacked_outputs = tf.layers.dense(stacked_rnn_outputs, num_outputs)
    final_outputs = tf.reshape(stacked_outputs,
                               [-1, num_time_steps, num_outputs])
    final_outputs = final_outputs[:, num_time_steps -
                                  1, :]  # keep only last output of sequence

    # Create a Mean Squared Error Loss Function and use it to minimize an AdamOptimizer.
    loss = tf.reduce_mean(tf.square(final_outputs - y))  # MSE
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train = optimizer.minimize(loss)

    # Initializing the global variable
    init = tf.global_variables_initializer()
    train_set_size = x_train.shape[0]

    test_set_size = x_test.shape[0]

    saver = tf.train.Saver()

    # with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
    with tf.Session() as sess:
        sess.run(init)
        for iteration in range(
                int(num_train_iterations * train_set_size / batch_size)):

            x_batch, y_batch = next_batch(batch_size)
            sess.run(train, feed_dict={X: x_batch, y: y_batch})

            if iteration % 100 == 0:
                mse_train = loss.eval(feed_dict={X: x_train, y: y_train})
                mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid})
                print(iteration, '\tTrain MSE:', mse_train,
                      '\tValidation MSE:', mse_valid)

        # Saving Model for future use
        saver.save(sess, './model/Stock_prediction_model')

    with tf.Session() as sess:
        # Using Saver instance to restore saved rnn
        saver.restore(sess, './model/Stock_prediction_model')

        y_pred = sess.run(final_outputs, feed_dict={X: x_test})

    y_test = sc.inverse_transform(y_test)
    y_pred = sc.inverse_transform(y_pred)
    #print(y_pred)
    # Comparing the actual versus predicted price
    latest_date = max(pd.to_datetime(html_data['Date']))
    ind = []

    for i in range(test_set_size):
        ind.append(latest_date - timedelta(days=test_set_size - i - 1))

    fig, ax = plt.subplots(figsize=(15, 7))
    plt.plot(
        ind, y_test[:, 0], color='black',
        label='Actual Price')  # Plotting the Open Market Price. Hence index 0
    # 0 = open, 1 = close, 2 = highest, 3 = lowest
    ax.plot(ind, y_pred[:, 0], color='green', label='Predicted Price')
    ax.set_title('Stock Price Prediction')
    ax.set_xlabel('Date')
    ax.set_ylabel('Price in rs')
    # set ticks every week
    #ax.xaxis.set_major_locator(mdates.WeekdayLocator())
    # set major ticks format
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
    ax.xaxis.set_tick_params(rotation=45)
    ax.legend(loc='best')
    plt.savefig('Actual_vs_Predicted_Stock_Price.jpeg', format='jpeg')
    plt.show()
    imgdata = BytesIO()
    fig.savefig(imgdata, format='jpeg')
    imgdata.seek(0)  # rewind the data
    im = Image.open(imgdata)
    canvas = FigureCanvas(im)
    response = django.http.HttpResponse(content_type='image/jpeg')
    canvas.print_jpeg(response)
    return response

    # Evaluating the model
    rmse = sqrt(mean_squared_error(y_pred[:, 0], y_test[:, 0]))
    normalized_rmse = rmse / (max(y_pred[:, 0]) - min(y_pred[:, 0]))
    print('Normalized RMSE: ', normalized_rmse)
Exemple #4
0
def current_meteo(request, size=800, interval=24, field='wind'):
    dpi = 72
    figsize = (size / dpi, 400 / dpi)
    title = {
        'wind': 'Wind Speed, m/s',
        'ambient_temp': 'Ambient Temperature, C',
        'sky_ambient_temp': 'Sky - Ambient Temperature, C',
        'humidity': 'Humidity, %',
        'dewpoint': 'Dew Point, C'
    }.get(field, '')

    #sqm = MeteoSqm.objects.filter(time__range=[datetime.datetime.utcnow()-datetime.timedelta(hours=interval), datetime.datetime.utcnow()]).order_by('-time')
    sqm = MeteoParameters.objects.filter(time__range=[
        timezone.now() - datetime.timedelta(hours=interval),
        timezone.now()
    ]).order_by('-time')
    if field == 'sky_ambient_temp':
        sqm = sqm.filter(sky_ambient_temp__gt=-100)

    x = []
    y = []

    tz = pytz.timezone('UTC')

    for s in sqm:
        x.append(s.time)
        y.append(getattr(s, field))

    fig = Figure(facecolor='white', figsize=figsize)
    ax = fig.add_subplot(111)
    ax.plot_date(x, y, '-', drawstyle='steps')
    ax.xaxis.set_major_formatter(
        DateFormatter('%H:%M:%S', tz=pytz.timezone(settings.TIME_ZONE)))

    ax.autoscale(False)

    if field == 'ambient_temp':
        ax.axhline(0, color='red', linestyle='--')
    elif field == 'sky_ambient_temp':
        ax.axhline(-25, color='red', linestyle='--')
        ax.axhline(-40, color='red', linestyle='..')
    elif field == 'wind':
        ax.axhline(10, color='red', linestyle='--')
    elif field == 'humidity':
        ax.axhline(90, color='red', linestyle='--')

    ax.set_xlabel("Time")

    if title:
        ax.set_ylabel(title)
    else:
        ax.set_ylabel(field)

    ax.set_title("Boltwood Cloud Sensor II, %s" %
                 (datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S MSK')))

    fig.autofmt_xdate()

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/jpeg')
    canvas.print_jpeg(response, dpi=120)

    return response
Exemple #5
0
    def render_GET(self, request):
        q = urlparse.urlparse(request.uri)
        args = urlparse.parse_qs(q.query)

        if q.path == self.base + '/status':
            return serve_json(request,
                              fast_connected=self.fast.factory.isConnected(),
                              fast=self.fast.fast_status)
        elif q.path == self.base + '/command':
            self.fast.factory.message(args['string'][0])
            return serve_json(request)
        elif q.path == self.base + '/image.jpg':
            if self.fast.image:
                request.responseHeaders.setRawHeaders("Content-Type",
                                                      ['image/jpeg'])
                request.responseHeaders.setRawHeaders(
                    "Content-Length", [str(len(self.fast.image))])
                return self.fast.image
            else:
                request.setResponseCode(400)
                return "No images"
        elif q.path == self.base + '/total_image.jpg':
            if self.fast.total_image:
                request.responseHeaders.setRawHeaders("Content-Type",
                                                      ['image/jpeg'])
                request.responseHeaders.setRawHeaders(
                    "Content-Length", [str(len(self.fast.total_image))])
                return self.fast.total_image
            else:
                request.setResponseCode(400)
                return "No images"
        elif q.path == self.base + '/total_flux.jpg':
            width = 800
            fig = Figure(facecolor='white',
                         figsize=(width / 72, width * 0.5 / 72),
                         tight_layout=True)
            ax = fig.add_subplot(111)

            ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

            x = np.array(self.fast.time)
            x = x - x[0]

            ax.plot(x, self.fast.mean, '-')
            ax.set_xlabel('Time, seconds')
            ax.set_ylabel('Flux, counts')

            canvas = FigureCanvas(fig)
            s = StringIO()
            canvas.print_jpeg(s, bbox_inches='tight')

            request.responseHeaders.setRawHeaders("Content-Type",
                                                  ['image/jpeg'])
            request.responseHeaders.setRawHeaders("Content-Length",
                                                  [str(s.len)])
            request.responseHeaders.setRawHeaders(
                "Cache-Control",
                ['no-store, no-cache, must-revalidate, max-age=0'])
            return s.getvalue()

        elif q.path == self.base + '/current_flux.jpg':
            width = 800
            fig = Figure(facecolor='white',
                         figsize=(width / 72, width * 0.5 / 72),
                         tight_layout=True)
            ax = fig.add_subplot(111)

            ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

            x = np.array(self.fast.time)
            if len(x):
                x = x - x[0]
            y = np.array(self.fast.mean)

            ax.plot(x[-1000:], y[-1000:], '-')
            ax.set_xlabel('Time, seconds')
            ax.set_ylabel('Flux, counts')

            canvas = FigureCanvas(fig)
            s = StringIO()
            canvas.print_jpeg(s, bbox_inches='tight')

            request.responseHeaders.setRawHeaders("Content-Type",
                                                  ['image/jpeg'])
            request.responseHeaders.setRawHeaders("Content-Length",
                                                  [str(s.len)])
            request.responseHeaders.setRawHeaders(
                "Cache-Control",
                ['no-store, no-cache, must-revalidate, max-age=0'])
            return s.getvalue()

        else:
            return q.path