text: from lib import * 
  
  def createDictFloor(input_dir,floor_name):
      dataset_name = os.listdir(input_dir)
      getdict = {}
      floors = [floor for floor in dataset_name if floor_name in floor]
      for name in floors:
          floorname = name.split(".")[0]
          getdict[floorname] = pd.read_csv(input_dir+name, index_col=0, parse_dates=["Date"])
          getdict[floorname] = getdict[floorname].sort_index()
      return getdict
  
  def Missing_values(data):
      total = data.isnull().sum().sort_values(ascending=False)
      percent = (data.isnull().sum()/data.isnull().count()).sort_values(ascending=False)
      missing_data = pd.concat([total,percent], axis=1, keys=['Total', 'Pourcentage'])
      print (missing_data[(percent>0)],'\n' )
  
  def seed_everything(seed=0):
      random.seed(seed)
      os.environ['PYTHONHASHSEED'] = str(seed)
      np.random.seed(seed)
      torch.manual_seed(seed)
      torch.cuda.manual_seed(seed)
      torch.backends.cudnn.deterministic = True
      
  def createPlot(date1, date2, features, data):
      for floor in data.keys():
          data[floor].resample("5T").mean()[features][date1:date2].plot(figsize=(20,7))
          
  
  def createDTFeat(date1, date2, datadict, features, resample_method="sum" ,scale=True):
      resample_move = {}
      index_nan = {}
      index_small = {}
      floors = list(datadict.keys())
      dates = []
      scalers = {}
      # floor_dict = deepcopy(datadict)
      for data in datadict.keys():
          if resample_method == "sum":
              resample_move[data] = datadict[data][date1:date2].resample(
                  "5min").sum()
              
          elif resample_method == "max":
              resample_move[data] = datadict[data][date1:date2].resample(
                  "5min").max().bfill()
          elif resample_method == "mean":
              resample_move[data] = datadict[data][date1:date2].resample(
                  "5min").mean().bfill()
              
          cols = resample_move[data].columns
          idx = resample_move[data].index
  
          if scale:
              scaler = MinMaxScaler(feature_range=(0.2,1))
              scaler.fit(resample_move[data])
              resample_move[data] = pd.DataFrame(scaler.transform(
                  resample_move[data]),
                                                 columns=cols,
                                                 index=idx)
              scalers[data] = scaler
  
          resample_move[data]["weekday"] = resample_move[data].index.day_name()
          resample_move[data]["date"] = resample_move[data].index.date
          resample_move[data]["time"] = resample_move[data].index.time
  
          nan = np.where(pd.isnull(resample_move[data][features]))[0]
          index_nan[data] = np.unique(resample_move[data].iloc[nan]["time"])
  
      return resample_move, scalers, index_nan
  
  def getInfoTimeShape(datadict):
      for floor in datadict.keys():
          data = datadict[floor]
          print("Floor : {} , shape :{} , TimeMin {} , TimeMax {}".format(floor,data.shape, data.index.min(), data.index.max()))
          Missing_values(data)
          
  def cleanNan(data,idx_nan):
      index=[]
      for k,v in idx_nan.items():
          for ele in v:
              index.append(ele)
      mynan = set(index)
      newdata = data.copy()
      remain_date = []
      for floor in idx_nan.keys():
          datafloor = data[floor]
          todropnan = datafloor[datafloor["date"].isin(list(mynan))].index
          datafloor = datafloor.drop(todropnan)
          newdata[floor] = datafloor
          for date in datafloor["date"]:
              remain_date.append(str(date))
      remain_date = sorted(set(remain_date))
      return data, remain_date
  
  def rolling_window(series, window_size):
      return np.array([series[i : (i + window_size)] for i in range(0, series.shape[0] - window_size + 1)])
  
  def to_timeseries_input(series, lookback, predictions):
      inputs = rolling_window(series[:-predictions], lookback)
      outputs = rolling_window(series[lookback:], predictions)
      return inputs, outputs
  
  def createDataByDate(datadict, features, dates):
      databyDate = defaultdict(lambda : defaultdict(dict))
      for date in dates:
          for floor in datadict.keys():
              databyDate[date][floor] = np.asarray(datadict[floor][date][features])
      return databyDate
  
  def splitDate(dates,cutoff):
      train_date = [x for x in dates if x <= cutoff]
      test_date = sorted(list(set(dates) - set(train_date)))
      return train_date, test_date
  
  def getInfoDataByDate(data, dates):
      for date in dates:
          print("Date : {}".format(date))
          for floor in data[date]:
              print("{} shape : {}".format(floor,data[date][floor].shape))
              
  def LoaderByZone(data,zone_name,dates, lookback, lookahead, batch_size, shuffle=False):
      loaderZ = {}
      print (f"Zone {zone_name}")
      for i,date in enumerate(dates):
          
          try:
              haruharu = data[date][zone_name]
              trainx, trainy = to_timeseries_input(haruharu, lookback, lookahead,)
              xshape = torch.tensor(trainx, dtype=torch.float).unsqueeze(-1)
              yshape = torch.tensor(trainy, dtype=torch.float)
              tensorwrap = TensorDataset(xshape,yshape)
              loaderxy = DataLoader(tensorwrap,batch_size = batch_size, shuffle=shuffle, drop_last=True)
              loaderZ[date] = loaderxy
          except:
              print (f"Failed to Load Data for Zone {zone_name}")
              
      return loaderZ
  
  # loaderZ1train = LoaderByZone(databyDate, z1, train_date, lookback, lookahead, batch_size, shuffle=True)
  # loaderZ1test = LoaderByZone(databyDate, z1, test_date, lookback, lookahead, batch_size)
  
  