def main(argv) :
    parser = argparse.ArgumentParser(description="utility to plot \
    the density of all persistence diagrams in a file.")
    parser.add_argument("-i", "--infile")    
    parser.add_argument("-d", "--degree", type=int)
    parser.add_argument("-l", "--label", help="Show only persistence diagrams of a particular label")
    args = parser.parse_args(argv[1:])
    if args.infile == None or not os.path.isfile(args.infile) :
        print "%s : --infile (%s) must specify file that exists" % \
            (argv[0], args.infile)
        sys.exit(0)

    persistence_json = load_data(args.infile, "persistence_diagrams", None, None, argv[0] + " : ")
    if persistence_json == None :
        print "Could not load --infile : %s" % (args.persistence_a,)
        exit()
    persistence = PD.fromJSONDict(persistence_json)
    labels = list(set([d.segment_info.max_label() for d in persistence.diagrams]))
    labels.sort()
    if args.label != None :
        diagrams = [d for d in persistence.diagrams if d.segment_info.max_label() == args.label]
        persistence.diagrams = diagrams
    else :
        print "Labels : %s" % labels

    try:
        app = App(0, 
                  persistence, 
                  args.degree)
        app.MainLoop()
    except KeyboardInterrupt:
        sys.exit(0)
 def __init__(self, argv):
     wx.Frame.__init__(self,None,-1,
                      'Segment Size',size=(550,350))
     parser = argparse.ArgumentParser(description="utility to plot multiple persistence diagrams")
     parser.add_argument('files', nargs="*")
     self.args = vars(parser.parse_args(argv[1:]))
     self.files = self.args['files']
     self.persistences = []
     for f in self.files :
         pf_json = load_data(f, 'persistence', None, None, None)
         if pf_json == None :
             print "Could not load persistence file : %s" % (f,)
             sys.exit(1)
         self.persistences.append(PD.fromJSONDict(pf_json))
     
     self.SetBackgroundColour(wx.NamedColour("WHITE"))
     self.displays = []
     self.sizer = wx.BoxSizer(wx.VERTICAL)
     for f in self.files :
         self.displays.append(dict([('figure', Figure())]))
         self.displays[-1]['axes'] = self.displays[-1]['figure'].add_subplot(111)
         self.displays[-1]['canvas'] = FigureCanvas(self, -1, self.displays[-1]['figure'])
         self.sizer.Add(NavigationToolbar2Wx(self.displays[-1]['canvas']), 1, wx.LEFT | wx.TOP | wx.GROW)
         self.sizer.Add(self.displays[-1]['canvas'], 8, wx.LEFT | wx.TOP | wx.GROW)
     self.SetSizer(self.sizer)
     self.Fit()
     self.background = self.displays[0]['axes'].figure.canvas.copy_from_bbox(self.displays[0]['axes'].bbox)
     self.colors = ['red', 'yellow', 'orange', 'blue', 'green', 'violet', 'black']
     self.Bind(wx.EVT_PAINT, self.OnPaint)
     self.Bind(wx.EVT_KEY_UP, self.KeyEvent)
     self.index = 0
     self.point_Refresh()
    def __init__(self, argv):
        wx.Frame.__init__(self, None, -1, 'Segment Size', size=(550, 350))
        parser = argparse.ArgumentParser(
            description="utility to plot multiple persistence diagrams")
        parser.add_argument('files', nargs="*")
        self.args = vars(parser.parse_args(argv[1:]))
        self.files = self.args['files']
        self.persistences = []
        for f in self.files:
            pf_json = load_data(f, 'persistence', None, None, None)
            if pf_json == None:
                print "Could not load persistence file : %s" % (f, )
                sys.exit(1)
            self.persistences.append(PD.fromJSONDict(pf_json))

        self.SetBackgroundColour(wx.NamedColour("WHITE"))
        self.displays = []
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        for f in self.files:
            self.displays.append(dict([('figure', Figure())]))
            self.displays[-1]['axes'] = self.displays[-1][
                'figure'].add_subplot(111)
            self.displays[-1]['canvas'] = FigureCanvas(
                self, -1, self.displays[-1]['figure'])
            self.sizer.Add(NavigationToolbar2Wx(self.displays[-1]['canvas']),
                           1, wx.LEFT | wx.TOP | wx.GROW)
            self.sizer.Add(self.displays[-1]['canvas'], 8,
                           wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()
        self.background = self.displays[0][
            'axes'].figure.canvas.copy_from_bbox(self.displays[0]['axes'].bbox)
        self.colors = [
            'red', 'yellow', 'orange', 'blue', 'green', 'violet', 'black'
        ]
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_KEY_UP, self.KeyEvent)
        self.index = 0
        self.point_Refresh()
Exemple #4
0
def main(argv):
    parser = argparse.ArgumentParser(description="utility to plot \
    the density of all persistence diagrams in a file.")
    parser.add_argument("-i", "--infile")
    parser.add_argument("-d", "--degree", type=int)
    parser.add_argument(
        "-l",
        "--label",
        help="Show only persistence diagrams of a particular label")
    args = parser.parse_args(argv[1:])
    if args.infile == None or not os.path.isfile(args.infile):
        print "%s : --infile (%s) must specify file that exists" % \
            (argv[0], args.infile)
        sys.exit(0)

    persistence_json = load_data(args.infile, "persistence_diagrams", None,
                                 None, argv[0] + " : ")
    if persistence_json == None:
        print "Could not load --infile : %s" % (args.persistence_a, )
        exit()
    persistence = PD.fromJSONDict(persistence_json)
    labels = list(
        set([d.segment_info.max_label() for d in persistence.diagrams]))
    labels.sort()
    if args.label != None:
        diagrams = [
            d for d in persistence.diagrams
            if d.segment_info.max_label() == args.label
        ]
        persistence.diagrams = diagrams
    else:
        print "Labels : %s" % labels

    try:
        app = App(0, persistence, args.degree)
        app.MainLoop()
    except KeyboardInterrupt:
        sys.exit(0)
def main(argv):
    current_dir = os.getcwd()
    parser = argparse.ArgumentParser(
        description="utility to plot distance persistence diagram")
    parser.add_argument("-s", "--segments")
    parser.add_argument("-P", "--persistence-diagrams")
    parser.add_argument("-d", "--distances")

    parser.add_argument(
        "-t",
        "--distance-type",
        default="bottleneck",
        help="Distance Measure to use, Bottleneck or Wasserstein")
    parser.add_argument("-p",
                        "--pool",
                        default=max(2,
                                    multiprocessing.cpu_count() - 4),
                        help="Threads of computation to use",
                        type=int)
    parser.add_argument("-m", "--max-simplices", default=1000000, type=int)
    parser.add_argument("-e", "--epsilon", type=float)

    args = parser.parse_args(argv[1:])

    if args.segments != None:
        segments = Segments.fromJSONDict(
            load_data(args.segments, "segments", None, None, argv[0] + ": "))
        segment_info = [SegmentInfo.fromJSONDict(s) for s in segments.segments]
    else:
        segments = None
    if args.persistence_diagrams != None:
        diagrams = PersistenceDiagrams.fromJSONDict(
            load_data(args.persistence_diagrams, "persistence diagrams", None,
                      None, argv[0] + ": "))
        segment_info = [d.segment_info for d in diagrams.diagrams]
    else:
        diagrams = None
    if args.distances != None:
        distances = Distances.fromJSONDict(
            load_data(args.distances, "distances", None, None, argv[0] + ": "))
        segment_info = distances.segment_info
    else:
        distances = None

    distance_array = multiprocessing.Array('d', [0.0] +
                                           [-1.0 for s in segment_info[1:]])
    segment_compare = multiprocessing.Value('i', 0)
    processes = []
    try:
        compute_thread = \
          multiprocessing.Process(target=compute,
                                  args=(args.distance_type, distance_array, segment_compare, args.pool-1,
                                        args.max_simplices, args.epsilon, segments, diagrams, distances))
        compute_thread.start()
        display_thread = \
          multiprocessing.Process(target=display,
                                  args=(args.distance_type, distance_array, segment_compare, segment_info))
        display_thread.start()
        processes.append(display_thread)
        display_thread.join()
        compute_thread.join()
    except KeyboardInterrupt:
        print "Caught cntl-c, shutting down..."
        exit(0)
def compute(distance_type,
            distance_array,
            segment_compare,
            pool,
            max_simplices,
            epsilon,
            segments=None,
            pds=None,
            ds=None):
    compute_pool = multiprocessing.Pool(pool)
    d_len = len(distance_array)
    d_rng = range(d_len)
    last = -1
    if pds == None and ds == None:
        persistence_diagrams = [None for x in segments.segments]
        print "Generating initial persistence diagram"
        persistence_diagrams[0] = PersistenceGenerator.process(
            (segments.segments[0], (max_simplices, epsilon)))
        diagram_generator = yieldPersistenceDiagramAndDistance(
            max_simplices, epsilon, persistence_diagrams[0], distance_type)
        results = compute_pool.imap(
            diagram_generator, itertools.izip(segments.segments[1:],
                                              d_rng[1:]))
        for (i, diagram, distance) in results:
            persistence_diagrams[i] = diagram
            distance_array[i] = distance

        config = segments.config
        config.max_simplices = max_simplices
        config.persistence_epsilon = epsilon
        diagrams = PersistenceDiagrams(config, persistence_diagrams)
        filename = PersistenceDiagrams.get_persistence_diagrams_filename(
            config)
        print "plot_persistence_distance.py: Writing %s" % (filename, )
        save_data(filename, diagrams.toJSONDict())
    elif pds != None:
        persistence_diagrams = pds.diagrams
        config = pds.config

    distances = [[None for y in d_rng] for x in d_rng]
    if ds == None:
        print "Computing Distance Array"
        distance_generator = yieldDistance(distance_type)
        results = compute_pool.imap(
            distance_generator,
            itertools.product(itertools.izip(persistence_diagrams, d_rng),
                              itertools.izip(persistence_diagrams, d_rng)),
            max(1, d_len**2 / (10 * pool)))
        for (i, j, distance) in results:
            distances[i][j] = Distance(None, distance, None, None)
            if segment_compare.value != last:
                last = segment_compare.value
                for k in d_rng:
                    distance_array[k] = distances[last][
                        k].mean if distances[last][k] != None else -1.0

        if distance_type == 'bottleneck':
            filename = BottleneckDistances.get_distances_filename(config)
        elif distance_type == 'wasserstein':
            filename = WassersteinDistances.get_distances_filename(config)

        print "plot_persistence_distance.py: Writing %s" % (filename, )
        save_data(
            filename,
            Distances(config, distances,
                      [d.segment_info
                       for d in persistence_diagrams]).toJSONDict())
    else:
        for i in d_rng:
            for j in d_rng:
                distances[i][j] = ds.distances[i][j]
        last = -1
    compute_pool.close()
    compute_pool.join()
    last = segment_compare.value
    for k in d_rng:
        distance_array[k] = distances[last][k].mean
    while True:
        if segment_compare.value != last:
            last = segment_compare.value
            for k in d_rng:
                distance_array[k] = distances[last][k].mean
        else:
            time.sleep(0.05)
def main(argv) :
    parser = argparse.ArgumentParser(description="utility to plot \
    data and dynamically generated persistence diagrams. Using \
    the persistence option uses precomputed persistence and ignores all \
    the other options.")
    parser.add_argument('-i', '--infile', help="Data to read")
    parser.add_argument('-m', '--max-simplices', default=2000000,
                        type=int, help="Maximum number of simplices for persistence \
                        generation")
    parser.add_argument('-I', '--data-index', help="Index of data field for data types that require it")
    parser.add_argument('-L', '--label-index', type=int, help="Index of label field for data types that require it")
    parser.add_argument('-s', '--segment-size', type=int, help="Segment size for data types that require it")
    parser.add_argument('-S', '--segment-stride', type=int, help="Segment stride for data types that require it")
    parser.add_argument('-w', '--window-size', help="Window size for \
    persistence generation. Integer is a literal window size, float \
    between 0 and 1 is a fraction of the total Segment size")
    parser.add_argument('-p', '--persistences', help="Precomputed persistence diagram")
    parser.add_argument('-t', '--data-type', default="UCRSegments", help="Data type of the segments in infile")
    args = parser.parse_args(argv[1:])
    if args.persistences != None :
        persistences_json = load_data(args.persistences, 'persistences', None, None, argv[0])
        if persistences_json == None :
            print "Could not load --persistences : %s" % (args.persistences,)
            exit()
        persistences = PD.fromJSONDict(persistences_json)
        full_config = copy(persistences.config)
        full_config.window_size = -1
        segments_module = importlib.import_module( 'persistence.' + persistences.config.data_type)    
        segments_class = getattr(segments_module, persistences.config.data_type) 

        full_data = segments_class(full_config)
        window_config = copy(persistences.config)
        windowed_data = segments_class(window_config)
    else :
        segments_module = importlib.import_module( 'persistence.' + args.data_type)    
        segments_class = getattr(segments_module, args.data_type) 
        full_config = Configuration.fromJSONDict(dict([ ("data_type", args.data_type),
                                                        ("data_file", args.infile),
                                                        ("label_index", 0),
                                                        ("max_simplices", args.max_simplices),
                                                        ("window_size", -1),
                                                        ("window_stride", 1)]))
        if full_config.data_file.find(":") != -1 :
            full_config.data_file = full_config.data_file.split(':')
        if args.segment_size != None :
            full_config.segment_size = args.segment_size
        if args.segment_stride != None :
            full_config.segment_stride = args.segment_stride
        if args.data_index != None :
            full_config.data_index = parse_index(args.data_index)[0]
        if args.label_index != None :
            full_config.label_index = args.label_index
        full_data = segments_class(full_config)
        window_size = float(args.window_size)
        if (window_size < 1.0) :
            window_size = int(window_size * full_data.config.window_size)
        else :
            window_size = int(args.window_size)
        window_config =  Configuration.fromJSONDict(dict([ ("data_type", args.data_type),
                                                           ("data_file", full_config.data_file),
                                                           ("label_index", 0),
                                                           ("max_simplices", args.max_simplices),
                                                           ("window_size", window_size),
                                                           ("window_stride", 1)]))
        if args.segment_size != None :
            window_config.segment_size = args.segment_size
        if args.segment_stride != None :
            window_config.segment_stride = args.segment_stride
        if args.data_index != None :
            window_config.data_index = parse_index(args.data_index)[0]
        if args.label_index != None :
            window_config.label_index = args.label_index
        windowed_data = segments_class(window_config)
        persistences = PD(windowed_data.config, [None for segment in windowed_data.segments])
    try:
        app = App(0, full_data, windowed_data, persistences)
        app.MainLoop()
    except KeyboardInterrupt:
        sys.exit(0)
Exemple #8
0
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.


import sys
import argparse
import importlib

from persistence.Datatypes.JSONObject import load_data, save_data
from persistence.Datatypes.PersistenceDiagrams import PersistenceDiagrams, PersistenceDiagram
from persistence.Datatypes.Configuration import Configuration
from persistence.Datatypes.Segments import SegmentInfo

if __name__ == "__main__" :
    parser = argparse.ArgumentParser(description="Utility to add SegmentInfo data to a PersistenceDiagrams file")
    parser.add_argument("--infile")
    parser.add_argument("--outfile")
    args = parser.parse_args(sys.argv[1:])
    in_json = load_data(args.infile, "persistence diagrams", None, None, sys.argv[0] + " : ")
    pd = PersistenceDiagrams.fromJSONDict(in_json)
    module = importlib.import_module('persistence.' + pd.config.data_type)
    module_class = getattr(module, pd.config.data_type)
    segment_filename = module_class.get_segment_filename(pd.config)
    seg_json = load_data(segment_filename, "segments", None, None, sys.argv[0] + " : ")
    
    for (diagram, segment) in zip(pd.diagrams, seg_json['segments']) :
        diagram.segment_info = SegmentInfo.fromJSONDict(segment)

    print "Writing %s" % (args.outfile,)
    save_data(args.outfile, pd.toJSONDict())
Exemple #9
0
        "python", "-u", "-O", "-m", "persistence.PartitionData", "--segments",
        segment_filename, "--learning-split",
        str(config.learning_split), "--learning-iterations",
        str(config.learning_iterations), "--cv-iterations", "5"
    ]
    subprocess.call(partition_command)
    partition_filename = TrainTestPartitions.get_partition_filename(config)

    #PersistenceDiagrams
    persistence_command = [
        "python", "-u", "-O", "-m", "persistence.PersistenceGenerator",
        "--pool",
        str(args.pool), "--infile", segment_filename
    ]
    subprocess.call(persistence_command)
    persistence_filename = PersistenceDiagrams.get_persistence_diagrams_filename(
        config)

    #PersistenceKernel
    persistence_kernel_command = [
        "python", "-u", "-O", "-m", "persistence.PersistenceKernel", "--pool",
        str(args.pool), "--infile", persistence_filename
    ]
    subprocess.call(persistence_kernel_command)
    persistence_kernel_filename = PersistenceKernel.get_kernel_filename(config)

    #ScaleSpaceSimilarities
    scale_space_command = [
        "python", "-u", "-O", "-m", "persistence.ScaleSpaceSimilarity",
        "--kernel-file", persistence_kernel_filename
    ]
    subprocess.call(scale_space_command)
Exemple #10
0
    samples = list(set([d['segment_start'] for d in output]))
    samples.sort()
    out_data = []
    for s in samples:
        sample_data = [d for d in output if d['segment_start'] == s]
        segment_sizes = list(set([x['segment_size'] for x in sample_data]))
        segment_sizes.sort()
        for size in segment_sizes:
            segment_data = [
                d for d in sample_data if d['segment_size'] == size
            ]
            w_distances = WassersteinDistances(
                None,
                PersistenceDiagrams(None, [
                    PersistenceDiagram.fromJSONDict(d['diagram'])
                    for d in segment_data
                ]),
                pool=None)
            distances = BottleneckDistances(
                None,
                PersistenceDiagrams(None, [
                    PersistenceDiagram.fromJSONDict(d['diagram'])
                    for d in segment_data
                ]),
                pool=None)
            out_data.append((segment_data, distances, w_distances))
    goal = len(out_data)
    computed = pool.imap(map_func, out_data)
    done = []

    for (c, i) in itertools.izip(computed, range(goal)):
    def __init__(self, data_file, persistence_file, kernel_file):
        wx.Frame.__init__(self,None,-1,
                         'Data Visualization',size=(550,350))
        self.segment_file = data_file
        segments_json = load_data(self.segment_file, 'segments', None, None, "explore_persistence: ")
        if segments_json == None:
            print "Could not load segment file : %s" % (self.segment_file,)
            exit()
        self.segments = Segments.fromJSONDict(segments_json)

        self.persistence_file = persistence_file
        persistence_json = load_data(self.persistence_file, 'persistence', None, None, "explore_persistence: ")
        if persistence_json == None :
            print "Could not load persistence file : %s" % (self.persistence_file,)
            exit()
        self.persistences = PD.fromJSONDict(persistence_json)

        self.kernel_file = kernel_file
        kernel_json = load_data(self.kernel_file, 'kernel', None, None, "explore_persistence: ")
        if kernel_json == None :
            print "Could not load kernel file : %s" % (self.kernel_file,)
            exit()
        self.kernel = Kernel.fromJSONDict(kernel_json)

        self.kernel_config = self.kernel.config

        self.spans = []
        self.similarities = []

        for segment in self.segments.segments :
            window_stride = segment.window_stride
            label = max_label(segment.labels)
            data = []
            # We need to account for data overlap in the windows, which is not useful for this visualization
            for window in segment.windows :
                data.extend(window[0:window_stride])
            data.extend(segment.windows[-1][window_stride:])
            self.spans.append((label, segment.segment_start, data))
        self.mins = None
        self.maxs = None
        for  (l, start, xs) in self.spans :
            for x in xs :
                if self.maxs == None or x > self.maxs :
                    self.maxs = x
                if self.mins == None or x < self.mins :
                    self.mins = x
        
        self.labels = set([span[0] for span in self.spans])
        
        self.index = 1

        self.SetBackgroundColour(wx.NamedColour("WHITE"))
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.title = self.figure.suptitle("Data for Column %s" % (self.index,))

        self.sub_figure_a = Figure()
        self.sub_axes_a = self.sub_figure_a.add_subplot(111)
        self.sub_canvas_a = FigureCanvas(self, -1, self.sub_figure_a)
        self.sub_title_a = self.sub_figure_a.suptitle("Data for Segment beginning at %s, label %s" % (" ", " "))

        self.sub_figure_ap = Figure()
        self.sub_axes_ap = self.sub_figure_ap.add_subplot(111)
        self.sub_canvas_ap = FigureCanvas(self, -1, self.sub_figure_ap)
        self.sub_title_ap = self.sub_figure_ap.suptitle("Persistence for Segment beginning at %s, label %s" % (" ", " "))

        self.sub_figure_b = Figure()
        self.sub_axes_b = self.sub_figure_b.add_subplot(111)
        self.sub_canvas_b = FigureCanvas(self, -1, self.sub_figure_b)
        self.sub_title_b = self.sub_figure_b.suptitle("Data for Segment beginning at %s, label %s" % (" ", " "))

        self.sub_figure_bp = Figure()
        self.sub_axes_bp = self.sub_figure_bp.add_subplot(111)
        self.sub_canvas_bp = FigureCanvas(self, -1, self.sub_figure_bp)
        self.sub_title_bp = self.sub_figure_bp.suptitle("Persistence for Segment beginning at %s, label %s" % (" ", " "))

        self.click_cid_down = self.canvas.mpl_connect('button_press_event', self.mouseDown)
        self.click_cid_up = self.canvas.mpl_connect('button_release_event', self.mouseUp)
        self.click_cid_move = self.canvas.mpl_connect('motion_notify_event', self.mouseMove)
        self.sizer = wx.GridBagSizer(hgap=5, vgap=5)
        self.sizer.Add(NavigationToolbar2Wx(self.canvas), pos=(0,0), span=(1,2), flag=wx.EXPAND)
        self.sizer.AddGrowableCol(1,0)
        self.sizer.Add(self.canvas, pos=(1,0), span=(8,2), flag=wx.EXPAND)
        self.sizer.AddGrowableCol(9,0)
        self.sizer.Add(self.sub_canvas_a, pos=(9,0), span=(4,1), flag=wx.EXPAND)
        self.sizer.Add(self.sub_canvas_b, pos=(9,1), span=(4,1), flag=wx.EXPAND)
        self.sizer.AddGrowableCol(13,0)
        self.sizer.Add(self.sub_canvas_ap, pos=(13,0), span=(4,1), flag=wx.EXPAND)
        self.sizer.Add(self.sub_canvas_bp, pos=(13,1), span=(4,1), flag=wx.EXPAND)
        
        self.SetSizer(self.sizer)

        self.caption = self.figure.text(0.15, 0.8, "%s Samples Read" % (\
                                        reduce((lambda x,y: x+y),[len(span[2]) for span in self.spans], 0)))
        self.caption.set_backgroundcolor('#ffffff')
        self.Fit()
        self.background = self.axes.figure.canvas.copy_from_bbox(self.axes.bbox)

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_KEY_UP, self.KeyEvent)
        self.click = 0
        self.index_a = None
        self.index_b = None
        self.point_Refresh()
        self.state = (None, 0)
def main(argv):
    parser = argparse.ArgumentParser(description="utility to plot \
    data and dynamically generated persistence diagrams. Using \
    the persistence option uses precomputed persistence and ignores all \
    the other options.")
    parser.add_argument('-i', '--infile', help="Data to read")
    parser.add_argument('-m',
                        '--max-simplices',
                        default=2000000,
                        type=int,
                        help="Maximum number of simplices for persistence \
                        generation")
    parser.add_argument(
        '-I',
        '--data-index',
        help="Index of data field for data types that require it")
    parser.add_argument(
        '-L',
        '--label-index',
        type=int,
        help="Index of label field for data types that require it")
    parser.add_argument('-s',
                        '--segment-size',
                        type=int,
                        help="Segment size for data types that require it")
    parser.add_argument('-S',
                        '--segment-stride',
                        type=int,
                        help="Segment stride for data types that require it")
    parser.add_argument('-w',
                        '--window-size',
                        help="Window size for \
    persistence generation. Integer is a literal window size, float \
    between 0 and 1 is a fraction of the total Segment size")
    parser.add_argument('-p',
                        '--persistences',
                        help="Precomputed persistence diagram")
    parser.add_argument('-t',
                        '--data-type',
                        default="UCRSegments",
                        help="Data type of the segments in infile")
    args = parser.parse_args(argv[1:])
    if args.persistences != None:
        persistences_json = load_data(args.persistences, 'persistences', None,
                                      None, argv[0])
        if persistences_json == None:
            print "Could not load --persistences : %s" % (args.persistences, )
            exit()
        persistences = PD.fromJSONDict(persistences_json)
        full_config = copy(persistences.config)
        full_config.window_size = -1
        segments_module = importlib.import_module(
            'persistence.' + persistences.config.data_type)
        segments_class = getattr(segments_module,
                                 persistences.config.data_type)

        full_data = segments_class(full_config)
        window_config = copy(persistences.config)
        windowed_data = segments_class(window_config)
    else:
        segments_module = importlib.import_module('persistence.' +
                                                  args.data_type)
        segments_class = getattr(segments_module, args.data_type)
        full_config = Configuration.fromJSONDict(
            dict([("data_type", args.data_type), ("data_file", args.infile),
                  ("label_index", 0), ("max_simplices", args.max_simplices),
                  ("window_size", -1), ("window_stride", 1)]))
        if full_config.data_file.find(":") != -1:
            full_config.data_file = full_config.data_file.split(':')
        if args.segment_size != None:
            full_config.segment_size = args.segment_size
        if args.segment_stride != None:
            full_config.segment_stride = args.segment_stride
        if args.data_index != None:
            full_config.data_index = parse_index(args.data_index)[0]
        if args.label_index != None:
            full_config.label_index = args.label_index
        full_data = segments_class(full_config)
        window_size = float(args.window_size)
        if (window_size < 1.0):
            window_size = int(window_size * full_data.config.window_size)
        else:
            window_size = int(args.window_size)
        window_config = Configuration.fromJSONDict(
            dict([("data_type", args.data_type),
                  ("data_file", full_config.data_file), ("label_index", 0),
                  ("max_simplices", args.max_simplices),
                  ("window_size", window_size), ("window_stride", 1)]))
        if args.segment_size != None:
            window_config.segment_size = args.segment_size
        if args.segment_stride != None:
            window_config.segment_stride = args.segment_stride
        if args.data_index != None:
            window_config.data_index = parse_index(args.data_index)[0]
        if args.label_index != None:
            window_config.label_index = args.label_index
        windowed_data = segments_class(window_config)
        persistences = PD(windowed_data.config,
                          [None for segment in windowed_data.segments])
    try:
        app = App(0, full_data, windowed_data, persistences)
        app.MainLoop()
    except KeyboardInterrupt:
        sys.exit(0)
    def __init__(self, data_file, persistence_file, kernel_file):
        wx.Frame.__init__(self,
                          None,
                          -1,
                          'Data Visualization',
                          size=(550, 350))
        self.segment_file = data_file
        segments_json = load_data(self.segment_file, 'segments', None, None,
                                  "explore_persistence: ")
        if segments_json == None:
            print "Could not load segment file : %s" % (self.segment_file, )
            exit()
        self.segments = Segments.fromJSONDict(segments_json)

        self.persistence_file = persistence_file
        persistence_json = load_data(self.persistence_file, 'persistence',
                                     None, None, "explore_persistence: ")
        if persistence_json == None:
            print "Could not load persistence file : %s" % (
                self.persistence_file, )
            exit()
        self.persistences = PD.fromJSONDict(persistence_json)

        self.kernel_file = kernel_file
        kernel_json = load_data(self.kernel_file, 'kernel', None, None,
                                "explore_persistence: ")
        if kernel_json == None:
            print "Could not load kernel file : %s" % (self.kernel_file, )
            exit()
        self.kernel = Kernel.fromJSONDict(kernel_json)

        self.kernel_config = self.kernel.config

        self.spans = []
        self.similarities = []

        for segment in self.segments.segments:
            window_stride = segment.window_stride
            label = max_label(segment.labels)
            data = []
            # We need to account for data overlap in the windows, which is not useful for this visualization
            for window in segment.windows:
                data.extend(window[0:window_stride])
            data.extend(segment.windows[-1][window_stride:])
            self.spans.append((label, segment.segment_start, data))
        self.mins = None
        self.maxs = None
        for (l, start, xs) in self.spans:
            for x in xs:
                if self.maxs == None or x > self.maxs:
                    self.maxs = x
                if self.mins == None or x < self.mins:
                    self.mins = x

        self.labels = set([span[0] for span in self.spans])

        self.index = 1

        self.SetBackgroundColour(wx.NamedColour("WHITE"))
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.title = self.figure.suptitle("Data for Column %s" %
                                          (self.index, ))

        self.sub_figure_a = Figure()
        self.sub_axes_a = self.sub_figure_a.add_subplot(111)
        self.sub_canvas_a = FigureCanvas(self, -1, self.sub_figure_a)
        self.sub_title_a = self.sub_figure_a.suptitle(
            "Data for Segment beginning at %s, label %s" % (" ", " "))

        self.sub_figure_ap = Figure()
        self.sub_axes_ap = self.sub_figure_ap.add_subplot(111)
        self.sub_canvas_ap = FigureCanvas(self, -1, self.sub_figure_ap)
        self.sub_title_ap = self.sub_figure_ap.suptitle(
            "Persistence for Segment beginning at %s, label %s" % (" ", " "))

        self.sub_figure_b = Figure()
        self.sub_axes_b = self.sub_figure_b.add_subplot(111)
        self.sub_canvas_b = FigureCanvas(self, -1, self.sub_figure_b)
        self.sub_title_b = self.sub_figure_b.suptitle(
            "Data for Segment beginning at %s, label %s" % (" ", " "))

        self.sub_figure_bp = Figure()
        self.sub_axes_bp = self.sub_figure_bp.add_subplot(111)
        self.sub_canvas_bp = FigureCanvas(self, -1, self.sub_figure_bp)
        self.sub_title_bp = self.sub_figure_bp.suptitle(
            "Persistence for Segment beginning at %s, label %s" % (" ", " "))

        self.click_cid_down = self.canvas.mpl_connect('button_press_event',
                                                      self.mouseDown)
        self.click_cid_up = self.canvas.mpl_connect('button_release_event',
                                                    self.mouseUp)
        self.click_cid_move = self.canvas.mpl_connect('motion_notify_event',
                                                      self.mouseMove)
        self.sizer = wx.GridBagSizer(hgap=5, vgap=5)
        self.sizer.Add(NavigationToolbar2Wx(self.canvas),
                       pos=(0, 0),
                       span=(1, 2),
                       flag=wx.EXPAND)
        self.sizer.AddGrowableCol(1, 0)
        self.sizer.Add(self.canvas, pos=(1, 0), span=(8, 2), flag=wx.EXPAND)
        self.sizer.AddGrowableCol(9, 0)
        self.sizer.Add(self.sub_canvas_a,
                       pos=(9, 0),
                       span=(4, 1),
                       flag=wx.EXPAND)
        self.sizer.Add(self.sub_canvas_b,
                       pos=(9, 1),
                       span=(4, 1),
                       flag=wx.EXPAND)
        self.sizer.AddGrowableCol(13, 0)
        self.sizer.Add(self.sub_canvas_ap,
                       pos=(13, 0),
                       span=(4, 1),
                       flag=wx.EXPAND)
        self.sizer.Add(self.sub_canvas_bp,
                       pos=(13, 1),
                       span=(4, 1),
                       flag=wx.EXPAND)

        self.SetSizer(self.sizer)

        self.caption = self.figure.text(0.15, 0.8, "%s Samples Read" % (\
                                        reduce((lambda x,y: x+y),[len(span[2]) for span in self.spans], 0)))
        self.caption.set_backgroundcolor('#ffffff')
        self.Fit()
        self.background = self.axes.figure.canvas.copy_from_bbox(
            self.axes.bbox)

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_KEY_UP, self.KeyEvent)
        self.click = 0
        self.index_a = None
        self.index_b = None
        self.point_Refresh()
        self.state = (None, 0)
Exemple #14
0
    #TrainTestSplit
    partition_command = ["python", "-u", "-O", "-m", "persistence.PartitionData", 
                         "--segments", segment_filename, 
                         "--learning-split", str(config.learning_split), 
                         "--learning-iterations", str(config.learning_iterations), 
                         "--cv-iterations", "5"]
    subprocess.call(partition_command)
    partition_filename = TrainTestPartitions.get_partition_filename(config)

    #PersistenceDiagrams
    persistence_command = ["python", "-u", "-O", "-m", "persistence.PersistenceGenerator", 
                           "--pool", str(args.pool),
                           "--infile", segment_filename]
    subprocess.call(persistence_command)
    persistence_filename = PersistenceDiagrams.get_persistence_diagrams_filename(config)

    #PersistenceKernel
    persistence_kernel_command = ["python", "-u", "-O", "-m", "persistence.PersistenceKernel", 
                                  "--pool", str(args.pool),
                                  "--infile", persistence_filename]
    subprocess.call(persistence_kernel_command)
    persistence_kernel_filename = PersistenceKernel.get_kernel_filename(config)
    
    #ScaleSpaceSimilarities
    scale_space_command = ["python", "-u", "-O", "-m", "persistence.ScaleSpaceSimilarity", 
                           "--kernel-file", persistence_kernel_filename]
    subprocess.call(scale_space_command)
    scale_space_filename = ScaleSpaceSimilarity.get_distances_filename(config)

    #ScaleSpaceSimilarityLearning
import os
import sys
import math
import itertools
import multiprocessing

from persistence.Datatypes.JSONObject import load_data, save_data
from persistence.Datatypes.PersistenceDiagrams import PersistenceDiagrams, PersistenceDiagram
def avg(l) :
    return sum(l,0.0) / len(l)
def average_density(diagram) :
    points = [(p[0], p[1]) for p in diagram.points if p[2] == 1]
    if len(points) > 2 :
        diagram_distances = []
        for (x0,y0) in points :
            distances = map(lambda (x1,y1) : math.sqrt((x0 - x1) * (x0 - x1) + (x0 - x1) * (x0 - x1)), points)
            diagram_distances.append(avg(distances[1:6]))
        return avg(diagram_distances)
    else :
        return 0.0



if __name__ == "__main__" :
    pool = multiprocessing.Pool(multiprocessing.cpu_count() - 2)
    for f in sys.argv[1:] :
        pds = PersistenceDiagrams.fromJSONDict(load_data(f, None, None, None, sys.argv[0] + " : "))
        densities = pool.map(average_density, pds.diagrams)
        save_data(f + "-density", list(densities))
Exemple #16
0
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import SymLogNorm
from persistence.Datatypes.JSONObject import load_data
from persistence.Datatypes.PersistenceDiagrams import PersistenceDiagrams, PersistenceDiagram
from scipy import stats

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--infile")
parser.add_argument("-d", "--degree", type=int)

parser.add_argument("-p", "--pool", default=multiprocessing.cpu_count(), type=int)
args = parser.parse_args(sys.argv[1:])

persistences = PersistenceDiagrams.fromJSONDict(load_data(args.infile, "persistences", None, None, sys.argv[0] + ": "))

labels = list(set([diagram.segment_info.max_label() for diagram in persistences.diagrams]))
labels.sort()

labels = dict([('1', 'Working at Computer'),
               ('2', 'Standing Up, Walking, Going Up\Down Stairs'),
               ('3', 'Standing'),
               ('4', 'Walking'),
               ('5', 'Going Up\Down Stairs'),
               ('6', 'Walking and Talking with Someone'),
               ('7', 'Talking while Standing')])
print "Labels: '%s'" % ("', '".join(labels.values()),)
xmin = []
xmax = []
ymin = []
Exemple #17
0
from persistence.Datatypes.JSONObject import load_data, save_data
from persistence.Datatypes.PersistenceDiagrams import PersistenceDiagrams, PersistenceDiagram


def avg(l):
    return sum(l, 0.0) / len(l)


def average_density(diagram):
    points = [(p[0], p[1]) for p in diagram.points if p[2] == 1]
    if len(points) > 2:
        diagram_distances = []
        for (x0, y0) in points:
            distances = map(
                lambda (x1, y1): math.sqrt((x0 - x1) * (x0 - x1) + (x0 - x1) *
                                           (x0 - x1)), points)
            diagram_distances.append(avg(distances[1:6]))
        return avg(diagram_distances)
    else:
        return 0.0


if __name__ == "__main__":
    pool = multiprocessing.Pool(multiprocessing.cpu_count() - 2)
    for f in sys.argv[1:]:
        pds = PersistenceDiagrams.fromJSONDict(
            load_data(f, None, None, None, sys.argv[0] + " : "))
        densities = pool.map(average_density, pds.diagrams)
        save_data(f + "-density", list(densities))