Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions pyknotid/representations/gausscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import numpy as n
import re
import sys
import logging


class GaussCode(object):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -429,4 +425,3 @@ def _get_crossing_numbers(gc):
for entry in line:
crossing_vals.add(entry[0])
return crossing_vals

23 changes: 8 additions & 15 deletions pyknotid/spacecurves/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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))

Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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))
Expand All @@ -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`
Expand Down Expand Up @@ -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)


36 changes: 12 additions & 24 deletions pyknotid/spacecurves/spacecurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

'''

import logging
import numpy as n
import numpy as np
import sys

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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.)
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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]
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
'''
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand All @@ -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):
'''
Expand Down Expand Up @@ -1159,6 +1150,3 @@ def interpolate(self, num_points, s=0, **kwargs):
new_points[:, 2] = points[2]

self.points = new_points



8 changes: 4 additions & 4 deletions pyknotid/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import sys
import numpy as n
import logging

def vprint(string='', newline=True, condition=True):
'''
Expand All @@ -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):
'''
Expand All @@ -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
Expand Down