Exemple #1
0
def test_detect_dormat():
    assert detect_format(os.path.join(DATADIR, 'di1.di')) == 'di'
    assert detect_format(os.path.join(DATADIR, 'di2.di')) == 'di'
    assert detect_format(os.path.join(DATADIR, 'example.ibw')) == 'ibw'
    assert detect_format(os.path.join(DATADIR, 'example.opd')) == 'opd'
    assert detect_format(os.path.join(DATADIR, 'example.x3p')) == 'x3p'
    assert detect_format(os.path.join(DATADIR, 'example1.mat')) == 'mat'
    assert detect_format(os.path.join(DATADIR, 'example.asc')) == 'xyz'
    assert detect_format(
        os.path.join(DATADIR, 'line_scan_1_minimal_spaces.asc')) == 'xyz'
    assert detect_format(os.path.join(DATADIR, 'example-2d.npy')) == 'npy'
    assert detect_format(os.path.join(DATADIR, 'surface.2048x2048.h5')) == 'h5'
Exemple #2
0
    def handle(self, *args, **options):

        format_counts = { None: 0 }
        num_cannot_openend = 0  # number of files which cannot be openend

        topographies = Topography.objects.all()
        if not options['all']:
            topographies = topographies.filter(datafile_format__isnull=True)
        num_topographies = topographies.count()

        for topo in topographies:
            if topo.datafile_format is None:
                try:
                    datafile = topo.datafile
                    # Workaround such that module "SurfaceTopography" recognizes this a binary stream
                    if not hasattr(datafile, 'mode'):
                        datafile.mode = 'rb'
                    datafile_format = detect_format(datafile)
                except CannotDetectFileFormat as exc:
                    msg = f"Could not detect format for topography id {topo.id}: "+str(exc)
                    self.stdout.write(self.style.WARNING(msg))
                    format_counts[None] += 1
                    continue
                except Exception as exc:
                    msg = f"Could not open file for topography id {topo.id}: " + str(exc)
                    self.stdout.write(self.style.WARNING(msg))
                    num_cannot_openend += 1
                    continue

                if not options['dry_run']:
                    topo.datafile_format = datafile_format
                    topo.save()

                if datafile_format not in format_counts:
                    format_counts[datafile_format] = 1
                else:
                    format_counts[datafile_format] += 1

        self.stdout.write(self.style.SUCCESS(f"Processed {num_topographies} specified topographies."))

        if num_cannot_openend == 0:
            self.stdout.write(self.style.SUCCESS("All specified topography files can be opened."))
        else:
            self.stdout.write(self.style.ERROR("In total {} of {} topographies currently cannot be opened.".format(
                num_cannot_openend, num_topographies)))

        self.stdout.write(self.style.SUCCESS("Frequencies of topographies which could be opened:"))
        for fmt, freq in format_counts.items():
            self.stdout.write(self.style.SUCCESS(f"  {fmt}: {freq}"))

        if format_counts[None] == 0:
            self.stdout.write(self.style.SUCCESS("All {} topography files which can be opened can also be loaded.".format(
                num_topographies-num_cannot_openend)))
        else:
            self.stdout.write(self.style.WARNING("In total {} topographies currently can be opened, but not be loaded.".format(
                format_counts[None])))

        if options['dry_run']:
            self.stdout.write(self.style.WARNING("This was a dry run, nothing has been changed."))
Exemple #3
0
 def test_detect_format_then_read(self):
     self.assertEqual(
         detect_format(
             os.path.join(DATADIR, 'line_scan_1_minimal_spaces.asc')),
         'xyz')
Exemple #4
0
 def test_detect_format_then_read(self):
     f = open(os.path.join(DATADIR, 'example.ibw'), 'rb')
     fmt = detect_format(f)
     self.assertTrue(fmt, 'ibw')
     open_topography(f, format=fmt).topography()
     f.close()