Skip to content

Commit

Permalink
Merge pull request #98 from shibasisp/table
Browse files Browse the repository at this point in the history
fixed formatting and clean up for tables
  • Loading branch information
tammojan committed Jun 19, 2017
2 parents 00fb70f + cd53f20 commit d6834d5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
17 changes: 9 additions & 8 deletions casacore/tables/msutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def addImagingColumns(msname, ack=True):
# Add IMAGING_WEIGHT which is 1-dim and has type float.
# It needs a shape, otherwise the CASA imager complains.
shp = []
if cdesc.has_key('shape'):
if 'shape' in cdesc:
shp = cdesc['shape']
if len(shp) > 0:
shp = [shp[0]] # use nchan from shape
Expand Down Expand Up @@ -142,7 +142,7 @@ def removeImagingColumns(msname):


def addDerivedMSCal(msname):
""" Add the derived columns like HA to an MS or CalTable
""" Add the derived columns like HA to an MS or CalTable.
It adds the columns HA, HA1, HA2, PA1, PA2, LAST, LAST1, LAST2, AZEL1,
AZEL2, and UVW_J2000.
Expand All @@ -158,7 +158,7 @@ def addDerivedMSCal(msname):
# Check that the columns needed by DerivedMSCal are present.
# Note that ANTENNA2 and FEED2 are not required.
for col in ["TIME", "ANTENNA1", "FIELD_ID", "FEED1"]:
if not col in colnames:
if col not in colnames:
raise ValueError("Columns " + colnames +
" should be present in table " + msname)
scols1 = ['HA', 'HA1', 'HA2', 'PA1', 'PA2']
Expand Down Expand Up @@ -189,7 +189,7 @@ def addDerivedMSCal(msname):


def removeDerivedMSCal(msname):
""" Remove the derived columns like HA from an MS or CalTable
""" Remove the derived columns like HA from an MS or CalTable.
It removes the columns using the data manager DerivedMSCal.
Such columns are HA, HA1, HA2, PA1, PA2, LAST, LAST1, LAST2, AZEL1,
Expand All @@ -210,7 +210,7 @@ def removeDerivedMSCal(msname):


def msconcat(names, newname, concatTime=False):
"""Virtually concatenate multiple MeasurementSets
"""Virtually concatenate multiple MeasurementSets.
Multiple MeasurementSets are concatenated into a single MeasurementSet.
The concatenation is done in an entirely or almost entirely virtual way,
Expand All @@ -221,7 +221,7 @@ def msconcat(names, newname, concatTime=False):
If concatenated in time, no indices need to be updated and the
concatenation is done in a single step.
If spectral windows are concatenated, tThe data-description-ids and
If spectral windows are concatenated, the data-description-ids and
spectral-window-ids in the resulting MS and its subtables are updated
to make them unique.
The spectral concatenation is done in two steps and results in two MSs:
Expand Down Expand Up @@ -277,7 +277,9 @@ def msconcat(names, newname, concatTime=False):
# to be changed.
# The new column is filled at the end.
tnew = table(newname, tdesc, nrow=tn.nrows(), dminfo={
'1': {'TYPE': 'ForwardColumnEngine', 'NAME': 'ForwardData', 'COLUMNS': tn.colnames(),
'1': {'TYPE': 'ForwardColumnEngine',
'NAME': 'ForwardData',
'COLUMNS': tn.colnames(),
'SPEC': {'FORWARDTABLE': tn.name()}}})
# Remove the DATA_DESC_ID column and recreate it in a stored way.
tnew.removecols('DATA_DESC_ID')
Expand Down Expand Up @@ -306,7 +308,6 @@ def msconcat(names, newname, concatTime=False):
nrdd = 0
nrspw = 0
nrmain = 0
useChanSel = True
for name in names:
t = table(name, ack=False)
tdd = table(t.getkeyword('DATA_DESCRIPTION'), ack=False)
Expand Down
50 changes: 24 additions & 26 deletions casacore/tables/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@

# Make interface to class TableProxy available.
from ._tables import (Table,
_default_ms,
_default_ms_subtable,
_required_ms_desc)
_default_ms,
_default_ms_subtable,
_required_ms_desc)

from casacore import six
from .tablehelper import _add_prefix, _remove_prefix, _do_remove_prefix
Expand Down Expand Up @@ -831,7 +831,7 @@ def isscalarcol(self, columnname):
def isvarcol(self, columnname):
"""Tell if the column holds variable shaped arrays."""
desc = self.getcoldesc(columnname)
return 'ndim' in desc and not 'shape' in desc
return 'ndim' in desc and 'shape' not in desc

def coldatatype(self, columnname):
"""Get the data type of a column.
Expand Down Expand Up @@ -1019,7 +1019,7 @@ def getcolnp(self, columnname, nparray, startrow=0, nrow=-1, rowincr=1):
rows (default all), and row stride (default 1).
"""
if not nparray.flags.c_contiguous or nparray.size == 0:
if (not nparray.flags.c_contiguous) or nparray.size == 0:
raise ValueError("Argument 'nparray' has to be a contiguous numpy array")
return self._getcolvh(columnname, startrow, nrow, rowincr, nparray)

Expand Down Expand Up @@ -1183,14 +1183,14 @@ def addcols(self, desc, dminfo={}, addtoparent=True):
tdesc = desc
# Create a tabdesc if only a coldesc is given.
if 'name' in desc:
import casacore.tables.tableutil
import casacore.tables.tableutil as pt
if len(desc) == 2 and 'desc' in desc:
# Given as output from makecoldesc
tdesc = casacore.tables.tableutil.maketabdesc(desc)
tdesc = pt.maketabdesc(desc)
elif 'valueType' in desc:
# Given as output of getcoldesc (with a name field added)
cd = casacore.tables.tableutil.makecoldesc(desc['name'], desc)
tdesc = casacore.tables.tableutil.maketabdesc(cd)
cd = pt.makecoldesc(desc['name'], desc)
tdesc = pt.maketabdesc(cd)
self._addcols(tdesc, dminfo, addtoparent)
self._makerow()

Expand Down Expand Up @@ -1279,8 +1279,8 @@ def getkeyword(self, keyword):
- a reference to a table which is returned as a string containing its
name prefixed by 'Table :'. It can be opened using the normal table
constructor which will remove the prefix.
- a struct which is returned as a dict. A struct is fully nestable, thus
each field in the struct can have one of the values described here.
- a struct which is returned as a dict. A struct is fully nestable,
thus each field in the struct can have one of the values described here.
Similar to method :func:`fieldnames` a keyword name can be given consisting
of multiple parts separated by dots. This represents nested structs,
Expand Down Expand Up @@ -1326,11 +1326,9 @@ def getcolkeywords(self, columnname):

def getsubtables(self):
"""Get the names of all subtables."""

keyset = self.getkeywords()
names = []
for key in keyset:
value = keyset[key]
for key, value in keyset.items():
if isinstance(value, str) and value.find('Table: ') == 0:
names.append(_do_remove_prefix(value))
return names
Expand Down Expand Up @@ -1438,7 +1436,6 @@ def getdesc(self, actual=True):
actual array shapes and data managers used).
`actual=False` means that the original description as made by
:func:`maketabdesc` is returned.
"""

tabledesc = self._getdesc(actual, True)
Expand All @@ -1451,7 +1448,7 @@ def getdesc(self, actual=True):
if "HCcoordnames" in hcdef and len(hcdef["HCcoordnames"]) == 0:
del hcdef["HCcoordnames"]
if "HCidnames" in hcdef and len(hcdef["HCidnames"]) == 0:
del hcdef ["HCidnames"]
del hcdef["HCidnames"]

return tabledesc

Expand All @@ -1473,6 +1470,7 @@ def coldesc(self, columnname, actual=True):
is doing with the description given by :func:`getcoldesc`.
"""
import casacore.tables.tableutil as pt
return pt.makecoldesc(columnname, self.getcoldesc(columnname, actual))

def getdminfo(self, columnname=None):
Expand Down Expand Up @@ -1595,8 +1593,7 @@ def summary(self, recurse=False):
if (len(ckeys) > 0):
six.print_(column, 'keywords:', ckeys)
if (recurse):
for key in tkeys.keys():
value = tkeys[key]
for key, value in tkeys.items():
tabname = _remove_prefix(value)
six.print_('Summarizing subtable:', tabname)
lt = table(tabname)
Expand Down Expand Up @@ -1766,7 +1763,8 @@ def browse(self, wait=True, tempname="/tmp/seltable"):
To make browsing of such tables possible, the argument `tempname` can
be used to specify a table name that will be used to form a persistent
table that can be browsed. Note that such a table is very small as it
does not contain data, but only references to rows in the original table.
does not contain data, but only references to rows in the original
table.
The default for `tempname` is '/tmp/seltable'.
If needed, the table can be deleted using the :func:`tabledelete`
Expand All @@ -1787,7 +1785,8 @@ def browse(self, wait=True, tempname="/tmp/seltable"):
waitstr1 = " &"
waitstr2 = "background ..."
if self.iswritable():
six.print_("Flushing data and starting casabrowser in the " + waitstr2)
six.print_("Flushing data and starting casabrowser in the " +
waitstr2)
else:
six.print_("Starting casabrowser in the " + waitstr2)
self.flush()
Expand All @@ -1796,19 +1795,19 @@ def browse(self, wait=True, tempname="/tmp/seltable"):
os.system('casabrowser ' + self.name() + waitstr1)
elif len(tempname) > 0:
six.print_(" making a persistent copy in table " + tempname)
tx = self.copy(tempname)
tx = 0
self.copy(tempname)
os.system('casabrowser ' + tempname + waitstr1)
if wait:
from casacore.tables import tabledelete
six.print_(" finished browsing")
tabledelete(tempname)

else:
six.print_(" after browsing use tabledelete('" + tempname + "') to delete the copy")
six.print_(" after browsing use tabledelete('" + tempname +
"') to delete the copy")
else:
six.print_("Cannot browse because the table is in memory only.")
six.print_("You can browse a (shallow) persistent copy of the table like:")
six.print_("You can browse a (shallow) persistent copy of the table like: ")
six.print_(" t.browse(True, '/tmp/tab1')")
else:
try:
Expand Down Expand Up @@ -1880,8 +1879,7 @@ def view(self, wait=True, tempname="/tmp/seltable"):
viewed = True
elif len(tempname) > 0:
six.print_(" making a persistent copy in table " + tempname)
tx = self.copy(tempname)
tx = 0
self.copy(tempname)
os.system('casaviewer ' + tempname + waitstr1)
viewed = True
if wait:
Expand Down
10 changes: 6 additions & 4 deletions casacore/tables/tablecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ class tablecolumn:
"""

def __init__(self, table, columnname):
if not columnname in table.colnames():
raise RuntimeError("Column " + columnname + " does not exist in table " + table.name())
if columnname not in table.colnames():
raise RuntimeError("Column " + columnname +
" does not exist in table " + table.name())
self._table = table
self._column = columnname

Expand Down Expand Up @@ -257,7 +258,7 @@ def __getattr__(self, name):
except:
pass
# _ or keys means all keywords.
if name == '_' or name == 'keys':
if name in ('_', 'keys'):
return self.getkeywords()
# Unknown name.
raise AttributeError("table has no attribute/keyword " + name)
Expand Down Expand Up @@ -295,7 +296,8 @@ def __setitem__(self, key, value):
else:
# Each row has its own value.
if len(value) != sei[1]:
raise RuntimeError("tablecolumn slice length differs from value length")
raise RuntimeError(
"tablecolumn slice length differs from value length")
for val in value:
self.putcell(rownr, val)
rownr += sei[2]
Expand Down
10 changes: 5 additions & 5 deletions casacore/tables/tablehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _check_index(key, name):

# Check a key or slice given to index a tablerow or tablecolumn object.
# A TypeError exception is raised if values or not integer or None.
# An IndexError is raised if incorrect values are given.
# An IndexError is raised if incorrect values are given.
# It returns a list of length 1 if a single index is given.
# Otherwise it returns [startrow, nrow, step].
def _check_key_slice(key, nrows, name):
Expand All @@ -79,21 +79,21 @@ def _check_key_slice(key, nrows, name):
# Given as start:stop:step where each part is optional and can
# be negative.
incr = 1
if key.step != None:
if key.step is not None:
incr = _check_index(key.step, name)
if incr == 0:
raise RunTimeError(name + " slice step cannot be zero")
raise RuntimeError(name + " slice step cannot be zero")
strow = 0
endrow = nrows
if incr < 0:
strow = nrows - 1
endrow = -1
if key.start != None:
if key.start is not None:
strow = _check_index(key.start, name)
if strow < 0:
strow += nrows
strow = min(max(strow, 0), nrows - 1)
if key.stop != None:
if key.stop is not None:
endrow = _check_index(key.stop, name)
if endrow < 0:
endrow += nrows
Expand Down
12 changes: 6 additions & 6 deletions casacore/tables/tableindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


class tableindex(TableIndex):
""" The Python interface to Casacore table index
"""The Python interface to Casacore table index.
A tableindex makes it possible to find rows in a :class:`table` based on
the contents of one or more columns. When constructing the `tableindex` it
Expand Down Expand Up @@ -79,7 +79,7 @@ def _makekey(self, key):
if not isinstance(d, dict):
cols = self.colnames()
if len(cols) != 1:
raise RunTimeError("key has to be given as a dict for a multi-column index")
raise RuntimeError("key has to be given as a dict for a multi-column index")
d = {cols[0]: key}
return d

Expand All @@ -90,7 +90,7 @@ def rownr(self, key):
single value.
Otherwise the key has to be given as a dict where the name of each
field in the dict should correspond with the column name in the index.
For example::
t = table('3c343.MS/ANTENNA')
Expand Down Expand Up @@ -168,13 +168,13 @@ def __getitem__(self, key):
if rnr < 0:
raise KeyError("key not found in tableindex")
return rnr
if key.step != None:
if key.step is not None:
raise RuntimeError("tableindex slicing cannot have a step")
lowerkey = 0
if key.start != None:
if key.start is not None:
lowerkey = key.start
upperkey = 2147483647; # highest int
if key.stop != None:
if key.stop is not None:
upperkey = key.stop
if (lowerkey >= upperkey):
raise RuntimeError("tableindex slice stop must be > start")
Expand Down
3 changes: 1 addition & 2 deletions casacore/tables/tableutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ def makedminfo(tabdesc, group_spec=None):
This should be used with care.
"""

if group_spec is None:
group_spec = {}

Expand Down Expand Up @@ -631,7 +630,7 @@ def tabledefinehypercolumn(tabdesc,
rec['HCcoordnames'] = coordcolumns
if not isinstance(idcolumns, bool):
rec['HCidnames'] = idcolumns
if not '_define_hypercolumn_' in tabdesc:
if '_define_hypercolumn_' not in tabdesc:
tabdesc['_define_hypercolumn_'] = {}
tabdesc['_define_hypercolumn_'][name] = rec

Expand Down

0 comments on commit d6834d5

Please sign in to comment.