Source

orange / Orange / data / io.py

Full commit
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
"""\
*****************
Data I/O (``io``)
*****************

Import/Export
=============

This module contains the functions for importing and exporting Orange
data tables from/to different file formats. This works by associating
a filename extension with a set of loading/saving functions using
:obj:`register_file_type`.

Support for some formats is already implemented:  
    
    - Weka `.arff` format
    - C4.5 `.data/.names` format
    - LibSVM data format
    - R `.R` data frame source (export only)


.. function:: register_file_type(format_name, load_func, save_func, extension)

    Register the ``save_func``, ``load_func`` pair for the 
    ``format_name``. The format is identified by the ``extension``.
    
    :param format_name: the name of the format.
    :type format_name: str
    
    :param load_func: a function used for loading the data (see 
        :ref:`custom-formats` for details)
    :type load_func: function
    
    :param save_func: a function used for saving the data (see 
        :ref:`custom-formats` for details)
    :type save_func: function
    
    :param extension: the file extension associated with this format 
        (e.g. '.myformat'). This can be a list of extension if the 
        format uses multiple extensions (for instance the 
        `.data` and `.names` file pairs in the C4.5 format)
    
    Example from the :obj:`~Orange.data.io` module that registers the Weka .arff 
    format ::
        
        register_file_type("Weka", load_ARFF, to_ARFF, ".arff")
        
``load_func`` or ``save_func`` can be None, indicating that the
corresponding functionality is not supported.
 
Loading and saving from/to custom formats then works the same way as
the standard Orange `.tab` file but with a different filename
extension. ::

    >>> import Orange
    >>> data = Orange.data.Table("iris.arff")
    >>> data.save("Copy of iris.arff")
  
    

.. _custom-formats:

Implementing custom import/export functions.
--------------------------------------------

The signature for the custom load functions should be

``load_myformat(filename, create_new_on=Orange.feature.Descriptor.MakeStatus.NoRecognizedValues, **kwargs)``
    
When constructing variables :obj:`Orange.feature.Descriptor.make` should 
be used with the ``create_new_on`` parameter. 
:obj:`~Orange.feature.Descriptor.make` will return an attribute and the 
status of the variable, telling whether a new attribute was created 
or the old one reused and why (see :mod:`Orange.feature`). 
Additional keyword arguments can be provided in the call to 
:obj:`~Orange.data.Table` constructor. These will be passed in the 
``**kwargs``. 
The function should return the build :obj:`~Orange.data.Table` object.
For examples see the source code for the ``Orange.data.io`` module

The save function is easier to implement.

``save_myformat(filename, table, **kwargs)``

Similar as above the ``**kwargs`` contains any additional arguments
:obj:`~Orange.data.Table.save`.
  
"""
import os
import warnings

import Orange
import Orange.feature
import Orange.misc
from Orange.core import \
     BasketFeeder, FileExampleGenerator, BasketExampleGenerator, \
     C45ExampleGenerator, TabDelimExampleGenerator, \
     registerFileType as register_file_type

import Orange.feature as variable
from Orange.feature import Descriptor
MakeStatus = Orange.feature.Descriptor.MakeStatus
make = Orange.feature.Descriptor.make

def loadARFF(filename, create_on_new=MakeStatus.Incompatible, **kwargs):
    """Return class:`Orange.data.Table` containing data from file in Weka ARFF format
       if there exists no .xml file with the same name. If it does, a multi-label
       dataset is read and returned.
    """
    if filename[-5:] == ".arff":
        filename = filename[:-5]
    if os.path.exists(filename + ".xml") and os.path.exists(filename + ".arff"):
        xml_name = filename + ".xml"
        arff_name = filename + ".arff"
        return Orange.multilabel.mulan.trans_mulan_data(xml_name, arff_name, create_on_new)
    else:
        return loadARFF_Weka(filename, create_on_new)

def loadARFF_Weka(filename, create_on_new=MakeStatus.Incompatible, **kwargs):
    """Return class:`Orange.data.Table` containing data from file in Weka ARFF format"""
    if not os.path.exists(filename) and os.path.exists(filename + ".arff"):
        filename = filename + ".arff"
    f = open(filename, 'r')

    attributes = []
    attributeLoadStatus = []

    name = ''
    state = 0 # header
    data = []
    for l in f.readlines():
        l = l.rstrip("\n") # strip \n
        l = l.replace('\t', ' ') # get rid of tabs
        x = l.split('%')[0] # strip comments
        if len(x.strip()) == 0:
            continue
        if state == 0 and x[0] != '@':
            print "ARFF import ignoring:", x
        if state == 1:
            if x[0] == '{':#sparse data format, begin with '{', ends with '}'
                r = [None] * len(attributes)
                dd = x[1:-1]
                dd = dd.split(',')
                for xs in dd:
                    y = xs.split(" ")
                    if len(y) <> 2:
                        raise ValueError("the format of the data is error")
                    r[int(y[0])] = y[1]
                data.append(r)
            else:#normal data format, split by ','
                dd = x.split(',')
                r = []
                for xs in dd:
                    y = xs.strip(" ")
                    if len(y) > 0:
                        if y[0] == "'" or y[0] == '"':
                            r.append(xs.strip("'\""))
                        else:
                            ns = xs.split()
                            for ls in ns:
                                if len(ls) > 0:
                                    r.append(ls)
                    else:
                        r.append('?')
                data.append(r[:len(attributes)])
        else:
            y = []
            for cy in x.split(' '):
                if len(cy) > 0:
                    y.append(cy)
            if str.lower(y[0][1:]) == 'data':
                state = 1
            elif str.lower(y[0][1:]) == 'relation':
                name = str.strip(y[1])
            elif str.lower(y[0][1:]) == 'attribute':
                if y[1][0] == "'":
                    atn = y[1].strip("' ")
                    idx = 1
                    while y[idx][-1] != "'":
                        idx += 1
                        atn += ' ' + y[idx]
                    atn = atn.strip("' ")
                else:
                    atn = y[1]
                z = x.split('{')
                w = z[-1].split('}')
                if len(z) > 1 and len(w) > 1:
                    # there is a list of values
                    vals = []
                    for y in w[0].split(','):
                        sy = y.strip(" '\"")
                        if len(sy) > 0:
                            vals.append(sy)
                    a, s = make(atn, Orange.feature.Type.Discrete, vals, [], create_on_new)
                else:
                    # real...
                    a, s = make(atn, Orange.feature.Type.Continuous, [], [], create_on_new)

                attributes.append(a)
                attributeLoadStatus.append(s)
    # generate the domain
    d = Orange.data.Domain(attributes)
    lex = []
    for dd in data:
        e = Orange.data.Instance(d, dd)
        lex.append(e)
    t = Orange.data.Table(d, lex)
    t.name = name

    #if hasattr(t, "attribute_load_status"):
    t.setattr("attribute_load_status", attributeLoadStatus)
    return t
loadARFF = Orange.utils.deprecated_keywords(
{"createOnNew": "create_on_new"}
)(loadARFF)


def toARFF(filename, table, try_numericize=0):
    """Save class:`Orange.data.Table` to file in Weka's ARFF format"""
    t = table
    if filename[-5:] == ".arff":
        filename = filename[:-5]
    #print filename
    f = open(filename + '.arff', 'w')
    f.write('@relation %s\n' % t.domain.classVar.name)
    # attributes
    ats = [i for i in t.domain.attributes]
    ats.append(t.domain.classVar)
    for i in ats:
        real = 1
        if i.varType == 1:
            if try_numericize:
                # try if all values numeric
                for j in i.values:
                    try:
                        x = float(j)
                    except:
                        real = 0 # failed
                        break
            else:
                real = 0
        iname = str(i.name)
        if iname.find(" ") != -1:
            iname = "'%s'" % iname
        if real == 1:
            f.write('@attribute %s real\n' % iname)
        else:
            f.write('@attribute %s { ' % iname)
            x = []
            for j in i.values:
                s = str(j)
                if s.find(" ") == -1:
                    x.append("%s" % s)
                else:
                    x.append("'%s'" % s)
            for j in x[:-1]:
                f.write('%s,' % j)
            f.write('%s }\n' % x[-1])

    # examples
    f.write('@data\n')
    for j in t:
        x = []
        for i in range(len(ats)):
            s = str(j[i])
            if s.find(" ") == -1:
                x.append("%s" % s)
            else:
                x.append("'%s'" % s)
        for i in x[:-1]:
            f.write('%s,' % i)
        f.write('%s\n' % x[-1])

def loadMULAN(filename, create_on_new=MakeStatus.Incompatible, **kwargs):
    """Return class:`Orange.data.Table` containing data from file in Mulan ARFF and XML format"""
    if filename[-4:] == ".xml":
        filename = filename[:-4]
    if os.path.exists(filename + ".xml") and os.path.exists(filename + ".arff"):
        xml_name = filename + ".xml"
        arff_name = filename + ".arff"
        return Orange.multilabel.mulan.trans_mulan_data(xml_name, arff_name)
    else:
        return None
loadARFF = Orange.utils.deprecated_keywords(
{"createOnNew": "create_on_new"}
)(loadARFF)

def toC50(filename, table, try_numericize=0):
    """Save class:`Orange.data.Table` to file in C50 format"""
    t = table
    # export names
#    basename = os.path.basename(filename)
    filename_prefix, ext = os.path.splitext(filename)
    f = open('%s.names' % filename_prefix, 'w')
    f.write('%s.\n\n' % t.domain.class_var.name)
    # attributes
    ats = [i for i in t.domain.attributes]
    ats.append(t.domain.classVar)
    for i in ats:
        real = 1
        # try if real
        if i.varType == Orange.core.VarTypes.Discrete:
            if try_numericize:
                # try if all values numeric
                for j in i.values:
                    try:
                        x = float(j)
                    except Exception:
                        real = 0 # failed
                        break
            else:
                real = 0
        if real == 1:
            f.write('%s: continuous.\n' % i.name)
        else:
            f.write('%s: ' % i.name)
            x = []
            for j in i.values:
                x.append('%s' % j)
            for j in x[:-1]:
                f.write('%s,' % j)
            f.write('%s.\n' % x[-1])
    # examples
    f.close()

    f = open('%s.data' % filename_prefix, 'w')
    for j in t:
        x = []
        for i in range(len(ats)):
            x.append('%s' % j[i])
        for i in x[:-1]:
            f.write('%s,' % i)
        f.write('%s\n' % x[-1])

def toR(filename, t):
    """Save class:`Orange.data.Table` to file in R format"""
    if str.upper(filename[-2:]) == ".R":
        filename = filename[:-2]
    f = open(filename + '.R', 'w')

    atyp = []
    aord = []
    labels = []
    as0 = []
    for a in t.domain.variables:
        as0.append(a)
#    as0.append(t.domain.class_var)
    for a in as0:
        labels.append(str(a.name))
        atyp.append(a.var_type)
        aord.append(a.ordered)

    f.write('data <- data.frame(\n')
    for i in xrange(len(labels)):
        if atyp[i] == 2: # continuous
            f.write('"%s" = c(' % (labels[i]))
            for j in xrange(len(t)):
                if t[j][i].isSpecial():
                    f.write('NA')
                else:
                    f.write(str(t[j][i]))
                if (j == len(t) - 1):
                    f.write(')')
                else:
                    f.write(',')
        elif atyp[i] == 1: # discrete
            if aord[i]: # ordered
                f.write('"%s" = ordered(' % labels[i])
            else:
                f.write('"%s" = factor(' % labels[i])
            f.write('levels=c(')
            for j in xrange(len(as0[i].values)):
                f.write('"x%s"' % (as0[i].values[j]))
                if j == len(as0[i].values) - 1:
                    f.write('),c(')
                else:
                    f.write(',')
            for j in xrange(len(t)):
                if t[j][i].isSpecial():
                    f.write('NA')
                else:
                    f.write('"x%s"' % str(t[j][i]))
                if (j == len(t) - 1):
                    f.write('))')
                else:
                    f.write(',')
        else:
            raise "Unknown attribute type."
        if (i < len(labels) - 1):
            f.write(',\n')
    f.write(')\n')


def toLibSVM(filename, example):
    """Save class:`Orange.data.Table` to file in LibSVM format"""
    import Orange.classification.svm
    Orange.classification.svm.tableToSVMFormat(example, open(filename, "wb"))


@Orange.utils.deprecated_keywords({"createOnNew": "create_on_new"})
def loadLibSVM(filename, create_on_new=MakeStatus.Incompatible, **kwargs):
    """Return class:`Orange.data.Table` containing data from file in LibSVM format"""
    attributeLoadStatus = {}
    def make_float(name):
        attr, s = Orange.feature.Descriptor.make(name, Orange.feature.Type.Continuous, [], [], create_on_new)
        attributeLoadStatus[attr] = s
        return attr

    def make_disc(name, unordered):
        attr, s = Orange.feature.Descriptor.make(name, Orange.feature.Type.Discrete, [], unordered, create_on_new)
        attributeLoadStatus[attr] = s
        return attr

    data = [line.split() for line in open(filename, "rb").read().splitlines() if line.strip()]
    vars = type("attr", (dict,), {"__missing__": lambda self, key: self.setdefault(key, make_float(key))})()
    item = lambda i, v: (vars[i], vars[i](v))
    values = [dict([item(*val.split(":"))  for val in ex[1:]]) for ex in data]
    classes = [ex[0] for ex in data]
    disc = all(["." not in c for c in classes])
    attributes = sorted(vars.values(), key=lambda var: int(var.name))
    classVar = make_disc("class", sorted(set(classes))) if disc else make_float("target")
    attributeLoadStatus = [attributeLoadStatus[attr] for attr in attributes] + \
                          [attributeLoadStatus[classVar]]
    domain = Orange.data.Domain(attributes, classVar)
    table = Orange.data.Table([Orange.data.Instance(domain, [ex.get(attr, attr("?")) for attr in attributes] + [c]) for ex, c in zip(values, classes)])
    table.setattr("attribute_load_status", attributeLoadStatus)
    return table


"""\
A general CSV file reader.
--------------------------

Currently not yet documented and not registered (needs testing).

"""

def split_escaped_str(str, split_str=" ", escape="\\"):
    res = []
    index = 0
    start = 0
    find_start = 0
    while index != -1:
        index = str.find(split_str, find_start)
        if index != -1 and index > 0:
            if str[index - 1] == escape: # Skip the escaped split_str
                find_start = index + 1
            else:
                res.append(str[start:index])
                start = find_start = index + 1

        elif index == -1:
            res.append(str[start:])
    return [r.replace(escape + split_str, split_str) for r in res]

def is_standard_var_def(cell):
    """Is the cell a standard variable definition (empty, cont, disc, string)
    """
    try:
        var_type(cell)
        return True
    except ValueError, ex:
        return False

def is_var_types_row(row):
    """ Is the row a variable type definition row (as in the orange .tab file)
    """
    return all(map(is_standard_var_def, row))

def var_type(cell):
    """ Return variable type from a variable type definition in cell. 
    """
    if cell in ["c", "continuous"]:
        return variable.Continuous
    elif cell in ["d", "discrete"]:
        return variable.Discrete
    elif cell in ["s", "string"]:
        return variable.String
    elif cell.startswith("pyhton"):
        return variable.Python
    elif cell == "":
        return variable.Descriptor
    elif len(split_escaped_str(cell, " ")) > 1:
        return variable.Discrete, split_escaped_str(cell, " ")
    else:
        raise ValueError("Unknown variable type definition %r." % cell)

def var_types(row):
    """ Return variable types from row. 
    """
    return map(var_type, row)

def is_var_attributes_row(row):
    """ Is the row an attribute definition row (i.e. the third row in the
    standard orange .tab file format).
    
    """
    return all(map(is_var_attributes_def, row))

def is_var_attributes_def(cell):
    """ Is the cell a standard variable attributes definition. 
    """
    try:
        var_attribute(cell)
        return True
    except ValueError, ex:
        raise
        return False

def _var_attribute_label_parse(cell):
    """ 
    """
    key_value = split_escaped_str(cell, "=")
    if len(key_value) == 2:
        return tuple(key_value)
    else:
        raise ValueError("Invalid attribute label definition %r." % cell)

def var_attribute(cell):
    """ Return variable specifier ("meta" or "class" or None) and attributes
    labels dict. 
    """
    items = split_escaped_str(cell, " ")
    if cell == "":
        return None, {}
    elif items:
        specifier = None
        if items[0] in ["m", "meta"]:
            specifier = "meta"
            items = items[1:]
        elif items[0] == "class":
            specifier = "class"
            items = items[1:]
        elif items[0] == "multiclass":
            specifier = "multiclass"
            items = items[1:]
        elif items[0] in ["i", "ignore"]:
            specifier = "ignore"
            items = items[1:]
        return specifier, dict(map(_var_attribute_label_parse, items))
    else:
        raise ValueError("Unknown attribute label definition")

def var_attributes(row):
    """ Return variable specifiers and label definitions for row.
    """
    return map(var_attribute, row)


class _var_placeholder(object):
    """ A place holder for an arbitrary variable while it's values are still unknown.
    """
    def __init__(self, name="", values=[]):
        self.name = name
        self.values = set(values)

class _disc_placeholder(_var_placeholder):
    """ A place holder for discrete variables while their values are not yet known.
    """
    pass

def is_val_cont(cell):
    """ Is cell a string representing a real value.
    """
    try:
        float(cell)
        return True
    except ValueError:
        return False

def is_variable_cont(values, n=None, cutoff=0.5):
    """ Is variable with ``values`` in column (``n`` rows) a continuous variable. 
    """
    cont = sum(map(is_val_cont, values)) or 1e-30
    if n is None:
        n = len(values) or 1
    return (float(cont) / n) >= cutoff


def is_variable_discrete(values, n=None, cutoff=0.3):
    """ Is variable with ``values`` in column (``n`` rows) a discrete variable. 
    """
    return not is_variable_cont(values, n, cutoff=1.0 - cutoff)

def is_variable_string(values, n=None, cutuff=0.75):
    """ Is variable with ``values`` in column (``n`` rows) a string variable. 
    """
    if n is None:
        n = len(values)
    return float(len(set(values))) / (n or 1.0) > cutoff

def load_csv(file, create_new_on=MakeStatus.Incompatible, 
             delimiter=None, quotechar=None, escapechar=None,
             skipinitialspace=None, has_header=None, 
             has_types=None, has_annotations=None, DK=None, **kwargs):
    """ Load an Orange.data.Table from s csv file.
    """
    import csv, numpy
    file = as_open_file(file, "rb")
    snifer = csv.Sniffer()

    # Max 5MB sample
    # TODO: What if this is not enough. Try with a bigger sample
    sample = file.read(5 * 2 ** 20)
    try:
        dialect = snifer.sniff(sample)
    except csv.Error:
        # try the default, hope the provided arguments are correct
        dialect = "excel"

    if has_header is None:
        try:
            has_header = snifer.has_header(sample)
        except csv.Error:
            has_header = False

    file.seek(0)  # Rewind

    def kwparams(**kwargs):
        """Return not None kwargs.
        """
        return dict([(k, v) for k, v in kwargs.items() if v is not None])

    # non-None format parameters.
    fmtparam = kwparams(delimiter=delimiter,
                        quotechar=quotechar,
                        escapechar=escapechar,
                        skipinitialspace=skipinitialspace)

    reader = csv.reader(file, dialect=dialect,
                        **fmtparam)

    header = types = var_attrs = None

    row = first_row = reader.next()

    if has_header:
        header = row
        # Eat this row and move to the next
        row = reader.next()

    # Guess types row
    if has_types is None:
        has_types = has_header and is_var_types_row(row)

    if has_types:
        types = var_types(row)
        # Eat this row and move to the next
        row = reader.next()

    # Guess variable annotations row
    if has_annotations is None:
        has_annotations = has_header and has_types and \
                          is_var_attributes_row(row)

    if has_annotations:
        labels_row = row
        var_attrs = var_attributes(row)
        # Eat this row and move to the next
        row = reader.next()

    if not header:
        # Create a default header
        header = ["F_%i" % i for i in range(len(first_row))]

    if not types:
        # Create blank variable types
        types = [None] * len(header)

    if not var_attrs:
        # Create blank variable attributes
        var_attrs = [None] * len(header)
    else:
        # Pad the vars_attrs if it is not complete
        # (orange tab format allows this line to be shorter then header).
        if len(var_attrs) < len(header):
            var_attrs += [None] * (len(header) - len(var_attrs))

    # start from the beginning
    file.seek(0)
    reader = csv.reader(file, dialect=dialect, **fmtparam)

    for defined in [has_header, has_types, has_annotations]:
        if defined:
            # skip definition rows if present in the file
            reader.next()

    variables = []
    undefined_vars = []
    # Missing value flags
    missing_flags = DK.split(",") if DK is not None else ["?", "", "NA", "~", "*"]
    missing_map = dict.fromkeys(missing_flags, "?")
    missing_translate = lambda val: missing_map.get(val, val)

    # Create domain variables or corresponding place holders
    for i, (name, var_t) in enumerate(zip(header, types)):
        if var_t == variable.Discrete:
            # We do not have values yet
            variables.append(_disc_placeholder(name))
            undefined_vars.append((i, variables[-1]))
        elif var_t == variable.Continuous:
            variables.append(make(name, Orange.feature.Type.Continuous, [], [], create_new_on))
        elif var_t == variable.String:
            variables.append(make(name, Orange.feature.Type.String, [], [], create_new_on))
        elif var_t == variable.Python:
            variables.append(variable.Python(name))
        elif isinstance(var_t, tuple):
            var_t, values = var_t
            if var_t == variable.Discrete:
                # We have values for discrete variable
                variables.append(make(name, Orange.feature.Type.Discrete, values, [], create_new_on))
            elif var_t == variable.Python:
                # Python variables are not supported yet
                raise NotImplementedError()
        elif var_t is None:
            # Unknown variable type, to be deduced at the end
            variables.append(_var_placeholder(name))
            undefined_vars.append((i, variables[-1]))

    data = []
    # Read all the rows
    for row in reader:
        # check for final newline.
        if row:
            row = map(missing_translate, row)
            data.append(row)
            # For undefined variables collect all their values
            for ind, var_def in undefined_vars:
                var_def.values.add(row[ind])

    # Process undefined variables now that we can deduce their type
    for ind, var_def in undefined_vars:
        values = var_def.values - set(["?", ""])  # TODO: Other unknown strings
        values = sorted(values)
        if isinstance(var_def, _disc_placeholder):
            variables[ind] = make(var_def.name, Orange.feature.Type.Discrete, [], values, create_new_on)
        elif isinstance(var_def, _var_placeholder):
            if is_variable_cont(values, cutoff=1.0):
                variables[ind] = make(var_def.name, Orange.feature.Type.Continuous, [], [], create_new_on)
            elif is_variable_discrete(values, cutoff=0.0):
                variables[ind] = make(var_def.name, Orange.feature.Type.Discrete, [], values, create_new_on)
            elif is_variable_string(values):
                variables[ind] = make(var_def.name, Orange.feature.Type.String, [], [], create_new_on)
            else:
                # Treat it as a string anyway
                variables[ind] = make(var_def.name, Orange.feature.Type.String, [], [], create_new_on)

    attribute_load_status = []
    meta_attribute_load_status = {}
    class_var_load_status = []
    multiclass_var_load_status = []

    attributes = []
    class_var = []
    class_vars = []
    metas = {}
    attribute_indices = []
    variable_indices = []
    class_indices = []
    multiclass_indices = []
    meta_indices = []
    ignore_indices = []
    for i, ((var, status), var_attr) in enumerate(zip(variables, var_attrs)):
        if var_attr:
            flag, attrs = var_attr
            if flag == "class":
                class_var.append(var)
                class_var_load_status.append(status)
                class_indices.append(i)
            elif flag == "multiclass":
                class_vars.append(var)
                multiclass_var_load_status.append(status)
                multiclass_indices.append(i)
            elif flag == "meta":
                mid = Orange.feature.Descriptor.new_meta_id()
                metas[mid] = var
                meta_attribute_load_status[mid] = status
                meta_indices.append((i, var))
            elif flag == "ignore":
                ignore_indices.append(i)
            else:
                attributes.append(var)
                attribute_load_status.append(status)
                attribute_indices.append(i)
            var.attributes.update(attrs)
        else:
            attributes.append(var)
            attribute_load_status.append(status)
            attribute_indices.append(i)

    if len(class_var) > 1:
        raise ValueError("Multiple class variables defined")
    if class_var and class_vars:
        raise ValueError("Both 'class' and 'multiclass' used.")

    class_var = class_var[0] if class_var else None

    attribute_load_status += class_var_load_status
    variable_indices = attribute_indices + class_indices
    domain = Orange.data.Domain(attributes, class_var, class_vars=class_vars)
    domain.add_metas(metas)
    normal = [[row[i] for i in variable_indices] for row in data]
    meta_part = [[row[i] for i, _ in meta_indices] for row in data]
    multiclass_part = [[row[i] for i in multiclass_indices] for row in data]
    table = Orange.data.Table(domain, normal)
    for ex, m_part, mc_part in zip(table, meta_part, multiclass_part):
        for (column, var), val in zip(meta_indices, m_part):
            ex[var] = var(val)
        if mc_part:
            ex.set_classes(mc_part)

    table.setattr("metaAttributeLoadStatus", meta_attribute_load_status)
    table.setattr("attributeLoadStatus", attribute_load_status)

    return table


def as_open_file(file, mode="rb"):
    if isinstance(file, basestring):
        file = open(file, mode)
    else:  # assuming it is file like with proper mode, could check for write, read
        pass
    return file

def save_csv(file, table, orange_specific=True, **kwargs):
    import csv
    file = as_open_file(file, "wb")
    writer = csv.writer(file, **kwargs)
    attrs = table.domain.attributes
    class_var = table.domain.class_var
    metas = [v for _, v in sorted(table.domain.get_metas().items(),
                                  reverse=True)]
    all_vars = attrs + ([class_var] if class_var else []) + metas
    names = [v.name for v in all_vars]
    writer.writerow(names)

    if orange_specific:
        type_cells = []
        for v in all_vars:
            if isinstance(v, variable.Discrete):
                escaped_values = [val.replace(" ", r"\ ") for val in v.values]
                type_cells.append(" ".join(escaped_values))
            elif isinstance(v, variable.Continuous):
                type_cells.append("continuous")
            elif isinstance(v, variable.String):
                type_cells.append("string")
            elif isinstance(v, variable.Python):
                type_cells.append("python")
            else:
                raise TypeError("Unknown variable type")
        writer.writerow(type_cells)

        var_attr_cells = []
        for spec, var in [("", v) for v in attrs] + \
                         ([("class", class_var)] if class_var else []) + \
                         [("m", v) for v in metas]:

            labels = ["{0}={1}".format(*t) for t in var.attributes.items()] # TODO escape spaces
            var_attr_cells.append(" ".join([spec] if spec else [] + labels))

        writer.writerow(var_attr_cells)

    for instance in table:
        instance = list(instance) + [instance[m] for m in metas]
        writer.writerow(instance)


register_file_type("R", None, toR, ".R")
register_file_type("Weka", loadARFF, toARFF, ".arff")
register_file_type("Mulan", loadMULAN, None, ".xml")
#registerFileType("C50", None, toC50, [".names", ".data", ".test"])
register_file_type("libSVM", loadLibSVM, toLibSVM, ".svm")

registerFileType = Orange.utils.deprecated_function_name(register_file_type)

__doc__ +=  \
"""\
Search Paths
============

Associate a prefix with a search path for easier data loading.
The paths can be stored in a user specific configuration file.

.. note: The '' (empty string) prefix can be used to add search paths
         to the default search path.

.. note: Add-ons can register their search paths using
        'orange.data.io.search_paths' entry point

Example

    >>> import Orange, os
    >>> from Orange.data import io
    >>> directory = os.path.expanduser("~/Documents/My Datasets"))
    >>> io.set_search_path("my_datasets", directory, persistent=True)
    >>> data = Orange.data.Table("my_datasets:dataset1.tab")
    >>> # Add directory to the default search path
    >>> io.set_search_path("", directory)
    >>> data = Orange.data.Table("dataset1.tab")


.. autofunction:: set_search_path

.. autofunction:: search_paths

.. autofunction:: persistent_search_paths

.. autofunction:: find_file

.. autofunction:: expand_filename

"""


# Non-persistent registered paths
_session_paths = []

import ConfigParser
from ConfigParser import SafeConfigParser

DATA_PATHS_ENTRY_POINT = "orange.data.io.search_paths"


def addon_data_search_paths():
    """Return the search paths registered by setuptools
    'orange.data.io.search_paths' entry point.

    """
    import pkg_resources
    search_paths = []
    for entry_point in pkg_resources.iter_entry_points(DATA_PATHS_ENTRY_POINT):
        try:
            call = entry_point.load()
            paths = call()
            for path in paths:
                if isinstance(path, tuple) and len(path) == 2 and \
                        all(isinstance(p, basestring) for p in path):
                    search_paths.append(path)
                elif isinstance(path, basestring):
                    search_paths.append(("", path))
                else:
                    warnings.warn("Invalid search path %r. Expected tuple or "
                                  "string, got %r" % (entry_point, type(path)))
        except pkg_resources.DistributionNotFound, ex:
            warnings.warn("Missing dependency for %r: %r" % (entry_point, ex),
                         UserWarning)
        except Exception, ex:
            warnings.warn("Error calling %r: %r" % (entry_point, ex),
                         UserWarning)
    return search_paths


@Orange.utils.lru_cache(maxsize=1)
def persistent_search_paths():
    """Return a list of persistent registered (prefix, path) pairs.
    """

    global_settings_dir = Orange.utils.environ.install_dir
    user_settings_dir = Orange.utils.environ.orange_settings_dir
    parser = SafeConfigParser()
    parser.read([os.path.join(global_settings_dir, "orange-search-paths.cfg"),
                 os.path.join(user_settings_dir, "orange-search-paths.cfg")])
    try:
        items = parser.items("paths")
        defaults = parser.defaults().items()
        items = [i for i in items if i not in defaults]
    except ConfigParser.NoSectionError:
        items = []
    # Replace "__default__" prefix with ""
    items = [item if item[0] != "__default__" else ("", item[1]) \
             for item in items]
    return items


def save_persistent_search_path(prefix, path):
    """Save the prefix, path pair. If path is None delete the
    registered prefix.

    """
    if isinstance(path, list):
        path = os.path.pathsep.join(path)

    if prefix == "":
        # Store "" prefix as "__default__"
        prefix = "__default__"

    user_settings_dir = Orange.utils.environ.orange_settings_dir
    if not os.path.exists(user_settings_dir):
        try:
            os.makedirs(user_settings_dir)
        except OSError:
            pass

    filename = os.path.join(user_settings_dir, "orange-search-paths.cfg")
    parser = SafeConfigParser()
    parser.read([filename])

    if not parser.has_section("paths"):
        parser.add_section("paths")

    if path is not None:
        parser.set("paths", prefix, path)
    elif parser.has_option("paths", prefix):
        # Remove the registered prefix
        parser.remove_option("paths", prefix)
    parser.write(open(filename, "wb"))


def search_paths(prefix=None):
    """Return the search path for `prefix`. The search path is
    the union of registered session paths, user specified persistent
    search paths and any search paths registered by
    'orange.data.io.search_path' `pkg_resources` entry points.

    """
    persistent_paths = persistent_search_paths()
    addon_paths = addon_data_search_paths()
    paths = _session_paths + persistent_paths + addon_paths
    if prefix is not None:
        ppaths = []
        for pref, path in paths:
            if pref == prefix:
                ppaths.extend(path.split(os.path.pathsep))
        return os.path.pathsep.join(ppaths)
    else:
        return paths


def set_search_path(prefix, path, persistent=False):
    """Associate a search path with a prefix.

    :param prefix: a prefix
    :type prefix: str

    :param path: search path (can also be a list of path strings)
    :type paths: str

    :param persistent: if `True` then the (prefix, path) pair will be
        saved between sessions (default False).
    :type persistent: bool

    """
    global _session_paths

    if isinstance(path, list):
        path = os.path.pathsep.join(path)

    if persistent:
        save_persistent_search_path(prefix, path)
        # Invalidate the persistent_search_paths cache.
        persistent_search_paths.clear()
    else:
        _session_paths.append((prefix, path))


def expand_filename(prefixed_name):
    """Expand the prefixed filename with the full path.

        >>> from Orange.data import io
        >>> io.set_search_paths("docs", "/Users/aleserjavec/Documents")
        >>> io.expand_filename("docs:my_tab_file.tab")
        '/Users/aleserjavec/Documents/my_tab_file.tab'

    """
    # TODO: handle windows drive letters.
    if ":" in prefixed_name:
        prefix, filename = prefixed_name.split(":", 1)
    else:
        prefix, filename = "", prefixed_name
    paths = search_paths(prefix)
    if paths:
        paths = paths.split(os.path.pathsep)
        for path in paths:
            if os.path.exists(os.path.join(path, filename)):
                return os.path.join(path, filename)
        raise ValueError("%r not found on the search path." % filename)
    else:
        raise ValueError("Unknown prefix %r." % prefix)


def find_file(prefixed_name):
    """Find the prefixed filename and return its full path.
    """
    # This function is called from C++ if the default search
    # fails
    if not os.path.exists(prefixed_name):
        return expand_filename(prefixed_name)
    else:
        return prefixed_name