Skip to content

Commit

Permalink
Fix: Use iteritems from six for Python3 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
jonls committed Jul 1, 2015
1 parent fc54182 commit d50a9cc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
8 changes: 4 additions & 4 deletions psamm/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from . import fluxanalysis, massconsistency, fastcore
from .lpsolver import generic

from six import add_metaclass
from six import add_metaclass, iteritems

# Module-level logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -430,7 +430,7 @@ def run_fba_minimized(self, reaction):
fmin_fluxes = dict(fluxanalysis.flux_minimization(
self._mm, {reaction: optimum}, solver=solver))
count = 0
for reaction_id, flux in fmin_fluxes.iteritems():
for reaction_id, flux in iteritems(fmin_fluxes):
if fba_fluxes[reaction_id] - epsilon > flux:
count += 1
yield reaction_id, fba_fluxes[reaction_id], flux
Expand All @@ -446,7 +446,7 @@ def run_tfba(self, reaction):
fluxes = dict(fluxanalysis.flux_balance(
self._mm, reaction, tfba=True, solver=solver))

for reaction_id, flux in fluxes.iteritems():
for reaction_id, flux in iteritems(fluxes):
yield reaction_id, fba_fluxes[reaction_id], flux


Expand Down Expand Up @@ -1237,7 +1237,7 @@ def main(command_class=None):

# Create parsers for subcommands
subparsers = parser.add_subparsers(title='Command')
for name, values in sorted(commands.iteritems()):
for name, values in sorted(iteritems(commands)):
title, command_class = values
subparser = subparsers.add_parser(name, help=title)
subparser.set_defaults(command=command_class)
Expand Down
6 changes: 3 additions & 3 deletions psamm/datasource/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import csv

import yaml
from six import string_types
from six import string_types, iteritems

from ..reaction import Reaction, Compound
from . import modelseed
Expand Down Expand Up @@ -303,7 +303,7 @@ def parse_compound_table_file(path, f):
if 'id' not in row or row['id'].strip() == '':
raise ParseError('Expected `id` column in table')

props = {key: value for key, value in row.iteritems() if value != ''}
props = {key: value for key, value in iteritems(row) if value != ''}
yield CompoundEntry(row['id'], props)


Expand Down Expand Up @@ -446,7 +446,7 @@ def parse_reaction_table_file(f):
if 'id' not in row or row['id'].strip() == '':
raise ParseError('Expected `id` column in table')

props = {key: value for key, value in row.iteritems() if value != ''}
props = {key: value for key, value in iteritems(row) if value != ''}

if 'equation' in props:
props['equation'] = modelseed.parse_reaction(props['equation'])
Expand Down
6 changes: 3 additions & 3 deletions psamm/datasource/sbml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from functools import partial
from itertools import count

from six import itervalues
from six import itervalues, iteritems

from ..reaction import Reaction, Compound

Expand Down Expand Up @@ -422,7 +422,7 @@ def write_model(self, file, model, compounds):
# Create list of compartments
compartments = ET.SubElement(
model_tag, self._sbml_tag('listOfCompartments'))
for compartment, compartment_id in model_compartments.iteritems():
for compartment, compartment_id in iteritems(model_compartments):
compartment_tag = ET.SubElement(
compartments, self._sbml_tag('compartment'))
compartment_tag.set(self._sbml_tag('id'), compartment_id)
Expand All @@ -431,7 +431,7 @@ def write_model(self, file, model, compounds):
# Create list of species
species_list = ET.SubElement(
model_tag, self._sbml_tag('listOfSpecies'))
for species, species_id in model_species.iteritems():
for species, species_id in iteritems(model_species):
species_tag = ET.SubElement(species_list,
self._sbml_tag('species'))
species_tag.set(self._sbml_tag('id'), species_id)
Expand Down
18 changes: 10 additions & 8 deletions psamm/gapfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
This implements a variant of the algorithms described in [Kumar07]_.
"""

from six import iteritems

from .lpsolver import lp


Expand Down Expand Up @@ -51,7 +53,7 @@ def gapfind(model, solver, epsilon=1e-5, v_max=1000):

# Define constraints on production of metabolites in reaction
binary_cons_lhs = {compound: 0 for compound in model.compounds}
for spec, value in model.matrix.iteritems():
for spec, value in iteritems(model.matrix):
compound, reaction_id = spec
if value != 0 and (reaction_id in model.reversible or value > 0):
prob.define(('w', reaction_id, compound),
Expand All @@ -74,15 +76,15 @@ def gapfind(model, solver, epsilon=1e-5, v_max=1000):
objective = sum(prob.var(('xp', compound)) for compound in model.compounds)
prob.set_linear_objective(objective)

for compound, lhs in binary_cons_lhs.iteritems():
for compound, lhs in iteritems(binary_cons_lhs):
prob.add_linear_constraints(lhs >= prob.var(('xp', compound)))

# Define mass balance constraints
massbalance_lhs = {compound: 0 for compound in model.compounds}
for spec, value in model.matrix.iteritems():
for spec, value in iteritems(model.matrix):
compound, reaction_id = spec
massbalance_lhs[compound] += prob.var(('v', reaction_id)) * value
for compound, lhs in massbalance_lhs.iteritems():
for compound, lhs in iteritems(massbalance_lhs):
# The constraint is merely >0 meaning that we have implicit sinks
# for all compounds.
prob.add_linear_constraints(lhs >= 0)
Expand Down Expand Up @@ -156,7 +158,7 @@ def gapfill(model, core, blocked, solver, epsilon=1e-5, v_max=1000):

# Define constraints on production of blocked metabolites in reaction
binary_cons_lhs = {compound: 0 for compound in blocked}
for spec, value in model.matrix.iteritems():
for spec, value in iteritems(model.matrix):
compound, reaction_id = spec
if compound in blocked and value != 0:
prob.define(('w', reaction_id, compound),
Expand All @@ -171,16 +173,16 @@ def gapfill(model, core, blocked, solver, epsilon=1e-5, v_max=1000):
if reaction_id in model.reversible or value > 0:
binary_cons_lhs[compound] += w

for compound, lhs in binary_cons_lhs.iteritems():
for compound, lhs in iteritems(binary_cons_lhs):
if compound in blocked:
prob.add_linear_constraints(lhs >= 1)

# Define mass balance constraints
massbalance_lhs = {compound: 0 for compound in model.compounds}
for spec, value in model.matrix.iteritems():
for spec, value in iteritems(model.matrix):
compound, reaction_id = spec
massbalance_lhs[compound] += prob.var(('v', reaction_id)) * value
for compound, lhs in massbalance_lhs.iteritems():
for compound, lhs in iteritems(massbalance_lhs):
# The constraint is merely >0 meaning that we have implicit sinks
# for all compounds.
prob.add_linear_constraints(lhs >= 0)
Expand Down
10 changes: 6 additions & 4 deletions psamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import re
import math

from six import iteritems


class LoggerFile(object):
"""File-like object that forwards to a logger.
Expand Down Expand Up @@ -80,20 +82,20 @@ def update_weight(value):
# the next is less than epsilon.
while True:
weights = {identifier: update_weight(value)
for identifier, value in result.iteritems()}
for identifier, value in iteritems(result)}
kwargs['weights'] = weights

last_result = result
full_result = f(*args, **kwargs)
result = dict_result(full_result)

delta = math.sqrt(sum(pow(value - last_result[identifier], 2)
for identifier, value in result.iteritems()))
for identifier, value in iteritems(result)))
if delta < epsilon:
break

if isinstance(full_result, tuple):
return (result.iteritems(),) + full_result[1:]
return result.iteritems()
return (iteritems(result),) + full_result[1:]
return iteritems(result)

return convex_cardinality_wrapper

0 comments on commit d50a9cc

Please sign in to comment.