Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Yoann LE BARS
analyseSondage
Commits
8157b7a3
Commit
8157b7a3
authored
Jan 26, 2022
by
Yoann LE BARS
Browse files
Finalisation du calcul de la matrice covariances-variances.
parent
1ed9728b
Changes
2
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
8157b7a3
...
...
@@ -28,7 +28,9 @@ suivant :
*
[
Progressbar
](
https://pypi.org/project/progressbar/
"Progressbar"
)
;
*
[
SciPy
](
https://www.scipy.org/
"SciPy"
)
;
*
[
NumPy
](
http://www.numpy.org/
"NumPy"
)
;
*
[
Matplotlib
](
https://matplotlib.org/
"Matplotlib"
)
.
*
[
Matplotlib
](
https://matplotlib.org/
"Matplotlib"
)
;
*
[
Seaborn
](
https://seaborn.pydata.org/
"Seaborn"
)
;
*
[
Pandas
](
https://pandas.pydata.org/
"Pandas"
)
.
Pour le lancer, il suffit d’exécuter le script nommé « analyseSondages.py »
situé dans le répertoire « src ». La syntaxte est la suivante :
...
...
src/calculMatCov.py
View file @
8157b7a3
...
...
@@ -13,14 +13,11 @@ situé dans le répertoire « src ». La syntaxte est la suivante :
::
calculMatCov [-h] [-v]
[-m MATRICE]
nomFichierSondages
calculMatCov [-h] [-v] nomFichierSondages
Avec `nomFichierSondages` le chemin d’accès au fichier contenant les sondages
à analyser (il y en a de disponibles dans le répertoire `ressources`).
L’option -m (ou `--matrice`) permet de spécifier un fichier où enregistrer la
matrice covariances-variances.
L’option `-h` (ou `--help`) produit l’affichage d’un message d’aide et met
fin au programme, tandis que l’option `-v` (ou `--version`) produit
l’affichage de la version du programme et met fin à l’exécution.
...
...
@@ -48,17 +45,19 @@ from utilitaires import positives, listeReponses
import
csv
import
progressbar
import
numpy
as
np
import
calculCov
import
seaborn
as
sn
import
matplotlib.pyplot
as
plt
import
pandas
as
pd
def
calculMatrice
(
nomFichierSondages
:
str
,
nomFichierMatrice
:
str
):
def
calculMatrice
(
nomFichierSondages
:
str
):
"""
Calcul la matrice covariances-variances d’une série de sondages, la
stocke dans un fichier CSV et la représente graphiquement.
:param str nomFichierSondages: Chemin d’accès vers le fichier contenant
les sondages.
:param str nomFichierMatrice: Chemin d’accès vers le fichier où stocker
la matrice.
:raises FichierInexistant: Si jamais un chemin d’accès vers un fichier
est invalide.
...
...
@@ -69,50 +68,9 @@ def calculMatrice (nomFichierSondages: str, nomFichierMatrice: str):
if
(
not
path
.
exists
(
nomFichierSondages
))
or
(
not
path
.
isfile
(
nomFichierSondages
)):
raise
FichierInexistant
(
nomFichierSondages
)
# Indique s’il faut ou non sauvegarder la matrice.
sauvegarder
=
False
# Indique s’il faut ou non afficher la matrice.
afficher
=
False
if
(
nomFichierMatrice
==
''
):
afficher
=
True
else
:
if
(
path
.
exists
(
nomFichierMatrice
)):
if
(
path
.
isfile
(
nomFichierMatrice
)):
# Réponse de l’utilisateur.
reponse
=
input
(
'Le fichier « %s » existe, voulez-vous '
'l’écraser ([o]ui ou '
'[n]on) ? '
%
(
nomFichierMatrice
))
while
(
not
reponse
in
listeReponses
):
print
(
'Réponse non valide.'
)
reponse
=
input
(
'Le fichier « %s » existe, voulez-vous '
'l’écraser ([o]ui ou '
'[n]on) ? '
%
(
nomFichierMatrice
))
if
(
reponse
in
positives
):
sauvegarder
=
True
else
:
raise
FichierInexistant
(
nomFichierMatrice
)
else
:
sauvegarder
=
True
if
(
sauvegarder
):
# Réponse de l’utilisateur.
reponse
=
input
(
'Afficher la matrice ([o]ui ou [n]on) ? '
)
while
(
not
reponse
in
listeReponses
):
print
(
'Réponse non valide.'
)
reponse
=
input
(
'Afficher la matrice ([o]ui ou [n]on) ? '
)
if
(
reponse
in
positives
):
afficher
=
True
else
:
afficher
=
True
# Liste des candidats à l’élection.
listeCandidats
=
[]
# Liste des intentions de votes.
listeIntentions
=
[]
# Liste des jours à supprimer.
listeJoursASupprimer
=
[]
with
open
(
nomFichierSondages
,
newline
=
''
,
encoding
=
'utf-8'
)
as
fichier
:
# Lecteur du fichier CSV.
...
...
@@ -124,53 +82,17 @@ def calculMatrice (nomFichierSondages: str, nomFichierMatrice: str):
ligne
=
next
(
lecteur
)
ligne
=
next
(
lecteur
)
# Index de la ligne du candidat.
nLigne
=
3
for
ligne
in
lecteur
:
nLigne
+=
1
# Nom du candidat courant.
candidatCourant
=
ligne
[
0
]
listeCandidats
.
append
(
candidatCourant
)
# Liste des intentions de votes du candidat courant non traitée.
listeCandCour
=
ligne
[
2
:]
# Liste des intentions de votes traitées.
listeTraite
=
[]
# Barre de progression indiquant l’avancement dans le traitement.
progression
=
progressbar
.
ProgressBar
(
widgets
=
[
candidatCourant
,
' '
,
progressbar
.
Percentage
(),
' '
,
progressbar
.
Bar
()])
for
i
in
progression
(
range
(
len
(
listeCandCour
))):
# Valeur courante.
valCour
=
listeCandCour
[
i
]
if
(
valCour
==
''
):
listeJoursASupprimer
.
append
(
i
)
# Valeur arbitraire qui sera supprimée de toute façon.
listeTraite
.
append
(
0.
)
else
:
try
:
# Intention de vote courrante.
intention
=
float
(
valCour
)
except
ValueError
as
ex
:
raise
IntentionInvalide
(
nomFichierSondages
,
nLigne
,
index
)
if
(
intention
<
0.
)
or
(
intention
>
100.
):
raise
IntentionHorsLimittes
(
nomFichierSondages
,
nLigne
,
index
)
listeTraite
.
append
(
intention
)
listeIntentions
.
append
(
listeTraite
)
listeJoursASupprimer
.
sort
()
# Liste corrigée des intentions de votes.
listeCorr
=
[]
# Matrice des intentions de votes.
#matriceIntentions = np.array(listeIntentions)
listeCandidats
.
append
(
ligne
[
0
])
# Matrice covariances-variances.
#matriceCov = np.cov(matriceIntentions)
#print(matriceCov)
matCovVar
=
[[
calculCov
.
traitement
(
nomFichierSondages
,
[
candidat1
,
candidat2
])
for
candidat2
in
listeCandidats
]
for
candidat1
in
listeCandidats
]
# Table des données.
table
=
pd
.
DataFrame
(
matCovVar
,
index
=
listeCandidats
,
columns
=
listeCandidats
)
print
(
listeIntentions
)
plt
.
title
(
'Matrice covariances-variances'
)
sn
.
heatmap
(
table
,
annot
=
True
,
fmt
=
'f'
)
plt
.
show
()
# Partie du code a exécuter au début du script.
...
...
@@ -185,21 +107,14 @@ if __name__ == '__main__':
analyseur
.
add_argument
(
'-v'
,
'--version'
,
action
=
'version'
,
version
=
'%(prog)s '
+
__version__
,
help
=
'Affiche la version du programme et quitte.'
)
analyseur
.
add_argument
(
'-m'
,
'--matrice'
,
type
=
str
,
help
=
'Nom du fichier où stocker la matrice.'
)
analyseur
.
add_argument
(
'nomFichierSondages'
,
help
=
'Nom du fichier contenant les sondages.'
)
# Arguments de la ligne de commande.
args
=
analyseur
.
parse_args
()
# Nom du fichier où enregistrer la matrice.
nomFichierMatrice
=
''
if
args
.
matrice
:
nomFichierMatrice
=
args
.
matrice
try
:
calculMatrice
(
args
.
nomFichierSondages
,
nomFichierMatrice
)
calculMatrice
(
args
.
nomFichierSondages
)
except
FichierInexistant
as
ex
:
print
(
'Pas de fichier nommé « %s ».'
%
(
ex
.
nomFichier
),
file
=
sys
.
stderr
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment