partition
クラス: matlab.io.datastore.Partitionable
名前空間: matlab.io.datastore
データストアを分割する
説明
入力引数
ds
— 入力データストア
matlab.io.Datastore
オブジェクト
入力データストア。matlab.io.Datastore
オブジェクトとして指定します。Datastore
オブジェクトを作成する場合は、matlab.io.Datastore
を参照してください。
n
— 分割数
正の整数
分割数。正の整数として指定します。n
に適切な値を取得するには、関数 numpartitions
を使用します。
データストアで使用可能な区画の範囲にない n
の値を指定する場合、partition
メソッドにより空のデータストアが返されます。詳細については、空のデータストアを参照してください。たとえば、データストアが 10
個の区画まで保持する可能性がある場合、partition
メソッドの出力は n
の値によって異なります。
n
の指定された値が10
以下の場合、partition
メソッドによってindex
で指定された区画が返されます。たとえば、partition(ds,10,1)
は元のデータストアds
の最初の区画のコピーを返します。n
の指定された値が10
より大きい場合、partition
メソッドによって空のデータストアが返されます。たとえば、partition(ds,100,11)
は空のデータストアを返します。
例: 3
データ型: double
index
— インデックス
正の整数
インデックス。正の整数として指定します。
例: 1
データ型: double
例
並列処理をサポートするデータストアの作成
並列処理をサポートするデータストアを作成し、これを使用してカスタム データまたは独自のデータを MATLAB® に取り込みます。次に、このデータを並列プールで処理します。
カスタム データストアを実装するコードを含む、.m
クラス定義ファイルを作成します。このファイルは作業フォルダーまたは MATLAB® パス上のフォルダーに保存しなければなりません。.m
ファイルの名前は、オブジェクト コンストラクター関数の名前と同じでなければなりません。たとえば、コンストラクター関数の名前を MyDatastorePar にする場合、.m
ファイルの名前は MyDatastorePar.m
でなければなりません。.m
クラス定義ファイルには、次の手順が含まれなければなりません。
手順 1: データストア クラスから継承します。
手順 2: コンストラクターと必須メソッドを定義します。
手順 3: カスタム ファイルの読み取り関数を定義します。
これらの手順に加えて、データの処理と解析に必要なその他のプロパティまたはメソッドを定義します。
%% STEP 1: INHERIT FROM DATASTORE CLASSES classdef MyDatastorePar < matlab.io.Datastore & ... matlab.io.datastore.Partitionable properties(Access = private) CurrentFileIndex double FileSet matlab.io.datastore.DsFileSet end % Property to support saving, loading, and processing of % datastore on different file system machines or clusters. % In addition, define the methods get.AlternateFileSystemRoots() % and set.AlternateFileSystemRoots() in the methods section. properties(Dependent) AlternateFileSystemRoots end %% STEP 2: DEFINE THE CONSTRUCTOR AND THE REQUIRED METHODS methods % Define your datastore constructor function myds = MyDatastorePar(location,altRoots) myds.FileSet = matlab.io.datastore.DsFileSet(location,... 'FileExtensions','.bin', ... 'FileSplitSize',8*1024); myds.CurrentFileIndex = 1; if nargin == 2 myds.AlternateFileSystemRoots = altRoots; end reset(myds); end % Define the hasdata method function tf = hasdata(myds) % Return true if more data is available tf = hasfile(myds.FileSet); end % Define the read method function [data,info] = read(myds) % Read data and information about the extracted data % See also: MyFileReader() if ~hasdata(myds) msgII = ['Use the reset method to reset the datastore ',... 'to the start of the data.']; msgIII = ['Before calling the read method, ',... 'check if data is available to read ',... 'by using the hasdata method.']; error('No more data to read.\n%s\n%s',msgII,msgIII); end fileInfoTbl = nextfile(myds.FileSet); data = MyFileReader(fileInfoTbl); info.Size = size(data); info.FileName = fileInfoTbl.FileName; info.Offset = fileInfoTbl.Offset; % Update CurrentFileIndex for tracking progress if fileInfoTbl.Offset + fileInfoTbl.SplitSize >= ... fileInfoTbl.FileSize myds.CurrentFileIndex = myds.CurrentFileIndex + 1 ; end end % Define the reset method function reset(myds) % Reset to the start of the data reset(myds.FileSet); myds.CurrentFileIndex = 1; end % Define the partition method function subds = partition(myds,n,ii) subds = copy(myds); subds.FileSet = partition(myds.FileSet,n,ii); reset(subds); end % Getter for AlternateFileSystemRoots property function altRoots = get.AlternateFileSystemRoots(myds) altRoots = myds.FileSet.AlternateFileSystemRoots; end % Setter for AlternateFileSystemRoots property function set.AlternateFileSystemRoots(myds,altRoots) try % The DsFileSet object manages AlternateFileSystemRoots % for your datastore myds.FileSet.AlternateFileSystemRoots = altRoots; % Reset the datastore reset(myds); catch ME throw(ME); end end end methods (Hidden = true) % Define the progress method function frac = progress(myds) % Determine percentage of data read from datastore if hasdata(myds) frac = (myds.CurrentFileIndex-1)/... myds.FileSet.NumFiles; else frac = 1; end end end methods(Access = protected) % If you use the FileSet property in the datastore, % then you must define the copyElement method. The % copyElement method allows methods such as readall % and preview to remain stateless function dscopy = copyElement(ds) dscopy = copyElement@matlab.mixin.Copyable(ds); dscopy.FileSet = copy(ds.FileSet); end % Define the maxpartitions method function n = maxpartitions(myds) n = maxpartitions(myds.FileSet); end end end %% STEP 3: IMPLEMENT YOUR CUSTOM FILE READING FUNCTION function data = MyFileReader(fileInfoTbl) % create a reader object using FileName reader = matlab.io.datastore.DsFileReader(fileInfoTbl.FileName); % seek to the offset seek(reader,fileInfoTbl.Offset,'Origin','start-of-file'); % read fileInfoTbl.SplitSize amount of data data = read(reader,fileInfoTbl.SplitSize); end
カスタム データストアの準備ができました。カスタム データストアを使用して、並列プールでデータを読み取り、処理します。
詳細
空のデータストア
空のデータストアはレコードが含まれない datastore オブジェクトです。空のデータストアの場合、カスタム データストア メソッドは次の条件を満たさなければなりません。
hasdata
はfalse
を返さなければならない。read
はエラーを返さなければならない。numpartitions
とmaxpartitions
は0
を返さなければならない。partition
は空のデータストアを返さなければならない。preview
とreadall
は非 tall 次元を保持する空のデータを返さなければならない。たとえば、空でないデータストアでread
メソッドがサイズ5
×15
×25
のデータを返す場合、preview
メソッドとreadall
メソッドはサイズ0
×15
×25
の空のデータを返さなければならない。
非 tall 次元
配列の最初の次元以外の次元。サイズ 5
×15
×25
の配列の場合、tall 次元は 5
で、非 tall 次元は 15
と 25
です。
ヒント
partition
メソッドの実装では、次の手順を含めなければなりません。分割されたデータストア
subds
を作成する前に、元のデータストアds
のディープ コピーを作成する。partition
メソッドの最後に、分割されたデータストアsubds
をリセットする。
partition
メソッドのサンプル実装については、並列処理のサポートの追加を参照してください。データストアの区画に読み取り可能なレコードが含まれていない場合、
read
メソッドは空のデータを返さなければなりません。この空のデータの非 tall 次元は、読み取り可能なレコードをもつ区画でread
メソッド出力の非 tall 次元と一致しなければなりません。この要件により、readall
メソッドの動作が関数gather
の動作と一致するようになります。
バージョン履歴
R2017b で導入
参考
MATLAB コマンド
次の MATLAB コマンドに対応するリンクがクリックされました。
コマンドを MATLAB コマンド ウィンドウに入力して実行してください。Web ブラウザーは MATLAB コマンドをサポートしていません。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)