Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tp2_hachage
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Samuel Nguyen
tp2_hachage
Commits
cfa9c55a
Commit
cfa9c55a
authored
2 months ago
by
Samuel Nguyen
Browse files
Options
Downloads
Patches
Plain Diff
Update tp_2_miso_mphf.py
parent
3ec7c773
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tp_2_miso_mphf.py
+41
-15
41 additions, 15 deletions
tp_2_miso_mphf.py
with
41 additions
and
15 deletions
tp_2_miso_mphf.py
+
41
−
15
View file @
cfa9c55a
...
@@ -41,39 +41,59 @@ def construction_mphf(set_kmer, n, gamma=2, nb_niveaux=3):
...
@@ -41,39 +41,59 @@ def construction_mphf(set_kmer, n, gamma=2, nb_niveaux=3):
l
=
len
(
set_kmer_courant
)
l
=
len
(
set_kmer_courant
)
tableau_principal
=
[
-
1
]
*
(
gamma
*
l
)
tableau_principal
=
[
-
1
]
*
(
gamma
*
l
)
for
kmer
in
set_kmer_courant
:
for
kmer
in
set_kmer_courant
:
pass
# compléter
# hacher le k-mer (attention, hash() peut rendre des entiers signés, nous voulons des entiers positifs)
# hacher le k-mer (attention, hash() peut rendre des entiers signés, nous voulons des entiers positifs)
hachage
=
hash
(
kmer
)
if
hachage
<
0
:
hachage
=
-
1
*
hachage
# récupérer l'adresse
# récupérer l'adresse
# si le tableau principal est déjà rempli à l'adresse:
# si le tableau principal est déjà rempli à l'adresse:
# mettre le kmer dans collision()
# mettre le kmer dans collision()
#sinon, écrire le hash à l'adresse dans le tableau principal
# sinon, écrire le hash à l'adresse dans le tableau principal
adresse
=
hachage
%
(
gamma
*
l
)
if
tableau_principal
[
adresse
]
!=
-
1
:
collision
.
add
(
kmer
)
else
:
tableau_principal
[
adresse
]
=
hachage
tableaux
.
append
(
tableau_principal
)
# expliquer
tableaux
.
append
(
tableau_principal
)
# expliquer
# Chaque niveau de hachage produit un nouveau tableau de hachage.
# Donc on enregistre le tableau dans tableaux pour garder une trace des niveaux.
set_kmer_courant
=
collision
.
copy
()
# expliquer
set_kmer_courant
=
collision
.
copy
()
# expliquer
# On hache à nouveau les k-mers qui ont causé des collisions.
collision
=
set
()
# expliquer
collision
=
set
()
# expliquer
# On réinitialise collision pour le prochain niveau de hachage, pour pas que les valeurs dont on n'a plus besoin s'accumulent.
# Construction de la MPHF
# Construction de la MPHF
mphf
=
[]
mphf
=
[]
grand_tableau
=
[]
grand_tableau
=
[]
for
tableau
in
tableaux
:
for
tableau
in
tableaux
:
grand_tableau
.
extend
(
tableau
)
# expliquer
grand_tableau
.
extend
(
tableau
)
# expliquer
# Après avoir fait tous les hachages, on regroupe les tableaux finaux de hachage dans un seul tableau.
rangs
=
[]
rangs
=
[]
max_rang
=
0
max_rang
=
0
i
=
0
i
=
0
for
kmer
in
set_kmer
:
for
kmer
in
set_kmer
:
pass
# compléter:
# hacher le kmer
# hacher le kmer
hache
=
hash
(
kmer
)
# si le hash est dans le grand_tableau
# si le hash est dans le grand_tableau
# récupérer son index
# récupérer son index
# récupérer son rang (utiliser la fonction count())
# récupérer son rang (utiliser la fonction count())
# ajouter à la mphf [h, rang]
# ajouter à la mphf [h, rang]
# mettre à jour max_rang
# mettre à jour max_rang
if
hache
in
grand_tableau
:
index
=
grand_tableau
.
index
(
hache
)
rang
=
grand_tableau
[:
index
].
count
(
hache
)
mphf
.
append
([
hache
,
rang
])
if
rang
>
max_rang
:
max_rang
=
rang
for
kmer
in
set_kmer_courant
:
#gestion des collisions: expliquer les 3 lignes du dessous
for
kmer
in
set_kmer_courant
:
#gestion des collisions: expliquer les 3 lignes du dessous
max_rang
+=
1
max_rang
+=
1
# On attribue un nouveau rang unique à chaque élément en collision.
h
=
abs
(
hash
(
kmer
))
h
=
abs
(
hash
(
kmer
))
# Recalcul du hash, on veut uniquement des entiers positifs.
mphf
.
append
([
h
,
max_rang
])
mphf
.
append
([
h
,
max_rang
])
# On ajoute le k-mer à mphf
# On fait ces étapes car les collisions n'ont pas pu être placées avant,
# on leur attribue un rang plus grand pour ne pas perturber l'ordre précédent
return
mphf
return
mphf
...
@@ -98,7 +118,11 @@ def get_hash_mphf(mphf, kmer):
...
@@ -98,7 +118,11 @@ def get_hash_mphf(mphf, kmer):
>>>
0
<=
hash_mphf
<
n
>>>
0
<=
hash_mphf
<
n
True
True
"""
"""
pass
# TODO modifier
hache
=
abs
(
hash
(
kmer
))
for
h
,
rang
in
mphf
:
if
h
==
hache
:
return
rang
def
create_hash_table
(
set_kmer
,
n
):
def
create_hash_table
(
set_kmer
,
n
):
"""
"""
...
@@ -121,13 +145,15 @@ def create_hash_table(set_kmer, n):
...
@@ -121,13 +145,15 @@ def create_hash_table(set_kmer, n):
>>>
all
(
kmer
in
tableau
for
kmer
in
set_kmer
)
>>>
all
(
kmer
in
tableau
for
kmer
in
set_kmer
)
True
True
"""
"""
pass
# TODO modifier
mphf
=
construction_mphf
(
set_kmer
,
n
)
# créer la mphf pour les kmers
# créer la mphf pour les kmers
# initialiser un tableau de taille n (une liste)
# initialiser un tableau de taille n (une liste)
tableau
=
[
None
]
*
n
# écrire les kmers aux adresses du tableau données par la mphf
# écrire les kmers aux adresses du tableau données par la mphf
# retourner le tableau et la mphf
for
kmer
in
set_kmer
:
adresse
=
get_hash_mphf
(
mphf
,
kmer
)
if
0
<=
adresse
<
n
:
tableau
[
adresse
]
=
kmer
return
tableau
,
mphf
# retourner le tableau et la mphf
def
generer_kmers
(
n
,
k
):
def
generer_kmers
(
n
,
k
):
...
@@ -149,7 +175,7 @@ def compare_taille(n_max, fichier_sortie):
...
@@ -149,7 +175,7 @@ def compare_taille(n_max, fichier_sortie):
for
n
in
range
(
100
,
n_max
,
1000
):
for
n
in
range
(
100
,
n_max
,
1000
):
set_kmer
=
generer_kmers
(
n
,
k
)
set_kmer
=
generer_kmers
(
n
,
k
)
tableau
,
mphf
=
create_hash_table
(
set_kmer
,
n
)
tableau
,
mphf
=
create_hash_table
(
set_kmer
,
n
)
n_values
.
append
(
n
)
n_values
.
append
(
n
)
table_size
.
append
(
sys
.
getsizeof
(
tableau
)
+
sys
.
getsizeof
(
mphf
))
# pourquoi ici on ne mesure pas juste la taille en mémoire du tableau ?
table_size
.
append
(
sys
.
getsizeof
(
tableau
)
+
sys
.
getsizeof
(
mphf
))
# pourquoi ici on ne mesure pas juste la taille en mémoire du tableau ?
...
@@ -166,4 +192,4 @@ def compare_taille(n_max, fichier_sortie):
...
@@ -166,4 +192,4 @@ def compare_taille(n_max, fichier_sortie):
plt
.
close
()
plt
.
close
()
# dé-commenter quand vous êtes prêts, expliquer les résultats
# dé-commenter quand vous êtes prêts, expliquer les résultats
#
compare_taille(10000,"mphf.png")
compare_taille
(
10000
,
"
mphf.png
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment