Skip to content

Commit

Permalink
add check for E221,E222,E227,E228 to the linter and extend it to dev/ (
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton authored Sep 10, 2023
1 parent 572c71e commit 4d0e825
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 116 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ jobs:
- name: Run pycodestyle
shell: bash -l {0}
# We currently only check for some warnings. We should enable & fix more of them.
run: pycodestyle --select=E111,E225,E302,E306,E401,E701,E702,E703,E704,W391,W605,E711,E713,E721 spherogram_src/
run: pycodestyle --select=E111,E221,E222,E225,E227,E228,E302,E306,E401,E701,E702,E703,E704,W391,W605,E711,E713,E721 spherogram_src/
run: pycodestyle --select=E111,E221,E222,E225,E227,E228,E306,E401,E701,E702,E703,E704,W391,W605,E711,E713,E721 dev/
- name: Run cython-lint
shell: bash -l {0}
run: cython-lint .
Expand Down
45 changes: 22 additions & 23 deletions dev/DTcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def next(self):

__next__ = next


class DTFatEdge(FatEdge):
"""
A fat edge which can be marked and belongs to a link component.
Expand All @@ -161,17 +162,17 @@ def PD_index(self):
This method returns the edge label.
"""
v = self[0]
if self.slot(v)%2 == 0:
if self.slot(v) % 2 == 0:
return v[0]
else:
return v[1]
return v[1]


class DTFatGraph(FatGraph):
edge_class = DTFatEdge

def __init__(self, pairs=[], singles=[]):
FatGraph.__init__(self, pairs, singles)
self.marked_valences = dict( (v,0) for v in self.vertices )
self.marked_valences = {v: 0 for v in self.vertices}
self.stack = []
self.pushed = False

Expand Down Expand Up @@ -290,7 +291,7 @@ def marked_arc(self, vertex):
raise ValueError('Marked graph is a circle')
edges = [e for e in self(V) if e.marked and e != edge]
if len(edges) == 0:
raise ValueError('Marked graph has a dead end at %s.'%V)
raise ValueError('Marked graph has a dead end at %s.' % V)
if len(edges) > 1:
break
else:
Expand Down Expand Up @@ -402,7 +403,6 @@ def _boundary_slots(self, edge, side):
"""
if not edge.marked:
raise ValueError('Must begin at a marked edge.')
result = set()
first_vertex = vertex = edge[1]
while True:
end = 0 if edge[0] is vertex else 1
Expand All @@ -412,7 +412,7 @@ def _boundary_slots(self, edge, side):
interior_edge = self(vertex)[slot]
if not interior_edge.marked:
# For lookups, slot values must be in 0,1,2,3
yield vertex, slot%4
yield vertex, slot % 4
else:
break
if k == 0:
Expand Down Expand Up @@ -441,9 +441,8 @@ def flip(self, vertex):
Move the edge at the North slot to the South slot, and
move the edge in the South slot to the North slot.
"""
#print 'flipping %s'%vertex
if self.marked_valences[vertex] > 2:
msg = 'Cannot flip %s with marked valence %d.'%(
msg = 'Cannot flip %s with marked valence %d.' % (
vertex, self.marked_valences[vertex])
raise FlippingError(msg)
self.reorder(vertex, (North, East, South, West))
Expand All @@ -462,7 +461,7 @@ def flipped(self, vertex):
Has this vertex been flipped?
"""
return bool(len([e for e in self(vertex)
if e[1] is vertex and e.slots[1] in (2,3)])%2)
if e[1] is vertex and e.slots[1] in (2, 3)]) % 2)

def sign(self, vertex):
"""
Expand Down Expand Up @@ -507,14 +506,14 @@ def KLP_dict(self, vertex, indices):
edges = self(vertex)
neighbors = self[vertex]
strands = [self.KLP_strand(vertex, edge) for edge in edges]
ids = [ indices[v] for v in neighbors ]
ids = [indices[v] for v in neighbors]

KLP['sign'] = 'R' if self.sign(vertex) == 1 else 'L'
slot = 1 if flipped else 0
KLP['Xbackward_neighbor'] = ids[slot]
KLP['Xbackward_strand'] = strands[slot]
slot = 3 if flipped else 2
KLP['Xforward_neighbor'] = ids[slot]
KLP['Xforward_neighbor'] = ids[slot]
KLP['Xforward_strand'] = strands[slot]
KLP['Xcomponent'] = edges[slot].component
slot = 2 if flipped else 1
Expand Down Expand Up @@ -568,7 +567,7 @@ def decode(self, dt, flips=None):
if dt[:2] == '0x':
dt_bytes = [int(dt[n:n+2], 16) for n in range(2,len(dt),2)]
self.code, self.flips = self.unpack_signed_DT(dt_bytes)
elif ord(dt[-1]) & 1<<7:
elif ord(dt[-1]) & 1 << 7:
dt_bytes = bytearray(dt)
self.code, self.flips = self.unpack_signed_DT(dt)
else:
Expand Down Expand Up @@ -628,12 +627,12 @@ def unpack_signed_DT(self, signed_dt):
component = []
flips = []
for byte in bytearray(signed_dt):
flips.append(bool(byte & 1<<6))
label = (1 + byte & 0x1f)<<1
if byte & 1<<5:
flips.append(bool(byte & 1 << 6))
label = (1 + byte & 0x1f) << 1
if byte & 1 << 5:
label = -label
component.append(label)
if byte & 1<<7:
if byte & 1 << 7:
dt.append(tuple(component))
component = []
return dt, flips
Expand All @@ -642,7 +641,7 @@ def convert_alpha(self, code):
code = string_to_ints(code)
num_crossings, components = code[:2]
comp_lengths = code[2:2+components]
crossings = [x<<1 for x in code[2+components:]]
crossings = [x << 1 for x in code[2+components:]]
assert len(crossings) == num_crossings
return partition_list(crossings, comp_lengths)

Expand Down Expand Up @@ -810,20 +809,20 @@ def signed_DT(self):
for component in self.code:
for label in component:
byte = abs(label)
byte = (byte>>1) - 1
byte = (byte >> 1) - 1
if label < 0:
byte |= 1<<5
byte |= 1 << 5
if next_flip():
byte |= 1<<6
byte |= 1 << 6
code_bytes.append(byte)
code_bytes[-1] |= 1<<7
code_bytes[-1] |= 1 << 7
return bytes(code_bytes)

def hex_signed_DT(self):
"""
Return the hex encoding of the signed DT byte sequence.
"""
return '0x'+''.join('%.2x'%b for b in bytearray(self.signed_DT()))
return '0x' + ''.join('%.2x' % b for b in bytearray(self.signed_DT()))

def PD(self, KnotTheory=False):
G = self.fat_graph
Expand Down
31 changes: 16 additions & 15 deletions dev/dev_malik/mutation/tangle_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def add_random_crossing(self,label):
new_strand = randint(0,3)
old_crossing[old_strand] = new_crossing[new_strand]
for i in range(1,4):
adj.insert(old_position,(new_crossing,(new_strand-i)%4))
adj.insert(old_position,(new_crossing,(new_strand-i) % 4))
adj[len(adj)/2:] = reversed(adj[len(adj)/2:])
tangle_copy.crossings.append(new_crossing)
tangle_copy.n = self.n+1
Expand Down Expand Up @@ -299,13 +299,13 @@ def _is_injection(pairs):
a strand on the boundary of a tangle and moving to the other
end of that strand.
"""
def cross_strand(self,i):
def cross_strand(self, i):
if i >= 2*self.n:
raise Exception("Not a valid start position for strand")
cs = self.adjacent[i]
strand = [cs]
while (cs[0],(cs[1]+2)%4) not in self.adjacent:
cs = cs[0].adjacent[(cs[1]+2)%4]
while (cs[0], (cs[1] + 2) % 4) not in self.adjacent:
cs = cs[0].adjacent[(cs[1] + 2) % 4]
strand.append(cs)
return strand

Expand All @@ -314,11 +314,11 @@ def cross_strand(self,i):
"""
def loop_strand(cs):
strand = [cs]
cs = cs[0].adjacent[(cs[1]+2)%4]
cs = cs[0].adjacent[(cs[1] + 2) % 4]
while cs not in strand:
# print(strand)
strand.append(cs)
cs = cs[0].adjacent[(cs[1]+2)%4]
cs = cs[0].adjacent[(cs[1] + 2) % 4]
return strand

"""
Expand All @@ -337,7 +337,7 @@ def all_cross_strands(self):
if i not in other_ends_seen:
strand = self.cross_strand(i)
cs = strand[-1]
end = self.adjacent.index((cs[0],(cs[1]+2)%4))
end = self.adjacent.index((cs[0],(cs[1]+2) % 4))
if end not in other_ends_seen:
strands.append(strand)
strands_with_ends.append((strand,end))
Expand All @@ -350,7 +350,7 @@ def all_cross_strands(self):
for strand in strands:
for cs in strand:
if cs[0] in seen_once:
loop = loop_strand((cs[0],(cs[1]+1)%4))
loop = loop_strand((cs[0],(cs[1]+1) % 4))
loops.append(loop)
cs_seen.extend(loop)
for loop_cs in loop:
Expand All @@ -370,14 +370,14 @@ def all_cross_strands(self):
for loop in loops:
for cs in loop:
if cs[0] in seen_once:
loop = loop_strand((cs[0],(cs[1]+1)%4))
loop = loop_strand((cs[0],(cs[1]+1) % 4))
loops.append(loop)
cs_seen.extend(loop)
for loop_cs in loop:
if loop_cs[0] in seen_once:
for seen_cs in cs_seen:
if loop_cs[0] == seen_cs[0]:
orientation = (loop_cs[1]-seen_cs[1])%4
orientation = (loop_cs[1]-seen_cs[1]) % 4
if orientation == 3:
orientation = -1
orientations[loop_cs[0]] = orientation
Expand Down Expand Up @@ -413,9 +413,10 @@ def cycle_basis(G):
vert_cycles = nx.cycle_basis(Gx)
return [edge_cycle(vert_cycle,G) for vert_cycle in vert_cycles]


def is_trivial(four_cycle):
crossings = map(lambda x: map(lambda y: y.crossing,x.interface), four_cycle)
return len(set(crossings[0])&set(crossings[1])&set(crossings[2])&set(crossings[3])) != 0
return bool(len(set(crossings[0]) & set(crossings[1]) & set(crossings[2]) & set(crossings[3])))


def all_four_cycles_at_vertex(G, start_vertex):
Expand Down Expand Up @@ -813,12 +814,12 @@ def tangle_cut(link, cycle):
clear_orientations(crossings0)
clear_orientations(crossings1)

#One of the tangles is the 'outside', and needs to be flipped
#Just check side0
# One of the tangles is the 'outside', and needs to be flipped
# Just check side0
side0_needs_flip = False
c,i = side0[0]
c, i = side0[0]
while True:
next_cep = c.crossing_strands()[(i+1)%4]
next_cep = c.crossing_strands()[(i+1) % 4]
c,i = next_cep.crossing,next_cep.strand_index
# print(c,i)
# print(side0)
Expand Down
16 changes: 9 additions & 7 deletions dev/dev_malik/petal_diagram/random_petaluma.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ def petaluma_knot(height_perm):
visited_dict[a,b] = True
old_crossing = next_crossing

first = crossing_dict[0,1]
last = crossing_dict[size-2,size-1]
first_open = first.adjacent.index(None) #last open spot
first = crossing_dict[0, 1]
last = crossing_dict[size-2, size-1]
first_open = first.adjacent.index(None) # last open spot
last_open = last.adjacent.index(None)
first[first_open] = last[last_open]
return sg.Link(crossing_dict.values())

def strands_to_cross(size,i):
mid = (size-1)/2

def strands_to_cross(size, i):
mid = (size - 1) / 2
L = []
for j in range(mid):
L.insert(0,(i-(2*(j+1)))%size)
L.append( (i+(2*(j+1)))%size )
L.insert(0, (i-(2*(j+1))) % size)
L.append((i+(2*(j+1))) % size)
return L


def permutation(size):
L = range(size)
random.shuffle(L)
Expand Down
19 changes: 11 additions & 8 deletions dev/dev_malik/random_knot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def random_knot(n, method='close_under', alternate=False,
available = available_strands(loose_cs)
strand_to_cross = choice(available)
if alternate:
over_or_under = 1-(i%2)
over_or_under = 1 - (i % 2)
else:
over_or_under = randint(0,1)
over_or_under = randint(0, 1)
loose_cs = cross_strand(crossings, loose_cs,
strand_to_cross, str(i+1), over_or_under)
same_face = set(available_strands(loose_cs)) == set(available_strands(final_cs))
Expand All @@ -57,17 +57,18 @@ def random_open_string(n, alternate=False, bias=False):
else:
strand_to_cross = choice(available)
if alternate:
over_or_under = 1-(i%2)
over_or_under = 1 - (i % 2)
else:
over_or_under = randint(0,1)
over_or_under = randint(0, 1)
loose_cs = cross_strand(crossings, loose_cs,
strand_to_cross, str(i+1), over_or_under)
return crossings, loose_cs, final_cs


def bias_middle(start_list):
biased_list = []
num_copies = range(len(start_list)/2)
if len(start_list)%2 == 1:
if len(start_list) % 2:
num_copies.append(len(start_list)/2)
num_copies.extend(reversed(range(len(start_list)/2)))
for i, x in enumerate(num_copies):
Expand Down Expand Up @@ -159,12 +160,13 @@ def function_evolution(n, function, simplify='level', skip_first=0):
values.append(open_string_evaluation(crossings, function, simplify))
return values


def turn_list_from_open_string(crossings, loose_cs, final_cs):
turn_list = []
while len(crossings) > 1:
# print(turn_list)
old_position_crossing, old_position_index = loose_cs.crossing.adjacent[(loose_cs.strand_index-1)%4]
old_crossing_sign = loose_cs.strand_index%2
old_position_crossing, old_position_index = loose_cs.crossing.adjacent[(loose_cs.strand_index-1) % 4]
old_crossing_sign = loose_cs.strand_index % 2
if old_crossing_sign == 0:
old_crossing_sign = -1
else:
Expand Down Expand Up @@ -480,6 +482,7 @@ def knot_from_turn_list(turn_list):
# if c != loose_cs.crossing and c != final_cs.crossing:
# c.rotate(randint(0,1))


def cross_strand(crossings, loose_cs, strand_to_cross, new_label, over_vs_under):
opposite = strand_to_cross.opposite()
new_crossing = Crossing(new_label)
Expand All @@ -492,7 +495,7 @@ def cross_strand(crossings, loose_cs, strand_to_cross, new_label, over_vs_under)
css = new_crossing.crossing_strands()
connect_crossing_strands(css[0+over_vs_under], loose_cs)
connect_crossing_strands(css[1+over_vs_under], strand_to_cross)
connect_crossing_strands(css[(3+over_vs_under)%4], opposite)
connect_crossing_strands(css[(3+over_vs_under) % 4], opposite)

return css[2+over_vs_under]

Expand Down
Loading

0 comments on commit 4d0e825

Please sign in to comment.