Skip to content

Commit

Permalink
Merge pull request #7 from caitlynachen/master
Browse files Browse the repository at this point in the history
Extend Parser to support exclusion as Spec field and associated tests
  • Loading branch information
dorisjlee committed Jun 1, 2020
2 parents 8b7c4e8 + c44bf13 commit 623582f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
16 changes: 9 additions & 7 deletions lux/compiler/Compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,10 @@ def populateWildcardOptions(ldf: LuxDataFrame) -> dict:
else:
options = convert2List(spec.attribute)
for optStr in options:
specCopy = copy.copy(spec)
specCopy.attribute = optStr
specOptions.append(specCopy)
if str(optStr) not in spec.exclude:
specCopy = copy.copy(spec)
specCopy.attribute = optStr
specOptions.append(specCopy)
specs["attributes"].append(specOptions)
else: # filters
attrLst = convert2List(spec.attribute)
Expand All @@ -369,10 +370,11 @@ def populateWildcardOptions(ldf: LuxDataFrame) -> dict:
else:
options.extend(convert2List(spec.value))
for optStr in options:
specCopy = copy.copy(spec)
specCopy.attribute = attr
specCopy.value = optStr
specOptions.append(specCopy)
if str(optStr) not in spec.exclude:
specCopy = copy.copy(spec)
specCopy.attribute = attr
specCopy.value = optStr
specOptions.append(specCopy)
specs["filters"].extend(specOptions)

return specs
5 changes: 4 additions & 1 deletion lux/context/Spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class Spec:
def __init__(self, description:typing.Union[str,list] ="",attribute: typing.Union[str,list] ="",value: typing.Union[str,list]="",
filterOp:str ="=", channel:str ="", dataType:str="",dataModel:str="",
aggregation:str = "", binSize:int=0, weight:float=1,sort:str=""):
aggregation:str = "", binSize:int=0, weight:float=1,sort:str="", exclude: typing.Union[str,list] =""):
"""
Spec is the object representation of a single unit of the specification.
Expand Down Expand Up @@ -54,6 +54,7 @@ def __init__(self, description:typing.Union[str,list] ="",attribute: typing.Unio
self.binSize = binSize
self.weight = weight
self.sort = sort
self.exclude = exclude

def __repr__(self):
attributes = []
Expand All @@ -73,6 +74,8 @@ def __repr__(self):
attributes.append(" dataType: " + str(self.dataType))
if self.binSize != None:
attributes.append(" binSize: " + str(self.binSize))
if len(self.exclude) != 0:
attributes.append(" exclude: " + str(self.exclude))
attributes[0] = "<Spec" + attributes[0][5:]
attributes[len(attributes) - 1] += " >"
return ',\n'.join(attributes)
14 changes: 13 additions & 1 deletion tests/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,16 @@ def test_filter_aggregation_fillzero_aligned():
assert result[result["Cylinders"]==8]["MilesPerGal"].values[0]==0
assert result[result["Cylinders"]==3]["MilesPerGal"].values[0]==externalValidation[3]
assert result[result["Cylinders"]==4]["MilesPerGal"].values[0]==externalValidation[4]
assert result[result["Cylinders"]==6]["MilesPerGal"].values[0]==externalValidation[6]
assert result[result["Cylinders"]==6]["MilesPerGal"].values[0]==externalValidation[6]

def test_exclude_attribute():
df = pd.read_csv("lux/data/car.csv")
df.setContext([lux.Spec("?", exclude=["Name", "Year"]),lux.Spec("Horsepower")])
view = df.viewCollection[0]
view.data = df
PandasExecutor.executeFilter(view)
for vc in df.viewCollection:
assert (vc.getAttrByChannel("x")[0].attribute != "Year")
assert (vc.getAttrByChannel("x")[0].attribute != "Name")
assert (vc.getAttrByChannel("y")[0].attribute != "Year")
assert (vc.getAttrByChannel("y")[0].attribute != "Year")

0 comments on commit 623582f

Please sign in to comment.