Changeset 7893:faf89837aa9b in orange


Ignore:
Timestamp:
05/06/11 13:25:08 (3 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
3e5a7080242a303a69919fa17ae0b081e3c8f041
Message:

Added 'createOnNew' arguments to load functions in orngIO with appropriate calls to orange.Variable.make.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orange/orngIO.py

    r6538 r7893  
    2121#       * registration in orange (Janez) 
    2222 
    23 import orange,string 
    24  
    25 def loadARFF(filename): 
    26     try: 
    27         f = open(filename,'r') 
    28     except: 
    29         f = open(filename+'.arff','r') 
     23import orange, string 
     24import os 
     25 
     26def loadARFF(filename, createOnNew=orange.Variable.MakeStatus.Incompatible, **kwargs): 
     27    if not os.path.exists(filename) and os.path.exists(filename + ".arff"): 
     28        filename = filename + ".arff"  
     29    f = open(filename,'r') 
     30     
    3031    attributes = [] 
     32    attributeLoadStatus = [] 
     33     
    3134    name = '' 
    3235    state = 0 # header 
     
    8588                            vals.append(sy) 
    8689                    #print atn,vals 
    87                     a = orange.EnumVariable(name=atn,values=vals) 
     90                    a, s = orange.Variable.make(atn, orange.VarTypes.Discrete, vals, [], createOnNew) 
     91#                    a = orange.EnumVariable(name=atn,values=vals) 
    8892                else: 
    8993                    # real... 
    90                     a = orange.FloatVariable(name=atn) 
     94                    a, s = orange.Variable.make(atn, orange.VarTypes.Continuous, [], [], createOnNew) 
     95#                    a = orange.FloatVariable(name=atn) 
     96                     
    9197                attributes.append(a) 
     98                attributeLoadStatus.append(s) 
    9299    # generate the domain 
    93100    d = orange.Domain(attributes) 
     
    98105    t = orange.ExampleTable(d,lex) 
    99106    t.name = name 
     107    t.attributeLoadStatus = attributeLoadStatus 
    100108    return t 
    101109 
     
    257265    orngSVM.exampleTableToSVMFormat(example, open(filename, "wb")) 
    258266     
    259 def loadLibSVM(filename): 
     267def loadLibSVM(filename, createOnNew=orange.Variable.MakeStatus.Incompatible, **kwargs): 
     268    attributeLoadStatus = {} 
     269    def make_float(name): 
     270        attr, s = orange.Variable.make(name, orange.VarTypes.Continuous, [], [], createOnNew) 
     271        attributeLoadStatus[attr] = s 
     272        return attr 
     273     
     274    def make_disc(name, unordered): 
     275        attr, s = orange.Variable.make(name, orange.VarTypes.Discrete, [], unordered, createOnNew) 
     276        attributeLoadStatus[attr] = s 
     277        return attr 
     278     
    260279    data = [line.split() for line in open(filename, "rb").read().splitlines() if line.strip()] 
    261     vars = type("attr", (dict,), {"__missing__": lambda self, key: self.setdefault(key, orange.FloatVariable(key))})() 
     280    vars = type("attr", (dict,), {"__missing__": lambda self, key: self.setdefault(key, make_float(key))})() 
    262281    item = lambda i, v: (vars[i], vars[i](v)) 
    263282    values = [dict([item(*val.split(":"))  for val in ex[1:]]) for ex in data] 
     
    265284    disc = all(["." not in c for c in classes]) 
    266285    attributes = sorted(vars.values(), key=lambda var: int(var.name)) 
    267     classVar = orange.EnumVariable("class", values=sorted(set(classes))) if disc else orange.FloatVariable("target") 
     286    classVar = make_disc("class", sorted(set(classes))) if disc else make_float("target") 
     287    attributeLoadStatus = [attributeLoadStatus[attr] for attr in attributes] + \ 
     288                          [attributeLoadStatus[classVar]] 
    268289    domain = orange.Domain(attributes, classVar) 
    269     return orange.ExampleTable([orange.Example(domain, [ex.get(attr, attr("?")) for attr in attributes] + [c]) for ex, c in zip(values, classes)]) 
    270  
     290    table = orange.ExampleTable([orange.Example(domain, [ex.get(attr, attr("?")) for attr in attributes] + [c]) for ex, c in zip(values, classes)]) 
     291    table.attributeLoadStatus = attributeLoadStatus 
     292    return table 
    271293 
    272294orange.registerFileType("R", None, toR, ".R") 
Note: See TracChangeset for help on using the changeset viewer.