text: import os, sys
  import numpy as np
  import torch
  from torchvision import datasets, transforms
  from sklearn.utils import shuffle
  
  
  ########################################################################################################################
  
  def get(data_path, args, seed=0, fixed_order=False, pc_valid=0.1, samples_per_task=-1):
      data = {}
      output_info = []
      size = [1, 28, 28]
  
      # number of tasks
      n_outputs = 10
      nperm = args.n_tasks
  
      seeds = np.array(list(range(nperm)), dtype=int)
      if not fixed_order:
          seeds = shuffle(seeds, random_state=seed)
  
      path = os.path.join(data_path, 'binary_pmnist')
      path = path + '%s' % (nperm)
  
      if not os.path.isdir(path):
          os.makedirs(path)
  
          # Pre-load
          # MNIST
          mean = (0.1307,)
          std = (0.3081,)
          dat = {}
          dat['train'] = datasets.MNIST(data_path, train=True, download=True, transform=transforms.Compose(
              [transforms.ToTensor(), transforms.Normalize(mean, std)]))
          dat['test'] = datasets.MNIST(data_path, train=False, download=True, transform=transforms.Compose(
              [transforms.ToTensor(), transforms.Normalize(mean, std)]))
  
          for i, r in enumerate(seeds):
              print(i, end=',')
              sys.stdout.flush()
              data[i] = {}
              data[i]['name'] = 'pmnist-{:d}'.format(i)
              # number of classes
              data[i]['ncla'] = 10
  
              for s in ['train', 'test']:
                  loader = torch.utils.data.DataLoader(dat[s], batch_size=1, shuffle=False)
                  data[i][s] = {'x': [], 'y': []}
                  for image, target in loader:
                      aux = image.view(-1).numpy()
                      aux = shuffle(aux, random_state=r * 100 + i)
                      image = torch.FloatTensor(aux).view(size)
                      data[i][s]['x'].append(image)
                      data[i][s]['y'].append(target.numpy()[0])
  
              # "Unify" and save
              for s in ['train', 'test']:
                  data[i][s]['x'] = torch.stack(data[i][s]['x']).view(-1, size[0], size[1], size[2])
                  data[i][s]['y'] = torch.LongTensor(np.array(data[i][s]['y'], dtype=int)).view(-1)
                  torch.save(data[i][s]['x'], os.path.join(os.path.expanduser(path), 'data' + str(r) + s + 'x.bin'))
                  torch.save(data[i][s]['y'], os.path.join(os.path.expanduser(path), 'data' + str(r) + s + 'y.bin'))
          print()
  
      else:
          # Load binary files
          for i, r in enumerate(seeds):
              data[i] = dict.fromkeys(['name', 'ncla', 'train', 'test'])
              data[i]['ncla'] = 10
              data[i]['name'] = 'pmnist-{:d}'.format(i)
  
              # Load
              for s in ['train', 'test']:
                  data[i][s] = {'x': [], 'y': []}
                  data[i][s]['x'] = torch.load(os.path.join(os.path.expanduser(path), 'data' + str(r) + s + 'x.bin'))
                  data[i][s]['y'] = torch.load(os.path.join(os.path.expanduser(path), 'data' + str(r) + s + 'y.bin'))
  
      # Validation
      for t in data.keys():
          r = np.arange(data[t]['train']['x'].size(0))
          r = np.array(shuffle(r, random_state=seed), dtype=int)
          if samples_per_task > 0:
              nvalid = int(pc_valid * samples_per_task)
              ivalid = torch.LongTensor(r[:nvalid])
              itrain = torch.LongTensor(r[nvalid:samples_per_task])
          else:
              nvalid = int(pc_valid * len(r))
              ivalid = torch.LongTensor(r[:nvalid])
              itrain = torch.LongTensor(r[nvalid:samples_per_task])
          data[t]['valid'] = {}
          data[t]['valid']['x'] = data[t]['train']['x'][ivalid].clone()
          data[t]['valid']['y'] = data[t]['train']['y'][ivalid].clone()
          data[t]['train']['x'] = data[t]['train']['x'][itrain].clone()
          data[t]['train']['y'] = data[t]['train']['y'][itrain].clone()
  
      # Others
      n = 0
      for t in data.keys():
          # tasks info
          output_info.append((t, data[t]['ncla']))
          n += data[t]['ncla']
  
      # total number of classes
      data['ncla'] = n
      n_inputs = size[0] * size[1] * size[2]
  
      return data, output_info, n_inputs, nperm, n_outputs