From fdccf0390589931bc4606689692c5202b29a9af4 Mon Sep 17 00:00:00 2001 From: Geoffrey Irons Date: Fri, 10 Nov 2017 15:57:04 +0100 Subject: [PATCH] Replaced several prints with logging statements --- pyknotid/representations/gausscode.py | 13 +++------- pyknotid/spacecurves/link.py | 23 ++++++----------- pyknotid/spacecurves/spacecurve.py | 36 +++++++++------------------ pyknotid/utils.py | 8 +++--- 4 files changed, 28 insertions(+), 52 deletions(-) diff --git a/pyknotid/representations/gausscode.py b/pyknotid/representations/gausscode.py index 6f5d60c..ab7abd5 100644 --- a/pyknotid/representations/gausscode.py +++ b/pyknotid/representations/gausscode.py @@ -15,6 +15,7 @@ import numpy as n import re import sys +import logging class GaussCode(object): @@ -101,7 +102,7 @@ def calculating_orientations(cls, code): from pyknotid.representations import representation rep = representation.Representation(gc) # rep.draw_planar_graph() - # print('ready to do space curve') + # logging.info('ready to do space curve') sp = rep.space_curve() # sp.rotate() return sp.gauss_code() @@ -271,7 +272,6 @@ def _do_reidemeister_moves(self, one=True, two=True, one_extended=True): # Next do RM1_extended separately (could be more efficient?) if one_extended: # Do extended RM1 as a separate step - print code = [(line[keep] if len(line) > 0 else line) for (line, keep) in zip(code, keeps)] keeps = [n.ones(l.shape[0], dtype=bool) for l in code] @@ -365,7 +365,7 @@ def simplify(self, one=True, two=True, one_extended=True): ''' if self.verbose: - print('Simplifying: initially {} crossings'.format( + logging.info('Simplifying: initially {} crossings'.format( n.sum([len(line) for line in self._gauss_code]))) number_of_runs = 0 @@ -377,15 +377,11 @@ def simplify(self, one=True, two=True, one_extended=True): new_len = n.sum([len(line) for line in new_gc]) number_of_runs += 1 if self.verbose: - sys.stdout.write('\r-> {} crossings after {} runs'.format( + logging.info('\r-> {} crossings after {} runs'.format( n.sum([len(line) for line in new_gc]), number_of_runs)) - sys.stdout.flush() if new_len == original_len: break - if self.verbose: - print() - def reindex_crossings(self): '''Replaces the indices of the crossings in the Gauss code with the integers from 1 to its length. @@ -429,4 +425,3 @@ def _get_crossing_numbers(gc): for entry in line: crossing_vals.add(entry[0]) return crossing_vals - diff --git a/pyknotid/spacecurves/link.py b/pyknotid/spacecurves/link.py index 563b749..21fa76c 100644 --- a/pyknotid/spacecurves/link.py +++ b/pyknotid/spacecurves/link.py @@ -25,9 +25,10 @@ from pyknotid.spacecurves import helpers from pyknotid.spacecurves.knot import Knot from pyknotid.visualise import plot_projection -from pyknotid.utils import (vprint, get_rotation_matrix, +from pyknotid.utils import (get_rotation_matrix, ensure_shape_tuple) +import logging class Link(object): ''' @@ -169,7 +170,7 @@ def raw_crossings(self, mode='use_max_jump', only_with_other_lines=True, if only_with_other_lines: crossings = [[] for _ in lines] else: - self._vprint('Calculating self-crossings for all {} ' + logging.info('Calculating self-crossings for all {} ' 'component lines'.format(len(lines))) crossings = [k.raw_crossings( mode=mode, include_closure=include_closures, @@ -197,7 +198,7 @@ def raw_crossings(self, mode='use_max_jump', only_with_other_lines=True, for line_index, line in enumerate(lines): for other_index, other_line in enumerate(lines[line_index+1:]): - self._vprint( + logging.info( '\rComparing line {} with {}'.format(line_index, other_index)) @@ -219,7 +220,7 @@ def raw_crossings(self, mode='use_max_jump', only_with_other_lines=True, for i in first_line_range: if i % 1000 == 0: - self._vprint( + logging.info( '\ri = {} / {}'.format( i, len(first_line_range)), False) v0 = points[i] @@ -246,7 +247,7 @@ def raw_crossings(self, mode='use_max_jump', only_with_other_lines=True, crossings[line_index].extend(first_crossings.tolist()) crossings[other_index].extend(sec_crossings.tolist()) - self._vprint('\n{} crossings found\n'.format( + logging.info('\n{} crossings found\n'.format( [len(cs) / 2 for cs in crossings])) [cs.sort(key=lambda s: s[0]) for cs in crossings] crossings = [n.array(cs) for cs in crossings] @@ -386,7 +387,7 @@ def octree_simplify(self, runs=1, plot=False, rotate=True, line.points = remove_nearby_points(line.points) for i in range(runs): if n.sum([len(knot.points) for knot in self.lines]) > 30: - vprint('\rRun {} of {}, {} points remain'.format( + logging.info('\rRun {} of {}, {} points remain'.format( i, runs, len(self)), False, self.verbose) if rotate: @@ -409,7 +410,7 @@ def octree_simplify(self, runs=1, plot=False, rotate=True, if plot: self.plot() - vprint('\nReduced to {} points'.format(len(self))) + logging.info('\nReduced to {} points'.format(len(self))) def __len__(self): return n.sum(map(len, self.lines)) @@ -426,12 +427,6 @@ def arclength(self, include_closures=True): ''' return n.sum(k.arclength(include_closures) for k in self.lines) - def _vprint(self, s, newline=True): - '''Prints s, with optional newline. Intended for internal use - in displaying progress.''' - if self.verbose: - vprint(s, newline) - def gauss_code(self, **kwargs): ''' Returns a :class:`~pyknotid.representations.gausscode.GaussCode` @@ -487,5 +482,3 @@ def multivariate_alexander(self, variables=-1., **kwargs): gc = self.gauss_code(**kwargs) gc.simplify(verbose=self.verbose) return multivariate_alexander(gc, variables) - - diff --git a/pyknotid/spacecurves/spacecurve.py b/pyknotid/spacecurves/spacecurve.py index 0d7a2fc..632077a 100644 --- a/pyknotid/spacecurves/spacecurve.py +++ b/pyknotid/spacecurves/spacecurve.py @@ -19,6 +19,7 @@ ''' +import logging import numpy as n import numpy as np import sys @@ -26,7 +27,7 @@ try: from pyknotid.spacecurves import chelpers except ImportError: - print('Could not import cythonised chelpers, using Python ' + logging.warning('Could not import cythonised chelpers, using Python ' 'alternative. This will ' 'give the same result, but is slower.') from pyknotid.spacecurves import helpers as chelpers @@ -133,15 +134,6 @@ def roll(self, distance, fix_endpoints=True): points = np.vstack([points, points[:1]]) self.points = points - def _vprint(self, s, newline=True): - '''Prints s, with optional newline. Intended for internal use - in displaying progress.''' - if self.verbose: - sys.stdout.write(s) - if newline: - sys.stdout.write('\n') - sys.stdout.flush() - def _add_closure(self): closing_distance = mag(self.points[-1] - self.points[0]) if closing_distance > 0.02: @@ -348,8 +340,8 @@ def from_braid_word(cls, word): for i in range(num_strands): cur_strand = strands_by_initial_index[index] line.extend(cur_strand) - print('index is', index) - print('cur strand is', cur_strand) + logging.info('index is' + str(index)) + logging.info('cur strand is' + str(cur_strand)) index = int(np.round(cur_strand[-1][0])) - 1 k = cls(n.array(line)*5.) @@ -416,7 +408,7 @@ def cuaps(self, include_closure=True): tangents = np.roll(points, -1, axis=0) - points angles = np.arctan2(tangents[:, 1], tangents[:, 0]) - print('angles', angles) + logging.info('angles' + str(angles)) cuaps = [] @@ -505,7 +497,7 @@ def raw_crossings(self, mode='use_max_jump', include_closure=True, else: helpers_module = helpers - self._vprint('Finding crossings') + logging.info('Finding crossings') points = self.points segment_lengths = n.roll(points[:, :2], -1, axis=0) - points[:, :2] @@ -526,8 +518,7 @@ def raw_crossings(self, mode='use_max_jump', include_closure=True, for i in range(len(points)-2): if self.verbose: if i % 100 == 0: - self._vprint('\ri = {} / {}'.format(i, numtries), - False) + logging.info('\ri = {} / {}'.format(i, numtries)) v0 = points[i] dv = points[(i+1) % len(points)] - v0 @@ -557,7 +548,7 @@ def raw_crossings(self, mode='use_max_jump', include_closure=True, max_segment_length, jump_mode)) - self._vprint('\n{} crossings found\n'.format(len(crossings) / 2)) + logging.info('\n{} crossings found\n'.format(len(crossings) / 2)) crossings.sort(key=lambda s: s[0]) crossings = n.array(crossings) self._crossings = crossings @@ -714,7 +705,7 @@ def representation(self, recalculate=False, **kwargs): gc = Representation(crossings, verbose=self.verbose) self._representation = gc return gc - + def planar_diagram(self, **kwargs): ''' @@ -840,7 +831,7 @@ def from_csv(cls, filen, **kwargs): ''' Loads knot points from the given csv file, and returns a :class:`SpaceCurve` with those points. - + Arguments are passed straight to :func:`pyknot.io.from_csv`. ''' return cls(from_csv(filen, **kwargs)) @@ -888,7 +879,7 @@ def octree_simplify(self, runs=1, plot=False, rotate=True, from pyknotid.simplify.octree import OctreeCell for i in range(runs): if len(self.points) > 30: - self._vprint('\rRun {} of {}, {} points remain'.format( + logging.info('\rRun {} of {}, {} points remain'.format( i, runs, len(self.points))) if rotate: @@ -906,7 +897,7 @@ def octree_simplify(self, runs=1, plot=False, rotate=True, if plot: self.plot() - self._vprint('\nReduced to {} points'.format(len(self.points))) + logging.info('\nReduced to {} points'.format(len(self.points))) def arclength(self, include_closure=True): ''' @@ -1159,6 +1150,3 @@ def interpolate(self, num_points, s=0, **kwargs): new_points[:, 2] = points[2] self.points = new_points - - - diff --git a/pyknotid/utils.py b/pyknotid/utils.py index b003af2..ac2e429 100644 --- a/pyknotid/utils.py +++ b/pyknotid/utils.py @@ -5,6 +5,7 @@ import sys import numpy as n +import logging def vprint(string='', newline=True, condition=True): ''' @@ -24,10 +25,9 @@ def vprint(string='', newline=True, condition=True): ''' if not condition: return - sys.stdout.write(string) if newline: - sys.stdout.write('\n') - sys.stdout.flush() + string += '\n' + logging.info(string) def mag(v): ''' @@ -51,7 +51,7 @@ def get_rotation_matrix(angles): angles : iterable The psi, theta and phi angles ''' - + phi, theta, psi = angles sin = n.sin cos = n.cos