Skip to content

Commit

Permalink
gwsumm.plot: use common hash function
Browse files Browse the repository at this point in the history
for generating plot name hashes
  • Loading branch information
duncanmmacleod committed Mar 19, 2019
1 parent 8c208e1 commit ae6f30d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
4 changes: 1 addition & 3 deletions gwsumm/plot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
for GWSumm
"""

import hashlib
import os.path
import re
import warnings
Expand Down Expand Up @@ -338,8 +337,7 @@ def pid(self):
filts = "".join(map(str, [
getattr(c, 'filter', getattr(c, 'frequency_response', ''))
for c in self.channels]))
h = (chans + filts).encode('utf-8')
self._pid = hashlib.md5(h).hexdigest()[:6]
self._pid = putils.hash(chans + filts)
return self.pid

@pid.setter
Expand Down
5 changes: 2 additions & 3 deletions gwsumm/plot/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from __future__ import division

import re
import hashlib
from math import pi

from six import string_types
Expand All @@ -35,6 +34,7 @@
from gwpy.timeseries import TimeSeries

from .registry import (get_plot, register_plot)
from .utils import hash
from ..data import (get_range_channel, get_range, get_timeseries)
from ..segments import get_segments
from ..channels import split as split_channels
Expand Down Expand Up @@ -146,8 +146,7 @@ def pid(self):
try:
return self._pid
except:
chans = "".join(map(str, self.channels+self.flags))
self._pid = hashlib.md5(chans).hexdigest()[:6]
self._pid = hash("".join(map(str, self.channels+self.flags)))
return self.pid

@pid.setter
Expand Down
19 changes: 8 additions & 11 deletions gwsumm/plot/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from __future__ import division

import hashlib
import bisect
from itertools import (cycle, combinations)
from numbers import Number
Expand Down Expand Up @@ -61,7 +60,7 @@
from .core import (BarPlot, PiePlot, format_label)
from .registry import (get_plot, register_plot)
from .mixins import SegmentLabelSvgMixin
from .utils import usetex_tex
from .utils import (hash, usetex_tex)

__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'

Expand Down Expand Up @@ -191,8 +190,7 @@ def pid(self):
try:
return self._pid
except AttributeError:
self._pid = hashlib.md5(
"".join(map(str, self.flags))).hexdigest()[:6]
self._pid = hash("".join(map(str, self.flags)))
return self.pid

@pid.setter
Expand Down Expand Up @@ -406,11 +404,10 @@ def pid(self):
try:
return self._pid
except:
chans = "".join(map(str, self.channels))
self._pid = hashlib.md5(chans).hexdigest()[:6]
basis = "".join(map(str, self.channels))
if self.pargs.get('bits', None):
self._pid = hashlib.md5(
self._pid + str(self.pargs['bits'])).hexdigest()[:6]
basis += str(self.pargs['bits'])
self._pid = hash(basis)
return self.pid

def _parse_labels(self, defaults=[]):
Expand Down Expand Up @@ -838,10 +835,10 @@ def pid(self):
except:
chans = "".join(map(str, self.channels))
masks = "".join(map(str, self.get_bitmask_channels()))
self._pid = hashlib.md5(chans+masks).hexdigest()[:6]
basis = chans + masks
if self.pargs.get('bits', None):
self._pid = hashlib.md5(
self._pid + str(self.pargs['bits'])).hexdigest()[:6]
basis += str(self.pargs["bits"])
self._pid = hash(basis)
return self.pid

def draw(self):
Expand Down
7 changes: 3 additions & 4 deletions gwsumm/plot/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from __future__ import division

import hashlib
import re
from collections import OrderedDict
from itertools import cycle
Expand All @@ -42,7 +41,7 @@
from ..data import (get_channel, get_timeseries, add_timeseries)
from ..triggers import (get_triggers, get_time_column)
from .registry import (get_plot, register_plot)
from .utils import (get_column_string, usetex_tex)
from .utils import (get_column_string, hash, usetex_tex)

__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'

Expand Down Expand Up @@ -80,8 +79,8 @@ def pid(self):
getattr(c, 'filter', getattr(c, 'frequency_response', ''))
for c in self.channels]))
if self.filterstr:
filts = "".join([filts, self.filterstr])
self._pid = hashlib.md5(chans+filts).hexdigest()[:6]
filts += self.filterstr
self._pid = hash(chans + filts)
return self.pid


Expand Down
26 changes: 26 additions & 0 deletions gwsumm/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"""Utilies for GWSumm plotting
"""

import hashlib
import itertools
import re

Expand Down Expand Up @@ -151,3 +152,28 @@ def usetex_tex(text):
if rcParams['text.usetex']:
return label_to_latex(text)
return text


def hash(string, num=6):
"""Generate an N-character hash string based using string to initialise
Parameters
----------
string : `str`
the initialisation string
num : `int`, optional
the length of the hash to produce
Returns
-------
hash : `str`
the new hash
Examples
--------
>>> from gwsumm.plot.utils import hash
>>> print(hash("I love gravitational waves"))
80c897
"""
return hashlib.md5(string.encode("utf-8")).hexdigest()[:num]

0 comments on commit ae6f30d

Please sign in to comment.