import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# def get_remote_dataset():
# def get_local_dataset():
def list_dir_content(dir_path, file_extension=''):
files_pattern = dir_path + '/*.' + file_extension
print("files_pattern : ", files_pattern)
#files_results_paths = glob.glob(files_pattern)
files_results_paths = glob.glob('../tsanalysisapp/static/tsanalysisapp/datasets/uts/*.')
print("files_results_paths : ", files_results_paths)
return files_results_paths
def get_dataset_name(file_path):
dataset_filename = os.path.split(file_path)[-1] # -> (head, tail); retrie e the last componnent, the tail, of the pathname path. Head is ignored.
dataset_name = dataset_filename.split('.')[0] # get the name without the extension file
return dataset_name
def get_nb_instances(file_path):
return 200
#from django.shortcuts import render
from django.http import HttpResponse #, HttpResponseRedirect
#from django.template import RequestContext
#from django.urls import reverse
# DRF imports
from rest_framework import viewsets, status
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse
from django.views.decorators.csrf import csrf_exempt
# models imports
from .models import Dataset
# serializers import
from .serializers import DatasetSerializer
# import of python ML libraries
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
#import base64
from io import BytesIO
from matplotlib.figure import Figure
import pandas as pd
from pandas.plotting import lag_plot
from pandas.plotting import autocorrelation_plot
# Other imports
import os
from django.conf import settings
# For testing purpose
DATASET_PATH = os.path.join(settings.BASE_DIR, 'tsanalysisapp/static/tsanalysisapp/datasets/daily-min-temperatures.csv')
# Create your views here.
### (Data) visualization views
def histogramplot(request):
#fig = Figure()
fig = plt.figure()
# get the time serie data saved in session
#timeseries = pd.read_json(request.session['ts_dataset'], orient='index')
timeseries = pd.read_csv(DATASET_PATH, header=0, index_col=0, parse_dates=True, squeeze=True)
print("DATASET_PATH : ", DATASET_PATH, "BASE_DIR : ", settings.BASE_DIR)
# Build the graphic
timeseries.hist()
plt.title('Histogram')
plt.grid(True)
buf = BytesIO()
#FigureCanvasAgg(fig)
#plt.savefig(buf, format="png")
#plt.close(fig)
canvas = FigureCanvasAgg(fig)
canvas.print_png(buf)
response = HttpResponse(buf.getvalue(), content_type="image/png")
fig.clear()
return response
def lineplot(request):
#fig = Figure()
fig = plt.figure()
# get the time serie data saved in session
#timeseries = pd.read_json(request.session['ts_dataset'], orient='index')
timeseries = pd.read_csv(DATASET_PATH, header=0, index_col=0, parse_dates=True, squeeze=True)
# Build the graphic
timeseries.plot()
plt.title('Line plot')
plt.grid(True)
buf = BytesIO()
#FigureCanvasAgg(fig)
#plt.savefig(buf, format="png")
#plt.close(fig)
canvas = FigureCanvasAgg(fig)
canvas.print_png(buf)
response = HttpResponse(buf.getvalue(), content_type="image/png")
fig.clear()
return response
def densityplot(request):
#fig = Figure()
fig = plt.figure()
# get the time serie data saved in session
#timeseries = pd.read_json(request.session['ts_dataset'], orient='index')
timeseries = pd.read_csv(DATASET_PATH, header=0, index_col=0, parse_dates=True, squeeze=True)
# Build the graphic
timeseries.plot(kind='kde')
plt.title('Density plot')
plt.grid(True)
buf = BytesIO()
#FigureCanvasAgg(fig)
#plt.savefig(buf, format="png")
#plt.close(fig)
canvas = FigureCanvasAgg(fig)
canvas.print_png(buf)
response = HttpResponse(buf.getvalue(), content_type="image/png")
fig.clear()
return response
def lagplot(request):
#fig = Figure()
fig = plt.figure()
# get the time serie data saved in session
#timeseries = pd.read_json(request.session['ts_dataset'], orient='index')
timeseries = pd.read_csv(DATASET_PATH, header=0, index_col=0, parse_dates=True, squeeze=True)
# Build the graphic
lag_plot(timeseries)
plt.title('Lag plot')
plt.grid(True)
#FigureCanvasAgg(fig)
#plt.savefig(buf, format="png")
#plt.close(fig)
buf = BytesIO()
canvas = FigureCanvasAgg(fig)
canvas.print_png(buf)
response = HttpResponse(buf.getvalue(), content_type="image/png")
fig.clear()
return response
def scatterplot(request, lag=1):
fig = plt.figure()
# get the time serie data saved in session
#timeseries = pd.read_json(request.session['ts_dataset'], orient='index')
timeseries = pd.read_csv(DATASET_PATH, header=0, index_col=0, parse_dates=True, squeeze=True)
values = pd.DataFrame(timeseries.values)
valuesAgo = values.shift(lag)
# Build the graphic
plt.scatter(x=values, y=valuesAgo)
plt.title('Scatter plot t vs. t+' + str(lag))
plt.grid(True)
buf = BytesIO()
#FigureCanvasAgg(fig)
#plt.savefig(buf, format="png")
#plt.close(fig)
canvas = FigureCanvasAgg(fig)
canvas.print_png(buf)
response = HttpResponse(buf.getvalue(), content_type="image/png")
fig.clear()
response['Content-Length'] = str(len(response.content))
return response
def autocorrelationplot(request):
#fig = Figure()
fig = plt.figure()
# get the time serie data saved in session
#timeseries = pd.read_json(request.session['ts_dataset'], orient='index')
timeseries = pd.read_csv(DATASET_PATH, header=0, index_col=0, parse_dates=True, squeeze=True)
# Build the graphic
autocorrelation_plot(timeseries)
plt.title('Autocorrelation plot')
plt.grid(True)
buf = BytesIO()
#FigureCanvasAgg(fig)
#plt.savefig(buf, format="png")
#plt.close(fig)
canvas = FigureCanvasAgg(fig)
canvas.print_png(buf)
response = HttpResponse(buf.getvalue(), content_type="image/png")
fig.clear()
response['Content-Length'] = str(len(response.content))
return response
## Acces datasets views
#@csrf_exempt
def server_uts_datasets(request):
if request.method == 'GET':
uts_datasets = Dataset.get_uts_datasets()
uts_datasets_serializer = DatasetSerializer(uts_datasets, many=True)
print(uts_datasets)
return JsonResponse(uts_datasets_serializer.data, safe=False)
#@csrf_exempt
def server_mts_datasets(request):
if request.method == 'GET':
mts_datasets = Dataset.get_mts_datasets()
mts_datasets_serializer = DatasetSerializer(mts_datasets, many=True)
print(mts_datasets)
return JsonResponse(mts_datasets_serializer.data, safe=False)
from django.db import models
from django.conf import settings
# utils methods import
import utils.data as data
import os
from django.conf import settings
# Create your models here.
class Dataset(models.Model):
"""docstring for dataset"""
dataset_path = models.CharField(max_length=256)
dataset_name = models.CharField(max_length=256)
dataset_nb_instances = models.CharField(max_length=256)
dataset_type = models.CharField(max_length=3)
dataset_adding_date = models.DateField()
def __str__(self):
return "dataset_path : {0} - dataset_name : {1} - number of instances : {2}".format(self.dataset_path, self.dataset_name, self.dataset_nb_instances)
@classmethod
def get_uts_datasets(cls):
#uts_datasets_path = os.path.join(settings.DATASETS_DIR, 'uts')
#print("pathname of uts datasets repositorie : ", uts_datasets_path)
uts_datasets_files = data.list_dir_content(settings.UTS_DATASETS_DIR)
print("UTS list of files : ", uts_datasets_files)
uts_datasets = []
for uts_datasets_file in uts_datasets_files:
dataset_type = 'uts'
dataset_path = uts_datasets_file
dataset_name = data.get_dataset_name(uts_datasets_file)
dataset_nb_instances = data.get_nb_instances(uts_datasets_file)
uts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type)
uts_datasets.append(uts_dataset)
print("UTS datasets : ", uts_datasets)
return uts_datasets
@classmethod
def get_mts_datasets(cls):
mts_datasets_path = settings.DATASETS_DIR + '/mts'
mts_datasets_files = data.list_dir_content(mts_datasets_path)
mts_datasets = []
for mts_datasets_file in mts_datasets_files:
dataset_type = 'mts'
dataset_path = mts_datasets_file
dataset_name = data.get_dataset_name(mts_datasets_file)
dataset_nb_instances = data.get_nb_instances(mts_datasets_file)
mts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type)
mts_datasets.append(mts_dataset)
return mts_datasets
from rest_framework import serializers
# import Models here
from .models import Dataset
# Define ModelSerializers below
class DatasetSerializer(serializers.HyperlinkedModelSerializer):
class Meta :
model = Dataset
fields = '__all__'
I have in my project a directory named uts in static/ folder that I want to list the content and send it in HttpResponse but the glob.glob() function doesn't work.
-- Here you have the structure of my project. The utility function list_dir_content which call glob.glob is located in the utils folder: utils/data.py
import globimport osimport pandas as pdimport numpy as npimport matplotlib.pyplot as plt
# def get_remote_dataset():
# def get_local_dataset():
def list_dir_content(dir_path, file_extension=''): files_pattern = dir_path + '/*.' + file_extension print("files_pattern : ", files_pattern) #files_results_paths = glob.glob(files_pattern) files_results_paths = glob.glob('../tsanalysisapp/static/tsanalysisapp/datasets/uts/*.') print("files_results_paths : ", files_results_paths) return files_results_paths## in models.py
from django.db import modelsfrom django.conf import settings
# utils methods importimport utils.data as dataimport osfrom django.conf import settings
# Create your models here.class Dataset(models.Model): """docstring for dataset""" dataset_path = models.CharField(max_length=256) dataset_name = models.CharField(max_length=256) dataset_nb_instances = models.CharField(max_length=256) dataset_type = models.CharField(max_length=3) dataset_adding_date = models.DateField()
def __str__(self): return "dataset_path : {0} - dataset_name : {1} - number of instances : {2}".format(self.dataset_path, self.dataset_name, self.dataset_nb_instances) @classmethod def get_uts_datasets(cls): #uts_datasets_path = os.path.join(settings.DATASETS_DIR, 'uts') #print("pathname of uts datasets repositorie : ", uts_datasets_path) uts_datasets_files = data.list_dir_content(settings.UTS_DATASETS_DIR) print("UTS list of files : ", uts_datasets_files) uts_datasets = [] for uts_datasets_file in uts_datasets_files: dataset_type = 'uts' dataset_path = uts_datasets_file dataset_name = data.get_dataset_name(uts_datasets_file) dataset_nb_instances = data.get_nb_instances(uts_datasets_file) uts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type) uts_datasets.append(uts_dataset) print("UTS datasets : ", uts_datasets)
return uts_datasets
@classmethod def get_mts_datasets(cls): mts_datasets_path = settings.DATASETS_DIR + '/mts' mts_datasets_files = data.list_dir_content(mts_datasets_path) mts_datasets = [] for mts_datasets_file in mts_datasets_files: dataset_type = 'mts' dataset_path = mts_datasets_file dataset_name = data.get_dataset_name(mts_datasets_file) dataset_nb_instances = data.get_nb_instances(mts_datasets_file) mts_dataset = Dataset(dataset_path = dataset_path, dataset_name = dataset_name, dataset_nb_instances = dataset_nb_instances, dataset_type = dataset_type) mts_datasets.append(mts_dataset)
return mts_datasets
## in views.py
def server_uts_datasets(request): if request.method == 'GET': uts_datasets = Dataset.get_uts_datasets() uts_datasets_serializer = DatasetSerializer(uts_datasets, many=True) print(uts_datasets) return JsonResponse(uts_datasets_serializer.data, safe=False)
Thanks for your kindly help in advance...
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ef243ed3-bf9c-4acd-a596-38b67c879230%40googlegroups.com.
No comments:
Post a Comment