summaryrefslogtreecommitdiff
path: root/szilagyi/_dataset/__init__.py
blob: e91251ede7ff4c851765ecfc468fd581bc13c9a1 (plain)
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
import csv
import os
import re


class Vector(complex):
	def __getitem__(self, index):
		if index == 0:
			return self.real
		if index == 1:
			return self.imag
		raise IndexError


def _load_from(root):
	def _vectors(iterable):
		for x, y in iterable:
			yield Vector(float(x), float(y))

	def _load(filename):
		with open(filename, encoding='utf-8') as fp:
			reader = csv.reader(fp)
			return list(_vectors(reader))

	def _files(directory):
		for filename in os.listdir(directory):
			match = re.match(r"SWI_(-?\d+)\.csv", filename)
			if match:
				yield int(match.group(1)), os.path.join(directory, filename)

	def _first(pair):
		return pair[0]

	return [(index, _load(path)) for index, path in sorted(_files(root), key=_first)]


INDICES = _load_from(os.path.dirname(os.path.abspath(__file__)))